Skip to content
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

add --field-prefix via click.option and update doc #124

Draft
wants to merge 8 commits into
base: master
Choose a base branch
from
Draft
1 change: 1 addition & 0 deletions .github/workflows/pythontests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ jobs:
wget --quiet http://scikit-hep.org/uproot3/examples/Event.root
histoprint -s Event.root -f T/event/fTracks/fTracks.fYfirst -f T/event/fTracks/fTracks.fYlast[:,0]
histoprint -s Event.root -f T/fTracks.fYfirst -f T/event/fTracks/fTracks.fYlast[:,0] -C 'fNtrack > 5'
histoprint -s Event.root --field-prefix T/event/fTracks/fTracks. -f fYfirst -f fYlast[:,0]
histoprint -s Event.root -f htime

pre-commit:
Expand Down
6 changes: 6 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,12 @@ It can read in files or take data directly from STDIN::
'tree/branch[:,2]' to histogram the 3rd
elements of a vector-like branch.

--field-prefix TEXT String to prepend to each argument passed to
--field option following --field-prefix E.g.
'histoprint -f Muon_eta --field-prefix Tau_
-f eta -f phi' ... would refer to fields
'Muon_eta', 'Tau_eta' and 'Tau_phi''

-C, --cut TEXT Filter the data to be plotted by a cut
condition. For ROOT files, variables must be
referenced by their branch name within the
Expand Down
16 changes: 16 additions & 0 deletions histoprint/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@
import histoprint.formatter as formatter


def fieldprefixer_callback(ctx, param, fields):
if "field_prefix" in ctx.params:
fields = map(lambda field: ctx.params["field_prefix"] + field, fields)
return tuple(fields)


@click.command()
@click.argument("infile", type=click.Path(exists=True, dir_okay=False, allow_dash=True))
@click.option(
Expand Down Expand Up @@ -74,6 +80,15 @@
"single TH1, or one or more paths to TTree branches. Also supports slicing "
"of array-like branches, e.g. use 'tree/branch[:,2]' to histogram the 3rd "
"elements of a vector-like branch.",
callback=fieldprefixer_callback,
)
@click.option(
"--field-prefix",
"field_prefix",
type=str,
multiple=False,
help="String to prepend to all values indicated with --field option, "
"ignores position where this and --field options are specified.",
Comment on lines +83 to +84
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
help="String to prepend to all values indicated with --field option, "
"ignores position where this and --field options are specified.",
help="String to prepend to all '--field' values",

Command line options usually do not care about order so mentioning it seems more confusing than helping.

Also need to update the README. I usually just copy the output of histoprint -h directly into it, so it is exactly the same.

)
@click.option(
"-C",
Expand Down Expand Up @@ -109,6 +124,7 @@ def histoprint(infile, **kwargs):

INFILE can be '-', in which case the data is read from STDIN.
"""
del kwargs["field_prefix"]

# Read file into buffer for use by implementations
try:
Expand Down
39 changes: 39 additions & 0 deletions tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import numpy as np
import pytest
from uhi.numpy_plottable import ensure_plottable_histogram
from click.testing import CliRunner

from histoprint.cli import histoprint as cli
import histoprint as hp


Expand Down Expand Up @@ -181,3 +183,40 @@ def test_rich_histogram():
tab.add_row(hist, Align.center(hist), Align.right(hist))

rich.print(tab)


def test_cli_opt_field_prefix():
"""Test equivalence of --field-prefix cli option to explicit prepend on --field."""

runner = CliRunner()

# tests for fields "two" and "three" since share prefix of "t"
res = runner.invoke(
cli,
[
"-s",
"tests/data/histograms.root",
"-f",
"two",
"-f",
"three",
],
)
res_prefixed = runner.invoke(
cli,
[
"-s",
"tests/data/histograms.root",
"-f",
"wo",
"-f",
"hree",
"--field-prefix",
"t",
],
)

# assert succesful equivalent output
assert res.exit_code == 0
assert res_prefixed.exit_code == 0
assert res.output == res_prefixed.output