-
Notifications
You must be signed in to change notification settings - Fork 15
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
base: main
Are you sure you want to change the base?
Changes from 8 commits
e6bb395
aed0699
c2e36f7
b846996
abe35c4
c35b681
5d851fd
5e31e4f
a7976a9
52243bd
26ccdd6
fc124a9
d023bc0
d91b4f1
af3145c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
Main ChemKED module | ||
""" | ||
# Standard libraries | ||
from os.path import exists | ||
from os.path import exists, isabs, dirname, join | ||
from collections import namedtuple | ||
from warnings import warn | ||
from copy import deepcopy | ||
|
@@ -111,8 +111,10 @@ def __init__(self, yaml_file=None, dict_input=None, *, skip_validation=False): | |
if yaml_file is not None: | ||
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 = None | ||
else: | ||
raise NameError("ChemKED needs either a YAML filename or dictionary as input.") | ||
|
||
|
@@ -121,7 +123,7 @@ 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)) | ||
self.datapoints.append(DataPoint(point, directory)) | ||
|
||
self.reference = Reference( | ||
volume=self._properties['reference'].get('volume'), | ||
|
@@ -589,6 +591,7 @@ 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 | ||
|
@@ -631,7 +634,7 @@ class DataPoint(object): | |
'compression-ratio' | ||
] | ||
|
||
def __init__(self, properties): | ||
def __init__(self, properties, directory=None): | ||
for prop in self.value_unit_props: | ||
if prop in properties: | ||
quant = self.process_quantity(properties[prop]) | ||
|
@@ -685,7 +688,10 @@ def __init__(self, properties): | |
values = np.array(hist['values']) | ||
else: | ||
# Load the values from a file | ||
values = np.genfromtxt(hist['values']['filename'], delimiter=',') | ||
filename = hist['values']['filename'] | ||
if not isabs(filename): | ||
filename = join(directory, filename) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this will fail if the input is a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Changing the default directory to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think that change will fix the problem on this line, because the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch. I just pushed another commit so that |
||
values = np.genfromtxt(filename, delimiter=',') | ||
|
||
time_history = TimeHistory( | ||
time=Q_(values[:, time_col], time_units), | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
--- | ||
file-authors: | ||
- name: Kyle E Niemeyer | ||
ORCID: 0000-0003-4425-7097 | ||
file-version: 0 | ||
chemked-version: 0.0.1 | ||
reference: | ||
doi: 10.1002/kin.20180 | ||
authors: | ||
- name: Gaurav Mittal | ||
- name: Chih-Jen Sung | ||
ORCID: 0000-0003-2046-8076 | ||
- name: Richard A Yetter | ||
journal: International Journal of Chemical Kinetics | ||
year: 2006 | ||
volume: 38 | ||
pages: 516-529 | ||
detail: Fig. 6, open circle | ||
experiment-type: ignition delay | ||
apparatus: | ||
kind: rapid compression machine | ||
institution: Case Western Reserve University | ||
facility: CWRU RCM | ||
datapoints: | ||
- temperature: | ||
- 297.4 kelvin | ||
ignition-delay: | ||
- 1.0 ms | ||
pressure: | ||
- 958.0 torr | ||
composition: | ||
kind: mole fraction | ||
species: | ||
- species-name: H2 | ||
InChI: 1S/H2/h1H | ||
amount: | ||
- 0.12500 | ||
- species-name: O2 | ||
InChI: 1S/O2/c1-2 | ||
amount: | ||
- 0.06250 | ||
- species-name: N2 | ||
InChI: 1S/N2/c1-2 | ||
amount: | ||
- 0.18125 | ||
- species-name: Ar | ||
InChI: 1S/Ar | ||
amount: | ||
- 0.63125 | ||
ignition-type: | ||
target: pressure | ||
type: d/dt max | ||
rcm-data: | ||
compression-time: | ||
- 38.0 ms | ||
time-histories: | ||
- type: volume | ||
time: | ||
units: s | ||
column: 0 | ||
quantity: | ||
units: cm3 | ||
column: 1 | ||
values: | ||
filename: rcm_history.csv | ||
... |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -252,14 +252,20 @@ def _validate_isvalid_history(self, isvalid_history, field, value): | |
'with ' + property_units['time']) | ||
|
||
# Check that the values have the right number of columns | ||
n_cols = len(value['values'][0]) | ||
max_cols = max(value['time']['column'], | ||
value['quantity']['column'], | ||
value.get('uncertainty', {}).get('column', 0)) + 1 | ||
if n_cols > max_cols: | ||
self._error(field, 'too many columns in the values') | ||
elif n_cols < max_cols: | ||
self._error(field, 'not enough columns in the values') | ||
# If reading from a file, the file will not be validated. | ||
# A file can have an arbitrary number of columns, and the columns | ||
# to be used are specified. | ||
if type(value['values']) is list: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree that we can assume the CSV file is specified relative to the yaml file, but I tried using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As far as I know, just trying to open the file will try to open it relative to the working directory of the Python process. Are you saying that if I have a file structure like
and I start Python in the
it won't work, because Python will assume the As on your other PR, I think the simpler/more "pythonic" way to do this is a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, that's exactly what I'm saying. I was using a script to read multiple yaml files in a complex directory structure within a I'm not sure that a
Since this PR is related to time histories, I have a somewhat related question for you that I just stumbled on. Let's say somebody has a csv file with three columns - time, pressure, and volume. Right now, these must be implemented as two separate There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just to keep this focused on the code here, I moved the other discussion to the main comment thread. Anyhow, two things:
or
which isn't all that confusing to me. The reason (to me) to avoid the |
||
n_cols = len(value['values'][0]) | ||
max_cols = max(value['time']['column'], | ||
value['quantity']['column'], | ||
value.get('uncertainty', {}).get('column', 0)) + 1 | ||
if n_cols > max_cols: | ||
self._error(field, 'too many columns in the values') | ||
elif n_cols < max_cols: | ||
self._error(field, 'not enough columns in the values') | ||
elif 'filename' not in value['values'].keys(): | ||
self._error(field, 'must include filename or list of values') | ||
|
||
def _validate_isvalid_quantity(self, isvalid_quantity, field, value): | ||
"""Checks for valid given value and appropriate units. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is now 2019
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.