Skip to content

Commit

Permalink
Replace datapaths with fixtures.
Browse files Browse the repository at this point in the history
- Minor cosmetic changes: wrapping lines within 80
  characters for keeping code consistent with PEP8
  guidelines.
  • Loading branch information
karandesai-96 committed Apr 1, 2016
1 parent 013a625 commit 9dfb49c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 20 deletions.
10 changes: 7 additions & 3 deletions tardis/atomic.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,16 @@ class AtomDataNotPreparedError(Exception):


def data_path(fname):
return os.path.join(os.path.dirname(os.path.realpath(__file__)), 'data', fname)
return os.path.join(
os.path.dirname(os.path.realpath(__file__)), 'data', fname)


default_atom_h5_path = data_path('atom_data.h5')
atomic_symbols_data = np.recfromtxt(data_path('atomic_symbols.dat'), names=['atomic_number', 'symbol'])
symbol2atomic_number = OrderedDict(zip(atomic_symbols_data['symbol'], atomic_symbols_data['atomic_number']))
atomic_symbols_data = np.recfromtxt(data_path('atomic_symbols.dat'),
names=['atomic_number', 'symbol'])

symbol2atomic_number = OrderedDict(zip(atomic_symbols_data['symbol'],
atomic_symbols_data['atomic_number']))
atomic_number2symbol = OrderedDict(atomic_symbols_data)


Expand Down
41 changes: 24 additions & 17 deletions tardis/tests/test_atomic.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,38 @@
from tardis import atomic


chianti_he_db_h5_path = os.path.join(
os.path.dirname(os.path.realpath(__file__)), 'data', 'chianti_he_db.h5')
@pytest.fixture(scope="module")
def default_atom_h5_path():
return atomic.default_atom_h5_path


@pytest.fixture(scope="module")
def chianti_he_db_h5_path():
return os.path.join(
os.path.dirname(os.path.realpath(__file__)), 'data', 'chianti_he_db.h5')


def test_data_path():
data_path = atomic.data_path('test')
assert data_path.split('/')[-3:] == ['tardis', 'data', 'test']


def test_read_basic_atom_data():
data = atomic.read_basic_atom_data(atomic.default_atom_h5_path)
def test_read_basic_atom_data(default_atom_h5_path):
data = atomic.read_basic_atom_data(default_atom_h5_path)
assert data['atomic_number'][13] == 14
assert data['symbol'][13] == "Si"
testing.assert_almost_equal(data['mass'][13], 28.085, decimal=4)


def test_read_ionization_data():
data = atomic.read_ionization_data(atomic.default_atom_h5_path)
def test_read_ionization_data(default_atom_h5_path):
data = atomic.read_ionization_data(default_atom_h5_path)
assert data['atomic_number'][0] == 1
assert data['ion_number'][0] == 1
testing.assert_almost_equal(data['ionization_energy'][0], 13.59844, decimal=4)


def test_read_levels_data():
data = atomic.read_levels_data(atomic.default_atom_h5_path)
def test_read_levels_data(default_atom_h5_path):
data = atomic.read_levels_data(default_atom_h5_path)
assert data['atomic_number'][4] == 14
assert data['ion_number'][4] == 0
assert data['level_number'][4] == 4
Expand All @@ -37,8 +44,8 @@ def test_read_levels_data():
assert data['metastable'][4] == False


def test_read_lines_data():
data = atomic.read_lines_data(atomic.default_atom_h5_path)
def test_read_lines_data(default_atom_h5_path):
data = atomic.read_lines_data(default_atom_h5_path)
assert data['line_id'][0] == 8
assert data['atomic_number'][0] == 14
assert data['ion_number'][0] == 5
Expand All @@ -49,15 +56,15 @@ def test_read_lines_data():
assert data['level_number_upper'][0] == 36.0


def test_read_synpp_refs():
def test_read_synpp_refs(chianti_he_db_h5_path):
data = atomic.read_synpp_refs(chianti_he_db_h5_path)
assert data['atomic_number'][0] == 1
assert data['ion_number'][0] == 0
testing.assert_almost_equal(data['wavelength'][0], 6562.7973633, decimal=4)
assert data['line_id'][0] == 564995


def test_read_zeta_data():
def test_read_zeta_data(default_atom_h5_path, chianti_he_db_h5_path):
data = atomic.read_zeta_data(chianti_he_db_h5_path)
testing.assert_almost_equal(data[2000][1][1], 0.339, decimal=4)
testing.assert_almost_equal(data[2000][1][2], 0.000, decimal=4)
Expand All @@ -69,10 +76,10 @@ def test_read_zeta_data():
atomic.read_zeta_data('fakepath')

with pytest.raises(ValueError):
atomic.read_zeta_data(atomic.default_atom_h5_path)
atomic.read_zeta_data(default_atom_h5_path)


def test_read_collision_data():
def test_read_collision_data(default_atom_h5_path, chianti_he_db_h5_path):
data = atomic.read_collision_data(chianti_he_db_h5_path)
assert data[0]['atomic_number'][0] == 2
assert data[0]['ion_number'][0] == 0
Expand All @@ -90,10 +97,10 @@ def test_read_collision_data():
atomic.read_zeta_data('fakepath')

with pytest.raises(ValueError):
atomic.read_zeta_data(atomic.default_atom_h5_path)
atomic.read_zeta_data(default_atom_h5_path)


def test_read_macro_atom_data():
def test_read_macro_atom_data(default_atom_h5_path, chianti_he_db_h5_path):
data = atomic.read_macro_atom_data(chianti_he_db_h5_path)
assert data[0]['atomic_number'][0] == 2
assert data[0]['ion_number'][0] == 0
Expand All @@ -114,7 +121,7 @@ def test_read_macro_atom_data():
atomic.read_macro_atom_data('fakepath')

with pytest.raises(ValueError):
atomic.read_macro_atom_data(atomic.default_atom_h5_path)
atomic.read_macro_atom_data(default_atom_h5_path)


def test_atom_levels():
Expand Down

0 comments on commit 9dfb49c

Please sign in to comment.