Skip to content

Commit

Permalink
Patches for GAMP parsing and Particle display
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
markjonestx committed Jun 10, 2021
1 parent 5237879 commit a6287c7
Show file tree
Hide file tree
Showing 8 changed files with 110 additions and 20 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion PyPWA/info.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
7 changes: 4 additions & 3 deletions PyPWA/libs/vectors/particle.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__(
Expand Down Expand Up @@ -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 = ""
Expand All @@ -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):
Expand Down
31 changes: 17 additions & 14 deletions PyPWA/plugins/data/gamp.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"""

from pathlib import Path
from typing import Dict, List
from typing import Dict, List, Tuple

import numpy as np

Expand Down Expand Up @@ -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)
)
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

__author__ = "PyPWA Team and Contributors"
__license__ = "GPLv3"
__version__ = "3.2.0"
__version__ = "3.2.1"
__email__ = "[email protected]"
__status__ = "development"

Expand Down
4 changes: 4 additions & 0 deletions tests/libs/vectors/test_particle.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
41 changes: 41 additions & 0 deletions tests/plugins/data/test_gamp.py
Original file line number Diff line number Diff line change
@@ -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)
35 changes: 35 additions & 0 deletions tests/test_data/docs/multiple.gamp
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit a6287c7

Please sign in to comment.