Skip to content

Commit

Permalink
Merge pull request #737 from dstl/tutorial_sets
Browse files Browse the repository at this point in the history
Maintain order of truths in tutorials for reproducibility
  • Loading branch information
sdhiscocks committed Oct 17, 2022
2 parents cc07548 + 077d11d commit b8984aa
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 76 deletions.
5 changes: 3 additions & 2 deletions docs/tutorials/06_DataAssociation-MultiTargetTutorial.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,11 @@
# %%
# Generate ground truth
# ^^^^^^^^^^^^^^^^^^^^^
from ordered_set import OrderedSet

np.random.seed(1991)

truths = set()
truths = OrderedSet()

transition_model = CombinedLinearGaussianTransitionModel([ConstantVelocity(0.005),
ConstantVelocity(0.005)])
Expand All @@ -93,7 +94,7 @@
truth.append(GroundTruthState(
transition_model.function(truth[k-1], noise=True, time_interval=timedelta(seconds=1)),
timestamp=start_time+timedelta(seconds=k)))
truths.add(truth)
_ = truths.add(truth)

# %%
# Plot the ground truth
Expand Down
3 changes: 2 additions & 1 deletion docs/tutorials/08_JPDATutorial.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@

from datetime import datetime
from datetime import timedelta
from ordered_set import OrderedSet
import numpy as np
from scipy.stats import uniform

Expand All @@ -77,7 +78,7 @@

np.random.seed(1991)

truths = set()
truths = OrderedSet()

