Skip to content

Commit

Permalink
redo/simplify the other_thresh interface
Browse files Browse the repository at this point in the history
  • Loading branch information
wspr committed Mar 31, 2024
1 parent 635ba64 commit 4e118fa
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 16 deletions.
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

## 2024-99-99 v1.3

* Add `label_duplicate` switch to avoid printing redundant labels.
* Internal code changes using OOP methods to assist further features.
* Add `label_duplicate` option to avoid printing redundant labels.
* Add `other_thresh_XX` option to allow recategorisation of entries with values below a certain threshold.
* Internal code changes using OOP methods to assist further features.


## 2024-03-25 v1.2.1
Expand Down
52 changes: 38 additions & 14 deletions ausankey/ausankey.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,32 @@ class Sankey:
appear in the previous stage. This minimises chart clutter but might
be confusing in cases, hence defaulting to True.
other_dict : dict
Sets thresholds to recategorise nodes that are below a certain value.
other_thresh_val : float
Sets threshold to recategorise nodes that are below a certain value.
Up to three dictionary keys can be set:
* `"val": v` — set node to other if it is less than `v`
* `"sum": s` — set node to other if it is less than `s` fraction
of the summed total of all nodes in the current stage
* `"max": m` — set node to other if is is less than `m` fraction
of the maximum node in the current stage
If any of these criteria are met the reclassification will occur.
other_thresh_sum : float
Sets threshold to recategorise nodes that are below a certain value.
Up to three dictionary keys can be set:
* `"val": v` — set node to other if it is less than `v`
* `"sum": s` — set node to other if it is less than `s` fraction
of the summed total of all nodes in the current stage
* `"max": m` — set node to other if is is less than `m` fraction
of the maximum node in the current stage
If any of these criteria are met the reclassification will occur.
other_thresh_max : float
Sets threshold to recategorise nodes that are below a certain value.
Up to three dictionary keys can be set:
* `"val": v` — set node to other if it is less than `v`
Expand Down Expand Up @@ -202,7 +226,9 @@ def __init__(
flow_lw=1,
node_lw=1,
frame_lw=1,
other_dict=None,
other_thresh_val=0,
other_thresh_max=0,
other_thresh_sum=0,
other_name="Other",
titles=None,
title_gap=0.05,
Expand Down Expand Up @@ -237,7 +263,9 @@ def __init__(
self.node_lw = node_lw
self.frame_lw = frame_lw
self.other_name = other_name
self.other_dict = other_dict or {}
self.other_thresh_val = other_thresh_val
self.other_thresh_max = other_thresh_max
self.other_thresh_sum = other_thresh_sum
self.titles = titles
self.title_font = title_font or {"fontweight": "bold"}
self.title_gap = title_gap
Expand Down Expand Up @@ -288,21 +316,17 @@ def setup(self, data):
self.node_sizes = {}
self.nodes_uniq = {}

# weight and reclassify
self.weight_labels()

# reclassify
thresh_val = self.other_dict.get("val", 0)
thresh_max = self.other_dict.get("max", 0)
thresh_sum = self.other_dict.get("sum", 0)
for ii in range(self.num_stages):
for nn, lbl in enumerate(self.data[2 * ii]):
val = self.node_sizes[ii][lbl]
if lbl is not None and (
val < thresh_val
or val < thresh_sum * self.weight_sum[ii]
or val < thresh_max * max(self.data[2 * ii + 1])
):
self.data.iat[nn, 2 * ii] = self.other_name
val < self.other_thresh_val or
val < self.other_thresh_sum * self.weight_sum[ii] or
val < self.other_thresh_max * max(self.data[2 * ii + 1])
):
self.data.iat[nn,2 * ii] = self.other_name
self.weight_labels()

# sort and calc
Expand Down

0 comments on commit 4e118fa

Please sign in to comment.