Skip to content

Add Multiple Model Framework#1257

Open
jswright-dstl wants to merge 29 commits intomainfrom
f3
Open

Add Multiple Model Framework#1257
jswright-dstl wants to merge 29 commits intomainfrom
f3

Conversation

@jswright-dstl
Copy link
Copy Markdown
Contributor

This PR adds the components for the Multiple model Framework and provides a tutorial introducing the KF, GPB1, GPB2 and IMM implemented through the Multiple Model Framework.

@jswright-dstl jswright-dstl requested a review from a team as a code owner February 24, 2026 14:25
@codecov
Copy link
Copy Markdown

codecov Bot commented Mar 31, 2026

Codecov Report

❌ Patch coverage is 91.94313% with 17 lines in your changes missing coverage. Please review.
✅ Project coverage is 94.45%. Comparing base (8f9cfb3) to head (812e78d).

Files with missing lines Patch % Lines
stonesoup/reducer/base.py 70.45% 11 Missing and 2 partials ⚠️
stonesoup/reducer/model.py 88.23% 3 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1257      +/-   ##
==========================================
+ Coverage   91.92%   94.45%   +2.53%     
==========================================
  Files         233      245      +12     
  Lines       16843    17053     +210     
  Branches     2372     2397      +25     
==========================================
+ Hits        15483    16108     +625     
+ Misses       1006      636     -370     
+ Partials      354      309      -45     
Flag Coverage Δ
integration 66.92% <89.09%> (?)
unittests 91.84% <89.57%> (-0.06%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Copy Markdown
Member

@sdhiscocks sdhiscocks left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @jswright-dstl

Doc strings are required for new classes.

It would be useful to have a brief explanation/summary for each algorithm in the tutorial, and possibly a metric for performance comparison.

Comment on lines +10 to +13
class Augmentor(Base):
transition_probabilities: TransitionMatrix = Property(doc="TPM")
transition_models: Sequence[TransitionModel] = Property(doc="List of transition models")
histories: int = Property(doc="Depth of history to be stored")
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could do with complete doc strings

from ..augmentor.base import Augmentor


class IdentityAugmentor(Augmentor):
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

doc string required

Comment thread stonesoup/reducer/base.py
Comment on lines +29 to +33
try:
(isinstance(states[0][0], WeightedGaussianState))
except TypeError or IndexError:
print(len(states))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Legacy debug?

Suggested change
try:
(isinstance(states[0][0], WeightedGaussianState))
except TypeError or IndexError:
print(len(states))

Comment thread stonesoup/reducer/base.py
Comment on lines +42 to +55
if isinstance(states[0], ExpandedModelAugmentedWeightedGaussianState):
pass
else:
Likelihood_j = []
for i in range(len(states)):
Likelihood_j.append(mvn.pdf(
states[i].hypothesis.measurement.state_vector.T,
states[i].hypothesis.measurement_prediction.mean.ravel(),
states[i].hypothesis.measurement_prediction.covar))
c_j = self.transition_probabilities[states[0]].T @ states.weights
weights = Likelihood_j * c_j.ravel()
weights = weights / np.sum(weights)
for i in range(len(states)):
states[i].weight = weights[i]
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if isinstance(states[0], ExpandedModelAugmentedWeightedGaussianState):
pass
else:
Likelihood_j = []
for i in range(len(states)):
Likelihood_j.append(mvn.pdf(
states[i].hypothesis.measurement.state_vector.T,
states[i].hypothesis.measurement_prediction.mean.ravel(),
states[i].hypothesis.measurement_prediction.covar))
c_j = self.transition_probabilities[states[0]].T @ states.weights
weights = Likelihood_j * c_j.ravel()
weights = weights / np.sum(weights)
for i in range(len(states)):
states[i].weight = weights[i]
if not isinstance(states[0], ExpandedModelAugmentedWeightedGaussianState):
Likelihood_j = []
for i in range(len(states)):
Likelihood_j.append(mvn.pdf(
states[i].hypothesis.measurement.state_vector.T,
states[i].hypothesis.measurement_prediction.mean.ravel(),
states[i].hypothesis.measurement_prediction.covar))
c_j = self.transition_probabilities[states[0]].T @ states.weights
weights = Likelihood_j * c_j.ravel()
weights = weights / np.sum(weights)
for i in range(len(states)):
states[i].weight = weights[i]

Comment thread stonesoup/reducer/base.py
Comment on lines +56 to +58
else:
pass

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
else:
pass

Comment thread stonesoup/reducer/base.py
Comment on lines +24 to +26
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

elif len(self.transition_probabilities[states[0]].ravel())**2 == len(states.weights):
m_ij_weights = (
self.transition_probabilities.transition_matrices[1].ravel() *
(states.weights/np.sum(states.weights)))*np.random.rand(len(states.weights))
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be good to be able to seed random for this, like models, etc..

Comment on lines +31 to +32
self.transition_probabilities[states[0]] @
(states.weights/np.sum(states.weights)))
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
self.transition_probabilities[states[0]] @
(states.weights/np.sum(states.weights)))
self.transition_probabilities[states[0]]
@ (states.weights/np.sum(states.weights)))

