From d84e3259f8da51800f3fc80f096f89199e7d10e1 Mon Sep 17 00:00:00 2001 From: Junghwan John Goh Date: Tue, 30 Aug 2022 01:02:43 +0900 Subject: [PATCH] fix: limit number of particles using the eventinfo (#103) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * take particles up to the eventinfo.nparticles. otherwise put into the optinal field * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * synchronize with the master branch * Add test * Please linting checks * ci: Add semantic PR check GHA workflow (#123) * As https://github.com/zeke/semantic-pull-requests is no longer maintained and is down, use the suggested alternative of https://github.com/amannn/action-semantic-pull-request to check that PR titles follow the Conventional Commits spec. * feat: Add IPython visualisation and switch to graphviz (#118) * Add graphviz>=0.12.0 as a core dependency. * Add graphviz.Digrap graph object as .graph property to LHEFile. - Add support for IPython visualization. * Add test for LHEEvent_graph. * refactor: Use absolute imports over explicit relative imports (#124) * Change all explicit relative imports to absolute imports. - Provides clearer more explicit imports for new developers. * Add absolufy-imports to pre-commit hooks * feat: Use tbump over bump2version (#125) * Add tbump to 'develop' extra and remove bump2version. * Add tbump.toml to configure tbump. - Allow for valid versions to follow SemVer and also support release candidates: ..rc * Remove .bumpversion.cfg. * Bump version: 0.2.1 → 0.3.0 * docs: Update dev team affiliations (#126) * Lukas Heinrich is a professor at Technical University of Munich as of March 2022. * Matthew Feickert is a postdoc at University of Wisconsin-Madison as of June 2022. * docs: Add Eduardo Rodrigues to dev team (#127) * Add Eduardo Rodrigues to development team listing. - Update Zenodo citation metadata, recommended citation, and setup.cfg. * chore: [pre-commit.ci] pre-commit autoupdate (#129) * Update pre-commit hooks: - github.com/asottile/pyupgrade: v2.32.0 → v2.32.1 * docs: Add a 'get started' example to the README (#130) * Add example to README that shows how to open an LHE file and interact with the events. * chore: [pre-commit.ci] pre-commit autoupdate (#133) * Update pre-commit hooks: - github.com/pre-commit/pre-commit-hooks: v4.2.0 → v4.3.0 - github.com/asottile/pyupgrade: v2.31.1 → v2.34.0 - github.com/psf/black: 22.3.0 → 22.6.0 * take particles up to the eventinfo.nparticles. otherwise put into the optinal field * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * synchronize with the master branch * Cover lines added/changed Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: eduardo-rodrigues Co-authored-by: Matthew Feickert --- README.md | 32 ++++++++++++++++++++++++++++++++ src/pylhe/__init__.py | 5 +++-- tests/test_lhe_reader.py | 22 +++++++++++++++++++++- 3 files changed, 56 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index d56be5ad..2a2a68b8 100644 --- a/README.md +++ b/README.md @@ -56,6 +56,38 @@ event.graph.render(filename="test", format="png", cleanup=True) event.graph.render(filename="test", format="pdf", cleanup=True) ``` +## Get started + +The example below provides a simple overview. +Full functionality can be inspected from the functions provided in the `pylhe` module. + +```python +import itertools + +# You can use LHE files from scikit-hep-testdata +from skhep_testdata import data_path + +import pylhe + +lhe_file = data_path("pylhe-testlhef3.lhe") +events = pylhe.read_lhe_with_attributes(lhe_file) +print(f"Number of events: {pylhe.read_num_events(lhe_file)}") + +# Get event 1 +event = next(itertools.islice(events, 1, 2)) + +# A DOT language graph of the event can be inspected as follows +print(event.graph.source) + +# The graph is nicely displayed as SVG in Jupyter notebooks +event + +# To save a DOT graph render the graph to a supported image format +# (refer to the Graphviz documentation for more) +event.graph.render(filename="test", format="png", cleanup=True) +event.graph.render(filename="test", format="pdf", cleanup=True) +``` + ## Citation The preferred BibTeX entry for citation of `pylhe` is diff --git a/src/pylhe/__init__.py b/src/pylhe/__init__.py index 6b83fd66..1bafcc5d 100644 --- a/src/pylhe/__init__.py +++ b/src/pylhe/__init__.py @@ -283,9 +283,10 @@ def read_lhe(filepath): with _extract_fileobj(filepath) as fileobj: for event, element in ET.iterparse(fileobj, events=["end"]): if element.tag == "event": - data = element.text.split("\n")[1:-1] + data = element.text.strip().split("\n") eventdata, particles = data[0], data[1:] eventinfo = LHEEventInfo.fromstring(eventdata) + particles = particles[: int(eventinfo.nparticles)] particle_objs = [LHEParticle.fromstring(p) for p in particles] yield LHEEvent(eventinfo, particle_objs) except ET.ParseError as excep: @@ -303,7 +304,7 @@ def read_lhe_with_attributes(filepath): for event, element in ET.iterparse(fileobj, events=["end"]): if element.tag == "event": eventdict = {} - data = element.text.split("\n")[1:-1] + data = element.text.strip().split("\n") eventdata, particles = data[0], data[1:] eventdict["eventinfo"] = LHEEventInfo.fromstring(eventdata) eventdict["particles"] = [] diff --git a/tests/test_lhe_reader.py b/tests/test_lhe_reader.py index 87f6baa1..0b022335 100644 --- a/tests/test_lhe_reader.py +++ b/tests/test_lhe_reader.py @@ -66,5 +66,25 @@ def test_lhe_init(testdata_gzip_file): def test_read_lhe(testdata_gzip_file): - assert pylhe.read_lhe(TEST_FILE) assert pylhe.read_lhe(testdata_gzip_file) + + +def test_read_lhe_internals(): + events = pylhe.read_lhe(TEST_FILE) + + assert events + for e in events: + assert e is not None + + +def test_issue_102(): + """ + Test a file containing lines starting with "#aMCatNLO". + """ + test_file = skhep_testdata.data_path("pylhe-testlhef3.lhe") + + assert pylhe.read_num_events(test_file) == 59 + assert ( + pylhe.read_lhe(test_file).__sizeof__() + == pylhe.read_lhe_with_attributes(test_file).__sizeof__() + )