pybatteryse 1.0.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pybatteryse-1.0.0/LICENSE.txt +29 -0
- pybatteryse-1.0.0/PKG-INFO +128 -0
- pybatteryse-1.0.0/README.md +106 -0
- pybatteryse-1.0.0/pybatteryse/__init__.py +1 -0
- pybatteryse-1.0.0/pybatteryse/coefficient.py +97 -0
- pybatteryse-1.0.0/pybatteryse/filters/__init__.py +3 -0
- pybatteryse-1.0.0/pybatteryse/filters/ekf.py +938 -0
- pybatteryse-1.0.0/pybatteryse/filters/pf.py +261 -0
- pybatteryse-1.0.0/pybatteryse/plotter.py +32 -0
- pybatteryse-1.0.0/pybatteryse/statespace.py +93 -0
- pybatteryse-1.0.0/pybatteryse.egg-info/PKG-INFO +128 -0
- pybatteryse-1.0.0/pybatteryse.egg-info/SOURCES.txt +16 -0
- pybatteryse-1.0.0/pybatteryse.egg-info/dependency_links.txt +1 -0
- pybatteryse-1.0.0/pybatteryse.egg-info/requires.txt +3 -0
- pybatteryse-1.0.0/pybatteryse.egg-info/top_level.txt +1 -0
- pybatteryse-1.0.0/setup.cfg +4 -0
- pybatteryse-1.0.0/setup.py +28 -0
- pybatteryse-1.0.0/tests/test_basic.py +7 -0
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
BSD 3-Clause License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026, A.M.A (Muiz) Sheikh
|
|
4
|
+
All rights reserved.
|
|
5
|
+
|
|
6
|
+
Redistribution and use in source and binary forms, with or without
|
|
7
|
+
modification, are permitted provided that the following conditions are met:
|
|
8
|
+
|
|
9
|
+
1. Redistributions of source code must retain the above copyright notice, this
|
|
10
|
+
list of conditions and the following disclaimer.
|
|
11
|
+
|
|
12
|
+
2. Redistributions in binary form must reproduce the above copyright notice,
|
|
13
|
+
this list of conditions and the following disclaimer in the documentation
|
|
14
|
+
and/or other materials provided with the distribution.
|
|
15
|
+
|
|
16
|
+
3. Neither the name of the copyright holder nor the names of its
|
|
17
|
+
contributors may be used to endorse or promote products derived from
|
|
18
|
+
this software without specific prior written permission.
|
|
19
|
+
|
|
20
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
21
|
+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
22
|
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
23
|
+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
24
|
+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
25
|
+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
26
|
+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
27
|
+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
28
|
+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
29
|
+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: pybatteryse
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Summary: Battery State Estimation in Python
|
|
5
|
+
Home-page: https://github.com/muizabdul29/PyBatterySE
|
|
6
|
+
Author: Muiz Sheikh
|
|
7
|
+
Classifier: Programming Language :: Python :: 3
|
|
8
|
+
Classifier: Operating System :: OS Independent
|
|
9
|
+
Description-Content-Type: text/markdown
|
|
10
|
+
License-File: LICENSE.txt
|
|
11
|
+
Requires-Dist: pybatteryid>=3.0.1
|
|
12
|
+
Requires-Dist: numpy>=2.1.0
|
|
13
|
+
Requires-Dist: tqdm>=4.67.3
|
|
14
|
+
Dynamic: author
|
|
15
|
+
Dynamic: classifier
|
|
16
|
+
Dynamic: description
|
|
17
|
+
Dynamic: description-content-type
|
|
18
|
+
Dynamic: home-page
|
|
19
|
+
Dynamic: license-file
|
|
20
|
+
Dynamic: requires-dist
|
|
21
|
+
Dynamic: summary
|
|
22
|
+
|
|
23
|
+
# PyBatterySE
|
|
24
|
+
|
|
25
|
+
<div>
|
|
26
|
+
|
|
27
|
+
[](https://github.com/muizabdul29/PyBatterySE/releases)
|
|
28
|
+
[](https://github.com/muizabdul29/PyBatterySE/actions/workflows/pylint.yml)
|
|
29
|
+
|
|
30
|
+
</div>
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
**PyBatterySE** — a shorthand for **Py**thon **Battery** **S**tate **E**stimation, is an open-source library for state estimation using Bayesian filters and linear parameter-varying (LPV) battery models.
|
|
34
|
+
|
|
35
|
+
## Installation
|
|
36
|
+
|
|
37
|
+
Use the package manager [pip](https://pip.pypa.io/en/stable/) to install PyBatterySE.
|
|
38
|
+
|
|
39
|
+
```bash
|
|
40
|
+
pip install pybatteryse
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## Requirements
|
|
44
|
+
|
|
45
|
+
PyBatterySE is a companion package to PyBatteryID, and utilises the models identified using PyBatteryID for state estimation. Whilst it is possible to use a custom model that follows the same format as PyBatteryID, it is recommended to use models identified using PyBatteryID for state estimation with PyBatterySE.
|
|
46
|
+
|
|
47
|
+
## Basic usage
|
|
48
|
+
|
|
49
|
+
In the following, an example usage of PyBatterySE has been demonstrated for performing state estimation of batteries, including SOC estimation, and ageing-aware parameter (and capacity) estimation.
|
|
50
|
+
|
|
51
|
+
#### 1. SOC estimation
|
|
52
|
+
|
|
53
|
+
For SOC estimation, we currently have two options, namely (i) extended Kalman filter (EKF), and (ii) particle filter (PF). In both cases, the state corresponding to overpotential model obtained using PyBatteryID is augmented with an extra SOC state, that is, $x = [s \ x_1 \ \cdots \ x_n]^\top$. Below, we give two basic example usages for SOC estimation using EKF and PF.
|
|
54
|
+
|
|
55
|
+
**Example 1: SOC estimation using EKF**
|
|
56
|
+
|
|
57
|
+
```python
|
|
58
|
+
from pybatteryid.utilities import load_model_from_file
|
|
59
|
+
from pybatteryse.filters.ekf import ExtendedKalmanFilter
|
|
60
|
+
|
|
61
|
+
model = load_model_from_file('path/to/model.npy')
|
|
62
|
+
dataset = helper.load_npy_datasets(f'path/to/dataset.npy')
|
|
63
|
+
bad_current_measurements = [ ... ] # Bad current measurements
|
|
64
|
+
|
|
65
|
+
# Initialize EKF
|
|
66
|
+
ekf = ExtendedKalmanFilter(
|
|
67
|
+
model=model,
|
|
68
|
+
sigma_nu=1e-3, # Input noise variance
|
|
69
|
+
sigma_ny_ne=1e-3 # Measurement noise variance
|
|
70
|
+
)
|
|
71
|
+
|
|
72
|
+
# Prepare initial conditions
|
|
73
|
+
x0 = np.array([[0.5], # Initial SOC
|
|
74
|
+
[0.0], # state 1
|
|
75
|
+
[0.0], # state 2
|
|
76
|
+
[0.0]]) # state 3
|
|
77
|
+
|
|
78
|
+
P0 = np.diag([1, 0.1, 0.1, 0.1]) # Initial covariance
|
|
79
|
+
|
|
80
|
+
# Run filter
|
|
81
|
+
state_estimates = ekf.run(
|
|
82
|
+
temperature_values=dataset['temperature_values'],
|
|
83
|
+
current_values=bad_current_measurements,
|
|
84
|
+
voltage_values=dataset['voltage_values'],
|
|
85
|
+
initial_state=x0,
|
|
86
|
+
initial_covariance=P0
|
|
87
|
+
)
|
|
88
|
+
|
|
89
|
+
# Extract SOC estimates
|
|
90
|
+
soc_estimates = state_estimates[:, 0]
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
**Example 2: SOC estimation using PF**
|
|
94
|
+
|
|
95
|
+
```python
|
|
96
|
+
from pybatteryid.utilities import load_model_from_file
|
|
97
|
+
from pybatteryse.filters.ekf import ParticleFilter
|
|
98
|
+
|
|
99
|
+
model = load_model_from_file('path/to/model.npy')
|
|
100
|
+
dataset = helper.load_npy_datasets(f'path/to/dataset.npy')
|
|
101
|
+
bad_current_measurements = [ ... ] # Bad current measurements
|
|
102
|
+
|
|
103
|
+
# Initialize PF
|
|
104
|
+
pf = ParticleFilter(
|
|
105
|
+
model,
|
|
106
|
+
num_particles=5,
|
|
107
|
+
eta_bounds=(-4, -1),
|
|
108
|
+
sigma_ny_ne=1e-3
|
|
109
|
+
)
|
|
110
|
+
|
|
111
|
+
initial_particles = np.hstack((
|
|
112
|
+
np.random.uniform(0.0001, 0.9999, size=(pf.num_particles, 1)),
|
|
113
|
+
np.zeros((pf.num_particles, model.model_order))
|
|
114
|
+
))
|
|
115
|
+
|
|
116
|
+
state_estimates = pf.run(initial_particles=initial_particles,
|
|
117
|
+
temperature_values=temperature_values,
|
|
118
|
+
current_values=bad_current_measurements,
|
|
119
|
+
voltage_values=voltage_values)
|
|
120
|
+
|
|
121
|
+
soc_estimates = state_estimates[:, 0]
|
|
122
|
+
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
#### 2. Ageing-aware model estimation
|
|
126
|
+
|
|
127
|
+
We can perform ageing-aware model estimation using recursive state estimation. The detailed methodology is explained in [X], which proposes alternative approaches as well. An example has been provided in the examples folder, which can be consulted for more details.
|
|
128
|
+
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
# PyBatterySE
|
|
2
|
+
|
|
3
|
+
<div>
|
|
4
|
+
|
|
5
|
+
[](https://github.com/muizabdul29/PyBatterySE/releases)
|
|
6
|
+
[](https://github.com/muizabdul29/PyBatterySE/actions/workflows/pylint.yml)
|
|
7
|
+
|
|
8
|
+
</div>
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
**PyBatterySE** — a shorthand for **Py**thon **Battery** **S**tate **E**stimation, is an open-source library for state estimation using Bayesian filters and linear parameter-varying (LPV) battery models.
|
|
12
|
+
|
|
13
|
+
## Installation
|
|
14
|
+
|
|
15
|
+
Use the package manager [pip](https://pip.pypa.io/en/stable/) to install PyBatterySE.
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
pip install pybatteryse
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## Requirements
|
|
22
|
+
|
|
23
|
+
PyBatterySE is a companion package to PyBatteryID, and utilises the models identified using PyBatteryID for state estimation. Whilst it is possible to use a custom model that follows the same format as PyBatteryID, it is recommended to use models identified using PyBatteryID for state estimation with PyBatterySE.
|
|
24
|
+
|
|
25
|
+
## Basic usage
|
|
26
|
+
|
|
27
|
+
In the following, an example usage of PyBatterySE has been demonstrated for performing state estimation of batteries, including SOC estimation, and ageing-aware parameter (and capacity) estimation.
|
|
28
|
+
|
|
29
|
+
#### 1. SOC estimation
|
|
30
|
+
|
|
31
|
+
For SOC estimation, we currently have two options, namely (i) extended Kalman filter (EKF), and (ii) particle filter (PF). In both cases, the state corresponding to overpotential model obtained using PyBatteryID is augmented with an extra SOC state, that is, $x = [s \ x_1 \ \cdots \ x_n]^\top$. Below, we give two basic example usages for SOC estimation using EKF and PF.
|
|
32
|
+
|
|
33
|
+
**Example 1: SOC estimation using EKF**
|
|
34
|
+
|
|
35
|
+
```python
|
|
36
|
+
from pybatteryid.utilities import load_model_from_file
|
|
37
|
+
from pybatteryse.filters.ekf import ExtendedKalmanFilter
|
|
38
|
+
|
|
39
|
+
model = load_model_from_file('path/to/model.npy')
|
|
40
|
+
dataset = helper.load_npy_datasets(f'path/to/dataset.npy')
|
|
41
|
+
bad_current_measurements = [ ... ] # Bad current measurements
|
|
42
|
+
|
|
43
|
+
# Initialize EKF
|
|
44
|
+
ekf = ExtendedKalmanFilter(
|
|
45
|
+
model=model,
|
|
46
|
+
sigma_nu=1e-3, # Input noise variance
|
|
47
|
+
sigma_ny_ne=1e-3 # Measurement noise variance
|
|
48
|
+
)
|
|
49
|
+
|
|
50
|
+
# Prepare initial conditions
|
|
51
|
+
x0 = np.array([[0.5], # Initial SOC
|
|
52
|
+
[0.0], # state 1
|
|
53
|
+
[0.0], # state 2
|
|
54
|
+
[0.0]]) # state 3
|
|
55
|
+
|
|
56
|
+
P0 = np.diag([1, 0.1, 0.1, 0.1]) # Initial covariance
|
|
57
|
+
|
|
58
|
+
# Run filter
|
|
59
|
+
state_estimates = ekf.run(
|
|
60
|
+
temperature_values=dataset['temperature_values'],
|
|
61
|
+
current_values=bad_current_measurements,
|
|
62
|
+
voltage_values=dataset['voltage_values'],
|
|
63
|
+
initial_state=x0,
|
|
64
|
+
initial_covariance=P0
|
|
65
|
+
)
|
|
66
|
+
|
|
67
|
+
# Extract SOC estimates
|
|
68
|
+
soc_estimates = state_estimates[:, 0]
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
**Example 2: SOC estimation using PF**
|
|
72
|
+
|
|
73
|
+
```python
|
|
74
|
+
from pybatteryid.utilities import load_model_from_file
|
|
75
|
+
from pybatteryse.filters.ekf import ParticleFilter
|
|
76
|
+
|
|
77
|
+
model = load_model_from_file('path/to/model.npy')
|
|
78
|
+
dataset = helper.load_npy_datasets(f'path/to/dataset.npy')
|
|
79
|
+
bad_current_measurements = [ ... ] # Bad current measurements
|
|
80
|
+
|
|
81
|
+
# Initialize PF
|
|
82
|
+
pf = ParticleFilter(
|
|
83
|
+
model,
|
|
84
|
+
num_particles=5,
|
|
85
|
+
eta_bounds=(-4, -1),
|
|
86
|
+
sigma_ny_ne=1e-3
|
|
87
|
+
)
|
|
88
|
+
|
|
89
|
+
initial_particles = np.hstack((
|
|
90
|
+
np.random.uniform(0.0001, 0.9999, size=(pf.num_particles, 1)),
|
|
91
|
+
np.zeros((pf.num_particles, model.model_order))
|
|
92
|
+
))
|
|
93
|
+
|
|
94
|
+
state_estimates = pf.run(initial_particles=initial_particles,
|
|
95
|
+
temperature_values=temperature_values,
|
|
96
|
+
current_values=bad_current_measurements,
|
|
97
|
+
voltage_values=voltage_values)
|
|
98
|
+
|
|
99
|
+
soc_estimates = state_estimates[:, 0]
|
|
100
|
+
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
#### 2. Ageing-aware model estimation
|
|
104
|
+
|
|
105
|
+
We can perform ageing-aware model estimation using recursive state estimation. The detailed methodology is explained in [X], which proposes alternative approaches as well. An example has been provided in the examples folder, which can be consulted for more details.
|
|
106
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"""Load"""
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
"""Utilities concerning model state-space representation."""
|
|
2
|
+
|
|
3
|
+
import re
|
|
4
|
+
from dataclasses import dataclass
|
|
5
|
+
|
|
6
|
+
import numpy as np
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
@dataclass
|
|
10
|
+
class CoefficientTerm:
|
|
11
|
+
"""
|
|
12
|
+
Represents a term in the model coefficient. Note that the
|
|
13
|
+
coefficient is made up of several terms, e.g.,
|
|
14
|
+
b_0(k) = 2 * s(k) × log[s](k) + 4 * T(k) × 1/s(k) is made up
|
|
15
|
+
of two terms.
|
|
16
|
+
"""
|
|
17
|
+
parameter: float
|
|
18
|
+
basis_function_strings: list[str]
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
Coefficients = dict[str, list[CoefficientTerm]]
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def extract_model_coefficients(model_terms, model_estimate, output_symbol='v', input_symbol='i'):
|
|
25
|
+
"""Extract p-dependent model coefficients."""
|
|
26
|
+
|
|
27
|
+
coefficients: dict[str, list[CoefficientTerm]] = {}
|
|
28
|
+
|
|
29
|
+
for term, parameter in zip(model_terms, model_estimate):
|
|
30
|
+
result = re.search(f'({output_symbol}|{input_symbol})\\(k(-(\\d+))?\\)', term)
|
|
31
|
+
if result is None:
|
|
32
|
+
raise ValueError('Invalid model terms.')
|
|
33
|
+
|
|
34
|
+
# Define coefficient
|
|
35
|
+
result_groups = result.groups()
|
|
36
|
+
coefficient_variable = 'a' if result_groups[0] == output_symbol else 'b'
|
|
37
|
+
coefficient_delay = result_groups[2] if result_groups[2] is not None else 0
|
|
38
|
+
|
|
39
|
+
# Remove input/output terms plus the time indices
|
|
40
|
+
coefficient_term = re.sub(f'({output_symbol}|{input_symbol})?\\(k(-(\\d+))?\\)', '', term)
|
|
41
|
+
# We split the term into a list of individual
|
|
42
|
+
# basis function strings
|
|
43
|
+
coefficient_term_bfs = coefficient_term.strip('×').split('×')
|
|
44
|
+
|
|
45
|
+
# Define coefficient key, e.g., b_0, b_1, ...
|
|
46
|
+
coefficient_key = f'{coefficient_variable}_{coefficient_delay}'
|
|
47
|
+
if coefficient_key not in coefficients:
|
|
48
|
+
coefficients[coefficient_key] = []
|
|
49
|
+
#
|
|
50
|
+
coefficient_parameter = -parameter if coefficient_variable == 'a' else parameter
|
|
51
|
+
coefficients[coefficient_key].append(CoefficientTerm(coefficient_parameter,
|
|
52
|
+
coefficient_term_bfs))
|
|
53
|
+
|
|
54
|
+
return coefficients
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def evaluate_coefficient(coefficient_terms: list[CoefficientTerm],
|
|
58
|
+
signal_trajectories: dict,
|
|
59
|
+
time_instant: int):
|
|
60
|
+
"""Evaluate a coefficient at a certain time instant."""
|
|
61
|
+
|
|
62
|
+
result = []
|
|
63
|
+
for coefficient_term in coefficient_terms:
|
|
64
|
+
#
|
|
65
|
+
term_value = coefficient_term.parameter
|
|
66
|
+
for bf_string in coefficient_term.basis_function_strings:
|
|
67
|
+
if bf_string == '':
|
|
68
|
+
continue
|
|
69
|
+
#
|
|
70
|
+
term_value *= signal_trajectories[f'{bf_string}(k)'][time_instant]
|
|
71
|
+
result.append(term_value)
|
|
72
|
+
|
|
73
|
+
return np.sum(result)
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
def update_model_parameters(coefficients: Coefficients, model_estimate: list[float]) -> None:
|
|
77
|
+
"""Update coefficient parameters in-place from a flat model_estimate vector.
|
|
78
|
+
|
|
79
|
+
Iterates coefficients.items() in insertion order (a_1, a_2, ..., b_0, b_1, ...)
|
|
80
|
+
which matches the canonical parameter order guaranteed by extract_model_coefficients.
|
|
81
|
+
a-term parameters are stored with negated sign (convention from extract_model_coefficients).
|
|
82
|
+
|
|
83
|
+
Raises:
|
|
84
|
+
ValueError: If len(model_estimate) does not match the total subterm count.
|
|
85
|
+
"""
|
|
86
|
+
all_keyed_subterms = [
|
|
87
|
+
(subterm, coefficient_key.startswith('a'))
|
|
88
|
+
for coefficient_key, subterms in coefficients.items()
|
|
89
|
+
for subterm in subterms
|
|
90
|
+
]
|
|
91
|
+
if len(model_estimate) != len(all_keyed_subterms):
|
|
92
|
+
raise ValueError(
|
|
93
|
+
f"Parameter count mismatch: expected {len(all_keyed_subterms)} parameters "
|
|
94
|
+
f"but got {len(model_estimate)}."
|
|
95
|
+
)
|
|
96
|
+
for (subterm, is_a_coefficient), new_parameter in zip(all_keyed_subterms, model_estimate):
|
|
97
|
+
subterm.parameter = -new_parameter if is_a_coefficient else new_parameter
|