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

Allow Simulation Tests to Run Independently #2407

Merged
merged 4 commits into from
Sep 15, 2023
Merged
Changes from all commits
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
87 changes: 64 additions & 23 deletions tardis/montecarlo/tests/test_packet_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,68 @@
)


@pytest.fixture
def packet_unit_test_fpath(tardis_ref_path):
return os.path.abspath(os.path.join(tardis_ref_path, "packet_unittest.h5"))


def test_bb_packet_sampling(request, tardis_ref_data, packet_unit_test_fpath):
montecarlo_configuration.LEGACY_MODE_ENABLED = True
bb = BlackBodySimpleSource(base_seed=1963, legacy_second_seed=2508)
# ref_df = pd.read_hdf('test_bb_sampling.h5')
if request.config.getoption("--generate-reference"):
ref_bb = pd.read_hdf(packet_unit_test_fpath, key="/blackbody")
ref_bb.to_hdf(
tardis_ref_data, key="/packet_unittest/blackbody", mode="a"
class TestPacketSource:
@pytest.fixture(scope="class")
def packet_unit_test_fpath(self, tardis_ref_path):
"""
Path to `packet_unittest.h5`.

Parameters
----------
tardis_ref_path : pd.HDFStore

Returns
-------
os.path
"""
return os.path.abspath(
os.path.join(tardis_ref_path, "packet_unittest.h5")
)
pytest.skip("Reference data was generated during this run.")

ref_df = tardis_ref_data["/packet_unittest/blackbody"]
bb.temperature = 10000
nus = bb.create_packet_nus(100)
mus = bb.create_packet_mus(100)
unif_energies = bb.create_packet_energies(100)
assert np.all(np.isclose(nus, ref_df["nus"]))
assert np.all(np.isclose(mus, ref_df["mus"]))
assert np.all(np.isclose(unif_energies, ref_df["energies"]))

@pytest.fixture(scope="class")
def blackbodysimplesource(self, request):
"""
Create BlackBodySimpleSource instance.

Yields
-------
tardis.montecarlo.packet_source.BlackBodySimpleSource
"""
cls = type(self)
montecarlo_configuration.LEGACY_MODE_ENABLED = True
cls.bb = BlackBodySimpleSource(base_seed=1963, legacy_second_seed=2508)
yield cls.bb
montecarlo_configuration.LEGACY_MODE_ENABLED = False

def test_bb_packet_sampling(
self,
request,
tardis_ref_data,
packet_unit_test_fpath,
blackbodysimplesource,
):
"""
Test generate_plot_mpl method.

Parameters
----------
request : _pytest.fixtures.SubRequest
tardis_ref_data: pd.HDFStore
packet_unit_test_fpath: os.path
blackbodysimplesource: tardis.montecarlo.packet_source.BlackBodySimpleSource
"""
if request.config.getoption("--generate-reference"):
ref_bb = pd.read_hdf(packet_unit_test_fpath, key="/blackbody")
ref_bb.to_hdf(
tardis_ref_data, key="/packet_unittest/blackbody", mode="a"
)
pytest.skip("Reference data was generated during this run.")

ref_df = tardis_ref_data["/packet_unittest/blackbody"]
self.bb.temperature = 10000
nus = self.bb.create_packet_nus(100)
mus = self.bb.create_packet_mus(100)
unif_energies = self.bb.create_packet_energies(100)
assert np.all(np.isclose(nus, ref_df["nus"]))
assert np.all(np.isclose(mus, ref_df["mus"]))
assert np.all(np.isclose(unif_energies, ref_df["energies"]))
Loading