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

Unit tests for untested methods of tardis/atomic.py #527

Merged
merged 6 commits into from
Apr 6, 2016
Merged
Show file tree
Hide file tree
Changes from 5 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
8 changes: 3 additions & 5 deletions tardis/atomic.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,13 @@ class AtomDataNotPreparedError(Exception):

logger = logging.getLogger(__name__)

default_atom_h5_path = os.path.join(os.path.dirname(__file__), 'data', 'atom_data.h5')

def data_path(fname):
data_dir = os.path.join(os.path.dirname(__file__), 'data')
return os.path.join(data_dir, fname)
return os.path.join(os.path.dirname(os.path.realpath(__file__)), 'data', fname)
Copy link
Member

Choose a reason for hiding this comment

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

is that more than 140 characters?

Copy link
Contributor

Choose a reason for hiding this comment

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

If you are talking about the line length PEP, the value is 80. I'm not sure whether this is too long but line 29 looks like it definitely is to long

Copy link
Member

Choose a reason for hiding this comment

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

sorry 80 yes. That's I think why I broke it up.

Copy link
Author

Choose a reason for hiding this comment

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

Is the rule followed as 80 chars or 120 ? Many orgs compromise this PEP8 rule as 120 characters. I'll follow whichever used and push a final cosmetic concluding commit.

Copy link
Contributor

Choose a reason for hiding this comment

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

As far as I know we stick with 80. Although we won't do PEP fixes on their own. If I stumble upon code that doesn't follow PEP I'll edit it on the fly.

Copy link
Author

Choose a reason for hiding this comment

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

I'll take care about it. But why merge if we know of existent PEP8 errors ? I'll include the fixes to cleanup this PR, if we are going with "edit on the fly" way, I'd better squash the PEP8 fixes into previous single commit.

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm trying to look out for PEP8 errors but since I'm not doing a checkout of every PR I have a look at I won't see them (I have a pep checker for my editor only).

Copy link
Author

Choose a reason for hiding this comment

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

I have a built-in PEP8 checker in Pycharm. I had compromised line wrap to 120 characters, but I switched it to 80 characters just now. I removed PEP8 errors from the part of code in this PR. Rest will be "edited on the fly" 😉


atomic_symbols_data = np.recfromtxt(data_path('atomic_symbols.dat'),
names=['atomic_number', 'symbol'])

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_number2symbol = OrderedDict(atomic_symbols_data)

Expand Down
116 changes: 103 additions & 13 deletions tardis/tests/test_atomic.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,122 @@
from tardis import atomic
from numpy import testing
import pytest
import os
import pytest
from numpy import testing
from tardis import atomic


chianti_he_db_h5_path = os.path.join(
os.path.dirname(os.path.realpath(__file__)), 'data', 'chianti_he_db.h5')


def test_atomic_h5_readin():
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)
assert data['atomic_number'][13] == 14
assert data['symbol'][13] == "Si"
si_mass = data['mass'][13]
testing.assert_almost_equal(si_mass, 28.085, decimal=4)
pass
testing.assert_almost_equal(data['mass'][13], 28.085, decimal=4)

def test_ionization_h5_readin():

def test_read_ionization_data():
data = atomic.read_ionization_data(atomic.default_atom_h5_path)
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd suggest using fixtures for the data paths.
That way they could easily be changed for all tests in case something changed.
This goes in line with the DRY principle.

Copy link
Author

Choose a reason for hiding this comment

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

Yes, I will correct it.

hi_ionization = data['ionization_energy'][0]
testing.assert_almost_equal(hi_ionization, 13.59844, decimal=4)
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_levels_h5_readin():
def test_read_levels_data():
data = atomic.read_levels_data(atomic.default_atom_h5_path)
assert data['atomic_number'][4] == 14
assert data['ion_number'][4] == 0
assert data['level_number'][4] == 4
si_energy = data['energy'][4]
testing.assert_almost_equal(si_energy, 1.90865, decimal=4)
testing.assert_almost_equal(data['energy'][4], 1.90865, decimal=4)
assert data['g'][4] == 1
assert data['metastable'][4] == False


def test_read_lines_data():
data = atomic.read_lines_data(atomic.default_atom_h5_path)
assert data['line_id'][0] == 8
assert data['atomic_number'][0] == 14
assert data['ion_number'][0] == 5
testing.assert_almost_equal(data['wavelength'][0], 66.772, decimal=4)
testing.assert_almost_equal(data['f_ul'][0], 0.02703, decimal=4)
testing.assert_almost_equal(data['f_lu'][0], 0.04054, decimal=4)
assert data['level_number_lower'][0] == 0.0
assert data['level_number_upper'][0] == 36.0


def test_read_synpp_refs():
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():
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)

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

with pytest.raises(IOError):
atomic.read_zeta_data('fakepath')

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


def test_read_collision_data():
data = atomic.read_collision_data(chianti_he_db_h5_path)
assert data[0]['atomic_number'][0] == 2
assert data[0]['ion_number'][0] == 0
assert data[0]['level_number_upper'][0] == 18
assert data[0]['level_number_lower'][0] == 2
assert data[0]['g_ratio'][0] == 1.0
testing.assert_almost_equal(data[0]['delta_e'][0], 35484.251143, decimal=4)
assert data[1][0] == 2000.0
assert data[1][1] == 4000.0

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

with pytest.raises(IOError):
atomic.read_zeta_data('fakepath')

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


def test_read_macro_atom_data():
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
assert data[0]['source_level_number'][0] == 0.0
assert data[0]['destination_level_number'][0] == 48.0
assert data[0]['transition_type'][0] == 1
assert data[0]['transition_probability'][0] == 0.0
assert data[0]['transition_line_id'][0] == 564957

assert data[1]['count_down'][0] == 0
assert data[1]['count_up'][0] == 7
assert data[1]['count_total'][0] == 7

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

with pytest.raises(IOError):
atomic.read_macro_atom_data('fakepath')

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


def test_atom_levels():
atom_data = atomic.AtomData.from_hdf5(atomic.default_atom_h5_path)
with pytest.raises(Exception):
Expand Down