Skip to content

Commit

Permalink
make the filter plot interactive
Browse files Browse the repository at this point in the history
  • Loading branch information
ZoeLi0525 committed Aug 21, 2024
1 parent 2316e4d commit c8009c2
Showing 1 changed file with 25 additions and 33 deletions.
58 changes: 25 additions & 33 deletions docs/notebooks/path_and_velocity.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
]
Expand Down Expand Up @@ -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"
]
},
{
Expand All @@ -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);"
]
},
{
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -485,11 +481,7 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"jupyter": {
"is_executing": true
}
},
"metadata": {},
"outputs": [],
"source": []
}
Expand Down

0 comments on commit c8009c2

Please sign in to comment.