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

Initial laydown of comparison plots for TestW7. #566

Merged
merged 3 commits into from
May 24, 2016
Merged
Show file tree
Hide file tree
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
14 changes: 14 additions & 0 deletions tardis/tests/tests_slow/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import numpy as np
import pytest
from astropy import units as u
import tardis


@pytest.fixture(scope="session")
Expand All @@ -18,6 +19,19 @@ def data_path():
return os.path.join(os.path.dirname(os.path.realpath(__file__)), "w7")


@pytest.fixture(scope="session")
def base_plot_dir():
githash_short = tardis.__githash__[0:7]
base_plot_dir = os.path.join("/tmp", "plots", githash_short)

# Remove plots generated from previous runs on same githash.
if os.path.exists(base_plot_dir):
os.rmdir(base_plot_dir)

os.makedirs(base_plot_dir)
return base_plot_dir


@pytest.fixture(scope="session")
def reference(request, slow_tests_datadir):
"""
Expand Down
93 changes: 68 additions & 25 deletions tardis/tests/tests_slow/test_w7.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os
import yaml
import pytest
import matplotlib.pyplot as plt
from numpy.testing import assert_allclose
from astropy.tests.helper import assert_quantity_allclose

Expand All @@ -17,7 +18,8 @@ class TestW7(object):

@classmethod
@pytest.fixture(scope="class", autouse=True)
def setup(self, reference, data_path, atomic_data_fname):
def setup(self, request, reference, data_path, atomic_data_fname,
slow_tests_datadir, base_plot_dir):
"""
This method does initial setup of creating configuration and performing
a single run of integration test.
Expand Down Expand Up @@ -48,90 +50,131 @@ def setup(self, reference, data_path, atomic_data_fname):
tardis_config = Configuration.from_config_dict(config_yaml, self.atom_data)

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

# Get the reference data through the fixture.
self.reference = reference

# Form a base directory to save plots for `W7` setup.
# TODO: Rough prototyping, parametrize this as more setups are added.
self.name = "w7"
self.base_plot_dir = os.path.join(base_plot_dir, self.name)
os.makedirs(self.base_plot_dir)

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

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

assert_quantity_allclose(
self.reference['j_blues_norm_factor'],
self.obtained_radial1d_model.j_blues_norm_factor)
self.result.j_blues_norm_factor)

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

assert_allclose(
self.reference['last_line_interaction_out_id'],
self.obtained_radial1d_model.last_line_interaction_out_id)
self.result.last_line_interaction_out_id)

assert_allclose(
self.reference['last_line_interaction_shell_id'],
self.obtained_radial1d_model.last_line_interaction_shell_id)
self.result.last_line_interaction_shell_id)

assert_quantity_allclose(
self.reference['last_line_interaction_angstrom'],
self.obtained_radial1d_model.last_line_interaction_angstrom)
self.result.last_line_interaction_angstrom)

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

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

def test_luminosity_inner(self):
assert_quantity_allclose(
self.reference['luminosity_inner'],
self.obtained_radial1d_model.luminosity_inner)
self.result.luminosity_inner)

def test_spectrum(self):
assert_quantity_allclose(
try:
assert_quantity_allclose(
self.reference['luminosity_density_nu'],
self.obtained_radial1d_model.spectrum.luminosity_density_nu)
self.result.spectrum.luminosity_density_nu)

assert_quantity_allclose(
assert_quantity_allclose(
self.reference['delta_frequency'],
self.obtained_radial1d_model.spectrum.delta_frequency)
self.result.spectrum.delta_frequency)

assert_quantity_allclose(
assert_quantity_allclose(
self.reference['wavelength'],
self.obtained_radial1d_model.spectrum.wavelength)
self.result.spectrum.wavelength)

assert_quantity_allclose(
assert_quantity_allclose(
self.reference['luminosity_density_lambda'],
self.obtained_radial1d_model.spectrum.luminosity_density_lambda)
self.result.spectrum.luminosity_density_lambda)

self.plot_spectrum(has_passed=True)
except Exception as e:
self.plot_spectrum(has_passed=False)
raise e

def plot_spectrum(self, has_passed):
plt.suptitle("Deviation in spectrum_quantities", fontweight="bold")

# `ldl_` prefixed variables associated with `luminosity_density_lambda`.
# Axes of subplot are extracted, if we wish to make multiple plots
# for different spectrum quantities all in one figure.
ldl_ax = plt.subplot(111)
ldl_ax.set_title("Deviation in luminosity_density_lambda")
Copy link
Contributor

Choose a reason for hiding this comment

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

Personally, I think that we, in addition to showing the relative error, should also show a comparison between the two spectra. This is very helpful in case the test fails. Then seeing difference in the absolute spectrum may help pinpointing the cause - thoughts, @yeganer, @wkerzendorf?

Copy link
Member

Choose a reason for hiding this comment

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

I agree with that - but I think we want to get the entire framework built before moving on. Here I just plot something.

ldl_ax.set_xlabel("Wavelength")
ldl_ax.set_ylabel("Relative error (1 - result / reference)")
deviation = 1 - (
Copy link
Contributor

Choose a reason for hiding this comment

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

@wkerzendorf We want the absolute value of relative differences, right?

Copy link
Contributor

Choose a reason for hiding this comment

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

@yeganer - looks good to me as it is. It calculates the relative error. I think this is the most sensible thing to do.

Copy link
Member

Choose a reason for hiding this comment

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

@yeganer I would just move on for now.

self.result.spectrum.luminosity_density_lambda.value /
self.reference['luminosity_density_lambda'].value)

if has_passed:
ldl_ax.text(0.8, 0.8, 'passed', transform=ldl_ax.transAxes,
bbox={'facecolor': 'green', 'alpha': 0.5, 'pad': 10})
ldl_ax.plot(self.reference['wavelength'], deviation,
color="green", marker=".")
else:
ldl_ax.text(0.8, 0.8, 'failed', transform=ldl_ax.transAxes,
bbox={'facecolor': 'red', 'alpha': 0.5, 'pad': 10})
ldl_ax.plot(self.reference['wavelength'], deviation,
color="red", marker=".")

# Figure is saved in `tmp` directory right now, till a suitable way of
# saving them is decided.
plt.savefig(os.path.join(self.base_plot_dir, "spectrum.png"))

def test_montecarlo_properties(self):
assert_quantity_allclose(
self.reference['montecarlo_luminosity'],
self.obtained_radial1d_model.montecarlo_luminosity)
self.result.montecarlo_luminosity)

assert_quantity_allclose(
self.reference['montecarlo_virtual_luminosity'],
self.obtained_radial1d_model.montecarlo_virtual_luminosity)
self.result.montecarlo_virtual_luminosity)

assert_quantity_allclose(
self.reference['montecarlo_nu'],
self.obtained_radial1d_model.montecarlo_nu)
self.result.montecarlo_nu)

def test_shell_temperature(self):
assert_quantity_allclose(
self.reference['t_rads'],
self.obtained_radial1d_model.t_rads)
self.result.t_rads)