start_time = datetime.now()
transition_model = CombinedLinearGaussianTransitionModel([ConstantVelocity(0.005),
Expand Down
3 changes: 2 additions & 1 deletion docs/tutorials/09_Initiators_&_Deleters.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from datetime import timedelta

import numpy as np
from ordered_set import OrderedSet

from stonesoup.models.transition.linear import CombinedLinearGaussianTransitionModel, \
ConstantVelocity
Expand All @@ -28,7 +29,7 @@
np.random.seed(1991)

start_time = datetime.now()
truths = set() # Truths across all time
truths = OrderedSet() # Truths across all time
current_truths = set() # Truths alive at current time

transition_model = CombinedLinearGaussianTransitionModel([ConstantVelocity(0.005),
Expand Down
3 changes: 2 additions & 1 deletion docs/tutorials/filters/GMPHDTutorial.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@
plt.style.use('seaborn-colorblind')
# Other general imports
import numpy as np
from ordered_set import OrderedSet
from datetime import datetime, timedelta
start_time = datetime.now()

Expand All @@ -129,7 +130,7 @@

from stonesoup.types.groundtruth import GroundTruthPath, GroundTruthState
start_time = datetime.now()
truths = set() # Truths across all time
truths = OrderedSet() # Truths across all time
current_truths = set() # Truths alive at current time
start_truths = set()
number_steps = 20
Expand Down
38 changes: 14 additions & 24 deletions docs/tutorials/sensormanagement/01_SingleSensorManagement.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@

import numpy as np
import random
from ordered_set import OrderedSet
from datetime import datetime, timedelta

start_time = datetime.now()
Expand Down Expand Up @@ -128,7 +129,7 @@
ConstantVelocity(0.005)])

yps = range(0, 100, 10) # y value for prior state
truths = []
truths = OrderedSet()
ntruths = 3 # number of ground truths in simulation
time_max = 50 # timestamps the simulation is observed over

Expand All @@ -144,7 +145,7 @@
truth.append(
GroundTruthState(transition_model.function(truth[k - 1], noise=True, time_interval=timedelta(seconds=1)),
timestamp=start_time + timedelta(seconds=k)))
truths.append(truth)
truths.add(truth)

# alternate directions when initiating tracks
xdirection *= -1
Expand All @@ -156,11 +157,8 @@

from stonesoup.plotter import Plotterly

# Stonesoup plotter requires sets not lists
truths_set = set(truths)

plotter = Plotterly()
plotter.plot_ground_truths(truths_set, [0, 2])
plotter.plot_ground_truths(truths, [0, 2])
plotter.fig

# %%
Expand Down Expand Up @@ -249,14 +247,10 @@
from stonesoup.types.track import Track

# Initialise tracks from the RandomSensorManager
tracksA = []
for j, prior in enumerate(priors):
tracksA.append(Track([prior]))
tracksA = {Track([prior]) for prior in priors}

# Initialise tracks from the BruteForceSensorManager
tracksB = []
for j, prior in enumerate(priors):
tracksB.append(Track([prior]))
tracksB = {Track([prior]) for prior in priors}

# %%
# Create sensor managers
Expand Down Expand Up @@ -345,8 +339,6 @@
# Here the chosen target for observation is selected randomly using the method :meth:`choose_actions()` from the class
# :class:`~.RandomSensorManager`.

from ordered_set import OrderedSet

# Generate list of timesteps from ground truth timestamps
timesteps = []
for state in truths[0]:
Expand All @@ -359,7 +351,7 @@
chosen_actions = randomsensormanager.choose_actions(tracksA, timestep)

# Create empty dictionary for measurements
measurementsA = []
measurementsA = set()

for chosen_action in chosen_actions:
for sensor, actions in chosen_action.items():
Expand All @@ -369,8 +361,7 @@

# Observe this ground truth
# i.e. {z}k
measurements = sensorA.measure(OrderedSet(truth[timestep] for truth in truths), noise=True)
measurementsA.extend(measurements)
measurementsA |= sensorA.measure(OrderedSet(truth[timestep] for truth in truths), noise=True)

hypotheses = data_associator.associate(tracksA,
measurementsA,
Expand All @@ -388,8 +379,8 @@

plotterA = Plotterly()
plotterA.plot_sensors(sensorA)
plotterA.plot_ground_truths(truths_set, [0, 2])
plotterA.plot_tracks(set(tracksA), [0, 2], uncertainty=True)
plotterA.plot_ground_truths(truths, [0, 2])
plotterA.plot_tracks(tracksA, [0, 2], uncertainty=True)
plotterA.fig

# %%
Expand Down Expand Up @@ -425,7 +416,7 @@
chosen_actions = bruteforcesensormanager.choose_actions(tracksB, timestep)

# Create empty dictionary for measurements
measurementsB = []
measurementsB = set()

for chosen_action in chosen_actions:
for sensor, actions in chosen_action.items():
Expand All @@ -435,8 +426,7 @@

# Observe this ground truth
# i.e. {z}k
measurements = sensorB.measure(OrderedSet(truth[timestep] for truth in truths), noise=True)
measurementsB.extend(measurements)
measurementsB |= sensorB.measure(OrderedSet(truth[timestep] for truth in truths), noise=True)

hypotheses = data_associator.associate(tracksB,
measurementsB,
Expand All @@ -453,8 +443,8 @@

plotterB = Plotterly()
plotterB.plot_sensors(sensorB)
plotterB.plot_ground_truths(truths_set, [0, 2])
plotterB.plot_tracks(set(tracksB), [0, 2], uncertainty=True)
plotterB.plot_ground_truths(truths, [0, 2])
plotterB.plot_tracks(tracksB, [0, 2], uncertainty=True)
plotterB.fig

# %%
Expand Down
34 changes: 14 additions & 20 deletions docs/tutorials/sensormanagement/02_MultiSensorManagement.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@

import numpy as np
import random
from ordered_set import OrderedSet
from datetime import datetime, timedelta

start_time = datetime.now()
Expand Down Expand Up @@ -79,7 +80,7 @@
ConstantVelocity(0.005)])

yps = range(0, 100, 10) # y value for prior state
truths = []
truths = OrderedSet()
ntruths = 3 # number of ground truths in simulation
time_max = 50 # timestamps the simulation is observed over

Expand All @@ -95,7 +96,7 @@
truth.append(
GroundTruthState(transition_model.function(truth[k - 1], noise=True, time_interval=timedelta(seconds=1)),
timestamp=start_time + timedelta(seconds=k)))
truths.append(truth)
truths.add(truth)

xdirection *= -1
if j % 2 == 0:
Expand All @@ -107,10 +108,9 @@
from stonesoup.plotter import Plotterly

# Stonesoup plotter requires sets not lists
truths_set = set(truths)

plotter = Plotterly()
plotter.plot_ground_truths(truths_set, [0, 2])
plotter.plot_ground_truths(truths, [0, 2])
plotter.fig

# %%
Expand Down Expand Up @@ -211,14 +211,10 @@
from stonesoup.types.track import Track

# Initialise tracks from the RandomSensorManager
tracksA = []
for j, prior in enumerate(priors):
tracksA.append(Track([prior]))
tracksA = {Track([prior]) for prior in priors}

# Initialise tracks from the BruteForceSensorManager
tracksB = []
for j, prior in enumerate(priors):
tracksB.append(Track([prior]))
tracksB = {Track([prior]) for prior in priors}

# %%
# Create sensor managers
Expand Down Expand Up @@ -322,7 +318,7 @@
chosen_actions = randomsensormanager.choose_actions(tracksA, timestep)

# Create empty dictionary for measurements
measurementsA = []
measurementsA = set()

for chosen_action in chosen_actions:
for sensor, actions in chosen_action.items():
Expand All @@ -332,8 +328,7 @@
sensor.act(timestep)

# Observe this ground truth
measurements = sensor.measure(OrderedSet(truth[timestep] for truth in truths), noise=True)
measurementsA.extend(measurements)
measurementsA |= sensor.measure(OrderedSet(truth[timestep] for truth in truths), noise=True)

hypotheses = data_associator.associate(tracksA,
measurementsA,
Expand All @@ -352,8 +347,8 @@

plotterA = Plotterly()
plotterA.plot_sensors(sensor_setA)
plotterA.plot_ground_truths(truths_set, [0, 2])
plotterA.plot_tracks(set(tracksA), [0, 2], uncertainty=True)
plotterA.plot_ground_truths(truths, [0, 2])
plotterA.plot_tracks(tracksA, [0, 2], uncertainty=True)
plotterA.fig

# %%
Expand Down Expand Up @@ -393,7 +388,7 @@
chosen_actions = bruteforcesensormanager.choose_actions(tracksB, timestep)

# Create empty dictionary for measurements
measurementsB = []
measurementsB = set()

for chosen_action in chosen_actions:
for sensor, actions in chosen_action.items():
Expand All @@ -403,8 +398,7 @@
sensor.act(timestep)

# Observe this ground truth
measurements = sensor.measure(OrderedSet(truth[timestep] for truth in truths), noise=True)
measurementsB.extend(measurements)
measurementsB |= sensor.measure(OrderedSet(truth[timestep] for truth in truths), noise=True)

hypotheses = data_associator.associate(tracksB,
measurementsB,
Expand All @@ -422,8 +416,8 @@

plotterB = Plotterly()
plotterB.plot_sensors(sensor_setB)
plotterB.plot_ground_truths(truths_set, [0, 2])
plotterB.plot_tracks(set(tracksB), [0, 2], uncertainty=True)
plotterB.plot_ground_truths(truths, [0, 2])
plotterB.plot_tracks(tracksB, [0, 2], uncertainty=True)
plotterB.fig

# %%
Expand Down
Loading

0 comments on commit b8984aa

Please sign in to comment.