.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/tracking/plotOnlineFilterFWGMouseTrajectory.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. or to run this example in your browser via Binder .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_tracking_plotOnlineFilterFWGMouseTrajectory.py: Online filtering of a foraging mouse trajectory =============================================== The code below performs online Kalman filtering of a trajectory of the mouse shown on the left image below, as it forages in the arena shown on the right image below. Click on the images to see their larger versions. .. image:: /_static/mouseOnWheel.png :width: 300 :alt: image of mouse on wheel .. image:: /_static/foragingMouse.png :width: 300 :alt: image of foraging mouse .. GENERATED FROM PYTHON SOURCE LINES 20-22 Import packages ~~~~~~~~~~~~~~~ .. GENERATED FROM PYTHON SOURCE LINES 22-35 .. code-block:: Python import sys import os import random import pickle import configparser import numpy as np import pandas as pd import plotly.graph_objects as go import lds.tracking.utils import lds.inference .. GENERATED FROM PYTHON SOURCE LINES 36-38 Setup configuration variables ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ .. GENERATED FROM PYTHON SOURCE LINES 38-46 .. code-block:: Python start_position = 0 number_positions = 10000 color_measures = "black" color_pattern_filtered = "rgba(255,0,0,{:f})" cb_alpha = 0.3 data_filename = "http://www.gatsby.ucl.ac.uk/~rapela/svGPFA/data/positions_session003_start0.00_end15548.27.csv" .. GENERATED FROM PYTHON SOURCE LINES 47-49 Get the mouse position measurements ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ .. GENERATED FROM PYTHON SOURCE LINES 49-54 .. code-block:: Python data = pd.read_csv(data_filename) data = data.iloc[start_position:start_position+number_positions,:] y = np.transpose(data[["x", "y"]].to_numpy()) .. GENERATED FROM PYTHON SOURCE LINES 55-57 Get the Kalman filter configuration parameters ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ .. GENERATED FROM PYTHON SOURCE LINES 57-69 .. code-block:: Python pos_x0 = y[0, 0] pos_y0 = y[0, 0] vel_x0 = 0.0 vel_y0 = 0.0 acc_x0 = 0.0 acc_y0 = 0.0 sigma_a = 1e4 sigma_x = 1e2 sigma_y = 1e2 sqrt_diag_V0_value = 1e-3 .. GENERATED FROM PYTHON SOURCE LINES 70-72 Build the Kalman filter matrices for tracking ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ .. GENERATED FROM PYTHON SOURCE LINES 72-80 .. code-block:: Python date_times = pd.to_datetime(data["time"]) dt = (date_times.iloc[1]-date_times.iloc[0]).total_seconds() B, Q, Z, R, Qe = lds.tracking.utils.getLDSmatricesForTracking( dt=dt, sigma_a=sigma_a, sigma_x=sigma_x, sigma_y=sigma_y) m0 = np.array([[y[0, 0], 0, 0, y[1, 0], 0, 0]], dtype=np.double).T V0 = np.diag(np.ones(len(m0))*sqrt_diag_V0_value**2) .. GENERATED FROM PYTHON SOURCE LINES 81-83 Apply the Kalman filter to the mouse position measurements ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ .. GENERATED FROM PYTHON SOURCE LINES 83-92 .. code-block:: Python onlineKF = lds.inference.OnlineKalmanFilter(B=B, Q=Q, m0=m0, V0=V0, Z=Z, R=R) filtered_means = np.empty((6, 1, y.shape[1]), dtype=np.double) filtered_covs = np.empty((6, 6, y.shape[1]), dtype=np.double) for i in range(y.shape[1]): _, _ = onlineKF.predict() filtered_means[:, :, i], filtered_covs[:, :, i] = \ onlineKF.update(y=y[:, i]) .. GENERATED FROM PYTHON SOURCE LINES 93-95 Plot the filtered positions with their 95% confidence bands ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ .. GENERATED FROM PYTHON SOURCE LINES 95-169 .. code-block:: Python time = (pd.to_datetime(data["time"]) - pd.to_datetime(data["time"][0])).dt.total_seconds().to_numpy() filter_mean_x = filtered_means[0, 0, :] filter_mean_y = filtered_means[3, 0, :] filter_std_x_y = np.sqrt(np.diagonal(a=filtered_covs, axis1=0, axis2=1)) filter_ci_x_upper = filter_mean_x + 1.96*filter_std_x_y[:, 0] filter_ci_x_lower = filter_mean_x - 1.96*filter_std_x_y[:, 0] filter_ci_y_upper = filter_mean_y + 1.96*filter_std_x_y[:, 3] filter_ci_y_lower = filter_mean_y - 1.96*filter_std_x_y[:, 3] fig = go.Figure() trace_mes_x = go.Scatter( x=time, y=y[0, :], mode="markers", marker={"color": color_measures}, name="measured x", showlegend=True, ) trace_mes_y = go.Scatter( x=time, y=y[1, :], mode="markers", marker={"color": color_measures}, name="measured y", showlegend=True, ) trace_filter_x = go.Scatter( x=time, y=filter_mean_x, mode="markers", marker={"color": color_pattern_filtered.format(1.0)}, name="filtered x", showlegend=True, legendgroup="filtered_x", ) trace_filter_x_cb = go.Scatter( x=np.concatenate([time, time[::-1]]), y=np.concatenate([filter_ci_x_upper, filter_ci_x_lower[::-1]]), fill="toself", fillcolor=color_pattern_filtered.format(cb_alpha), line=dict(color=color_pattern_filtered.format(0.0)), showlegend=False, legendgroup="filtered_x", ) trace_filter_y = go.Scatter( x=time, y=filter_mean_y, mode="markers", marker={"color": color_pattern_filtered.format(1.0)}, name="filtered y", showlegend=True, legendgroup="filtered_y", ) trace_filter_y_cb = go.Scatter( x=np.concatenate([time, time[::-1]]), y=np.concatenate([filter_ci_y_upper, filter_ci_y_lower[::-1]]), fill="toself", fillcolor=color_pattern_filtered.format(cb_alpha), line=dict(color=color_pattern_filtered.format(0.0)), showlegend=False, legendgroup="filtered_y", ) fig.add_trace(trace_mes_x) fig.add_trace(trace_mes_y) fig.add_trace(trace_filter_x) fig.add_trace(trace_filter_x_cb) fig.add_trace(trace_filter_y) fig.add_trace(trace_filter_y_cb) fig.update_layout(xaxis_title="time (seconds)", yaxis_title="position (pixels)", paper_bgcolor='rgba(0,0,0,0)', plot_bgcolor='rgba(0,0,0,0)', yaxis_range=[filter_mean_x.min(), filter_mean_x.max()]) fig .. raw:: html


.. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 5.972 seconds) .. _sphx_glr_download_auto_examples_tracking_plotOnlineFilterFWGMouseTrajectory.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: binder-badge .. image:: images/binder_badge_logo.svg :target: https://mybinder.org/v2/gh/joacorapela/lds_python/gh-pages?filepath=notebooks/auto_examples/tracking/plotOnlineFilterFWGMouseTrajectory.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plotOnlineFilterFWGMouseTrajectory.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plotOnlineFilterFWGMouseTrajectory.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plotOnlineFilterFWGMouseTrajectory.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_