From 1cee8b9d8e341842c57ddf95e5d303ccdecfb615 Mon Sep 17 00:00:00 2001 From: Jeff Santner Date: Wed, 13 Jun 2018 17:08:14 -0700 Subject: [PATCH 01/11] Correctly read Celsius temperatures Previously, code would interpret a value in Celsius, such as `50 degC` as `50 * 1 degC`. However, relative quantities like Celcius and Fahrenheit cannot be multiplied. This commit fixes the problem. --- pyked/validation.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/pyked/validation.py b/pyked/validation.py index 4814201..7d85d5c 100644 --- a/pyked/validation.py +++ b/pyked/validation.py @@ -273,7 +273,11 @@ def _validate_isvalid_quantity(self, isvalid_quantity, field, value): {'isvalid_quantity': {'type': 'bool'}, 'field': {'type': 'str'}, 'value': {'type': 'list'}} """ - quantity = Q_(value[0]) + if type(value[0]) is str: + values = value[0].split() + quantity = Q_(*[float(values[0])] + values[1:]) + else: + quantity = Q_(value[0]) low_lim = 0.0 * units(property_units[field]) try: From cfdbae25167c587b350627c3ee9745b0ed8e274e Mon Sep 17 00:00:00 2001 From: Jeff Santner Date: Wed, 13 Jun 2018 17:42:50 -0700 Subject: [PATCH 02/11] Add test with temperature in Celsius --- pyked/tests/test_validation.py | 2 +- pyked/tests/testfile_rcm3.yaml | 178 +++++++++++++++++++++++++++++++++ 2 files changed, 179 insertions(+), 1 deletion(-) create mode 100644 pyked/tests/testfile_rcm3.yaml diff --git a/pyked/tests/test_validation.py b/pyked/tests/test_validation.py index 50c46f0..e9d2c1b 100644 --- a/pyked/tests/test_validation.py +++ b/pyked/tests/test_validation.py @@ -363,7 +363,7 @@ def properties(self, request): @pytest.mark.parametrize("properties", [ 'testfile_st.yaml', 'testfile_st2.yaml', 'testfile_rcm.yaml', 'testfile_required.yaml', - 'testfile_uncertainty.yaml', 'testfile_rcm2.yaml', + 'testfile_uncertainty.yaml', 'testfile_rcm2.yaml', 'testfile_rcm3.yaml' ], indirect=['properties']) def test_valid_yaml(self, properties): """Ensure ChemKED YAML is validated diff --git a/pyked/tests/testfile_rcm3.yaml b/pyked/tests/testfile_rcm3.yaml new file mode 100644 index 0000000..c11f1a3 --- /dev/null +++ b/pyked/tests/testfile_rcm3.yaml @@ -0,0 +1,178 @@ +--- +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: + - 24.25 degC + ignition-delay: + - 1.0 ms + first-stage-ignition-delay: + - 0.5 ms + - uncertainty-type: relative + uncertainty: 0.01 + pressure: + - 958.0 torr + rcm-data: + compressed-pressure: + - 7.1 bar + compressed-temperature: + - 765 K + - uncertainty: 0.01 + uncertainty-type: relative + compression-time: + - 38.0 ms + stroke: + - 10.0 inch + clearance: + - 2.5 cm + compression-ratio: + - 12.0 + 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 + time-histories: + - type: volume + time: + units: s + column: 0 + quantity: + units: cm3 + column: 1 + values: + - [0.00E+000, 5.47669375000E+002] + - [1.00E-003, 5.46608789894E+002] + - [2.00E-003, 5.43427034574E+002] + - [3.00E-003, 5.38124109043E+002] + - [4.00E-003, 5.30700013298E+002] + - [5.00E-003, 5.21154747340E+002] + - [6.00E-003, 5.09488311170E+002] + - [7.00E-003, 4.95700704787E+002] + - [8.00E-003, 4.79791928191E+002] + - [9.00E-003, 4.61761981383E+002] + - [1.00E-002, 4.41610864362E+002] + - [1.10E-002, 4.20399162234E+002] + - [1.20E-002, 3.99187460106E+002] + - [1.30E-002, 3.77975757979E+002] + - [1.40E-002, 3.56764055851E+002] + - [1.50E-002, 3.35552353723E+002] + - [1.60E-002, 3.14340651596E+002] + - [1.70E-002, 2.93128949468E+002] + - [1.80E-002, 2.71917247340E+002] + - [1.90E-002, 2.50705545213E+002] + - [2.00E-002, 2.29493843085E+002] + - [2.10E-002, 2.08282140957E+002] + - [2.20E-002, 1.87070438830E+002] + - [2.30E-002, 1.65858736702E+002] + - [2.40E-002, 1.44647034574E+002] + - [2.50E-002, 1.23435332447E+002] + - [2.60E-002, 1.02223630319E+002] + - [2.70E-002, 8.10119281915E+001] + - [2.80E-002, 6.33355097518E+001] + - [2.90E-002, 5.27296586879E+001] + - [3.00E-002, 4.91943750000E+001] + - [3.10E-002, 4.97137623933E+001] + - [3.20E-002, 5.02063762048E+001] + - [3.30E-002, 5.06454851923E+001] + - [3.40E-002, 5.10218564529E+001] + - [3.50E-002, 5.13374097598E+001] + - [3.60E-002, 5.16004693977E+001] + - [3.70E-002, 5.18223244382E+001] + - [3.80E-002, 5.20148449242E+001] + - [3.90E-002, 5.21889350372E+001] + - [4.00E-002, 5.23536351113E+001] + - [4.10E-002, 5.25157124459E+001] + - [4.20E-002, 5.26796063730E+001] + - [4.30E-002, 5.28476160610E+001] + - [4.40E-002, 5.30202402028E+001] + - [4.50E-002, 5.31965961563E+001] + - [4.60E-002, 5.33748623839E+001] + - [4.70E-002, 5.35527022996E+001] + - [4.80E-002, 5.37276399831E+001] + - [4.90E-002, 5.38973687732E+001] + - [5.00E-002, 5.40599826225E+001] + - [5.10E-002, 5.42141273988E+001] + - [5.20E-002, 5.43590751578E+001] + - [5.30E-002, 5.44947289126E+001] + - [5.40E-002, 5.46215686913E+001] + - [5.50E-002, 5.47405518236E+001] + - [5.60E-002, 5.48529815402E+001] + - [5.70E-002, 5.49603582190E+001] + - [5.80E-002, 5.50642270863E+001] + - [5.90E-002, 5.51660349836E+001] + - [6.00E-002, 5.52670070646E+001] + - [6.10E-002, 5.53680520985E+001] + - [6.20E-002, 5.54697025392E+001] + - [6.30E-002, 5.55720927915E+001] + - [6.40E-002, 5.56749762728E+001] + - [6.50E-002, 5.57777790517E+001] + - [6.60E-002, 5.58796851466E+001] + - [6.70E-002, 5.59797461155E+001] + - [6.80E-002, 5.60770054561E+001] + - [6.90E-002, 5.61706266985E+001] + - [7.00E-002, 5.62600130036E+001] + - [7.10E-002, 5.63449057053E+001] + - [7.20E-002, 5.64254496625E+001] + - [7.30E-002, 5.65022146282E+001] + - [7.40E-002, 5.65761642150E+001] + - [7.50E-002, 5.66485675508E+001] + - [7.60E-002, 5.67208534842E+001] + - [7.70E-002, 5.67944133373E+001] + - [7.80E-002, 5.68703658198E+001] + - [7.90E-002, 5.69493069272E+001] + - [8.00E-002, 5.70310785669E+001] + - [8.10E-002, 5.71146023893E+001] + - [8.20E-002, 5.71978399741E+001] + - [8.30E-002, 5.72779572372E+001] + - [8.40E-002, 5.73517897984E+001] + - [8.50E-002, 5.74167271960E+001] + - [8.60E-002, 5.74721573687E+001] + - [8.70E-002, 5.75216388520E+001] + - [8.80E-002, 5.75759967785E+001] + - [8.90E-002, 5.76575701358E+001] + - [9.00E-002, 5.78058719368E+001] + - [9.10E-002, 5.80849611077E+001] + - [9.20E-002, 5.85928651155E+001] + - [9.30E-002, 5.94734357453E+001] + - [9.40E-002, 6.09310671165E+001] + - [9.50E-002, 6.32487551103E+001] + - [9.60E-002, 6.68100309742E+001] +... From 2c0b278181c2c0ecde226b647b4b52650ac694f1 Mon Sep 17 00:00:00 2001 From: Jeff Santner Date: Wed, 13 Jun 2018 18:01:41 -0700 Subject: [PATCH 03/11] Minor fix in arguments --- pyked/validation.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pyked/validation.py b/pyked/validation.py index 7d85d5c..bb94b6d 100644 --- a/pyked/validation.py +++ b/pyked/validation.py @@ -275,7 +275,8 @@ def _validate_isvalid_quantity(self, isvalid_quantity, field, value): """ if type(value[0]) is str: values = value[0].split() - quantity = Q_(*[float(values[0])] + values[1:]) + value_alt = [float(values[0])] + values[1:] + quantity = Q_(*value_alt) else: quantity = Q_(value[0]) low_lim = 0.0 * units(property_units[field]) From 73551ab330fffd684998d7b0d1f5092536fe37d1 Mon Sep 17 00:00:00 2001 From: Jeff Santner Date: Wed, 13 Jun 2018 18:19:02 -0700 Subject: [PATCH 04/11] Only apply to celsius and fahrenheit --- pyked/validation.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/pyked/validation.py b/pyked/validation.py index bb94b6d..e198ca2 100644 --- a/pyked/validation.py +++ b/pyked/validation.py @@ -275,8 +275,11 @@ def _validate_isvalid_quantity(self, isvalid_quantity, field, value): """ if type(value[0]) is str: values = value[0].split() - value_alt = [float(values[0])] + values[1:] - quantity = Q_(*value_alt) + if temp in values for temp in ['degC', 'degF', 'celsius', 'fahrenheit']: + value_alt = [float(values[0])] + values[1:] + quantity = Q_(*value_alt) + else: + quantity = Q_(value[0]) else: quantity = Q_(value[0]) low_lim = 0.0 * units(property_units[field]) From 368179194423b6da831164a89cf8b5350631a014 Mon Sep 17 00:00:00 2001 From: Jeff Santner Date: Wed, 13 Jun 2018 18:23:32 -0700 Subject: [PATCH 05/11] Syntax --- pyked/validation.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pyked/validation.py b/pyked/validation.py index e198ca2..8bfd101 100644 --- a/pyked/validation.py +++ b/pyked/validation.py @@ -275,7 +275,8 @@ def _validate_isvalid_quantity(self, isvalid_quantity, field, value): """ if type(value[0]) is str: values = value[0].split() - if temp in values for temp in ['degC', 'degF', 'celsius', 'fahrenheit']: + if any([temp in values for temp in + ['degC', 'degF', 'celsius', 'fahrenheit']]): value_alt = [float(values[0])] + values[1:] quantity = Q_(*value_alt) else: From f759ab1b64ed80aa92a0241826057666cf2a143e Mon Sep 17 00:00:00 2001 From: jsantner Date: Thu, 14 Jun 2018 14:24:57 -0700 Subject: [PATCH 06/11] Fix behavior in chemked.process_quantity --- pyked/chemked.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/pyked/chemked.py b/pyked/chemked.py index a8573e9..d8c83f1 100644 --- a/pyked/chemked.py +++ b/pyked/chemked.py @@ -718,7 +718,16 @@ def __init__(self, properties): def process_quantity(self, properties): """Process the uncertainty information from a given quantity and return it """ - quant = Q_(properties[0]) + if type(properties[0]) is str: + values = properties[0].split() + if any([temp in values for temp in + ['degC', 'degF', 'celsius', 'fahrenheit']]): + value_alt = [float(values[0])] + values[1:] + quant = Q_(*value_alt) + else: + quant = Q_(properties[0]) + else: + quant = Q_(properties[0]) if len(properties) > 1: unc = properties[1] uncertainty = unc.get('uncertainty', False) From 2147780394e694d5149ac1f2e0a37c9168438422 Mon Sep 17 00:00:00 2001 From: jsantner Date: Mon, 25 Jun 2018 09:29:51 -0700 Subject: [PATCH 07/11] Fixing changes that were requested. Simplify handling of Offset Units. Also, add functionality for 'nan' in yaml files. --- pyked/chemked.py | 18 +++++++++--------- pyked/validation.py | 17 ++++++++--------- 2 files changed, 17 insertions(+), 18 deletions(-) diff --git a/pyked/chemked.py b/pyked/chemked.py index d8c83f1..1b5076e 100644 --- a/pyked/chemked.py +++ b/pyked/chemked.py @@ -9,6 +9,7 @@ import xml.etree.ElementTree as etree import xml.dom.minidom as minidom from itertools import chain +import pint import numpy as np @@ -718,16 +719,15 @@ def __init__(self, properties): def process_quantity(self, properties): """Process the uncertainty information from a given quantity and return it """ - if type(properties[0]) is str: - values = properties[0].split() - if any([temp in values for temp in - ['degC', 'degF', 'celsius', 'fahrenheit']]): - value_alt = [float(values[0])] + values[1:] - quant = Q_(*value_alt) - else: - quant = Q_(properties[0]) - else: + try: quant = Q_(properties[0]) + except pint.OffsetUnitCalculusError: + values = properties[0].split() + quant = Q_(float(values[0]), ''.join(values[1:])) + except pint.UndefinedUnitError: + values = properties[0].split() + if values[0] == 'nan': + quant = Q_(np.nan, ''.join(values[1:])) if len(properties) > 1: unc = properties[1] uncertainty = unc.get('uncertainty', False) diff --git a/pyked/validation.py b/pyked/validation.py index 8bfd101..a63aa74 100644 --- a/pyked/validation.py +++ b/pyked/validation.py @@ -273,16 +273,15 @@ def _validate_isvalid_quantity(self, isvalid_quantity, field, value): {'isvalid_quantity': {'type': 'bool'}, 'field': {'type': 'str'}, 'value': {'type': 'list'}} """ - if type(value[0]) is str: - values = value[0].split() - if any([temp in values for temp in - ['degC', 'degF', 'celsius', 'fahrenheit']]): - value_alt = [float(values[0])] + values[1:] - quantity = Q_(*value_alt) - else: - quantity = Q_(value[0]) - else: + try: quantity = Q_(value[0]) + except pint.OffsetUnitCalculusError: + values = value[0].split() + quantity = Q_(float(values[0]), ''.join(values[1:])) + except pint.UndefinedUnitError: + values = value[0].split() + if values[0] == 'nan': + quantity = Q_(np.nan, ''.join(values[1:])) low_lim = 0.0 * units(property_units[field]) try: From 5c74873f28377b1a7638ba3def13d41020c75d6b Mon Sep 17 00:00:00 2001 From: jsantner Date: Mon, 25 Jun 2018 09:40:15 -0700 Subject: [PATCH 08/11] raise pint.UndefinedUnitError if it can't be fixed by np.nan --- pyked/chemked.py | 2 ++ pyked/validation.py | 2 ++ 2 files changed, 4 insertions(+) diff --git a/pyked/chemked.py b/pyked/chemked.py index 1b5076e..42f5717 100644 --- a/pyked/chemked.py +++ b/pyked/chemked.py @@ -728,6 +728,8 @@ def process_quantity(self, properties): values = properties[0].split() if values[0] == 'nan': quant = Q_(np.nan, ''.join(values[1:])) + else: + raise if len(properties) > 1: unc = properties[1] uncertainty = unc.get('uncertainty', False) diff --git a/pyked/validation.py b/pyked/validation.py index a63aa74..d455734 100644 --- a/pyked/validation.py +++ b/pyked/validation.py @@ -282,6 +282,8 @@ def _validate_isvalid_quantity(self, isvalid_quantity, field, value): values = value[0].split() if values[0] == 'nan': quantity = Q_(np.nan, ''.join(values[1:])) + else: + raise low_lim = 0.0 * units(property_units[field]) try: From ae14c433db477e2bbc5ff500110349efe288675f Mon Sep 17 00:00:00 2001 From: jsantner Date: Tue, 26 Jun 2018 11:01:21 -0700 Subject: [PATCH 09/11] Remove 'nan' handling --- pyked/chemked.py | 6 ------ pyked/validation.py | 6 ------ 2 files changed, 12 deletions(-) diff --git a/pyked/chemked.py b/pyked/chemked.py index 42f5717..d6a9997 100644 --- a/pyked/chemked.py +++ b/pyked/chemked.py @@ -724,12 +724,6 @@ def process_quantity(self, properties): except pint.OffsetUnitCalculusError: values = properties[0].split() quant = Q_(float(values[0]), ''.join(values[1:])) - except pint.UndefinedUnitError: - values = properties[0].split() - if values[0] == 'nan': - quant = Q_(np.nan, ''.join(values[1:])) - else: - raise if len(properties) > 1: unc = properties[1] uncertainty = unc.get('uncertainty', False) diff --git a/pyked/validation.py b/pyked/validation.py index d455734..ed26e00 100644 --- a/pyked/validation.py +++ b/pyked/validation.py @@ -278,12 +278,6 @@ def _validate_isvalid_quantity(self, isvalid_quantity, field, value): except pint.OffsetUnitCalculusError: values = value[0].split() quantity = Q_(float(values[0]), ''.join(values[1:])) - except pint.UndefinedUnitError: - values = value[0].split() - if values[0] == 'nan': - quantity = Q_(np.nan, ''.join(values[1:])) - else: - raise low_lim = 0.0 * units(property_units[field]) try: From befbe89d06d65639e93492b9f1c6504dfc32ea39 Mon Sep 17 00:00:00 2001 From: jsantner Date: Tue, 10 Jul 2018 16:56:32 -0700 Subject: [PATCH 10/11] Add test for changes in chemked.py --- pyked/tests/test_chemked.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/pyked/tests/test_chemked.py b/pyked/tests/test_chemked.py index 8a1561f..ab8b452 100644 --- a/pyked/tests/test_chemked.py +++ b/pyked/tests/test_chemked.py @@ -723,6 +723,9 @@ def test_temperature(self): properties = self.load_properties('testfile_required.yaml') d = DataPoint(properties[0]) assert np.isclose(d.temperature, Q_(1164.48, 'K')) + properties = self.load_properties('testfile_rcm3.yaml') + d = DataPoint(properties[0]) + assert np.isclose(d.temperature, Q_(297.4, 'K')) def test_rcm_data(self): properties = self.load_properties('testfile_rcm2.yaml') From 6eee6dfc5b6d11979c7290cb50994fc997313190 Mon Sep 17 00:00:00 2001 From: jsantner Date: Tue, 10 Jul 2018 17:10:23 -0700 Subject: [PATCH 11/11] pint quantities must be in same unit to be compared with np.isclose --- pyked/tests/test_chemked.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyked/tests/test_chemked.py b/pyked/tests/test_chemked.py index ab8b452..8c16636 100644 --- a/pyked/tests/test_chemked.py +++ b/pyked/tests/test_chemked.py @@ -725,7 +725,7 @@ def test_temperature(self): assert np.isclose(d.temperature, Q_(1164.48, 'K')) properties = self.load_properties('testfile_rcm3.yaml') d = DataPoint(properties[0]) - assert np.isclose(d.temperature, Q_(297.4, 'K')) + assert np.isclose(d.temperature.to('K'), Q_(297.4, 'K')) def test_rcm_data(self): properties = self.load_properties('testfile_rcm2.yaml')