Skip to content

Commit

Permalink
new approach to sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
wspr committed Mar 22, 2024
1 parent 92efe90 commit 518172c
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions ausankey/ausankey.py
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,10 @@ def _sankey(
weighted_sort(labels_lr[0], weights_lr[0], sorting),
weighted_sort(labels_lr[1], weights_lr[1], sorting),
]
nodes_lr = [

Check failure on line 392 in ausankey/ausankey.py

View workflow job for this annotation

GitHub Actions / lint

Ruff (F841)

ausankey/ausankey.py:392:9: F841 Local variable `nodes_lr` is assigned to but never used
sort_nodes(labels_lr[0],node_sizes[ii]),
sort_nodes(labels_lr[1],node_sizes[ii+1]),
]

# check labels
check_data_matches_labels(bar_lr[0], labels_lr[0], "left")
Expand Down Expand Up @@ -589,9 +593,55 @@ def draw_bar(x, dx, y, dy, label):
snap=True,
)

###########################################

def sort_nodes(lbl, node_sizes):
"""creates a sorted list of labels by their summed weights"""

arr = {}
for uniq in lbl.unique():
arr[uniq] = True

sort_arr = sorted(
arr.items(),
key=lambda item: list(node_sizes).index(item[0]),
# sorting = 0,1,-1 affects this
)

return list(dict(sort_arr).keys())

###########################################

def sort_dict(lbl, sorting):
"""creates a sorted list of labels by their summed weights"""

if sorting == "top":
s = 1
elif sorting == "bottom":
s = -1
elif sorting == "center":
s = 1
else:
s = 0


sort_arr = sorted(
list(lbl.items()),
key=lambda item: s * item[1],
# sorting = 0,1,-1 affects this
)

Check failure on line 632 in ausankey/ausankey.py

View workflow job for this annotation

GitHub Actions / lint

Ruff (C414)

ausankey/ausankey.py:628:16: C414 Unnecessary `list` call within `sorted()`

sorted_labels = list(dict(sort_arr))

if sorting == "center":
# this kinda works but i dont think it's a good idea because you lose perception of relative sizes
# probably has an off-by-one even/odd error
sorted_labels = sorted_labels[1::2] + sorted_labels[-1::-2]

return sorted_labels


###########################################

def weighted_sort(lbl, wgt, sorting):
"""creates a sorted list of labels by their summed weights"""
Expand All @@ -605,10 +655,12 @@ def weighted_sort(lbl, wgt, sorting):
else:
s = 0

arr = {}
for uniq in lbl.unique():
arr[uniq] = wgt[lbl == uniq].sum()

sort_arr = sorted(
list(arr.items()),
key=lambda item: s * item[1],
# sorting = 0,1,-1 affects this
)

Check failure on line 666 in ausankey/ausankey.py

View workflow job for this annotation

GitHub Actions / lint

Ruff (C414)

ausankey/ausankey.py:662:16: C414 Unnecessary `list` call within `sorted()`
Expand Down

0 comments on commit 518172c

Please sign in to comment.