Skip to content

Commit

Permalink
Allow Simulation Tests to Run Independently (#2407)
Browse files Browse the repository at this point in the history
* Modify test_packet_source.py so that sim and tardis/tests can run

* remove BasePacketSource

* format

* Add docstrings
  • Loading branch information
atharva-2001 authored Sep 15, 2023
1 parent 8605bea commit 99247ae
Showing 1 changed file with 64 additions and 23 deletions.
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"]))

0 comments on commit 99247ae

Please sign in to comment.