In [6]:
from tardis.io.parsers import csvy
import pytest
import os
from astropy import units as u
import numpy.testing as npt

In [18]:
test_data_directory = os.path.dirname(__file__)

def data_path(filename):
    data_dir = os.path.dirname(__file__)
    return os.path.join(data_dir, 'data', filename)

def test_csvy_finds_csv_first_line():
    yaml_dict, csv = csvy.load_csvy(data_path('csvy_full.csvy'))
    assert csv['velocity'][0] == 10000


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-18-ffee143d6537> in <module>
----> 1 test_data_directory = os.path.dirname(__file__)
      2 
      3 def data_path(filename):
      4     data_dir = os.path.dirname(__file__)
      5     return os.path.join(data_dir, 'data', filename)

NameError: name '__file__' is not defined

In [5]:
from tardis.io.tests.test_csvy_reader import test_csvy_finds_csv_first_line
from tardis.io.tests.test_csvy_reader import test_csv_colnames_equiv_datatype_fields
from tardis.io.tests.test_csvy_reader import test_csvy_nocsv_data_is_none
from tardis.io.tests.test_csvy_reader import csvy_full_fname
from tardis.io.tests.test_csvy_reader import csvy_nocsv_fname

test_csvy_finds_csv_first_line(csvy_full_fname())
test_csv_colnames_equiv_datatype_fields(csvy_full_fname())
test_csvy_nocsv_data_is_none(csvy_nocsv_fname())


---------------------------------------------------------------------------
Failed                                    Traceback (most recent call last)
<ipython-input-5-1a6d820d82f8> in <module>
      5 from tardis.io.tests.test_csvy_reader import csvy_nocsv_fname
      6 
----> 7 test_csvy_finds_csv_first_line(csvy_full_fname())
      8 test_csv_colnames_equiv_datatype_fields(csvy_full_fname())
      9 test_csvy_nocsv_data_is_none(csvy_nocsv_fname())

~/anaconda2/envs/tardis3/lib/python3.6/site-packages/_pytest/fixtures.py in result(*args, **kwargs)
    947     @six.wraps(function)
    948     def result(*args, **kwargs):
--> 949         fail(message, pytrace=False)
    950 
    951     # keep reference to the original function in our own custom attribute so we don't unwrap

~/anaconda2/envs/tardis3/lib/python3.6/site-packages/_pytest/outcomes.py in fail(msg, pytrace)
    111     """
    112     __tracebackhide__ = True
--> 113     raise Failed(msg=msg, pytrace=pytrace)
    114 
    115 

Failed: Fixture "csvy_full_fname" called directly. Fixtures are not meant to be called directly,
but are created automatically when test functions request them as parameters.
See https://docs.pytest.org/en/latest/fixture.html for more information about fixtures, and
https://docs.pytest.org/en/latest/deprecations.html#calling-fixtures-directly about how to update your code.

In [ ]:


In [ ]:


In [34]:
y,d= csvy.load_csvy('data/csvy_desc.csvy')

In [35]:
y


Out[35]:
OrderedDict([('tardis_model_config_version', 'v1.0'),
             ('datatype',
              OrderedDict([('fields',
                            [OrderedDict([('name', 'velocity'),
                                          ('unit', 'km/s')]),
                             OrderedDict([('name', 'density'),
                                          ('unit', 'g/cm^3'),
                                          ('desc', 'density of shell.')]),
                             OrderedDict([('name', 'H'),
                                          ('desc', 'fractional H abundance')]),
                             OrderedDict([('name', 'He'),
                                          ('desc',
                                           'fractional He abundance')])])])),
             ('v_inner_boundary', <Quantity 9000. km / s>),
             ('v_outer_boundary', <Quantity 15000. km / s>)])

In [4]:
y['datatype']['fields'][0]['name']


Out[4]:
'velocity'

In [17]:
names = [od['name'] for od in y['datatype']['fields']]

In [18]:
names = [od['name'] for od in y['datatype']['fields']]
for key in d.columns:
    print(key in names)


True
True
True
True

In [17]:
d['velocity'][0]


Out[17]:
10000

In [ ]:


In [ ]:


In [6]:
y_only = csvy.load_yaml_from_csvy('../tests/data/csvy_nocsv.csvy')
y_only


Out[6]:
OrderedDict([('name', 'csvy_nocsv'),
             ('description', 'Example csvy config file for TARDIS.'),
             ('tardis_model_config_version', 'v1.0'),
             ('atom_data', 'path/to/atom_data'),
             ('velocity',
              OrderedDict([('start', <Quantity 9000. km / s>),
                           ('stop', <Quantity 15000. km / s>),
                           ('num', 10)])),
             ('density', OrderedDict([('type', 'branch85_w7')])),
             ('abundance', OrderedDict([('type', 'uniform')])),
             ('v_inner_boundary', <Quantity 9000. km / s>),
             ('v_outer_boundary', <Quantity 15000. km / s>)])

In [7]:
d_only = csvy.load_csv_from_csvy('../tests/data/csvy_full.csvy')

In [8]:
d_only


Out[8]:
velocity density H He
0 10000 5.000000e-09 0.3 0.7
1 11000 2.000000e-09 0.6 0.4

In [16]:
y,d= csvy.load_csvy('../tests/data/csvy_nocsv.csvy')

In [23]:
print(y)
print(d)


OrderedDict([('name', 'csvy_nocsv'), ('description', 'Example csvy config file for TARDIS.'), ('tardis_model_config_version', 'v1.0'), ('atom_data', 'path/to/atom_data'), ('velocity', OrderedDict([('start', <Quantity 9000. km / s>), ('stop', <Quantity 15000. km / s>), ('num', 10)])), ('density', OrderedDict([('type', 'branch85_w7')])), ('abundance', 'uniform'), ('v_inner_boundary', <Quantity 9000. km / s>), ('v_outer_boundary', <Quantity 15000. km / s>)])
None

In [28]:
from tardis.io.config_validator import validate_dict

In [36]:
vy = validate_dict(y, schemapath='/home/mew488/src/dev/tardis/tardis/io/schemas/csvy_model.yml')

In [37]:
vy


Out[37]:
OrderedDict([('tardis_model_config_version', 'v1.0'),
             ('datatype',
              OrderedDict([('fields',
                            [OrderedDict([('name', 'velocity'),
                                          ('unit', 'km/s')]),
                             OrderedDict([('name', 'density'),
                                          ('unit', 'g/cm^3'),
                                          ('desc', 'density of shell.')]),
                             OrderedDict([('name', 'H'),
                                          ('desc', 'fractional H abundance')]),
                             OrderedDict([('name', 'He'),
                                          ('desc',
                                           'fractional He abundance')])])])),
             ('v_inner_boundary', <Quantity 9000. km / s>),
             ('v_outer_boundary', <Quantity 15000. km / s>)])

In [29]:
vy = validate_dict(y, schemapath='/home/mew488/src/dev/tardis/tardis/io/schemas/csvy_model_v2.yml')

In [ ]: