-
Notifications
You must be signed in to change notification settings - Fork 185
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
feat: Enhance phys/root/filter functionality #3250
Merged
fgvieira
merged 7 commits into
snakemake:master
from
GoodingJamie:enhance-phys-root-filter
Sep 17, 2024
Merged
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
f6fbe03
Add cut label functionality and list/dict criteria parsing
GoodingJamie 2fa7501
Add tests for other criteria cases
GoodingJamie 200d883
Fix dict parsing and formatting
GoodingJamie 7506314
Update tests with correct respective criteria
GoodingJamie 46c76b6
Fix to dict criteria implementation and report booking
GoodingJamie 37233f4
Remove default value of criteria parameter
GoodingJamie 42f09d7
Update "meta.yaml"
GoodingJamie 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,52 @@ | ||
rule define_columns: | ||
rule filter_str: | ||
input: | ||
"ntuple0.root", | ||
output: | ||
"ntuple0_output.root", | ||
"ntuple0_str_output.root", | ||
log: | ||
"logs/filter/filter.log", | ||
"logs/filter/filter_str.log", | ||
params: | ||
input_tree_name="TestTree", | ||
output_tree_name="TestTree", | ||
criteria="(pt > 1400) && (pz > 19000)", | ||
branches_to_save=["pz", "pt", "p"], | ||
threads: 2 | ||
wrapper: | ||
"master/phys/root/filter" | ||
|
||
rule filter_list: | ||
input: | ||
"ntuple0.root", | ||
output: | ||
"ntuple0_list_output.root", | ||
log: | ||
"logs/filter/filter_list.log", | ||
params: | ||
input_tree_name="TestTree", | ||
output_tree_name="TestTree", | ||
criteria="pt > 1400", | ||
criteria=["pt > 1400", "pz > 19000"], | ||
branches_to_save=["pz", "pt", "p"], | ||
verbose=True | ||
threads: 2 | ||
wrapper: | ||
"master/phys/root/filter" | ||
|
||
rule filter_dict: | ||
input: | ||
"ntuple0.root", | ||
output: | ||
"ntuple0_dict_output.root", | ||
log: | ||
"logs/filter/filter.log", | ||
params: | ||
input_tree_name="TestTree", | ||
output_tree_name="TestTree", | ||
criteria={ | ||
"PT cut": "pt > 1400", | ||
"PZ cut": "pz > 19000" | ||
}, | ||
branches_to_save=["pz", "pt", "p"], | ||
verbose=True | ||
threads: 1 | ||
wrapper: | ||
"master/phys/root/filter" |
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 |
---|---|---|
@@ -1,18 +1,39 @@ | ||
__author__ = "Anfeng Li" | ||
__author__ = "Anfeng Li, Jamie Gooding" | ||
__copyright__ = "Copyright 2024, Anfeng Li" | ||
__email__ = "[email protected]" | ||
__email__ = "[email protected], [email protected]" | ||
__license__ = "MIT" | ||
|
||
|
||
from typing import Dict, List | ||
import ROOT | ||
|
||
ROOT.EnableImplicitMT(snakemake.threads) | ||
if snakemake.threads > 1: | ||
ROOT.EnableImplicitMT(snakemake.threads) | ||
else: | ||
ROOT.DisableImplicitMT() | ||
|
||
# Parse criteria | ||
_smk_criteria = snakemake.params.get("criteria", "true") | ||
if isinstance(_smk_criteria, str): | ||
criteria = [_smk_criteria] | ||
labels = [_smk_criteria] | ||
elif isinstance(_smk_criteria, List): | ||
criteria = _smk_criteria | ||
labels = _smk_criteria | ||
elif isinstance(_smk_criteria, Dict): | ||
criteria = _smk_criteria.values() | ||
labels = _smk_criteria.keys() | ||
else: | ||
raise TypeError("Parameter 'criteria' should be type of 'str', 'list' or 'dict'") | ||
|
||
criteria = snakemake.params.get("criteria", "true") | ||
branches_to_save = snakemake.params.get("branches_to_save", None) | ||
|
||
df = ROOT.RDataFrame(snakemake.params.input_tree_name, snakemake.input[0]) | ||
df = df.Filter(criteria) | ||
for criterion, label in zip(criteria, labels): | ||
df = df.Filter(criterion, label) | ||
|
||
if snakemake.params.get("verbose", False): | ||
report = df.Report() | ||
|
||
if branches_to_save is not None: | ||
df.Snapshot( | ||
snakemake.params.output_tree_name, | ||
|
@@ -21,3 +42,6 @@ | |
) | ||
else: | ||
df.Snapshot(snakemake.params.output_tree_name, snakemake.output[0]) | ||
|
||
if snakemake.params.get("verbose", False): | ||
report.Print() |
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.
Won't
"true"
be interpreted as a string filtering criteria with'criterion = "true"; label = "true"
? Is this intended?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.
Ah, yes it will be! This is the behaviour in master currently—the only use case I can think of would be to copy a tree in one file to a differently named tree in another file, but there's
rootcp
for this (may be the subject of a PR in the near future!). So I think here it would be safe to change this and require thatcriteria
is passed:where a missing
params.criteria
should raise aTypeError
when being converted tocriteria
andlabels
.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.
But isn't
criteria
optional? At least that is what it says on themeta.yaml
file.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.
criteria
is optional currently, but there isn't a use case I can think of for not passingcriteria
which isn't already covered byrootcp
(for which I've opened #3251). I'll update this inmeta.yaml
to reflect this