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

Added csvy parser #928

Merged
merged 41 commits into from
May 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
d77f915
Added csvy parser
marxwillia May 1, 2019
e19203d
Added two examples of TARDIS csvy files.
marxwillia May 2, 2019
5da8003
Removed non-model related fields.
marxwillia May 8, 2019
e7169f7
Added TARDIS specific YAML loader to correctly interpret astropy quan…
marxwillia May 8, 2019
56fe945
Tested TARDIS specific YAML loader.
marxwillia May 8, 2019
7d63e95
Fixed datatype fields being overwritten.
marxwillia May 8, 2019
90206f2
Added csvy_model schema. Work in progress.
marxwillia May 8, 2019
77c5b76
Tested
marxwillia May 8, 2019
573e37a
Added option to run TARDIS with builtin density and uniform abundance
marxwillia May 14, 2019
1d4dda1
Added methods to load only the yaml or only the csv part of a csvy file.
marxwillia May 15, 2019
49817af
cleaned repo
marxwillia May 15, 2019
2d3b77d
cleaned repo
marxwillia May 15, 2019
cfa5592
Added 'model' to version field.
marxwillia May 15, 2019
f96a2c6
Added velocity to schema.
marxwillia May 15, 2019
0299482
Removed csvy option from structure and abundance fields.
marxwillia May 15, 2019
5621d12
Added ConfigurationNamespace from_csvy class method
marxwillia May 17, 2019
e91f034
Started from_csvy for Radial1DModel
marxwillia May 17, 2019
1ae1a8f
Removed from_csvy
marxwillia May 17, 2019
407c0fb
Revert "Removed from_csvy"
marxwillia May 17, 2019
2dae39f
Removed from_csvy
marxwillia May 17, 2019
a097ee8
Added datatype to schema
marxwillia May 17, 2019
d8c4631
Added try except to load csvy with no csv
marxwillia May 17, 2019
8908061
Fixed schema issue with csvy_nocsv
marxwillia May 17, 2019
9c521be
Added csvy unit tests
marxwillia May 17, 2019
b9ec8fa
Removed from_csvy
marxwillia May 17, 2019
9ed0cc8
run csvy unit tests
marxwillia May 17, 2019
bace9c9
Removed atom_data, Added comment for datatype name.
marxwillia May 22, 2019
4968da4
Removed description fields from schema
marxwillia May 22, 2019
fe32c28
Added docstring to load_csvy
marxwillia May 22, 2019
f989a71
Updated error messages
marxwillia May 22, 2019
087f315
Removed atom_data field
marxwillia May 22, 2019
60c4392
Used pytest fixtures
marxwillia May 29, 2019
d6487e4
Added docstrings
marxwillia May 29, 2019
5ccb902
Updated required properties
marxwillia May 29, 2019
2f9404e
Added tardis/io/tests/data/*
marxwillia May 29, 2019
af4e85e
Replaced assert with assert_almost_equal
marxwillia May 29, 2019
4403073
Updated required properties. Fixed required alignment mistake.
marxwillia May 29, 2019
63dcf27
Moved to docs
marxwillia May 29, 2019
f008753
Moved load .csvy files from setup.py to io/setup_package
marxwillia May 29, 2019
e85db2d
Added test that validation fails when missing required property in csvy
marxwillia May 29, 2019
de64084
Replaced assert with pytest.raises(Exception)
marxwillia May 29, 2019
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
443 changes: 443 additions & 0 deletions docs/notebooks/csvy_test.ipynb

Large diffs are not rendered by default.

84 changes: 84 additions & 0 deletions tardis/io/parsers/csvy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import yaml
import pandas as pd
from tardis.io.util import YAMLLoader

YAML_DELIMITER = '---'
def load_csvy(fname):
marxwillia marked this conversation as resolved.
Show resolved Hide resolved
"""
Parameters
----------

fname : string
Path to csvy file

Returns
-------
yaml_dict : dictionary
YAML part of the csvy file

data : pandas.dataframe
csv data from csvy file
"""
with open(fname) as fh:
yaml_lines = []
yaml_end_ind = -1
for i, line in enumerate(fh):
if i == 0:
assert line.strip() == YAML_DELIMITER, 'First line of csvy file is not \'---\''
yaml_lines.append(line)
if i > 0 and line.strip() == YAML_DELIMITER:
yaml_end_ind = i
break
else:
raise ValueError('End %s not found'%(YAML_DELIMITER))
yaml_dict = yaml.load(''.join(yaml_lines[1:-1]), YAMLLoader)
try:
data = pd.read_csv(fname, skiprows=yaml_end_ind + 1)
except pd.io.common.EmptyDataError as e:
data = None

return yaml_dict, data

def load_yaml_from_csvy(fname):
"""
Parameters
----------

fname : string
Path to csvy file

Returns
-------
yaml_dict : dictionary
YAML part of the csvy file
"""
with open(fname) as fh:
yaml_lines = []
yaml_end_ind = -1
for i, line in enumerate(fh):
if i == 0:
assert line.strip() == YAML_DELIMITER, 'First line of csvy file is not \'---\''
yaml_lines.append(line)
if i > 0 and line.strip() == YAML_DELIMITER:
yaml_end_ind = i
break
else:
raise ValueError('End %s not found'%(YAML_DELIMITER))
yaml_dict = yaml.load(''.join(yaml_lines[1:-1]), YAMLLoader)
return yaml_dict

def load_csv_from_csvy(fname):
"""
Parameters
----------

fname : string
Path to csvy file

Returns
-------
data : pandas.dataframe
csv data from csvy file
"""
yaml_dict, data = load_csvy(fname)
return data
143 changes: 143 additions & 0 deletions tardis/io/schemas/csvy_model.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
$schema: http://json-schema.org/draft-04/schema#
type: object
additionalProperties: false
properties:
name:
type: string
description: name of the model being run
marxwillia marked this conversation as resolved.
Show resolved Hide resolved

description:
Copy link
Member

Choose a reason for hiding this comment

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

are these required keys?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not currently.

type: string
description: description of the model being run

tardis_model_config_version:
Copy link
Member

Choose a reason for hiding this comment

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

that should be required

Copy link
Contributor Author

Choose a reason for hiding this comment

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

tardis_model_config_version is now the only property required.

type: string
description: Version of the configuration file

# datatype name used for consistency with astropy ecsv files.
datatype:
marxwillia marked this conversation as resolved.
Show resolved Hide resolved
type: object
description: fields found in csv part of csvy file
additionalProperties: False
properties:
fields:
type: array

v_inner_boundary:
type: quantity
description: velocity of the inner boundary

v_outer_boundary:
type: quantity
description: velocity of the outer boundary of the last shell

velocity:
type: object
properties:
start:
type: quantity
stop:
type: quantity
num:
type: number
multipleOf: 1.0

density:
oneOf:
- $ref: '#/definitions/density/branch85_w7'
- $ref: '#/definitions/density/exponential'
- $ref: '#/definitions/density/power_law'
- $ref: '#/definitions/density/uniform'

abundance:
$ref: '#/definitions/abundance/uniform'

required:
- tardis_model_config_version

definitions:
density:
branch85_w7:
type: object
additionalProperties: false
properties:
type:
enum:
- branch85_w7
w7_time_0:
type: quantity
default: 0.000231481 day
description: This needs no change - DO NOT TOUCH
w7_rho_0:
type: quantity
default: 3e29 g/cm^3
description: This needs no change - DO NOT TOUCH
w7_v_0:
type: quantity
default: 1 km/s
description: This needs no change - DO NOT TOUCH
exponential:
type: object
additionalProperties: false
properties:
type:
enum:
- exponential
time_0:
type: quantity
description: Time at which the pure model densities are right
rho_0:
type: quantity
description: density at time_0
v_0:
type: quantity
description: at what velocity the density rho_0 applies
required:
- rho_0
- v_0
power_law:
type: object
additionalProperties: false
properties:
type:
enum:
- power_law
time_0:
type: quantity
description: Time at which the pure model densities are right
rho_0:
type: quantity
description: density at time_0
v_0:
type: quantity
description: at what velocity the density rho_0 applies
exponent:
type: number
description: exponent for exponential density profile
required:
- rho_0
- v_0
- exponent
uniform:
type: object
additionalProperties: false
properties:
type:
enum:
- uniform
time_0:
type: quantity
description: Time at which the pure model densities are right
value:
type: quantity
description: value for uniform density
required:
- value
abundance:
uniform:
type: object
additionalProperties: true
properties:
type:
enum:
- uniform
2 changes: 1 addition & 1 deletion tardis/io/setup_package.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Licensed under a 3-clause BSD style license - see LICENSE.rst

def get_package_data():
return {'tardis.io.tests': ['data/*.dat', 'data/*.yml', 'data/*.csv']}
return {'tardis.io.tests': ['data/*.dat', 'data/*.yml', 'data/*.csv', 'data/*.csvy']}
23 changes: 23 additions & 0 deletions tardis/io/tests/data/csvy_full.csvy
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
marxwillia marked this conversation as resolved.
Show resolved Hide resolved
name: csvy_full
description: Example csvy config file for TARDIS.
tardis_model_config_version: v1.0
datatype:
fields:
- name: velocity
unit: km/s
desc: velocities of shell outer bounderies.
marxwillia marked this conversation as resolved.
Show resolved Hide resolved
- name: density
unit: g/cm^3
desc: density of shell.
- name: H
desc: fractional H abundance
- name: He
desc: fractional He abundance

v_inner_boundary: 9000 km/s
v_outer_boundary: 15000 km/s
---
velocity,density,H,He
10000, 5e-9, 0.3, 0.7
11000, 2e-9, 0.6, 0.4
19 changes: 19 additions & 0 deletions tardis/io/tests/data/csvy_missing.csvy
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
datatype:
fields:
- name: velocity
unit: km/s
- name: density
unit: g/cm^3
desc: density of shell.
- name: H
desc: fractional H abundance
- name: He
desc: fractional He abundance

v_inner_boundary: 9000 km/s
v_outer_boundary: 15000 km/s
---
velocity,density,H,He
10000, 5e-9, 0.3, 0.7
11000, 2e-9, 0.6, 0.4
16 changes: 16 additions & 0 deletions tardis/io/tests/data/csvy_nocsv.csvy
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
name: csvy_nocsv
description: Example csvy config file for TARDIS.
tardis_model_config_version: v1.0

velocity:
start: 9000 km/s
stop: 15000 km/s
num: 10
density:
type: branch85_w7
abundance:
type: uniform
v_inner_boundary: 9000 km/s
v_outer_boundary: 15000 km/s
---
45 changes: 45 additions & 0 deletions tardis/io/tests/test_csvy_reader.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import tardis
from tardis.io.parsers import csvy
from tardis.io.config_validator import validate_dict
from jsonschema import exceptions as json_schema_exc
import pytest
import os
from astropy import units as u
import numpy.testing as npt


DATA_PATH = os.path.join(tardis.__path__[0], 'io', 'tests', 'data')
Copy link
Member

Choose a reason for hiding this comment

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

could be a fixture as well.


@pytest.fixture
def csvy_full_fname():
return os.path.join(DATA_PATH, 'csvy_full.csvy')

@pytest.fixture
def csvy_nocsv_fname():
return os.path.join(DATA_PATH, 'csvy_nocsv.csvy')

@pytest.fixture
def csvy_missing_fname():
return os.path.join(DATA_PATH, 'csvy_missing.csvy')

def test_csvy_finds_csv_first_line(csvy_full_fname):
yaml_dict, csv = csvy.load_csvy(csvy_full_fname)
npt.assert_almost_equal(csv['velocity'][0],10000)

def test_csv_colnames_equiv_datatype_fields(csvy_full_fname):
yaml_dict, csv = csvy.load_csvy(csvy_full_fname)
datatype_names = [od['name'] for od in yaml_dict['datatype']['fields']]
for key in csv.columns:
assert key in datatype_names
for name in datatype_names:
assert name in csv.columns

def test_csvy_nocsv_data_is_none(csvy_nocsv_fname):
yaml_dict, csv = csvy.load_csvy(csvy_nocsv_fname)
assert csv is None

def test_missing_required_property(csvy_missing_fname):
yaml_dict, csv = csvy.load_csvy(csvy_missing_fname)
with pytest.raises(Exception):
vy = validate_dict(yaml_dict, schemapath=os.path.join(tardis.__path__[0], 'io', 'schemas',
'csvy_model.yml'))