Skip to content

Commit

Permalink
Adds unit tests for the spectrum solver
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewfullard committed Jul 22, 2024
1 parent 7d5a340 commit 7d03b2e
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 2 deletions.
4 changes: 2 additions & 2 deletions tardis/spectrum/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ def spectrum_real_packets_reabsorbed(self):
def spectrum_virtual_packets(self):
if np.all(self.montecarlo_virtual_luminosity == 0):
warnings.warn(

Check warning on line 50 in tardis/spectrum/base.py

View check run for this annotation

Codecov / codecov/patch

tardis/spectrum/base.py#L49-L50

Added lines #L49 - L50 were not covered by tests
"SpectrumSolver.spectrum_virtual_packets"
"is zero. Please run the montecarlo simulation with"
"SpectrumSolver.spectrum_virtual_packets "
"is zero. Please run the montecarlo simulation with "
"no_of_virtual_packets > 0",
UserWarning,
)
Expand Down
89 changes: 89 additions & 0 deletions tardis/spectrum/tests/test_spectrum_solver.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
from pathlib import Path

import astropy.units as u
import numpy as np
import pandas as pd
import pytest
from astropy.tests.helper import assert_quantity_allclose

from tardis.io.configuration.config_reader import Configuration
from tardis.simulation.base import Simulation
from tardis.spectrum.base import SpectrumSolver
from tardis.tests.fixtures.regression_data import RegressionData


class TestSpectrumSolver:
regression_data: RegressionData = None

@pytest.fixture(scope="class")
def simulation(
self,
request,
atomic_data_fname,
generate_reference,
example_configuration_dir: Path,
):
config = Configuration.from_yaml(

Check warning on line 26 in tardis/spectrum/tests/test_spectrum_solver.py

View check run for this annotation

Codecov / codecov/patch

tardis/spectrum/tests/test_spectrum_solver.py#L26

Added line #L26 was not covered by tests
str(example_configuration_dir / "tardis_configv1_verysimple.yml")
)
config["atom_data"] = atomic_data_fname

Check warning on line 29 in tardis/spectrum/tests/test_spectrum_solver.py

View check run for this annotation

Codecov / codecov/patch

tardis/spectrum/tests/test_spectrum_solver.py#L29

Added line #L29 was not covered by tests

simulation = Simulation.from_config(config)
simulation.run_final()

Check warning on line 32 in tardis/spectrum/tests/test_spectrum_solver.py

View check run for this annotation

Codecov / codecov/patch

tardis/spectrum/tests/test_spectrum_solver.py#L31-L32

Added lines #L31 - L32 were not covered by tests

request.cls.regression_data = RegressionData(request)
request.cls.regression_data.sync_hdf_store(simulation)

Check warning on line 35 in tardis/spectrum/tests/test_spectrum_solver.py

View check run for this annotation

Codecov / codecov/patch

tardis/spectrum/tests/test_spectrum_solver.py#L34-L35

Added lines #L34 - L35 were not covered by tests

return simulation

Check warning on line 37 in tardis/spectrum/tests/test_spectrum_solver.py

View check run for this annotation

Codecov / codecov/patch

tardis/spectrum/tests/test_spectrum_solver.py#L37

Added line #L37 was not covered by tests

def get_expected_data(self, key: str):
return pd.read_hdf(self.regression_data.fpath, key)

Check warning on line 40 in tardis/spectrum/tests/test_spectrum_solver.py

View check run for this annotation

Codecov / codecov/patch

tardis/spectrum/tests/test_spectrum_solver.py#L40

Added line #L40 was not covered by tests

def test_initialization(self, simulation):
transport_state = simulation.transport.transport_state
spectrum_frequency = simulation.transport.spectrum_frequency

Check warning on line 44 in tardis/spectrum/tests/test_spectrum_solver.py

View check run for this annotation

Codecov / codecov/patch

tardis/spectrum/tests/test_spectrum_solver.py#L43-L44

Added lines #L43 - L44 were not covered by tests

