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

csv file time history #115

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Requested changes.
Revert DataPoint to its original form, use Path objects to indicate location of csv file. Still need to add tests for 100% coverage.
  • Loading branch information
jsantner committed Jun 27, 2018
commit 26ccdd680f58cf877da31da567d04db1752c48fb
35 changes: 26 additions & 9 deletions pyked/chemked.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@
Main ChemKED module
"""
# Standard libraries
from os.path import exists, isabs, dirname, join
from os.path import exists
from collections import namedtuple
from warnings import warn
from copy import deepcopy
import xml.etree.ElementTree as etree
import xml.dom.minidom as minidom
from itertools import chain
from pathlib import Path
from distutils.version import LooseVersion
import sys

import numpy as np

Expand Down Expand Up @@ -78,6 +81,13 @@
Composition.amount.__doc__ = '(`~pint.Quantity`) The amount of this species'


if LooseVersion(sys.version) < '3.6':
# Allow old version of python to open Path objects.
oldopen = open
def open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None):
return oldopen(str(file), mode, buffering, encoding, errors, newline, closefd, opener)


class ChemKED(object):
"""Main ChemKED class.

Expand Down Expand Up @@ -109,12 +119,11 @@ class ChemKED(object):
"""
def __init__(self, yaml_file=None, dict_input=None, *, skip_validation=False):
if yaml_file is not None:
yaml_file = Path(yaml_file)
with open(yaml_file, 'r') as f:
self._properties = yaml.safe_load(f)
directory = dirname(yaml_file)
elif dict_input is not None:
self._properties = dict_input
directory = ''
else:
raise NameError("ChemKED needs either a YAML filename or dictionary as input.")

Expand All @@ -123,7 +132,18 @@ def __init__(self, yaml_file=None, dict_input=None, *, skip_validation=False):

self.datapoints = []
for point in self._properties['datapoints']:
self.datapoints.append(DataPoint(point, directory))
if 'time-histories' in point:
for th in point['time-histories']:
try:
filename = Path(th['values']['filename'])
except TypeError:
pass
else:
if yaml_file is not None:
th['values']['filename'] = (yaml_file.parent / filename).resolve()
else:
th['values']['filename'] = filename.resolve()
self.datapoints.append(DataPoint(point))

self.reference = Reference(
volume=self._properties['reference'].get('volume'),
Expand Down Expand Up @@ -591,7 +611,6 @@ class DataPoint(object):

Arguments:
properties (`dict`): Dictionary adhering to the ChemKED format for ``datapoints``
directory (`str`, optional): Directory to look for auxiliary files

Attributes:
composition (`list`): List of dictionaries representing the species and their quantities
Expand Down Expand Up @@ -634,7 +653,7 @@ class DataPoint(object):
'compression-ratio'
]

def __init__(self, properties, directory=''):
def __init__(self, properties):
for prop in self.value_unit_props:
if prop in properties:
quant = self.process_quantity(properties[prop])
Expand Down Expand Up @@ -689,9 +708,7 @@ def __init__(self, properties, directory=''):
else:
# Load the values from a file
filename = hist['values']['filename']
if not isabs(filename):
filename = join(directory, filename)
values = np.genfromtxt(filename, delimiter=',')
values = np.genfromtxt(hist['values']['filename'], delimiter=',')

time_history = TimeHistory(
time=Q_(values[:, time_col], time_units),
Expand Down