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

refactor: Deprecate reader API for more Pythonic style #87

Merged
merged 6 commits into from
Apr 20, 2021
Merged
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
2 changes: 1 addition & 1 deletion examples/awkward_example.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
],
"source": [
"pylhe.register_awkward()\n",
"arr = pylhe.to_awkward(pylhe.readLHEWithAttributes(\"unweighted_events.lhe\"))\n",
"arr = pylhe.to_awkward(pylhe.read_lhe_with_attributes(\"unweighted_events.lhe\"))\n",
"arr"
]
},
Expand Down
4 changes: 2 additions & 2 deletions examples/zpeak.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@
}
],
"source": [
"for e in pylhe.readLHE(\"./unweighted_events.lhe\"):\n",
"for e in pylhe.read_lhe(\"./unweighted_events.lhe\"):\n",
" h.fill(invariant_mass(e.particles[-1], e.particles[-2]), weight=e.eventinfo.weight)\n",
"h"
]
Expand Down Expand Up @@ -143,7 +143,7 @@
"source": [
"import itertools\n",
"\n",
"events = pylhe.readLHE(\"./unweighted_events.lhe\")\n",
"events = pylhe.read_lhe(\"./unweighted_events.lhe\")\n",
"for i, e in enumerate(itertools.islice(events, 0, 2)):\n",
" filename = f\"event{i}.pdf\"\n",
" pylhe.visualize(e, filename)"
Expand Down
4 changes: 4 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,9 @@ install_requires =
where = src

[flake8]
# E501: line too long
extend-ignore = E501
max-line-length = 88
max-complexity = 18
count = True
statistics = True
20 changes: 10 additions & 10 deletions src/pylhe/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
"LHEParticle",
"LHEProcInfo",
"loads",
"readLHE",
"readLHEInit",
"readLHEWithAttributes",
"readNumEvents",
"read_lhe",
"read_lhe_init",
"read_lhe_with_attributes",
"read_num_events",
"register_awkward",
"to_awkward",
"visualize",
Expand Down Expand Up @@ -165,7 +165,7 @@ def _extract_fileobj(filepath):
)


def readLHEInit(filepath):
def read_lhe_init(filepath):
"""
Read and return the init blocks. This encodes the weight group
and related things according to https://arxiv.org/abs/1405.1067
Expand Down Expand Up @@ -214,7 +214,7 @@ def readLHEInit(filepath):
return initDict


def readLHE(filepath):
def read_lhe(filepath):
try:
with _extract_fileobj(filepath) as fileobj:
for event, element in ET.iterparse(fileobj, events=["end"]):
Expand All @@ -229,9 +229,9 @@ def readLHE(filepath):
return


def readLHEWithAttributes(filepath):
def read_lhe_with_attributes(filepath):
"""
Iterate through file, similar to readLHE but also set
Iterate through file, similar to read_lhe but also set
weights and attributes.
"""
try:
Expand Down Expand Up @@ -271,9 +271,9 @@ def readLHEWithAttributes(filepath):
return


def readNumEvents(filepath):
def read_num_events(filepath):
"""
Moderately efficent way to get the number of events stored in file.
Moderately efficient way to get the number of events stored in file.
"""
with _extract_fileobj(filepath) as fileobj:
return sum(
Expand Down
8 changes: 4 additions & 4 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ def test_top_level_api():
"LHEParticle",
"LHEProcInfo",
"loads",
"readLHE",
"readLHEInit",
"readLHEWithAttributes",
"readNumEvents",
"read_lhe",
"read_lhe_init",
"read_lhe_with_attributes",
"read_num_events",
"register_awkward",
"to_awkward",
"visualize",
Expand Down
16 changes: 8 additions & 8 deletions tests/test_lhe_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,15 @@ def test_gzip_open(tmpdir, testdata_gzip_file):
assert isinstance(pylhe._extract_fileobj(Path(testdata_gzip_file)), gzip.GzipFile)


def test_event_count(testdata_gzip_file):
assert pylhe.readNumEvents(TEST_FILE) == 791
assert pylhe.readNumEvents(TEST_FILE) == pylhe.readNumEvents(testdata_gzip_file)
def test_read_num_events(testdata_gzip_file):
assert pylhe.read_num_events(TEST_FILE) == 791
assert pylhe.read_num_events(TEST_FILE) == pylhe.read_num_events(testdata_gzip_file)


def test_lhe_init(testdata_gzip_file):
assert pylhe.readLHEInit(TEST_FILE) == pylhe.readLHEInit(testdata_gzip_file)
assert pylhe.read_lhe_init(TEST_FILE) == pylhe.read_lhe_init(testdata_gzip_file)

init_data = pylhe.readLHEInit(TEST_FILE)
init_data = pylhe.read_lhe_init(TEST_FILE)
init_info = init_data["initInfo"]
assert init_info["beamA"] == pytest.approx(1.0)
assert init_info["beamB"] == pytest.approx(2.0)
Expand All @@ -65,6 +65,6 @@ def test_lhe_init(testdata_gzip_file):
assert init_info["numProcesses"] == pytest.approx(8.0)


def test_readLHE(testdata_gzip_file):
assert pylhe.readLHE(TEST_FILE)
assert pylhe.readLHE(testdata_gzip_file)
def test_read_lhe(testdata_gzip_file):
assert pylhe.read_lhe(TEST_FILE)
assert pylhe.read_lhe(testdata_gzip_file)
2 changes: 1 addition & 1 deletion tests/test_visualize.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

def test_visualize(tmpdir):
lhe_file = skhep_testdata.data_path("pylhe-testfile-pr29.lhe")
events = pylhe.readLHEWithAttributes(lhe_file)
events = pylhe.read_lhe_with_attributes(lhe_file)
start_event = 1
stop_event = 2
filename = tmpdir.join(f"event{start_event}.pdf")
Expand Down