From c8009c29418942d936c8e1960d1b57bb6b56d2ab Mon Sep 17 00:00:00 2001 From: ZoeLi0525 <1558079116@qq.com> Date: Wed, 21 Aug 2024 11:47:44 +0200 Subject: [PATCH] make the filter plot interactive --- docs/notebooks/path_and_velocity.ipynb | 58 +++++++++++--------------- 1 file changed, 25 insertions(+), 33 deletions(-) diff --git a/docs/notebooks/path_and_velocity.ipynb b/docs/notebooks/path_and_velocity.ipynb index 27f8ee6..ae1318c 100644 --- a/docs/notebooks/path_and_velocity.ipynb +++ b/docs/notebooks/path_and_velocity.ipynb @@ -18,6 +18,7 @@ "import matplotlib.pyplot as plt\n", "import numpy as np\n", "import vstt\n", + "from ipywidgets import interact\n", "from scipy.signal import savgol_filter\n", "from vstt.stats import get_velocity" ] @@ -291,7 +292,7 @@ "metadata": {}, "source": [ "### apply a Savitzky-Golay filter example\n", - "Here is an example for illustrating how to apply a Savitzky-Golay filter to the velocity" + "Here is an example for illustrating how to apply a [Savitzky-Golay filter](https://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.savgol_filter.html) to the velocity" ] }, { @@ -300,25 +301,28 @@ "metadata": {}, "outputs": [], "source": [ - "# The length of the filter window (i.e., the number of coefficients). If mode is ‘interp’, window_length must be less than or equal to the size of x.\n", - "window_length = 40\n", + "def plot_filter(window_length, polyorder):\n", + " \"\"\"\n", + " plot the original function and filtered function\n", + " :param window_length: The length of the filter window (i.e., the number of coefficients). If mode is ‘interp’, window_length must be less than or equal to the size of x.\n", + " :param polyorder: The order of the polynomial used to fit the samples. polyorder must be less than window_length.\n", + " \"\"\"\n", + " for _, group in stats.groupby([\"i_trial\", \"i_rep\", \"condition_index\"]):\n", + " for positions, timestamps in zip(\n", + " group.to_target_mouse_positions,\n", + " group.to_target_timestamps,\n", + " ):\n", + " velocity = get_velocity(timestamps, positions)\n", + " plt.plot(timestamps[:-1], velocity, linestyle=\"dashed\")\n", + " filtered_velocity = savgol_filter(velocity, window_length, polyorder)\n", + " plt.plot(timestamps[:-1], filtered_velocity)\n", + " break\n", + " break\n", + " plt.legend([\"original velocity\", \"filtered velocity\"], loc=\"upper left\")\n", + " plt.show()\n", "\n", - "# The order of the polynomial used to fit the samples. polyorder must be less than window_length.\n", - "polyorder = 8\n", "\n", - "for _, group in stats.groupby([\"i_trial\", \"i_rep\", \"condition_index\"]):\n", - " for positions, timestamps in zip(\n", - " group.to_target_mouse_positions,\n", - " group.to_target_timestamps,\n", - " ):\n", - " velocity = get_velocity(timestamps, positions)\n", - " plt.plot(timestamps[:-1], velocity, linestyle=\"dashed\")\n", - " filtered_velocity = savgol_filter(velocity, window_length, polyorder)\n", - " plt.plot(timestamps[:-1], filtered_velocity)\n", - " break\n", - " break\n", - "plt.legend([\"original velocity\", \"filtered velocity\"], loc=\"upper left\")\n", - "plt.show()" + "interact(plot_filter, window_length=30, polyorder=8);" ] }, { @@ -378,11 +382,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "jupyter": { - "is_executing": true - } - }, + "metadata": {}, "outputs": [], "source": [ "fig, axs = plt.subplots(nTrials, nReps * 8, figsize=(6 * 8, 6 * nTrials * nReps))\n", @@ -439,11 +439,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "jupyter": { - "is_executing": true - } - }, + "metadata": {}, "outputs": [], "source": [ "fig, axs = plt.subplots(nTrials, nReps * 8, figsize=(6 * 8, 6 * nTrials * nReps))\n", @@ -485,11 +481,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "jupyter": { - "is_executing": true - } - }, + "metadata": {}, "outputs": [], "source": [] }