Skip to content

Commit

Permalink
added tests for v11.50
Browse files Browse the repository at this point in the history
  • Loading branch information
JhonFlash3008 authored and chatcannon committed Feb 3, 2024
1 parent 46f296f commit 7a6ac1c
Show file tree
Hide file tree
Showing 16 changed files with 156 additions and 3 deletions.
16 changes: 14 additions & 2 deletions galvani/BioLogic.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ def fieldname_to_dtype(fieldname):
"|Z|/Ohm",
"Re(Z)/Ohm",
"-Im(Z)/Ohm",
"Re(M)",
"Im(M)",
"|M|",
"Re(Permittivity)",
"Im(Permittivity)",
"|Permittivity|",
"Tan(Delta)",
):
return (fieldname, np.float_)
elif fieldname in (
Expand All @@ -60,13 +67,13 @@ def fieldname_to_dtype(fieldname):
"Capacity/mA.h",
):
return (fieldname, np.float_)
elif fieldname in ("cycle number", "I Range", "Ns", "half cycle"):
elif fieldname in ("cycle number", "I Range", "Ns", "half cycle", "z cycle"):
return (fieldname, np.int_)
elif fieldname in ("dq/mA.h", "dQ/mA.h"):
return ("dQ/mA.h", np.float_)
elif fieldname in ("I/mA", "<I>/mA"):
return ("I/mA", np.float_)
elif fieldname in ("Ewe/V", "<Ewe>/V", "Ecell/V"):
elif fieldname in ("Ewe/V", "<Ewe>/V", "Ecell/V", "<Ewe/V>"):
return ("Ewe/V", np.float_)
elif fieldname.endswith(
(
Expand All @@ -87,8 +94,13 @@ def fieldname_to_dtype(fieldname):
"/mF",
"/uF",
"/µF",
"/nF",
"/C",
"/Ohm",
"/Ohm-1",
"/Ohm.cm",
"/mS/cm",
"/%",
)
):
return (fieldname, np.float_)
Expand Down
101 changes: 100 additions & 1 deletion tests/test_BioLogic.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from datetime import date, datetime

import numpy as np
from numpy.testing import assert_array_almost_equal, assert_array_equal
from numpy.testing import assert_array_almost_equal, assert_array_equal, assert_allclose
import pytest

from galvani import BioLogic, MPTfile, MPRfile
Expand Down Expand Up @@ -209,6 +209,89 @@ def assert_field_exact(fieldname):
except AttributeError:
pass

def assert_MPR_matches_MPT_v2(mpr, mpt, comments):
"""
Asserts that the fields in the MPR.data ar the same as in the MPT. Modified from assert_MPR_matches_MPT.
Automatically converts dtype from MPT data to dtype from MPR data before comparing the columns.
Speficicity for EIS_indicators : these fields are valid only at f<100kHz so their values are replaced
by -1 or 0 at high frequency in the MPT file, this is not the case in the MPR.data.
Parameters
----------
mpr : MPRfile
Data extracted with the MPRfile class.
mpt : np.array
Data extracted with MPTfile method.
Returns
-------
None.
"""

def assert_field_matches(fieldname):
EIS_quality_indicators = [
"THD Ewe/%",
"NSD Ewe/%",
"NSR Ewe/%",
"|Ewe h2|/V",
"|Ewe h3|/V",
"|Ewe h4|/V",
"|Ewe h5|/V",
"|Ewe h6|/V",
"|Ewe h7|/V",
"THD I/%",
"NSD I/%",
"NSR I/%",
"|I h2|/A",
"|I h3|/A",
"|I h4|/A",
"|I h5|/A",
"|I h6|/A",
"|I h7|/A",
]

