-
Notifications
You must be signed in to change notification settings - Fork 0
/
datareaders.py
185 lines (159 loc) · 5.95 KB
/
datareaders.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import csv
import warnings
from dataclasses import dataclass
import pandas as pd
import numpy as np
from fitter import PulseDataset
@dataclass
class Headers:
command: str
time: str
current: str
voltage: str
charging: str
discharging: str
resting: str
BasytecHeaders = Headers(
"Command", "~Time[s]", "I[A]", "U[V]", "Charge", "Discharge", "Pause"
)
NewareHeaders = Headers(
"Step Type", "Total Time", "Current(A)", "Voltage(V)", "CC Chg", "CC DChg", "Rest"
)
def _get_basytec_header_line_number(filename):
with open(filename, "r", encoding="utf8", errors="ignore") as f:
csv_reader = csv.reader(f)
for i, row in enumerate(csv_reader):
if row[0][0] != "~":
return max(i - 1, 0)
return 0
def import_data(filename: str, cycler: str) -> pd.DataFrame:
if cycler == "neware":
return import_neware(filename)
if cycler == "basytec":
return import_basytec(filename)
raise ValueError(
"Cycler must be basytec or neware, but received {cycler}"
)
def import_basytec(filename: str) -> pd.DataFrame:
header_line = _get_basytec_header_line_number(filename)
return pd.read_csv( ############## TODO HAVE ENCODING OPTION (tabs / comma separators)
filename, header=header_line, sep="\s+", encoding="unicode_escape"
)
def import_neware(filename: str) -> pd.DataFrame:
try:
df = pd.read_csv(filename, encoding_errors="ignore")
except pd.errors.ParserError:
df = pd.read_excel(filename, sheet_name="record")
seconds = (
df[NewareHeaders.time]
.str.split(":")
.apply(lambda x: int(x[0]) * 3600 + int(x[1]) * 60 + int(x[2]))
)
df[NewareHeaders.time] = seconds.to_numpy()
return df
def get_pulse_data(
df: pd.DataFrame,
socs: np.ndarray,
headers: Headers,
direction: str,
ignore_rests: bool = False,
skip_initial_points: int = 0,
) -> list[PulseDataset]:
if direction not in ["charge", "discharge", "switch"]:
raise ValueError(
f"direction must be charge, discharge, or switch; received {direction}"
)
active_command = (
headers.charging if direction == "charge" else headers.discharging
)
wrong_command = (
headers.discharging if direction == "charge" else headers.charging
)
warnings.warn("This pulse-getter may miss out the last pulse in a GITT")
end_of_rests = df[
df[headers.command].eq(headers.resting)
& df.shift(-1)[headers.command].eq(active_command)
]
ret = []
for start, end in zip(end_of_rests.index, end_of_rests.index[1:]):
pulse_df = df.iloc[start:end]
if any(pulse_df[headers.command].eq(wrong_command)):
if direction != "switch":
continue
if not any(pulse_df[headers.command].eq(active_command)):
continue
elif direction == "switch":
continue
if ignore_rests:
pulse_df = pulse_df[
pulse_df[headers.command].ne(headers.resting)
]
soclist = socs[pulse_df.index]
unique_times = np.r_[True, np.diff(pulse_df[headers.time]) != 0]
if not all(unique_times):
warnings.warn(
f"Skipping double-counted time-sample \n{pulse_df[np.logical_not(unique_times)]}"
)
pulse_df = pulse_df[unique_times]
soclist = soclist[unique_times]
ts = pulse_df[headers.time].to_numpy()[skip_initial_points:]
dataset = PulseDataset(
ts - ts[0],
pulse_df[headers.voltage].to_numpy()[skip_initial_points:],
soclist[skip_initial_points:],
-pulse_df[headers.current].to_numpy()[skip_initial_points:],
)
ret.append(dataset)
return ret
def get_ocvs_from_df(
df: pd.DataFrame,
socs: np.ndarray,
headers: Headers,
direction: str,
) -> tuple[np.ndarray, np.ndarray]:
active_command = (
headers.charging if direction == "charge" else headers.discharging
)
end_of_rests = df[
df[headers.command].eq(headers.resting)
& df.shift(-1)[headers.command].eq(active_command)
]
vs = end_of_rests[headers.voltage].to_numpy()
warnings.warn("This OCV-getter may miss out the last pulse in a GITT")
return socs[end_of_rests.index], vs
def get_ocvs_from_pulsedataset_list(
pulses: list[PulseDataset],
method: str = "rested",
) -> tuple[np.ndarray, np.ndarray]:
if method not in ["rested", "final"]:
raise ValueError("Method must be rested or final")
socs, vs = np.zeros(len(pulses) + 1), np.zeros(len(pulses) + 1)
socs[0] = pulses[0].socs[0]
vs[0] = pulses[0].vs[0]
for i, pulse in enumerate(pulses):
is_resting = pulse.currents == 0
if method == "rested":
# Find the longest continuous set of rests, and take the
# most rested result as OCV
# https://stackoverflow.com/questions/56301970/how-can-i-return-the-longest-continuous-occurrence-of-true-in-boolean-and-rep
bools = np.r_[False, is_resting, False]
# Get indices of group shifts
shiftpoints = np.flatnonzero(bools[:-1] != bools[1:])
# Get group lengths and hence the max index group
groupsizes = (shiftpoints[1::2] - shiftpoints[::2]).argmax()
# Initialize array and assign only the largest True island as True.
out = np.zeros_like(is_resting)
out[
shiftpoints[2 * groupsizes] : shiftpoints[2 * groupsizes + 1]
] = 1
# Get last datapoint from longest continuous rest
rest_soc = pulse.socs[out][-1]
rest_v = pulse.vs[out][-1]
elif method == "final":
# Take last datapoint
rest_soc = pulse.socs[is_resting][-1]
rest_v = pulse.vs[is_resting][-1]
socs[i+1] = rest_soc
vs[i+1] = rest_v
ordering = np.argsort(socs)
return socs[ordering], vs[ordering]