jwql
jwql copied to clipboard
filter/pattern with most entries not sort-stable
Describe the bug The code to find the filters and patterns with the most entries may not be stable in the event of a tie.
To Reproduce https://github.com/spacetelescope/jwql/blob/d3804cffd437b1f1d80d8ec07926413f31fa63c1/jwql/instrument_monitors/common_monitors/bad_pixel_monitor.py#L462-L470
- Line 462, a
setis being converted to alist. This is not a bug per se, but is unnecessary, and makes the variable name misleading. - Since filter_set is a set (or derived from one anyway), the order is subject to change at each run. If multiple filters have the same
filter_list.count, this code will select the first one it encounters in filter_set. But since the order is not guaranteed, consecutive runs with identical input could produce different results. - These seven lines of code could be reduced to one, which is sort-stable:
maxfilt = max(dict.fromkeys(filter_list), key=filter_list.count, default='')
Similar arguments can be made about https://github.com/spacetelescope/jwql/blob/d3804cffd437b1f1d80d8ec07926413f31fa63c1/jwql/instrument_monitors/common_monitors/bad_pixel_monitor.py#L484-L491
For discussion: Is it possible for filters/patterns to have a tie in most frequent? If that happens, is the desired behavior to pick one at random?
This issue is tracked on JIRA as JWQL-221.