Skip to content

Commit

Permalink
Add plot_spectrum method in TestW7 class.
Browse files Browse the repository at this point in the history
- A call to this method is made by test_spectrum method
  which asserts spectrum quantities.

- The pass / fail status is mentioned on the plot itself,
  by passing a boolean parameter 'passed' to plot_spectrum.

- plot_spectrum is called in different ways from try and
  except blocks of test_spectrum, for the same.
  • Loading branch information
Karan Desai committed May 24, 2016
1 parent e743801 commit 9365b03
Showing 1 changed file with 30 additions and 22 deletions.
52 changes: 30 additions & 22 deletions tardis/tests/tests_slow/test_w7.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,46 +104,54 @@ def test_luminosity_inner(self):
self.obtained_radial1d_model.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)

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

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

spectrum_fig = plt.figure()
spectrum_fig.suptitle("Deviation in spectrum quantities", fontweight="bold")
assert_quantity_allclose(
self.reference['luminosity_density_lambda'],
self.obtained_radial1d_model.spectrum.luminosity_density_lambda)

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

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

# `ldl_` prefixed variables associated with `luminosity_density_lambda`.
ldl_axes = spectrum_fig.add_subplot(111)
ldl_axes.set_title("Deviation in luminosity_density_lambda")
ldl_axes.set_xlabel("N-th packet")
ldl_axes.set_ylabel("Relative error (1 - obtained / baseline)")
# 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")
ldl_ax.set_xlabel("N-th packet")
ldl_ax.set_ylabel("Relative error (1 - obtained / baseline)")
deviation = 1 - (
self.obtained_radial1d_model.spectrum.luminosity_density_lambda.value /
self.reference['luminosity_density_lambda'].value)

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

ldl_axes.text(0.9, 0.9, 'PASSED', transform=ldl_axes.transAxes,
if passed:
ldl_ax.text(0.8, 0.8, 'passed', transform=ldl_ax.transAxes,
bbox={'facecolor': 'green', 'alpha': 0.5, 'pad': 10})
ldl_axes.plot(deviation, "g+")
except Exception as e:
ldl_axes.set_yscale("log")
ldl_axes.text(0.9, 0.9, 'FAILED', transform=ldl_axes.transAxes,
ldl_ax.plot(deviation, "g+")
else:
ldl_ax.set_yscale("log")
ldl_ax.text(0.8, 0.8, 'failed', transform=ldl_ax.transAxes,
bbox={'facecolor': 'red', 'alpha': 0.5, 'pad': 10})
ldl_axes.plot(deviation, "rx")
raise e
ldl_ax.plot(deviation, "rx")

# Figure is saved in `tmp` directory of slow tests baseline data.
# Figure is saved in `tmp` directory right now, till a suitable way of
# saving them is decided.
plt.savefig(os.path.join("/tmp", "spectrum_plot.png"))

def test_montecarlo_properties(self):
Expand Down

0 comments on commit 9365b03

Please sign in to comment.