Skip to content

Commit

Permalink
Bug fixes. (#52)
Browse files Browse the repository at this point in the history
* Fix rates - first row is NaN

* Don't propagate uncertainties through cumsum.

* black

* Filter points where I == 0 in fe.
  • Loading branch information
PeterKraus authored Aug 16, 2022
1 parent add39a9 commit 50b29a3
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 10 deletions.
18 changes: 16 additions & 2 deletions src/dgpost/transform/electrochemistry.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,15 @@
from yadg.dgutils import ureg
import numpy as np
import pandas as pd
from dgpost.utils.helpers import load_data, name_to_chem, electrons_from_smiles
from typing import Iterable
from uncertainties import UFloat
from uncertainties import unumpy as unp
from dgpost.utils.helpers import (
load_data,
separate_data,
name_to_chem,
electrons_from_smiles,
)


@load_data(
Expand Down Expand Up @@ -177,6 +185,8 @@ def fe(
"""
etot = abs(I) / (ureg("elementary_charge") * ureg("avogadro_constant"))
if isinstance(etot.m, Iterable):
etot.m[etot.m == 0] = np.NaN
pretag = "fe" if output is None else output
ret = {}
for k, v in rate.items():
Expand Down Expand Up @@ -243,7 +253,11 @@ def charge(

dQ = I * dt

Q = np.cumsum(dQ).to_base_units()
if any([isinstance(i.m, UFloat) for i in dQ]):
dQn, dQs, dQu = separate_data(dQ)
Q = ureg.Quantity(unp.uarray(np.cumsum(dQn), dQs), dQu).to_base_units()
else:
Q = np.cumsum(dQ).to_base_units()

ret = {output: Q}
return ret
Expand Down
7 changes: 3 additions & 4 deletions src/dgpost/transform/rates.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
"""
import pint

from yadg.dgutils import ureg
import pandas as pd
import numpy as np
Expand Down Expand Up @@ -172,16 +171,16 @@ def batch_to_molar(
c0 = ureg.Quantity(0, "mol/m³")
for k, v in c.items():
c[k] = np.insert(v, 0, c0)
elif isinstance(V.magnitude, Iterable) and len(V) == nts:
elif isinstance(V.m, Iterable) and len(V) == nts:
V = V[1:]

dt = np.diff(time)
dc = {k: np.diff(c_k) for k, c_k in c.items()}
dc = {k: np.diff(v) for k, v in c.items()}
ret = {}
for k, dc_k in dc.items():
r = (dc_k / dt) * V
if len(r) < nts:
r0 = ureg.Quantity(0, "mol/s")
r0 = ureg.Quantity(np.nan, "mol/s")
r = np.insert(r, 0, r0)
ret[f"{output}->{k}"] = r.to_base_units()
return ret
Binary file not shown.
Binary file not shown.
20 changes: 19 additions & 1 deletion tests/test_electrochemistry_fe.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,24 @@
"fe->CO": ureg.Quantity(0.02858024),
},
),
( # ts2 - arrays, with I == 0, all Quantities, weird units
{
"I": ureg.Quantity([0, 100], "mA"),
"rate": {
"H2": ureg.Quantity([0.6811524, 0.6811524], "mmol/hour"),
"CH4": ureg.Quantity([0.1134709, 0.1134709], "mmol/hour"),
"C2H4": ureg.Quantity([0.0239087, 0.0239087], "mmol/hour"),
"CO": ureg.Quantity([0.0533184, 0.0533184], "mmol/hour"),
},
"charges": {"C": 4, "H": 1, "O": -2},
},
{
"fe->H2": ureg.Quantity([np.NaN, 0.36511734]),
"fe->CH4": ureg.Quantity([np.NaN, 0.24329496]),
"fe->C2H4": ureg.Quantity([np.NaN, 0.07689462]),
"fe->CO": ureg.Quantity([np.NaN, 0.02858024]),
},
),
],
)
def test_electrochemistry_fe_direct(inputs, output):
Expand All @@ -53,7 +71,7 @@ def test_electrochemistry_fe_direct(inputs, output):
print(ret)
for k in inputs["rate"].keys():
n = f"{tag}->{k}"
assert np.allclose(ret[n], output[n])
assert np.allclose(ret[n], output[n], equal_nan=True)


@pytest.mark.parametrize(
Expand Down
10 changes: 7 additions & 3 deletions tests/test_rates_batchtomolar.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,20 @@
"V": ureg.Quantity(1, "l"),
},
{
"rate->A": ureg.Quantity([0.0, 10.0 / 60, 5.0 / 60, 5.0 / 60], "mol/s"),
"rate->B": ureg.Quantity([0.0, 5.0 / 60, 5.0 / 60, 1.0 / 60], "mol/s"),
"rate->A": ureg.Quantity(
[np.NaN, 10.0 / 60, 5.0 / 60, 5.0 / 60], "mol/s"
),
"rate->B": ureg.Quantity(
[np.NaN, 5.0 / 60, 5.0 / 60, 1.0 / 60], "mol/s"
),
},
),
],
)
def test_rates_batchtomolar_direct(spec, ref):
ret = rates.batch_to_molar(**spec)
for k, v in ref.items():
assert np.allclose(v, ret[k])
assert np.allclose(v, ret[k], equal_nan=True)


@pytest.mark.parametrize(
Expand Down
Binary file modified tests/test_rates_batchtomolar/ref.batchtomolar.plain.df.pkl
Binary file not shown.
Binary file modified tests/test_rates_batchtomolar/ref.batchtomolar.units.df.pkl
Binary file not shown.

0 comments on commit 50b29a3

Please sign in to comment.