Skip to content

Commit 78a41a7

Browse files
rwestsevyharris
authored andcommitted
Hopefully, a way to read data from CSV files
1 parent 1902bfe commit 78a41a7

File tree

1 file changed

+66
-3
lines changed

1 file changed

+66
-3
lines changed

pyked/chemked.py

Lines changed: 66 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
Main ChemKED module
33
"""
44
# Standard libraries
5+
import os
56
from os.path import exists
67
from collections import namedtuple
78
from warnings import warn
@@ -11,6 +12,7 @@
1112
from itertools import chain
1213

1314
import numpy as np
15+
import pandas as pd
1416

1517
# Local imports
1618
from .validation import schema, OurValidator, yaml, Q_
@@ -120,8 +122,16 @@ def __init__(self, yaml_file=None, dict_input=None, *, skip_validation=False):
120122
self.validate_yaml(self._properties)
121123

122124
self.datapoints = []
123-
for point in self._properties['datapoints']:
124-
self.datapoints.append(IgnitionDataPoint(point))
125+
if self._properties['experiment-type'] == 'ignition delay':
126+
for point in self._properties['datapoints']:
127+
self.datapoints.append(IgnitionDataPoint(point))
128+
elif self._properties['experiment-type'] == 'species profile':
129+
assert len(self._properties['datapoints']) == 1, "Only one CSV file per YAML file please"
130+
for point in self._properties['datapoints']:
131+
csv_file = os.path.join(os.path.split(yaml_file)[0], point['csvfile'])
132+
csv_df= pd.read_csv(csv_file)
133+
self.datapoints.extend(SpeciesProfileDataPoint(point, csv_df))
134+
125135

126136
self.reference = Reference(
127137
volume=self._properties['reference'].get('volume'),
@@ -590,6 +600,31 @@ class DataPoint(object):
590600
591601
Specific types of data point should inherit from this.
592602
"""
603+
def process_column(self, properties, csv_df):
604+
"""
605+
Process a column data and return as a list of units.Quantity objects
606+
csv_df is a Pandas DataFrame.
607+
"""
608+
column_name = properties['column-name']
609+
data_list = []
610+
for value in df[column_name]:
611+
for p in properties:
612+
units = p.get('units', '')
613+
if units: break
614+
#todo: schema should enforce at most 1 units entry
615+
616+
value_properties = [ f'{value} {units}' ]
617+
618+
for p in properties:
619+
if p.get('uncertainty-type', False):
620+
# this is the uncertainty data
621+
value_properties.append(p)
622+
623+
quant = self.process_quantity(value_properties)
624+
data_list.append(quant)
625+
return data_list
626+
627+
593628
def process_quantity(self, properties):
594629
"""
595630
Process the units and uncertainty information from a given quantity
@@ -629,9 +664,37 @@ def process_quantity(self, properties):
629664
'"lower-uncertainty" need to be specified.')
630665
else:
631666
raise ValueError('uncertainty-type must be one of "absolute" or "relative"')
632-
633667
return quant
634668

669+
670+
class SpeciesProfileDataPoint(DataPoint):
671+
"""
672+
Class for a single JSR experiment data point.
673+
674+
"""
675+
value_unit_props = [
676+
'pressure', 'reactor-volume', 'residence-time'
677+
]
678+
679+
column_unit_props = [
680+
'temperature',
681+
]
682+
683+
def __init__(self, properties, csv_df):
684+
for prop in self.value_unit_props:
685+
if prop in properties:
686+
quant = self.process_quantity(properties[prop])
687+
setattr(self, prop.replace('-', '_'), quant)
688+
else:
689+
setattr(self, prop.replace('-', '_'), None)
690+
691+
for prop in self.column_unit_props:
692+
if prop in properties:
693+
data_list = self.process_column(properties[prop], csv_df)
694+
setattr(self, prop.replace('-', '_'), data_list)
695+
else:
696+
setattr(self, prop.replace('-', '_'), None)
697+
635698

636699
class IgnitionDataPoint(DataPoint):
637700
"""Class for a single ignition delay datapoint.

0 commit comments

Comments
 (0)