Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a RandomState parameter to the clutter model #587

Merged
merged 6 commits into from
Jan 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions stonesoup/models/clutter/clutter.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# -*- coding: utf-8 -*-

import numpy as np
from typing import Set, Union, Callable, Tuple
from scipy.stats import poisson
from typing import Set, Union, Callable, Tuple, Optional
from abc import ABC

from ..base import Model
Expand Down Expand Up @@ -42,6 +43,21 @@ class ClutterModel(Model, ABC):
"The default defines the space for a uniform distribution in 2D. The call "
"`np.array([self.distribution(*arg) for arg in self.dist_params])` "
"must return a numpy array of length equal to the number of dimensions.")
seed: Optional[Union[int, np.random.RandomState]] = Property(
default=None,
doc="Seed or state for random number generation. If defined as an integer, "
"it will be used to create a numpy RandomState. Or it can be defined directly "
"as a RandomState (useful if you want to pass one of the random state's "
"functions as the :attr:`distribution`).")

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
if isinstance(self.seed, int):
self.random_state = np.random.RandomState(self.seed)
elif isinstance(self.seed, np.random.RandomState):
self.random_state = self.seed
else:
self.random_state = None

def function(self, ground_truths: Set[GroundTruthState], **kwargs) -> Set[Clutter]:
"""
Expand All @@ -68,7 +84,7 @@ def function(self, ground_truths: Set[GroundTruthState], **kwargs) -> Set[Clutte

# Generate the clutter for this time step
clutter = set()
for _ in range(np.random.poisson(self.clutter_rate)):
for _ in range(poisson.rvs(self.clutter_rate, random_state=self.random_state)):
# Call the distribution function to generate a random vector in the space
random_vector = np.array([self.distribution(*arg) for arg in self.dist_params])

Expand Down
25 changes: 21 additions & 4 deletions stonesoup/sensor/radar/tests/test_radar.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import datetime

import numpy as np
from scipy.stats import poisson
import pytest
from pytest import approx

Expand Down Expand Up @@ -664,10 +665,26 @@ def test_target_rcs():
)
def test_clutter_model(radar, clutter_params):
# Test that the radar correctly adds clutter when it has a clutter
# model. Make clutter rate sufficiently high that there is clutter
model_test = ClutterModel(clutter_rate=5,
distribution=np.random.default_rng().uniform,
dist_params=clutter_params)
# model.

# The amount of clutter generated by the model is determined by a
# Poisson distribution. To ensure it produces at least one false alarm,
# we use a RandomState
clutter_rate = 5
seed = 1
random_state_test = np.random.RandomState(seed=seed)
p_test = poisson.rvs(clutter_rate, random_state=random_state_test)
while p_test <= 0:
seed += 1
random_state_test = np.random.RandomState(seed=seed)
p_test = random_state_test.poisson(clutter_rate)
# Now we know this seed will give us a poisson value >= 1
random_state = np.random.RandomState(seed=seed)

model_test = ClutterModel(clutter_rate=clutter_rate,
distribution=random_state.uniform,
dist_params=clutter_params,
seed=random_state)
radar.clutter_model = model_test
truth = State(StateVector([1, 1, 1, 1, 1, 1]), timestamp=datetime.datetime.now())
measurements = radar.measure({truth})
Expand Down