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

Integration Test class for Stratified W7 setup. #558

Merged
merged 13 commits into from
May 12, 2016
3 changes: 3 additions & 0 deletions tardis/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ def pytest_addoption(parser):
parser.addoption("--slow", action="store_true",
help="include running slow tests during run")

parser.addoption("--baseline-data", dest="baseline-data",
help="path of directory containing baseline data")


def pytest_report_header(config):

Expand Down
6 changes: 5 additions & 1 deletion tardis/tests/coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,8 @@ exclude_lines =
def main\(.*\):

# Ignore branches that don't pertain to this version of Python
pragma: py{ignore_python_version}
pragma: py{ignore_python_version}

omit =
# Slow Tests should be run but need not appear in "html report"
tests_slow
Empty file.
163 changes: 163 additions & 0 deletions tardis/tests/tests_slow/test_w7.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
import os
import yaml
import numpy as np
import pytest
from numpy.testing import assert_allclose
from astropy import units as u

from tardis.simulation.base import Simulation
from tardis.model import Radial1DModel
from tardis.io.config_reader import Configuration


def data_path(fname):
return os.path.join(os.path.dirname(os.path.realpath(__file__)), "w7", fname)


@pytest.mark.skipif(not pytest.config.getoption("--slow"),
reason="slow tests can only be run using --slow")
@pytest.mark.skipif(not pytest.config.getvalue("baseline-data"),
reason="--baseline-data was not specified")
class TestW7:
Copy link
Member

Choose a reason for hiding this comment

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

inherit from object to get new-style classes.

"""
Slow integration test for Stratified W7 setup.

Assumed two compressed binaries (.npz) are placed in `baseline/w7`
directory, whose path is provided by command line argument:

* ndarrays.npz | * quantities.npz
Contents (all (.npy)): | Contents (all (.npy)):
* last_interaction_type | * t_rads
* last_line_interaction_out_id | * luminosity_inner
* last_line_interaction_in_id | * montecarlo_luminosity
* j_estimators | * montecarlo_virtual_luminousity
* j_blue_estimators | * time_of_simulation
* last_line_interaction_shell_id | * montecarlo_nu
* nubar_estimators | * last_line_interaction_angstrom
* ws | * j_blues_norm_factor

Also assumed `kurucz_cd23_chianti_H_He.h5` file exists in `tmp` directory.
Copy link
Member

Choose a reason for hiding this comment

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

why?

Copy link
Author

Choose a reason for hiding this comment

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

It needs an atomic dataset. Once the test gets running, I will update this. Atomic dataset will be provided by user through command line (before this PR gets merged). It is actually too long to type on command line 😅

Copy link
Member

Choose a reason for hiding this comment

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

you should use the existing infrastructure to pass the atomic datafile

Copy link
Author

Choose a reason for hiding this comment

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

Sure, I will do it same as test_tardis_full.py

"""
Copy link
Author

Choose a reason for hiding this comment

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

Docstring for explanation related to work in progress.


@classmethod
@pytest.fixture(scope="class", autouse=True)
def setup(self):
"""
This method does initial setup of creating configuration and performing
a single run of integration test.
"""
self.config_file = data_path("config_w7.yml")
self.abundances = data_path("abundancies_w7.dat")
self.densities = data_path("densities_w7.dat")

# First we check whether atom data file exists at desired path.
assert os.path.exists('/tmp/kurucz_cd23_chianti_H_He.h5'), \
'kurucz_cd23_chianti_H_He.h5 atom data file does not exist'

# The available config file doesn't have file paths of atom data file,
# densities and abundances profile files as desired. We form dictionary
# from the config file and override those parameters by putting file
# paths of these three files at proper places.
config_yaml = yaml.load(open(self.config_file))
config_yaml['atom_data'] = '/tmp/kurucz_cd23_chianti_H_He.h5'
config_yaml['model']['abundances']['filename'] = self.abundances
config_yaml['model']['structure']['filename'] = self.densities

# The config hence obtained will be having appropriate file paths.
tardis_config = Configuration.from_config_dict(config_yaml)
Copy link
Contributor

@yeganer yeganer May 11, 2016

Choose a reason for hiding this comment

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

