From a6287c7450a6dda2ab48c20d82d24d07839cd1e9 Mon Sep 17 00:00:00 2001 From: Mark Jones Date: Thu, 10 Jun 2021 10:47:34 -0400 Subject: [PATCH] Patches for GAMP parsing and Particle display The memory module of GAMP would use a dictionary to store the particle data as an intermediate step, using the particle IDs as the key in the dictionary. This worked great, until there were multiple particles with the same ID. This patches that out by using the index of the particle instead. This assumes that each event will have particles appear in the same order consistently throughout the file, which _should_ be a safe bet. There was also an issue with some rushed out code involving the how Particles are visually represented in iPython and Jupyter. The underlying data structures were safe, but when the user would try to view the values, errors would occur. This should also patch those issues. However, there are no improvements in Testing with these sort of functions since at this time I have not found a reliable way to test that the display function is operating correctly. --- CHANGELOG.md | 8 +++++- PyPWA/info.py | 2 +- PyPWA/libs/vectors/particle.py | 7 ++--- PyPWA/plugins/data/gamp.py | 31 ++++++++++++---------- setup.py | 2 +- tests/libs/vectors/test_particle.py | 4 +++ tests/plugins/data/test_gamp.py | 41 +++++++++++++++++++++++++++++ tests/test_data/docs/multiple.gamp | 35 ++++++++++++++++++++++++ 8 files changed, 110 insertions(+), 20 deletions(-) create mode 100644 tests/plugins/data/test_gamp.py create mode 100644 tests/test_data/docs/multiple.gamp diff --git a/CHANGELOG.md b/CHANGELOG.md index 4768ba07..36aa2d6b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,11 @@ All changes important to the user will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/) +## [3.2.1] - 2021-6-10 +### Fixed +- Gamp no longer combines particles with the same ID +- Fixed issue where display_raw would fail in Jupyter with Particles + ## [3.2.0] - 2021-6-1 ### Added - Vectors now support iPython and Jupyter Pretty printing @@ -189,7 +194,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/) - PySim plugin - Packaging -[Unreleased]: https://github.com/JeffersonLab/PyPWA/compare/v3.2.0...main +[Unreleased]: https://github.com/JeffersonLab/PyPWA/compare/v3.2.1...main +[3.2.1]: https://github.com/JeffersonLab/PyPWA/compare/v3.2.0...v3.2.1 [3.2.0]: https://github.com/JeffersonLab/PyPWA/compare/v3.1.0...v3.2.0 [3.1.0]: https://github.com/JeffersonLab/PyPWA/compare/v3.0.0...v3.1.0 [3.0.0]: https://github.com/JeffersonLab/PyPWA/compare/v3.0.0a1...v3.0.0 diff --git a/PyPWA/info.py b/PyPWA/info.py index 6b72b413..2896e109 100644 --- a/PyPWA/info.py +++ b/PyPWA/info.py @@ -91,7 +91,7 @@ __credits__ = ["Mark Jones"] AUTHOR = "PyPWA Team and Contributors" -VERSION = "3.2.0" +VERSION = "3.2.1" RELEASE = f"{VERSION}" LICENSE = "GPLv3" STATUS = "development" diff --git a/PyPWA/libs/vectors/particle.py b/PyPWA/libs/vectors/particle.py index 3b88117a..544d41ec 100644 --- a/PyPWA/libs/vectors/particle.py +++ b/PyPWA/libs/vectors/particle.py @@ -149,9 +149,8 @@ def display_raw(self): df = pd.DataFrame() df['e'], df['x'], df['y'], df['z'] = self.e, self.x, self.y, self.z - display( - f'{self.__particle_id}: {self.__particle_name}', raw=True - ) + name = f"{self.__particle_id}: {self.__particle_name}" + display(name) display(df) def __getitem__( @@ -311,6 +310,7 @@ def _repr_pretty_(self, p, cycle): else: for particle in self.__particle_list: particle._repr_pretty_(p, cycle) + p.text('\n') def _repr_html_(self): html = "" @@ -320,6 +320,7 @@ def _repr_html_(self): def display_raw(self): for p in self.__particle_list: + print('\n') p.display_raw() def __len__(self): diff --git a/PyPWA/plugins/data/gamp.py b/PyPWA/plugins/data/gamp.py index b65f3945..8feac3da 100644 --- a/PyPWA/plugins/data/gamp.py +++ b/PyPWA/plugins/data/gamp.py @@ -30,7 +30,7 @@ """ from pathlib import Path -from typing import Dict, List +from typing import Dict, List, Tuple import numpy as np @@ -223,16 +223,16 @@ def parse(self, filename: Path) -> vectors.ParticlePool: for event_index, line in enumerate(stream): particle_num = int(line) - for i in range(particle_num): + for index in range(particle_num): line = stream.readline() p_id, charge, x, y, z, e = line.strip("\n").split() - particle_dict[int(p_id)][event_index]["x"] = x - particle_dict[int(p_id)][event_index]["y"] = y - particle_dict[int(p_id)][event_index]["z"] = z - particle_dict[int(p_id)][event_index]["e"] = e + particle_dict[index][1][event_index]["x"] = x + particle_dict[index][1][event_index]["y"] = y + particle_dict[index][1][event_index]["z"] = z + particle_dict[index][1][event_index]["e"] = e particles = [] - for p_id, momenta in particle_dict.items(): + for index, (p_id, momenta) in particle_dict.items(): particles.append( vectors.Particle(p_id, momenta) ) @@ -242,19 +242,22 @@ def parse(self, filename: Path) -> vectors.ParticlePool: @staticmethod def _make_particle_dict( filename: Path, particle_length: int = 1 - ) -> Dict[int, np.ndarray]: + ) -> Dict[int, Tuple[int, np.ndarray]]: with filename.open() as stream: count = int(stream.readline()) lines = [stream.readline() for i in range(count)] particles = dict() - for line in lines: + for index, line in enumerate(lines): p_id, charge, x, y, z, e = line.strip("\n").split() - particles[int(p_id)] = np.empty( - particle_length, - dtype=np.dtype( - [("x", "f8"), ("y", "f8"), ("z", "f8"), ("e", "f8")], - align=True + particles[index] = ( + int(p_id), + np.empty( + particle_length, + dtype=np.dtype( + [("x", "f8"), ("y", "f8"), ("z", "f8"), ("e", "f8")], + align=True + ) ) ) return particles diff --git a/setup.py b/setup.py index 25b21fe3..ee383ef4 100644 --- a/setup.py +++ b/setup.py @@ -20,7 +20,7 @@ __author__ = "PyPWA Team and Contributors" __license__ = "GPLv3" -__version__ = "3.2.0" +__version__ = "3.2.1" __email__ = "pypwa@jlab.org" __status__ = "development" diff --git a/tests/libs/vectors/test_particle.py b/tests/libs/vectors/test_particle.py index 1812ba99..e7ab4492 100644 --- a/tests/libs/vectors/test_particle.py +++ b/tests/libs/vectors/test_particle.py @@ -52,3 +52,7 @@ def test_particle_can_do_math(): assert c.x == 8.2 assert c.y == 12.2 assert c.z == 16.2 + + +def test_particle_raw_display(random_particle_pool): + random_particle_pool.display_raw() diff --git a/tests/plugins/data/test_gamp.py b/tests/plugins/data/test_gamp.py new file mode 100644 index 00000000..1e004d6c --- /dev/null +++ b/tests/plugins/data/test_gamp.py @@ -0,0 +1,41 @@ +import pytest + +from PyPWA.plugins.data import gamp +from PyPWA.libs.vectors import particle + +from pathlib import Path + +ROOT = (Path(__file__).parent / "../../test_data/docs").resolve() +LARGE = ROOT / "large.gamp" +MULTI = ROOT / "multiple.gamp" + + +@pytest.fixture() +def gamp_mem(): + return gamp._GampMemory() + + +@pytest.fixture() +def large_gamp(gamp_mem): + return gamp_mem.parse(LARGE) + + +@pytest.fixture() +def multi_gamp(gamp_mem): + return gamp_mem.parse(MULTI) + + +def test_large_has_all_events(large_gamp): + assert 1000 == large_gamp.event_count + + +def test_multi_has_all_events(multi_gamp): + assert 5 == multi_gamp.event_count + + +def test_large_has_four_particles(large_gamp): + assert 4 == len(large_gamp) + + +def test_multi_has_six_particles(multi_gamp): + assert 6 == len(multi_gamp) diff --git a/tests/test_data/docs/multiple.gamp b/tests/test_data/docs/multiple.gamp new file mode 100644 index 00000000..f3f1f49b --- /dev/null +++ b/tests/test_data/docs/multiple.gamp @@ -0,0 +1,35 @@ +6 +1 0 0 0 9 9 +14 1 0.489366 1.16687 1.53077 2.19651 +11 1 -0.120747 -0.252622 1.82881 1.91485 +12 -1 -0.0471136 -0.468373 2.86627 2.94632 +1 0 0.144554 -0.0924801 0.396306 0.431864 +1 0 -0.46606 -0.353392 2.37784 2.44872 +6 +1 0 0 0 9 9 +14 1 0.0482325 0.574326 0.588864 1.24871 +11 1 0.147839 0.175565 3.20347 3.2494 +12 -1 0.113935 0.0947125 1.91567 1.9838 +1 0 0.116443 -0.468739 1.61144 1.68227 +1 0 -0.426449 -0.375865 1.68055 1.77409 +6 +1 0 0 0 9 9 +14 1 -0.603583 0.264803 7.6721 7.75731 +11 1 0.277164 -0.0236316 0.372865 0.678319 +12 -1 0.0582607 -0.150773 0.285835 0.592907 +1 0 0.0214529 0.210537 0.337733 0.39856 +1 0 0.246705 -0.300936 0.331466 0.511171 +6 +1 0 0 0 9 9 +14 1 -0.6922 -0.280769 4.1272 4.29791 +11 1 0.59143 0.0433621 2.47691 2.59431 +12 -1 0.56086 0.262788 2.14699 2.28843 +1 0 -0.220055 0.120091 0.374064 0.450299 +1 0 -0.240035 -0.145471 -0.125159 0.307316 +6 +1 0 0 0 9 9 +14 1 -1.08832 -0.149006 2.61188 2.98478 +11 1 0.620169 -0.015586 2.69034 2.80472 +12 -1 0.534044 -0.126931 3.35861 3.43879 +1 0 -0.00930206 -0.143393 0.206859 0.251871 +1 0 -0.0565951 0.434916 0.132317 0.458108