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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added
- add support for --field-prefix option at cli

## [2.4.0]

### Addded
### Added
- RichHistogram class for use with the `rich` package.
- Argument to set maximum count in bins for plotting.

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
10 changes: 10 additions & 0 deletions histoprint/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,14 @@
"of array-like branches, e.g. use 'tree/branch[:,2]' to histogram the 3rd "
"elements of a vector-like branch.",
)
@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",
"--cut",
Expand Down Expand Up @@ -109,6 +117,8 @@ def histoprint(infile, **kwargs):

INFILE can be '-', in which case the data is read from STDIN.
"""
if (prefix := kwargs.pop("field_prefix", None)) is not None:
kwargs["fields"] = map(lambda s: prefix + s, kwargs["fields"])

# Read file into buffer for use by implementations
try:
Expand Down
10 changes: 10 additions & 0 deletions tests/data/2D-prefixable.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Txx,Txy
1,4
2,5
2,5
3,6
3,nan
3,6
4,7
4,7
5,8
38 changes: 38 additions & 0 deletions tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

import numpy as np
import pytest
from click.testing import CliRunner
from uhi.numpy_plottable import ensure_plottable_histogram

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


def test_hist():
Expand Down Expand Up @@ -181,3 +183,39 @@ 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()

res = runner.invoke(
cli,
[
"-s",
"tests/data/2D-prefixable.csv",
"-f",
"Txx",
"-f",
"Txy",
],
)
res_prefixed = runner.invoke(
cli,
[
"-s",
"tests/data/2D-prefixable.csv",
"-f",
"x",
"-f",
"y",
"--field-prefix",
"Tx",
],
)

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