From 75ef1c9e61f044306652901fcc5722158a85f2b2 Mon Sep 17 00:00:00 2001 From: yokochin Date: Wed, 4 Dec 2024 16:19:40 -0600 Subject: [PATCH 1/2] Fixed xarray_wrappers.py and test, removed demos --- autoreact/ktp_xarray/xarray_demo_DataArray.py | 60 ------------------- autoreact/ktp_xarray/xarray_demo_DataSet.py | 26 -------- autoreact/ktp_xarray/xarray_wrappers.py | 4 +- autoreact/ktp_xarray/xarray_wrappers_test.py | 22 ++++--- 4 files changed, 17 insertions(+), 95 deletions(-) delete mode 100644 autoreact/ktp_xarray/xarray_demo_DataArray.py delete mode 100644 autoreact/ktp_xarray/xarray_demo_DataSet.py diff --git a/autoreact/ktp_xarray/xarray_demo_DataArray.py b/autoreact/ktp_xarray/xarray_demo_DataArray.py deleted file mode 100644 index 7015ed96..00000000 --- a/autoreact/ktp_xarray/xarray_demo_DataArray.py +++ /dev/null @@ -1,60 +0,0 @@ -import xarray -import numpy - -temps = [1000, 1500, 2000, 2500] -press = [1, 10, numpy.inf] - -# Define the array of rates (note: it makes sense to structure it as a list -# of rates at a given P based on how the MESS parser is written, though it -# doesn't really matter) -ktp_vals = [[1e1, 1e2, 1e3, 1e4], [1e5, 1e6, 1e7, 1e8], [1e9, 1e10, 1e11, 1e12]] - -# Create the DataArray -ktp = xarray.DataArray(ktp_vals, [("pres", press), ("temp", temps)]) -print(ktp) - -# Get a slice at a selected pressure or temperature value -print('\nPressure slice by value') -print(ktp.sel(pres=numpy.inf)) # the fact that you can use np.inf is very convenient! -print('\nTemperature slice by value') -print(ktp.sel(temp=1500)) - -# Get a specific value at a selected temperature value and pressure value -print('\nTemperature slice by value') -print(ktp.sel(temp=1500, pres=1)) - -# Get a slice at a selected pressure or temperature index -print('\nPressure slice by index') -print(ktp.isel(pres=0)) -print('\nTemperature slice by index') -print(ktp.isel(temp=0)) - -# Can also add some metadata (probably unnecessary for ktp objects, but cool) -ktp.attrs["units"] = "s^-1" # any number of arbitrary attributes can be added -ktp.name = "N2O=N2+O" - -# Can do various arithmetic operations -print('\nVarious operations') -print(ktp + ktp) -print(3 * ktp) - -# To get the actual values, use the values OR data attribute -print('\nGet the values, which should be a numpy array') -print(ktp.values) -print('type of ktp.values: ', type(ktp.values)) -print('\nTemperature slice by value, as an array') -print(ktp.sel(temp=1500, pres=1).data) -print(type(ktp.sel(temp=1500, pres=1).data)) - -# Can get all dimensions associated with an array usings the dims attribute -print('\nNames of all dimensions, as a tuple') -print(ktp.dims) - -# Can get the coordinates associated with a dimension by using that coordinate name -print('\nPressure values: ', ktp.pres.data) -print('\nTemperature values: ', ktp.temp.data) - -# Can do lookup on values with the nearest value -# This is giving an error even though I thought I copied it from the website? -#print('\nTemperature slice by nearest lookup') -#print(ktp.sel(temp=1600), method='nearest') diff --git a/autoreact/ktp_xarray/xarray_demo_DataSet.py b/autoreact/ktp_xarray/xarray_demo_DataSet.py deleted file mode 100644 index 2148424e..00000000 --- a/autoreact/ktp_xarray/xarray_demo_DataSet.py +++ /dev/null @@ -1,26 +0,0 @@ -import xarray -import numpy - -temps = [1000, 1500, 2000, 2500] -enthalpy = [60, 70, 80, 90] -gibbs = [10, 15, 20, 25] -c_p = [1, 1.1, 1.2, 1.3] -entropy = [3, 4, 5, 6] - -therm = xarray.Dataset( - { - "enthalpy": (["temp"], enthalpy), - "gibbs": (["temp"], gibbs), - "c_p": (["temp"], c_p), - "entropy": (["temp"], entropy), - }, - coords={ - "temp": temps, - }, -) - -print(therm) -print(therm.temp) -print(therm.temp.values) - - diff --git a/autoreact/ktp_xarray/xarray_wrappers.py b/autoreact/ktp_xarray/xarray_wrappers.py index a0d3098d..8496b256 100644 --- a/autoreact/ktp_xarray/xarray_wrappers.py +++ b/autoreact/ktp_xarray/xarray_wrappers.py @@ -10,7 +10,7 @@ def from_data(temps, press, rates): Construct a KTP DataArray from data """ - ktp = xarray.DataArray(rates, [("pres", press), ("temp", temps)]) + ktp = xarray.DataArray(rates, (("pres", press), ("temp", temps))) return ktp @@ -88,5 +88,5 @@ def set_rates(ktp, rates, pres, temp): Sets the KTP values """ - ktp.loc[dict(pres=pres, temp=temp)] = rates + ktp.loc[{"pres": pres, "temp": temp}] = rates return ktp diff --git a/autoreact/ktp_xarray/xarray_wrappers_test.py b/autoreact/ktp_xarray/xarray_wrappers_test.py index 06c4cba5..60b9c0f8 100644 --- a/autoreact/ktp_xarray/xarray_wrappers_test.py +++ b/autoreact/ktp_xarray/xarray_wrappers_test.py @@ -1,6 +1,7 @@ -import xarray_wrappers -import xarray +"""Tests xarray_wrappers.py's functions""" + import numpy +from autoreact.ktp_xarray import xarray_wrappers Temps = [1000, 1500, 2000, 2500] Press = [1, 10, numpy.inf] @@ -9,53 +10,60 @@ Ktp = xarray_wrappers.from_data(Temps, Press, Rates) print(Ktp) -def test_set_rates(): - ktp = xarray_wrappers.set_rates(Ktp, Rates) - print(ktp) - def test_get_temperatures(): + """Tests the get_temperatures function""" temp = xarray_wrappers.get_temperatures(Ktp) print(temp) def test_get_pressures(): + """Tests the get_pressures function""" pres = xarray_wrappers.get_pressures(Ktp) print(pres) def test_get_values(): + """Tests the get_values function""" vals = xarray_wrappers.get_values(Ktp) print(vals) def test_get_pslice(): + """Tests the get_pslice function""" pslice = xarray_wrappers.get_pslice(Ktp, numpy.inf) print(pslice) def test_get_tslice(): + """Tests the get_tslice function""" tslice = xarray_wrappers.get_tslice(Ktp, 1500) print(tslice) def test_get_spec_vals(): + """Tests the get_spec_values function""" vals = xarray_wrappers.get_spec_vals(Ktp, 1500, 1) print(vals) def test_get_ipslice(): + """Tests the get_ipslice function""" ipslice = xarray_wrappers.get_ipslice(Ktp, 0) print(ipslice) def test_get_itslice(): + """Tests the get_itslice function""" itslice = xarray_wrappers.get_itslice(Ktp, 0) print(itslice) + def test_set_rates(): + """Tests the set_rates function""" new_rates = xarray_wrappers.set_rates(Ktp, 1e11, 10, 2000) print(new_rates) - + + test_get_pressures() test_get_temperatures() test_get_values() From d0b03a52f1ee586570d2291ffd32304bdde91f9a Mon Sep 17 00:00:00 2001 From: yokochin Date: Wed, 4 Dec 2024 16:25:03 -0600 Subject: [PATCH 2/2] Fixed ktp_xarray/__init__.py --- autoreact/ktp_xarray/__init__.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/autoreact/ktp_xarray/__init__.py b/autoreact/ktp_xarray/__init__.py index 4d45d0c4..64660ddd 100644 --- a/autoreact/ktp_xarray/__init__.py +++ b/autoreact/ktp_xarray/__init__.py @@ -1,3 +1,5 @@ +"""Initializes the ktp_xarray folder""" + from autoreact.ktp_xarray import xarray_wrappers __all__ = [