Comment thread stonesoup/types/matrix.py
from ..base import Base, Property


class TransitionMatrix(Base):
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inherit from Type?

Comment on lines +34 to +36
from stonesoup.models.transition.linear import (CombinedLinearGaussianTransitionModel as CLGTM,
ConstantVelocity as CV,
KnownTurnRate as CT)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are re-imported a couple lines down

Comment on lines +66 to +67
segment_lengths = [150, 200, 40, 165, 45]
segment_lengths = [int(x) for x in segment_lengths]
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
segment_lengths = [150, 200, 40, 165, 45]
segment_lengths = [int(x) for x in segment_lengths]
segment_lengths = [150, 200, 40, 165, 45]

The second line will return the same list right?

Comment thread stonesoup/reducer/base.py
print(len(states))

if isinstance(states[0], list):
if isinstance(states[0][0], WeightedGaussianState):
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what happens if this if is false? is it correct that states will remain as the original list

Comment thread stonesoup/reducer/base.py
Comment on lines +46 to +50
for i in range(len(states)):
Likelihood_j.append(mvn.pdf(
states[i].hypothesis.measurement.state_vector.T,
states[i].hypothesis.measurement_prediction.mean.ravel(),
states[i].hypothesis.measurement_prediction.covar))
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
for i in range(len(states)):
Likelihood_j.append(mvn.pdf(
states[i].hypothesis.measurement.state_vector.T,
states[i].hypothesis.measurement_prediction.mean.ravel(),
states[i].hypothesis.measurement_prediction.covar))
for state in states:
Likelihood_j.append(mvn.pdf(
state.hypothesis.measurement.state_vector.T,
state.hypothesis.measurement_prediction.mean.ravel(),
state.hypothesis.measurement_prediction.covar))

Comment thread stonesoup/types/matrix.py Outdated
Comment on lines +44 to +45
if str(history_length) not in [x for x in self.transition_matrices.keys()]:
history_length = max([x for x in self.transition_matrices.keys()]) + 1
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if str(history_length) not in [x for x in self.transition_matrices.keys()]:
history_length = max([x for x in self.transition_matrices.keys()]) + 1
if history_length not in self.transition_matrices.keys():
history_length = max(self.transition_matrices.keys()) + 1

Comment thread stonesoup/types/state.py
Comment on lines +1276 to +1279
if self.model_histories is None:
self.model_histories = []
if self.existence is None:
self.existence = Probability(1)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can move this into the "default factory" way

Comment on lines +129 to +132
gtplotter = Plotter()
ms = [m for _, m in all_measurements]
gtplotter.plot_ground_truths(truths, [0, 2], linewidth=2)
gtplotter.fig
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
gtplotter = Plotter()
ms = [m for _, m in all_measurements]
gtplotter.plot_ground_truths(truths, [0, 2], linewidth=2)
gtplotter.fig
plotter = Plotter()
ms = [m for _, m in all_measurements]
plotter.plot_ground_truths(truths, [0, 2], linewidth=2)
plotter.fig

Makes sense to have the truth on the same plot as the tracks

measurement_reducer, priors, all_measurements)

# %%
plotter = Plotter()
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
plotter = Plotter()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Availability of the "Unified Multiple Model Framework" (IMM/GPB) described in recent Dstl paper

3 participants