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

test: add test for generate_data #148

Merged
merged 2 commits into from
Nov 11, 2020
Merged
Changes from 1 commit
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
45 changes: 43 additions & 2 deletions tests/data/test_generate.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import expertsystem.amplitude.model as es
import numpy as np
import pytest

from tensorwaves.data.generate import generate_phsp
from tensorwaves.data.generate import generate_data, generate_phsp
from tensorwaves.physics.helicity_formalism.amplitude import IntensityBuilder
from tensorwaves.physics.helicity_formalism.kinematics import (
HelicityKinematics,
ParticleReactionKinematicsInfo,
Expand Down Expand Up @@ -30,7 +33,7 @@
),
],
)
def test_shape_generate_phsp(
def test_generate_phsp(
sample_size, initial_state_names, final_state_names, expected_shape, pdg
):
reaction_info = ParticleReactionKinematicsInfo(
Expand All @@ -41,3 +44,41 @@ def test_shape_generate_phsp(
kin = HelicityKinematics(reaction_info)
sample = generate_phsp(sample_size, kin)
assert sample.shape == expected_shape


def test_generate_data(canonical_model: es.AmplitudeModel):
n_phsp = 1000
n_data = 100
model = canonical_model
kinematics = HelicityKinematics.from_model(model)
phsp_sample = generate_phsp(n_phsp, kinematics)
builder = IntensityBuilder(model.particles, kinematics, phsp_sample)
intensity = builder.create_intensity(model)
data_sample = generate_data(n_data, kinematics, intensity)
assert len(data_sample) == len(model.kinematics.final_state)
for sample in data_sample:
assert len(sample) == n_data
data_sq = data_sample ** 2
e_sq = data_sq[:, :, 3]
p3_sq = data_sq[:, :, :3]
m_sq = np.abs(e_sq - p3_sq.sum(axis=2))
assert pytest.approx(list(np.sqrt(m_sq.mean(axis=1))), abs=1e-4) == [
0,
0.135,
0.135,
]
data_set = kinematics.convert(data_sample)
assert set(data_set) == {
"mSq_2",
"mSq_2_3_4",
"mSq_3",
"mSq_3_4",
"mSq_4",
"phi+3+4_vs_2",
"phi+3_4+2",
"theta+3+4_vs_2",
"theta+3_4+2",
}
assert pytest.approx(data_set["mSq_2"].mean()) == 0
assert pytest.approx(data_set["mSq_3"].mean()) == data_set["mSq_4"].mean()
assert pytest.approx(data_set["mSq_3_4"].mean(), abs=1e-1) == 1
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would test these kinematic related things like masses inside the phase space generation, since that is related to that. The generate data is related to the model, so we should perform checks that test for some expected distribution or so.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Which checks do you suggest? (The masses are already checked in test_generate_phsp.)

Copy link
Member

@spflueger spflueger Nov 10, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are some distribution checks in pycompwa that we could move here https://github.com/ComPWA/pycompwa/tree/master/tests/angular-distribution-tests . But they are slighly more complicated though and took a few more seconds to run.

For now we could just check that the sample sizes match and extend the tests to distribution verifications later on

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.