solver = SpectrumSolver(transport_state, spectrum_frequency)
assert solver.transport_state == transport_state
assert np.array_equal(

Check warning on line 48 in tardis/spectrum/tests/test_spectrum_solver.py

View check run for this annotation

Codecov / codecov/patch

tardis/spectrum/tests/test_spectrum_solver.py#L46-L48

Added lines #L46 - L48 were not covered by tests
solver.spectrum_frequency.value, spectrum_frequency.value
)
assert np.array_equal(

Check warning on line 51 in tardis/spectrum/tests/test_spectrum_solver.py

View check run for this annotation

Codecov / codecov/patch

tardis/spectrum/tests/test_spectrum_solver.py#L51

Added line #L51 was not covered by tests
solver._montecarlo_virtual_luminosity.value,
np.zeros_like(spectrum_frequency.value),
)
assert solver._integrator is None
assert solver.integrator_settings is None
assert solver._spectrum_integrated is None

Check warning on line 57 in tardis/spectrum/tests/test_spectrum_solver.py

View check run for this annotation

Codecov / codecov/patch

tardis/spectrum/tests/test_spectrum_solver.py#L55-L57

Added lines #L55 - L57 were not covered by tests

def test_spectrum_real_packets(self, simulation):
transport_state = simulation.transport.transport_state
spectrum_frequency = simulation.transport.spectrum_frequency

Check warning on line 61 in tardis/spectrum/tests/test_spectrum_solver.py

View check run for this annotation

Codecov / codecov/patch

tardis/spectrum/tests/test_spectrum_solver.py#L60-L61

Added lines #L60 - L61 were not covered by tests

solver = SpectrumSolver(transport_state, spectrum_frequency)
result = solver.spectrum_real_packets.luminosity
key = "simulation/spectrum_solver/spectrum_real_packets/luminosity"
expected = self.get_expected_data(key)

Check warning on line 66 in tardis/spectrum/tests/test_spectrum_solver.py

View check run for this annotation

Codecov / codecov/patch

tardis/spectrum/tests/test_spectrum_solver.py#L63-L66

Added lines #L63 - L66 were not covered by tests

luminosity = u.Quantity(expected, "erg /s")

Check warning on line 68 in tardis/spectrum/tests/test_spectrum_solver.py

View check run for this annotation

Codecov / codecov/patch

tardis/spectrum/tests/test_spectrum_solver.py#L68

Added line #L68 was not covered by tests

assert_quantity_allclose(

Check warning on line 70 in tardis/spectrum/tests/test_spectrum_solver.py

View check run for this annotation

Codecov / codecov/patch

tardis/spectrum/tests/test_spectrum_solver.py#L70

Added line #L70 was not covered by tests
result,
luminosity,
)

def test_spectrum_real_packets_reabsorbed(self, simulation):
transport_state = simulation.transport.transport_state
spectrum_frequency = simulation.transport.spectrum_frequency

Check warning on line 77 in tardis/spectrum/tests/test_spectrum_solver.py

View check run for this annotation

Codecov / codecov/patch

tardis/spectrum/tests/test_spectrum_solver.py#L76-L77

Added lines #L76 - L77 were not covered by tests

solver = SpectrumSolver(transport_state, spectrum_frequency)
result = solver.spectrum_real_packets_reabsorbed.luminosity
key = "simulation/spectrum_solver/spectrum_real_packets_reabsorbed/luminosity"
expected = self.get_expected_data(key)

Check warning on line 82 in tardis/spectrum/tests/test_spectrum_solver.py

View check run for this annotation

Codecov / codecov/patch

tardis/spectrum/tests/test_spectrum_solver.py#L79-L82

Added lines #L79 - L82 were not covered by tests

luminosity = u.Quantity(expected, "erg /s")

Check warning on line 84 in tardis/spectrum/tests/test_spectrum_solver.py

View check run for this annotation

Codecov / codecov/patch

tardis/spectrum/tests/test_spectrum_solver.py#L84

Added line #L84 was not covered by tests

assert_quantity_allclose(

Check warning on line 86 in tardis/spectrum/tests/test_spectrum_solver.py

View check run for this annotation

Codecov / codecov/patch

tardis/spectrum/tests/test_spectrum_solver.py#L86

Added line #L86 was not covered by tests
result,
luminosity,
)

0 comments on commit 7d03b2e

Please sign in to comment.