Skip to content

Commit

Permalink
clean up maccor
Browse files Browse the repository at this point in the history
  • Loading branch information
jepegit committed Mar 3, 2024
1 parent 8fcc264 commit 99f8069
Show file tree
Hide file tree
Showing 13 changed files with 9,263 additions and 266 deletions.
13 changes: 10 additions & 3 deletions cellpy/readers/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import sys
import time
import warnings
from typing import Any, Tuple, Dict, List, Union, TypeVar
from typing import Any, Tuple, Dict, List, Union, TypeVar, Optional

import numpy as np
import pandas as pd
Expand Down Expand Up @@ -711,9 +711,16 @@ def generate_default_factory():


# TODO: v1.1.0 - implement plugins and local instrument readers
def find_all_instruments() -> Dict[str, Tuple[str, pathlib.Path]]:
def find_all_instruments(
name_contains: Optional[str] = None,
) -> Dict[str, Tuple[str, pathlib.Path]]:
"""finds all the supported instruments"""

if name_contains:
glob_txt = f"*{name_contains}*.py"
else:
glob_txt = "*.py"

import cellpy.readers.instruments as hard_coded_instruments_site

instruments_found = {}
Expand All @@ -724,7 +731,7 @@ def find_all_instruments() -> Dict[str, Tuple[str, pathlib.Path]]:
).parent
modules_in_hard_coded_instruments_site = [
s
for s in hard_coded_instruments_site.glob("*.py")
for s in hard_coded_instruments_site.glob(glob_txt)
if not (
str(s.name).startswith("_")
or str(s.name).startswith("dev_")
Expand Down
22 changes: 17 additions & 5 deletions cellpy/readers/instruments/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -670,12 +670,23 @@ class TxtLoader(AutoLoader, ABC):
raw_ext = "*"

# override this if needed
def parse_loader_parameters(self, **kwargs):
sep = kwargs.get("sep", None)
if sep is not None:
self.sep = sep
if self.sep is None:
def parse_loader_parameters(self, auto_formatter=None, **kwargs):
"""Parse the loader parameters.
Args:
auto_formatter: if True, the formatter will be set to auto-formatting.
**kwargs: keyword arguments.
"""

if auto_formatter:
self._auto_formatter()
else:
# backup option - do auto-formatting if sep is not given
sep = kwargs.get("sep", None)
if sep is not None:
self.sep = sep
if self.sep is None:
self._auto_formatter()

# override this if needed
def parse_formatter_parameters(self, **kwargs):
Expand Down Expand Up @@ -715,6 +726,7 @@ def parse_formatter_parameters(self, **kwargs):
)

def _auto_formatter(self):
print("auto-formatting")
separator, first_index = find_delimiter_and_start(
self.name,
separators=None,
Expand Down
71 changes: 0 additions & 71 deletions cellpy/readers/instruments/configurations/maccor_txt_four.py

This file was deleted.

6 changes: 5 additions & 1 deletion cellpy/readers/instruments/configurations/maccor_txt_one.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
# not updated yet
"""Configuration for tab-delimited Maccor txt files with three comment rows (used by UBham in the SIMBA project)."""

# currently skips the comment rows instead of parsing them (change this by modifying the formatters
# and add preprocessing step for parsing them)

unit_labels = {
"resistance": "Ohms",
# not observed yet:
Expand Down
87 changes: 41 additions & 46 deletions cellpy/readers/instruments/configurations/maccor_txt_three.py
Original file line number Diff line number Diff line change
@@ -1,82 +1,77 @@
# Works for data from WMG and UBham (SIMBA project)
"""Configuration for tab-delimited Maccor txt files with two comment rows (used by WMG in the SIMBA project)."""

# currently skips the comment rows instead of parsing them (change this by modifying the formatters
# and add preprocessing step for parsing them)

file_info = {"raw_extension": "txt"}

unit_labels = {
"resistance": "Ohms",
"time": "s",
"current": "Amps",
"voltage": "Volts",
"power": "Watt-hr",
"capacity": "Amp-hr",
"current": "A",
"voltage": "V",
"power": "W",
"capacity": "Ah",
"energy": "Wh",
"temperature": "C",
}

prefixes = {
"G": 1000_000_000,
"M": 1000_000,
"k": 1000.0,
"h": 100.0,
"d": 10.0,
"c": 0.01,
"m": 0.001,
"micro": 0.000_001,
"n": 0.000_000_001,
}

normal_headers_renaming_dict = {
"data_point_txt": f"Rec#",
"datetime_txt": f"DPt Time",
"test_time_txt": f"TestTime",
"step_time_txt": f"StepTime",
"cycle_index_txt": f"Cyc#",
"step_index_txt": f"Step",
"data_point_txt": "Rec#",
"cycle_index_txt": "Cyc#",
"step_index_txt": "Step",
"test_time_txt": "TestTime",
"step_time_txt": "StepTime",
"charge_capacity_txt": "mAmp-hr",
"charge_energy_txt": "mWatt-hr",
"current_txt": "mAmps",
"voltage_txt": "Volts",
"datetime_txt": "DPt Time",
}

file_info = {"raw_extension": "txt"}

states = {
"column_name": "State",
"charge_keys": ["C"],
"discharge_keys": ["D"],
"rest_keys": ["R"],
}


raw_units = {"current": "A", "charge": "Ah", "mass": "g", "voltage": "V"}

# raw_limits = {
# "current_hard": 0.000_000_000_000_1,
# "current_soft": 0.000_01,
# "stable_current_hard": 2.0,
# "stable_current_soft": 4.0,
# "stable_voltage_hard": 2.0,
# "stable_voltage_soft": 4.0,
# "stable_charge_hard": 0.001,
# "stable_charge_soft": 5.0,
# "ir_change": 0.00001,
# }
raw_limits = {
"current_hard": 0.000_000_000_000_1,
"current_soft": 0.000_01,
"stable_current_hard": 2.0,
"stable_current_soft": 4.0,
"stable_voltage_hard": 2.0,
"stable_voltage_soft": 4.0,
"stable_charge_hard": 0.001,
"stable_charge_soft": 5.0,
"ir_change": 0.00001,
}

formatters = {
"skiprows": 3, # 12 for other file
"skiprows": 2,
"sep": "\t",
"header": 0, # 0 for other file
"header": 0,
"encoding": "ISO-8859-1", # options: "ISO-8859-1", "utf-8", "cp1252"
"decimal": ".",
"thousands": ",",
"decimal": ",",
"thousands": None,
}

pre_processors = {}
pre_processors = {
"remove_empty_lines": True,
}

post_processors = {
"get_column_names": True,
"split_capacity": True,
"split_current": False,
"split_current": True,
"set_index": True,
"rename_headers": True,
"remove_last_if_bad": True,
"set_cycle_number_not_zero": True,
"remove_last_if_bad": True,
"convert_date_time_to_datetime": True,
"convert_step_time_to_timedelta": True,
"convert_test_time_to_timedelta": True,
"convert_units": True,
"select_columns_to_keep": True,
}
5 changes: 4 additions & 1 deletion cellpy/readers/instruments/configurations/maccor_txt_two.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# Works for data from KIT (SIMBA project) with ',' as decimal and dropping bad last rows.
"""Configuration for tab-delimited Maccor txt files with 12 comment rows (used by KIT in the SIMBA project)."""

# currently skips the comment rows instead of parsing them (change this by modifying the formatters
# and add preprocessing step for parsing them)

unit_labels = {
"resistance": "Ohms",
Expand Down
82 changes: 72 additions & 10 deletions cellpy/readers/instruments/maccor_txt.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,13 @@


SUPPORTED_MODELS = {
"ZERO": "maccor_txt_zero",
"ZERO": "maccor_txt_zero", # needs to be updated (does no postprocessing at the moment)
"ONE": "maccor_txt_one",
"TWO": "maccor_txt_two",
"THREE": "maccor_txt_three",
"FOUR": "maccor_txt_four",
"WMG_SIMBA": "maccor_txt_three",
"KIT_SIMBA": "maccor_txt_four",
"KIT_COMMA_SIMBA": "maccor_txt_two",
"UBHAM_SIMBA": "maccor_txt_three",
"S4000-WMG": "maccor_txt_four",
"S4000-KIT": "maccor_txt_four",
"S4000-UBHAM": "maccor_txt_three",
"S4000-UBHAM": "maccor_txt_one",
"S4000-KIT": "maccor_txt_two",
"S4000-WMG": "maccor_txt_three",
}


Expand Down Expand Up @@ -424,7 +419,74 @@ def _check_loader_from_outside_with_get2():
c.save(outfile)


def _fix_bugs_now():
import pathlib

import cellpy
import pandas as pd
import matplotlib.pyplot as plt

DATADIR = r"C:\scripting\cellpy_dev_resources\dev_data\simba_Maccor\S4000"
FILENAME = "01_UBham_CD_M50_Validation_0deg_01.txt"
INSTRUMENT = "maccor_txt"
MODEL = "ONE"

pd.options.display.max_columns = 100
datadir = pathlib.Path(DATADIR)
name = datadir / FILENAME
out = pathlib.Path(r"C:\scripting\trash")
print(f"File exists? {name.is_file()}")
if not name.is_file():
print(f"could not find {name} ")
return

c = cellpy.get(
filename=name,
instrument=INSTRUMENT,
model=MODEL,
mass=1.0,
auto_summary=False,
# auto_formatter=True, # does not work for UBHAM (finds only two comment rows, but should be three)
# post_processors={"another_param": False}, # This parameter will be available in the post-processors
)
print(f"loaded the file - now lets see what we got")
raw = c.data.raw
raw.to_clipboard()
print(raw.head())
c.make_step_table()
steps = c.data.steps
summary = c.data.summary

raw.to_csv(out / "raw.csv", sep=";")
steps.to_csv(out / "steps.csv", sep=";")
summary.to_csv(out / "summary.csv", sep=";")

fig_1, (ax1, ax2, ax3, ax4) = plt.subplots(
4,
1,
figsize=(6, 10),
constrained_layout=True,
sharex=True,
)
raw.plot(x="test_time", y="voltage", ax=ax1, xlabel="")
raw.plot(x="test_time", y="current", ax=ax2, xlabel="")
raw.plot(
x="test_time", y=["charge_capacity", "discharge_capacity"], ax=ax3, xlabel=""
)
raw.plot(x="test_time", y="cycle_index", ax=ax4)
fig_1.suptitle(f"{name.name}", fontsize=16)

n = c.get_number_of_cycles()
print(f"Number of cycles: {n}")

plt.legend()
plt.show()

outfile = out / "test_out"
c.save(outfile)


if __name__ == "__main__":
# check_dev_loader2(model="two")
# check_loader(number=2, model="two")
_check_loader_from_outside_with_get()
_fix_bugs_now()

0 comments on commit 99f8069

Please sign in to comment.