Skip to content

Commit

Permalink
Move common hdf fixtures to conftest.py
Browse files Browse the repository at this point in the history
  • Loading branch information
vg3095 committed Jun 20, 2017
1 parent ef754d8 commit 50d3577
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 45 deletions.
28 changes: 28 additions & 0 deletions tardis/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
from tardis.atomic import AtomData
from tardis.io.config_reader import Configuration
from tardis.io.util import yaml_load_config_file
from tardis.model import Radial1DModel
from tardis.model.density import HomologousDensity

###
# Astropy
Expand Down Expand Up @@ -117,3 +119,29 @@ def included_he_atomic_data(test_data_path):
def tardis_config_verysimple():
return yaml_load_config_file(
'tardis/io/tests/data/tardis_configv1_verysimple.yml')

###
# HDF Fixtures
###

@pytest.fixture(scope="session")
def hdf_file_path(tmpdir_factory):
path = tmpdir_factory.mktemp('hdf_buffer').join('test.hdf')
return str(path)

@pytest.fixture(scope="session")
def hdf_config():
filename = 'tardis_configv1_verysimple.yml'
path = os.path.abspath(os.path.join('tardis/io/tests/data/', filename))
config = Configuration.from_yaml(path)
return config

@pytest.fixture(scope="session")
def model(hdf_config):
model = Radial1DModel.from_config(hdf_config)
return model

@pytest.fixture(scope="session")
def homologous_density(hdf_config):
density = HomologousDensity.from_config(hdf_config)
return density
4 changes: 2 additions & 2 deletions tardis/model/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@

from tardis.util import quantity_linspace, element_symbol2atomic_number
from tardis.io.model_reader import read_density_file, read_abundances_file
from tardis.io.util import HDFWriter
from tardis.io.util import HDFWriter as HDFWriterMixin
from density import HomologousDensity

logger = logging.getLogger(__name__)


class Radial1DModel(HDFWriter, object):
class Radial1DModel(HDFWriterMixin):
"""An object that hold information about the individual shells.
Parameters
Expand Down
4 changes: 2 additions & 2 deletions tardis/model/density.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import numpy as np

from tardis.util import quantity_linspace
from tardis.io.util import HDFWriter
from tardis.io.util import HDFWriter as HDFWriterMixin

class HomologousDensity(HDFWriter, object):
class HomologousDensity(HDFWriterMixin):
"""A class that holds an initial density and time
Parameters
Expand Down
27 changes: 6 additions & 21 deletions tardis/model/tests/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,43 +209,28 @@ def test_ascii_reader_exponential_law():
# Save and Load
###


@pytest.fixture(scope="module")
def hdf_file_path(tmpdir_factory):
path = tmpdir_factory.mktemp('hdf_buffer').join('model.hdf')
return str(path)


@pytest.fixture(scope="module")
def actual_model():
filename = 'tardis_configv1_verysimple.yml'
config = Configuration.from_yaml(data_path(filename))
model = Radial1DModel.from_config(config)
return model


@pytest.fixture(scope="module", autouse=True)
def to_hdf_buffer(hdf_file_path, actual_model):
actual_model.to_hdf(hdf_file_path)
def to_hdf_buffer(hdf_file_path, model):
model.to_hdf(hdf_file_path)

model_scalar_attrs = ['t_inner']

@pytest.mark.parametrize("attr", model_scalar_attrs)
def test_hdf_model_scalars(hdf_file_path, actual_model, attr):
def test_hdf_model_scalars(hdf_file_path, model, attr):
path = os.path.join('model', 'scalars')
expected = pd.read_hdf(hdf_file_path, path)[attr]
actual = getattr(actual_model, attr)
actual = getattr(model, attr)
if hasattr(actual, 'cgs'):
actual = actual.cgs.value
assert_almost_equal(actual, expected)

model_nparray_attrs = ['w', 'v_inner', 'v_outer']

@pytest.mark.parametrize("attr", model_nparray_attrs)
def test_hdf_model_nparray(hdf_file_path, actual_model, attr):
def test_hdf_model_nparray(hdf_file_path, model, attr):
path = os.path.join('model', attr)
expected = pd.read_hdf(hdf_file_path, path)
actual = getattr(actual_model, attr)
actual = getattr(model, attr)
if hasattr(actual, 'cgs'):
actual = actual.cgs.value
assert_almost_equal(actual, expected.values)
21 changes: 1 addition & 20 deletions tardis/model/tests/test_density.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,11 @@
import pytest
from numpy.testing import assert_almost_equal

from tardis.io.config_reader import Configuration
from tardis.model.density import HomologousDensity


###
# Save and Load
###

def data_path(filename):
return os.path.join('tardis/io/tests/data/', filename)

@pytest.fixture(scope="module")
def hdf_file_path(tmpdir_factory):
path = tmpdir_factory.mktemp('hdf_buffer').join('density.hdf')
return str(path)

@pytest.fixture(scope="module")
def homologous_density():
filename = 'tardis_configv1_verysimple.yml'
config = Configuration.from_yaml(data_path(filename))
density = HomologousDensity.from_config(config)
return density

@pytest.fixture(scope="module",autouse=True)
@pytest.fixture(scope="module", autouse=True)
def to_hdf_buffer(hdf_file_path,homologous_density):
homologous_density.to_hdf(hdf_file_path)

Expand Down

0 comments on commit 50d3577

Please sign in to comment.