if fieldname in EIS_quality_indicators: # EIS quality indicators only valid for f < 100kHz
index_inf_100k = np.where(mpr.data["freq/Hz"] < 100000)[0]
assert_allclose(
mpr.data[index_inf_100k][fieldname],
mpt[index_inf_100k][fieldname].astype(mpr.data[fieldname].dtype),
)
elif fieldname == "<Ewe>/V":
assert_allclose(
mpr.data[fieldname],
mpt["Ewe/V"].astype(mpr.data[fieldname].dtype),
)
elif fieldname == "<I>/mA":
assert_allclose(
mpr.data[fieldname],
mpt["I/mA"].astype(mpr.data[fieldname].dtype),
)
elif fieldname == "dq/mA.h":
assert_allclose(
mpr.data[fieldname],
mpt["dQ/mA.h"].astype(mpr.data[fieldname].dtype),
)
else:
assert_allclose(
mpr.data[fieldname],
mpt[fieldname].astype(mpr.data[fieldname].dtype),
)

def assert_field_exact(fieldname):
if fieldname in mpr.dtype.fields:
assert_array_equal(mpr.data[fieldname], mpt[fieldname])

for key in mpr.flags_dict.keys():
assert_array_equal(mpr.get_flag(key), mpt[key])

for d in mpr.dtype.descr[1:]:
assert_field_matches(d[0])

try:
assert timestamp_from_comments(comments) == mpr.timestamp.replace(microsecond=0)
except AttributeError:
pass

@pytest.mark.parametrize(
"basename",
Expand Down Expand Up @@ -252,3 +335,19 @@ def test_MPR6_matches_MPT6(testdata_dir):
mpt, comments = MPTfile(os.path.join(testdata_dir, "bio_logic6.mpt"))
mpr.data = mpr.data[:958] # .mpt file is incomplete
assert_MPR_matches_MPT(mpr, mpt, comments)

@pytest.mark.parametrize(
"basename_v1150",
["v1150_CA", "v1150_CP", "v1150_GCPL", "v1150_GEIS", "v1150_MB", "v1150_OCV", "v1150_PEIS"],
)
def test_MPR_matches_MPT_v1150(testdata_dir, basename_v1150):
"""Check the MPR parser against the MPT parser.
Load a binary .mpr file and a text .mpt file which should contain
exactly the same data. Check that the loaded data actually match.
"""
binpath = os.path.join(testdata_dir, "v1150", basename_v1150 + ".mpr")
txtpath = os.path.join(testdata_dir, "v1150", basename_v1150 + ".mpt")
mpr = MPRfile(binpath)
mpt, comments = MPTfile(txtpath, encoding="latin1")
assert_MPR_matches_MPT_v2(mpr, mpt, comments)
3 changes: 3 additions & 0 deletions tests/testdata/v1150/v1150_CA.mpr
Git LFS file not shown
3 changes: 3 additions & 0 deletions tests/testdata/v1150/v1150_CA.mpt
Git LFS file not shown
3 changes: 3 additions & 0 deletions tests/testdata/v1150/v1150_CP.mpr
Git LFS file not shown
3 changes: 3 additions & 0 deletions tests/testdata/v1150/v1150_CP.mpt
Git LFS file not shown
3 changes: 3 additions & 0 deletions tests/testdata/v1150/v1150_GCPL.mpr
Git LFS file not shown
3 changes: 3 additions & 0 deletions tests/testdata/v1150/v1150_GCPL.mpt
Git LFS file not shown
3 changes: 3 additions & 0 deletions tests/testdata/v1150/v1150_GEIS.mpr
Git LFS file not shown
3 changes: 3 additions & 0 deletions tests/testdata/v1150/v1150_GEIS.mpt
Git LFS file not shown
3 changes: 3 additions & 0 deletions tests/testdata/v1150/v1150_MB.mpr
Git LFS file not shown
3 changes: 3 additions & 0 deletions tests/testdata/v1150/v1150_MB.mpt
Git LFS file not shown
3 changes: 3 additions & 0 deletions tests/testdata/v1150/v1150_OCV.mpr
Git LFS file not shown
3 changes: 3 additions & 0 deletions tests/testdata/v1150/v1150_OCV.mpt
Git LFS file not shown
3 changes: 3 additions & 0 deletions tests/testdata/v1150/v1150_PEIS.mpr
Git LFS file not shown
3 changes: 3 additions & 0 deletions tests/testdata/v1150/v1150_PEIS.mpt
Git LFS file not shown

0 comments on commit 7a6ac1c

Please sign in to comment.