Skip to content

Commit 81d8448

Browse files
authored
Merge pull request #77 from nasa/feature/particle_filter_fix
fixing bug: array and keys did not match in particle_filter
2 parents ff234f0 + f7b01e3 commit 81d8448

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

src/progpy/state_estimators/particle_filter.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,8 @@ def __init__(self, model, x0, **kwargs):
6767
# Added to avoid float/int issues
6868
self.parameters['num_particles'] = int(self.parameters['num_particles'])
6969
sample_gen = x0.sample(self.parameters['num_particles'])
70-
samples = [array(sample_gen.key(k), dtype=float64) for k in x0.keys()]
71-
72-
self.particles = model.StateContainer(array(samples, dtype=float64))
70+
samples = {k: array(sample_gen.key(k), dtype=float64) for k in x0.keys()}
71+
self.particles = model.StateContainer(samples)
7372

7473
if 'R' in self.parameters:
7574
# For backwards compatibility

tests/test_state_estimators.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
sys.path.append(join(dirname(__file__), ".."))
88

99
from progpy import PrognosticsModel, LinearModel
10-
from progpy.models import ThrownObject, BatteryElectroChem, PneumaticValveBase
10+
from progpy.models import ThrownObject, BatteryElectroChem, PneumaticValveBase, BatteryElectroChemEOD
1111
from progpy.state_estimators import ParticleFilter, KalmanFilter, UnscentedKalmanFilter
1212
from progpy.uncertain_data import ScalarData, MultivariateNormalDist, UnweightedSamples
1313

@@ -561,6 +561,20 @@ def future_loading(t, x=None):
561561
for t, u, z in zip(times, inputs.data, outputs.data):
562562
kf.estimate(t, u, z)
563563

564+
def test_PF_particle_ordering(self):
565+
"""
566+
This is testing for a bug found by @mstraut where particle filter was mixing up the keys if users:
567+
1. Do not call m.initialize(), and instead
568+
2. provide a state as a dictionary instead of a state container, and
569+
3. order the states in a different order than m.states
570+
"""
571+
m = BatteryElectroChemEOD()
572+
x0 = m.parameters['x0'] # state as a dictionary with the wrong order
573+
filt = ParticleFilter(m, x0, num_particles=2)
574+
for key in m.states:
575+
self.assertEqual(filt.particles[key][0], x0[key])
576+
self.assertEqual(filt.particles[key][1], x0[key])
577+
564578
# This allows the module to be executed directly
565579
def main():
566580
# This ensures that the directory containing StateEstimatorTemplate is in the python search directory

0 commit comments

Comments
 (0)