That's where the config get's validated and where atom_data becomes an object.

If I'm wrong, you can "load" the atomdata manually and pass the object to the config or the model. IIRC they all accept either a string or an AtomData object.
This might be easier anyway, instead of manipulating the config, load the AtomData directly.

Copy link
Author

Choose a reason for hiding this comment

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

This strategy sounds better. I am trying it.


# We now do a run with prepared config and get radial1d model.
self.obtained_radial1d_model = Radial1DModel(tardis_config)
simulation = Simulation(tardis_config)
simulation.legacy_run_simulation(self.obtained_w7_radial1d_model)

# The baseline data against which assertions are to be made is ingested
# from already available compressed binaries (.npz). These will return
# dictionaries of numpy.ndarrays for performing assertions.
self.baseline_data_dir = os.path.join(os.path.expanduser(
os.path.expandvars(pytest.config.getvalue('baseline-data'))), "w7")

self.expected_ndarrays = np.load(os.path.join(self.baseline_data_dir,
"ndarrays.npz"))
self.expected_quantities = np.load(os.path.join(self.baseline_data_dir,
"quantities.npz"))

def test_j_estimators(self):
assert_allclose(
self.expected_ndarrays['j_estimators'],
self.obtained_radial1d_model.j_estimators)

def test_j_blue_estimators(self):
assert_allclose(
self.expected_ndarrays['j_blue_estimators'],
self.obtained_radial1d_model.j_blue_estimators)

j_blues_norm_factor = self.expected_quantities['j_blues_norm_factor']
j_blues_norm_factor = j_blues_norm_factor * u.Unit('1 / (cm2 s)')

assert_allclose(
j_blues_norm_factor,
self.obtained_radial1d_model.j_blues_norm_factor)

def test_last_line_interactions(self):
assert_allclose(
self.expected_ndarrays['last_line_interaction_in_id'],
self.obtained_radial1d_model.last_line_interaction_in_id)

assert_allclose(
self.expected_ndarrays['last_line_interaction_out_id'],
self.obtained_radial1d_model.last_line_interaction_out_id)

assert_allclose(
self.expected_ndarrays['last_line_interaction_shell_id'],
self.obtained_radial1d_model.last_line_interaction_shell_id)

last_line_interaction_angstrom = self.expected_quantities['last_line_interaction_angstrom']
last_line_interaction_angstrom = last_line_interaction_angstrom * u.Unit('Angstrom')

assert_allclose(
last_line_interaction_angstrom,
self.obtained_radial1d_model.last_line_interaction_angstrom)

def test_nubar_estimators(self):
assert_allclose(
self.expected_ndarrays['nubar_estimators'],
self.obtained_radial1d_model.nubar_estimators)

def test_ws(self):
assert_allclose(
self.expected_ndarrays['ws'],
self.obtained_radial1d_model.ws)

def test_spectrum(self):
luminosity_inner = self.expected_quantities['luminosity_inner']
luminosity_inner = luminosity_inner * u.Unit('erg / s')

assert_allclose(
luminosity_inner,
self.obtained_radial1d_model.luminosity_inner)

def test_montecarlo_properties(self):
montecarlo_luminosity = self.expected_quantities['montecarlo_luminosity']
montecarlo_luminosity = montecarlo_luminosity * u.Unit('erg / s')

montecarlo_virtual_luminosity = self.expected_quantities['montecarlo_virtual_luminosity']
montecarlo_virtual_luminosity = montecarlo_virtual_luminosity * u.Unit('erg / s')

montecarlo_nu = self.expected_quantities['montecarlo_nu']
montecarlo_nu = montecarlo_nu * u.Unit('Hz')

assert_allclose(
montecarlo_luminosity,
self.obtained_radial1d_model.montecarlo_luminosity)

assert_allclose(
montecarlo_virtual_luminosity,
self.obtained_radial1d_model.montecarlo_virtual_luminosity)

assert_allclose(montecarlo_nu, self.obtained_radial1d_model.montecarlo_nu)

def test_shell_temperature(self):
t_rads = self.expected_quantities['t_rads']
t_rads = t_rads * u.Unit('K')
assert_allclose(t_rads, self.obtained_radial1d_model.t_rads)
Loading