-
Notifications
You must be signed in to change notification settings - Fork 26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
optimization of multiplicity, theta and gh cut #204
Open
maxnoe
wants to merge
13
commits into
main
Choose a base branch
from
faster_optimize_cuts
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 9 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
caa226b
Speed-up cut calculation by computing quantiles in one go
maxnoe 67188b1
Use tqdm.auto
maxnoe db27011
Store best efficiency
maxnoe 3eb0221
Add full cut optimization
maxnoe c4640e9
Fix fill_value unit error
maxnoe 5bc7488
Fix setuptools to < 65.6 to fix distutils import error
maxnoe 46ec05f
Avoid recomputing bin indices
maxnoe 9bef4fe
Fix import
maxnoe 3358d03
Fix a bug in optimize_cuts where looping over a new multiplicity cut …
JBernete 81654b1
Merge remote-tracking branch 'origin/main' into faster_optimize_cuts
maxnoe 16b6e2f
Rename variable
maxnoe f390222
Add changelog
maxnoe d4ac7c5
Fix codacy complaints
maxnoe File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -62,20 +62,34 @@ def calculate_percentile_cut( | |
bin_index, valid = calculate_bin_indices(bin_values, bins) | ||
by_bin = table[valid].group_by(bin_index[valid]) | ||
|
||
n_bins = len(bins) - 1 | ||
cut_table = QTable() | ||
cut_table["low"] = bins[:-1] | ||
cut_table["high"] = bins[1:] | ||
cut_table["center"] = bin_center(bins) | ||
cut_table["n_events"] = 0 | ||
cut_table["cut"] = np.asanyarray(fill_value, values.dtype) | ||
|
||
unit = None | ||
if hasattr(fill_value, 'unit'): | ||
unit = fill_value.unit | ||
fill_value = fill_value.value | ||
|
||
percentile = np.asanyarray(percentile) | ||
if percentile.shape == (): | ||
cut_table["cut"] = np.asanyarray(fill_value, values.dtype) | ||
else: | ||
cut_table["cut"] = np.full((n_bins, len(percentile)), fill_value, dtype=values.dtype) | ||
|
||
if unit is not None: | ||
cut_table["cut"].unit = unit | ||
|
||
for bin_idx, group in zip(by_bin.groups.keys, by_bin.groups): | ||
# replace bins with too few events with fill_value | ||
n_events = len(group) | ||
cut_table["n_events"][bin_idx] = n_events | ||
|
||
if n_events < min_events: | ||
cut_table["cut"][bin_idx] = fill_value | ||
cut_table["cut"].value[bin_idx] = fill_value | ||
else: | ||
value = np.nanpercentile(group["values"], percentile) | ||
if min_value is not None or max_value is not None: | ||
|
@@ -93,6 +107,37 @@ def calculate_percentile_cut( | |
return cut_table | ||
|
||
|
||
def evaluate_binned_cut_by_index(values, bin_index, valid, cut_table, op): | ||
""" | ||
Evaluate a binned cut as defined in cut_table on given events. | ||
|
||
Events with bin_values outside the bin edges defined in cut table | ||
will be set to False. | ||
|
||
Parameters | ||
---------- | ||
values: ``~numpy.ndarray`` or ``~astropy.units.Quantity`` | ||
The values on which the cut should be evaluated | ||
cut_table: ``~astropy.table.Table`` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. missing doc for bin_index |
||
A table describing the binned cuts, e.g. as created by | ||
``~pyirf.cuts.calculate_percentile_cut``. | ||
Required columns: | ||
- `low`: lower edges of the bins | ||
- `high`: upper edges of the bins, | ||
- `cut`: cut value | ||
op: callable(a, b) -> bool | ||
A function taking two arguments, comparing element-wise and | ||
returning an array of booleans. | ||
Must support vectorized application. | ||
""" | ||
if not isinstance(cut_table, QTable): | ||
raise ValueError('cut_table needs to be an astropy.table.QTable') | ||
|
||
result = np.zeros(len(bin_index), dtype=bool) | ||
result[valid] = op(values[valid], cut_table["cut"][bin_index[valid]]) | ||
return result | ||
|
||
|
||
def evaluate_binned_cut(values, bin_values, cut_table, op): | ||
""" | ||
Evaluate a binned cut as defined in cut_table on given events. | ||
|
@@ -123,10 +168,7 @@ def evaluate_binned_cut(values, bin_values, cut_table, op): | |
|
||
bins = np.append(cut_table["low"], cut_table["high"][-1]) | ||
bin_index, valid = calculate_bin_indices(bin_values, bins) | ||
|
||
result = np.zeros(len(values), dtype=bool) | ||
result[valid] = op(values[valid], cut_table["cut"][bin_index[valid]]) | ||
return result | ||
return evaluate_binned_cut_by_index(values, bin_index, valid, cut_table, op) | ||
|
||
|
||
def compare_irf_cuts(cuts): | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doc is wrong: it's not the cuts being passed here, but the array of values over which the brute-force optimization scan is done, right? Also, for symmetry, shouldn't there be a similar input for multiplicity?