forked from pybamm-team/PyBaMM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.py
347 lines (291 loc) · 10.7 KB
/
util.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
#
# Utility classes for PyBaMM
#
# The code in this file is adapted from Pints
# (see https://github.com/pints-team/pints)
#
import argparse
import importlib.util
import numbers
import os
import pathlib
import pickle
import subprocess
import sys
import timeit
from platform import system
import difflib
from warnings import warn
import numpy as np
import importlib.metadata
import pybamm
# versions of jax and jaxlib compatible with PyBaMM
JAX_VERSION = "0.4"
JAXLIB_VERSION = "0.4"
def root_dir():
"""return the root directory of the PyBaMM install directory"""
return str(pathlib.Path(pybamm.__path__[0]).parent)
def get_git_commit_info():
"""
Get the git commit info for the current PyBaMM version, e.g. v22.8-39-gb25ce8c41
(version 22.8, commit b25ce8c41)
"""
try:
# Get the latest git commit hash
return str(
subprocess.check_output(["git", "describe", "--tags"], cwd=root_dir())
.strip()
.decode()
)
except subprocess.CalledProcessError: # pragma: no cover
# Not a git repository so just return the version number
return f"v{pybamm.__version__}"
class FuzzyDict(dict):
def get_best_matches(self, key):
"""Get best matches from keys"""
return difflib.get_close_matches(key, list(self.keys()), n=3, cutoff=0.5)
def __getitem__(self, key):
try:
return super().__getitem__(key)
except KeyError:
if key in ["Negative electrode SOC", "Positive electrode SOC"]:
domain = key.split(" ")[0]
raise KeyError(
f"Variable '{domain} electrode SOC' has been renamed to "
f"'{domain} electrode stoichiometry' to avoid confusion "
"with cell SOC"
)
if "Measured open circuit voltage" in key:
raise KeyError(
"The variable that used to be called "
"'Measured open circuit voltage [V]' is now called "
"'Surface open-circuit voltage [V]'. There is also another "
"variable called 'Bulk open-circuit voltage [V]' which is the"
"open-circuit voltage evaluated at the average particle "
"concentrations."
)
if "Open-circuit voltage at 0% SOC [V]" in key:
raise KeyError(
"Parameter 'Open-circuit voltage at 0% SOC [V]' not found."
"In most cases this should be set to be equal to "
"'Lower voltage cut-off [V]'"
)
if "Open-circuit voltage at 100% SOC [V]" in key:
raise KeyError(
"Parameter 'Open-circuit voltage at 100% SOC [V]' not found."
"In most cases this should be set to be equal to "
"'Upper voltage cut-off [V]'"
)
best_matches = self.get_best_matches(key)
for k in best_matches:
if key in k and k.endswith("]"):
raise KeyError(
f"'{key}' not found. Use the dimensional version '{k}' instead."
)
raise KeyError(f"'{key}' not found. Best matches are {best_matches}")
def search(self, key, print_values=False):
"""
Search dictionary for keys containing 'key'. If print_values is True, then
both the keys and values will be printed. Otherwise just the values will
be printed. If no results are found, the best matches are printed.
"""
key_in = key
key = key_in.lower()
# Sort the keys so results are stored in alphabetical order
keys = list(self.keys())
keys.sort()
results = {}
# Check if any of the dict keys contain the key we are searching for
for k in keys:
if key in k.lower():
results[k] = self[k]
if results == {}:
# If no results, return best matches
best_matches = self.get_best_matches(key)
print(
f"No results for search using '{key_in}'. "
f"Best matches are {best_matches}"
)
elif print_values:
# Else print results, including dict items
print("\n".join("{}\t{}".format(k, v) for k, v in results.items()))
else:
# Just print keys
print("\n".join("{}".format(k) for k in results.keys()))
def copy(self):
return FuzzyDict(super().copy())
class Timer(object):
"""
Provides accurate timing.
Example
-------
timer = pybamm.Timer()
print(timer.time())
"""
def __init__(self):
self._start = timeit.default_timer()
def reset(self):
"""
Resets this timer's start time.
"""
self._start = timeit.default_timer()
def time(self):
"""
Returns the time (float, in seconds) since this timer was created,
or since meth:`reset()` was last called.
"""
return TimerTime(timeit.default_timer() - self._start)
class TimerTime:
def __init__(self, value):
"""A string whose value prints in human-readable form"""
self.value = value
def __str__(self):
"""
Formats a (non-integer) number of seconds, returns a string like
"5 weeks, 3 days, 1 hour, 4 minutes, 9 seconds", or "0.0019 seconds".
"""
time = self.value
if time < 1e-6:
return "{:.3f} ns".format(time * 1e9)
if time < 1e-3:
return "{:.3f} us".format(time * 1e6)
if time < 1:
return "{:.3f} ms".format(time * 1e3)
elif time < 60:
return "{:.3f} s".format(time)
output = []
time = int(round(time))
units = [(604800, "week"), (86400, "day"), (3600, "hour"), (60, "minute")]
for k, name in units:
f = time // k
if f > 0 or output:
output.append(str(f) + " " + (name if f == 1 else name + "s"))
time -= f * k
output.append("1 second" if time == 1 else str(time) + " seconds")
return ", ".join(output)
def __repr__(self):
return f"pybamm.TimerTime({self.value})"
def __add__(self, other):
if isinstance(other, numbers.Number):
return TimerTime(self.value + other)
else:
return TimerTime(self.value + other.value)
def __radd__(self, other):
return self.__add__(other)
def __sub__(self, other):
if isinstance(other, numbers.Number):
return TimerTime(self.value - other)
else:
return TimerTime(self.value - other.value)
def __rsub__(self, other):
if isinstance(other, numbers.Number):
return TimerTime(other - self.value)
def __mul__(self, other):
if isinstance(other, numbers.Number):
return TimerTime(self.value * other)
else:
return TimerTime(self.value * other.value)
def __rmul__(self, other):
return self.__mul__(other)
def __truediv__(self, other):
if isinstance(other, numbers.Number):
return TimerTime(self.value / other)
else:
return TimerTime(self.value / other.value)
def __rtruediv__(self, other):
if isinstance(other, numbers.Number):
return TimerTime(other / self.value)
def __eq__(self, other):
return self.value == other.value
def rmse(x, y):
"""
Calculate the root-mean-square-error between two vectors x and y, ignoring NaNs
"""
# Check lengths
if len(x) != len(y):
raise ValueError("Vectors must have the same length")
return np.sqrt(np.nanmean((x - y) ** 2))
def load(filename):
"""Load a saved object"""
with open(filename, "rb") as f:
obj = pickle.load(f)
return obj
def get_parameters_filepath(path):
"""Returns path if it exists in current working dir,
otherwise get it from package dir"""
if os.path.exists(path):
return path
else:
return os.path.join(pybamm.__path__[0], path)
def have_jax():
"""Check if jax and jaxlib are installed with the correct versions"""
return (
(importlib.util.find_spec("jax") is not None)
and (importlib.util.find_spec("jaxlib") is not None)
and is_jax_compatible()
)
def is_jax_compatible():
"""Check if the available version of jax and jaxlib are compatible with PyBaMM"""
return (
importlib.metadata.distribution("jax").version.startswith(JAX_VERSION)
and importlib.metadata.distribution("jaxlib").version.startswith(JAXLIB_VERSION)
)
def is_constant_and_can_evaluate(symbol):
"""
Returns True if symbol is constant and evaluation does not raise any errors.
Returns False otherwise.
An example of a constant symbol that cannot be "evaluated" is PrimaryBroadcast(0).
"""
if symbol.is_constant():
try:
symbol.evaluate()
return True
except NotImplementedError:
return False
else:
return False
def install_jax(arguments=None): # pragma: no cover
"""
Install compatible versions of jax, jaxlib.
Command Line Interface::
$ pybamm_install_jax
| optional arguments:
| -h, --help show help message
| -f, --force force install compatible versions of jax and jaxlib
"""
parser = argparse.ArgumentParser(description="Install jax and jaxlib")
parser.add_argument(
"-f",
"--force",
action="store_true",
help="force install compatible versions of"
f" jax ({JAX_VERSION}) and jaxlib ({JAXLIB_VERSION})",
)
args = parser.parse_args(arguments)
if system() == "Windows":
raise NotImplementedError("Jax is not available on Windows")
# Raise an error if jax and jaxlib are already installed, but incompatible
# and --force is not set
elif importlib.util.find_spec("jax") is not None:
if not args.force and not is_jax_compatible():
raise ValueError(
"Jax is already installed but the installed version of jax or jaxlib is"
" not supported by PyBaMM. \nYou can force install compatible versions"
f" of jax ({JAX_VERSION}) and jaxlib ({JAXLIB_VERSION}) using the"
" following command: \npybamm_install_jax --force"
)
msg = (
"pybamm_install_jax is deprecated,"
" use 'pip install pybamm[jax]' to install jax & jaxlib"
)
warn(msg, DeprecationWarning)
subprocess.check_call(
[
sys.executable,
"-m",
"pip",
"install",
f"jax>={JAX_VERSION}",
f"jaxlib>={JAXLIB_VERSION}",
]
)