Skip to content

Commit

Permalink
added tests for MomentumArray #143
Browse files Browse the repository at this point in the history
  • Loading branch information
jacanchaplais committed Aug 31, 2023
1 parent 4dfd465 commit 8d3a4d4
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 6 deletions.
31 changes: 31 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: tests

on:
- push

jobs:
test:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
pyver: ["3.8", "3.9", "3.10", "3.11"]
steps:
- uses: actions/checkout@v3

- name: Install Conda environment from environment.yml
uses: mamba-org/provision-with-micromamba@main
with:
cache-env: true
extra-specs: |
python=${{ matrix.pyver }}
pytest=6.2.5
hypothesis=6.62.0
- name: Install graphicle
shell: bash -l {0}
run: pip install .

- name: Run tests
shell: bash -l {0}
run: pytest
67 changes: 61 additions & 6 deletions tests/test_data.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,68 @@
import cmath
import dataclasses as dc
import math

import numpy as np
import typicle

import graphicle
import graphicle as gcl

ZERO_TOL = 1.0e-10 # absolute tolerance for detecting zero-values


@dc.dataclass
class MomentumExample:
"""Dataclass for representing the four-momentum of a particle, based
on its cylindrical beamline coordinates.
Parameters
----------
pt, phi, pz, energy : float
Components of the four-momentum in cylindrical coordinates.
Note that ``phi`` is given in units of pi.
"""

pt: float = 3.0
phi: float = 0.75
pz: float = 4.0
energy: float = 5.0

@property
def _polar(self) -> complex:
return self.pt * cmath.exp(complex(0, self.phi * math.pi))

types_ = typicle.Types()
@property
def px(self) -> float:
"""x-component of four momentum."""
return self._polar.real

@property
def py(self) -> float:
"""y-component of four momentum."""
return self._polar.imag

def to_momentum_array(self) -> gcl.MomentumArray:
"""Formats the components in a ``MomentumArray``."""
return gcl.MomentumArray([(self.px, self.py, self.pz, self.energy)])


def test_pdgs():
pdg_vals = np.arange(1, 7, dtype=types_.int)
pdgs = graphicle.PdgArray(pdg_vals)
assert list(pdgs.name) == ["d", "u", "s", "c", "b", "t"]
pdg_vals = np.arange(1, 7, dtype=np.int32)
pdgs = gcl.PdgArray(pdg_vals)
assert pdgs.name.tolist() == ["d", "u", "s", "c", "b", "t"]


def test_pmu_coords() -> None:
momentum = MomentumExample()
pmu = momentum.to_momentum_array()
assert math.isclose(pmu.pt.item(), momentum.pt)
assert math.isclose(pmu.phi.item(), momentum.phi * math.pi)
assert math.isclose(pmu.mass.item(), 0.0, abs_tol=ZERO_TOL)
assert math.isclose(pmu.theta.item(), math.atan(momentum.pt / momentum.pz))


def test_pmu_transform() -> None:
momentum = MomentumExample()
pmu = momentum.to_momentum_array()
zero_transverse = pmu.shift_phi(math.pi) + pmu
assert math.isclose(0.0, zero_transverse.pt.item(), abs_tol=ZERO_TOL)
assert math.isclose(zero_transverse.mass.item(), 6.0)

0 comments on commit 8d3a4d4

Please sign in to comment.