From ec0ebffc807f152a937136de484bef1e8f931704 Mon Sep 17 00:00:00 2001 From: CBroz1 Date: Tue, 19 Dec 2023 10:40:11 -0600 Subject: [PATCH] Update notebooks --- CHANGELOG.md | 10 ++--- notebooks/24_Linearization.ipynb | 53 ++++++++++++------------ notebooks/py_scripts/24_Linearization.py | 33 +++++++++++---- 3 files changed, 56 insertions(+), 40 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3917b247b..7961b5a52 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,17 +9,17 @@ - Add Mixin class to centralize `fetch_nwb` functionality. #692 - Refactor restriction use in `delete_downstream_merge` #703 - Add `cautious_delete` to Mixin class, initial implementation. #711 -- Add `deprecation_factory` to facilitate table migration +- Add `deprecation_factory` to facilitate table migration. #717 ### Pipelines - Position: Refactor input validation in DLC pipeline. #688 -- Spike sorting: Add SpikeSorting V1 pipeline #651 -- LFP: Minor fixes to LFPBandV1 populator #706 +- Spike sorting: Add SpikeSorting V1 pipeline. #651 +- LFP: Minor fixes to LFPBandV1 populator. #706 - Linearization: - Minor fixes to LinearizedPositionV1 pipeline #695 - - Rename `position_linearization` -> `linearization` - - Migrate linearization tables: `common_position` -> `linearization.v0` + - Rename `position_linearization` -> `linearization`. #717 + - Migrate tables: `common_position` -> `linearization.v0`. #717 ## [0.4.3] (November 7, 2023) diff --git a/notebooks/24_Linearization.ipynb b/notebooks/24_Linearization.ipynb index cbd1d8d59..4b6e44bda 100644 --- a/notebooks/24_Linearization.ipynb +++ b/notebooks/24_Linearization.ipynb @@ -4,14 +4,14 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "# Position - Linearization" + "# Position - Linearization\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "## Overview" + "## Overview\n" ] }, { @@ -33,7 +33,7 @@ "\"linearizes\" it to 1D position. If you haven't already done so, please generate\n", "input data with either the [Trodes](./20_Position_Trodes.ipynb) or DLC notebooks\n", "([1](./21_Position_DLC_1.ipynb), [2](./22_Position_DLC_2.ipynb),\n", - "[3](./23_Position_DLC_3.ipynb))." + "[3](./23_Position_DLC_3.ipynb)).\n" ] }, { @@ -81,7 +81,7 @@ "\n", "import spyglass.common as sgc\n", "import spyglass.position.v1 as sgp\n", - "import spyglass.position_linearization.v1 as sgpl\n", + "import spyglass.linearization.v1 as sgpl\n", "\n", "# ignore datajoint+jupyter async warnings\n", "import warnings\n", @@ -94,7 +94,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "## Retrieve 2D position " + "## Retrieve 2D position\n" ] }, { @@ -102,7 +102,7 @@ "metadata": {}, "source": [ "To retrieve 2D position data, we'll specify an nwb file, a position time\n", - "interval, and the set of parameters used to compute the position info." + "interval, and the set of parameters used to compute the position info.\n" ] }, { @@ -133,7 +133,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "We will fetch the pandas dataframe from the `PositionOutput` table." + "We will fetch the pandas dataframe from the `PositionOutput` table.\n" ] }, { @@ -354,7 +354,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Before linearizing, plotting the head position will help us understand the data." + "Before linearizing, plotting the head position will help us understand the data.\n" ] }, { @@ -411,11 +411,11 @@ "\n", "- `node_positions` (cm): the 2D positions of the graph\n", "- `edges`: how the nodes are connected, as pairs of node indices, labeled by\n", - " their respective index in `node_positions`. \n", - "- `linear_edge_order`: layout of edges in linear space in *order*, as tuples. \n", + " their respective index in `node_positions`.\n", + "- `linear_edge_order`: layout of edges in linear space in _order_, as tuples.\n", "- `linear_edge_spacing`: spacing between each edge, as either a single number\n", - "for all gaps or an array with a number for each gap. Gaps may be important for\n", - "edges not connected in 2D space.\n", + " for all gaps or an array with a number for each gap. Gaps may be important for\n", + " edges not connected in 2D space.\n", "\n", "For example, (79.910, 216.720) is the 2D position of node 0 and (183.784,\n", "45.375) is the 2D position of node 8. Edge (0, 8) means there is an edge between\n", @@ -423,8 +423,8 @@ "from node 0 to 1. Edge (1, 0) would connect from node 1 to 0, reversing the\n", "linear positions for that edge.\n", "\n", - "For more examples, see \n", - "[this notebook](https://github.com/LorenFrankLab/track_linearization/blob/master/notebooks/)." + "For more examples, see\n", + "[this notebook](https://github.com/LorenFrankLab/track_linearization/blob/master/notebooks/).\n" ] }, { @@ -481,7 +481,7 @@ "metadata": {}, "source": [ "With these variables, we then add a `track_graph_name` and the corresponding\n", - "`environment`." + "`environment`.\n" ] }, { @@ -611,7 +611,7 @@ "metadata": {}, "source": [ "`TrackGraph` has several methods for visualizing in 2D and 1D space.\n", - "`plot_track_graph` plots in 2D to make sure our layout makes sense." + "`plot_track_graph` plots in 2D to make sure our layout makes sense.\n" ] }, { @@ -648,7 +648,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "`plot_track_graph_as_1D` shows what this looks like in 1D." + "`plot_track_graph_as_1D` shows what this looks like in 1D.\n" ] }, { @@ -683,7 +683,6 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "\n", "By default, linearization assigns each 2D position to its nearest point on the\n", "track graph. This is then translated into 1D space.\n", "\n", @@ -691,7 +690,7 @@ "The HMM takes into account the prior position and edge, and can keep the\n", "position from suddenly jumping to another. Position jumping like this may occur\n", "at intersections or the head position swings closer to a non-target reward well\n", - "while on another edge." + "while on another edge.\n" ] }, { @@ -806,15 +805,15 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "## Linearization" + "## Linearization\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "With linearization parameters, we specify the position interval we wish to \n", - "linearize from the `PositionOutput` table and create an entry in `LinearizationSelection`" + "With linearization parameters, we specify the position interval we wish to\n", + "linearize from the `PositionOutput` table and create an entry in `LinearizationSelection`\n" ] }, { @@ -1051,7 +1050,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "And then run linearization by populating `LinearizedPositionV1`." + "And then run linearization by populating `LinearizedPositionV1`.\n" ] }, { @@ -1225,7 +1224,7 @@ "- `time`: dataframe index\n", "- `linear_position`: 1D linearized position\n", "- `track_segment_id`: index number of the edges given to track graph\n", - "- `projected_{x,y}_position`: 2D position projected to the track graph" + "- `projected_{x,y}_position`: 2D position projected to the track graph\n" ] }, { @@ -1394,7 +1393,7 @@ " \"linearization_param_name\": \"default\",\n", "}\n", "\n", - "from spyglass.position_linearization import LinearizedPositionOutput\n", + "from spyglass.linearization.merge import LinearizedPositionOutput\n", "\n", "linear_position_df = (LinearizedPositionOutput & linear_key).fetch1_dataframe()\n", "linear_position_df" @@ -1405,7 +1404,7 @@ "metadata": {}, "source": [ "We'll plot the 1D position over time, colored by edge, and use the 1D track\n", - "graph layout on the y-axis." + "graph layout on the y-axis.\n" ] }, { @@ -1455,7 +1454,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "We can also plot the 2D position projected to the track graph" + "We can also plot the 2D position projected to the track graph\n" ] }, { diff --git a/notebooks/py_scripts/24_Linearization.py b/notebooks/py_scripts/24_Linearization.py index 772163a6f..25c0eb6bd 100644 --- a/notebooks/py_scripts/24_Linearization.py +++ b/notebooks/py_scripts/24_Linearization.py @@ -13,8 +13,10 @@ # --- # # Position - Linearization +# # ## Overview +# # _Developer Note:_ if you may make a PR in the future, be sure to copy this # notebook, and use the `gitignore` prefix `temp` to avoid future conflicts. @@ -32,6 +34,7 @@ # input data with either the [Trodes](./20_Position_Trodes.ipynb) or DLC notebooks # ([1](./21_Position_DLC_1.ipynb), [2](./22_Position_DLC_2.ipynb), # [3](./23_Position_DLC_3.ipynb)). +# # ## Imports # @@ -53,7 +56,7 @@ import spyglass.common as sgc import spyglass.position.v1 as sgp -import spyglass.position_linearization.v1 as sgpl +import spyglass.linearization.v1 as sgpl # ignore datajoint+jupyter async warnings import warnings @@ -63,17 +66,22 @@ # - # ## Retrieve 2D position +# # To retrieve 2D position data, we'll specify an nwb file, a position time # interval, and the set of parameters used to compute the position info. +# +# + from spyglass.utils.nwb_helper_fn import get_nwb_copy_filename nwb_file_name = "chimi20200216_new.nwb" nwb_copy_file_name = get_nwb_copy_filename(nwb_file_name) nwb_copy_file_name +# - # We will fetch the pandas dataframe from the `PositionOutput` table. +# # + from spyglass.position import PositionOutput @@ -92,6 +100,7 @@ # - # Before linearizing, plotting the head position will help us understand the data. +# fig, ax = plt.subplots(1, 1, figsize=(5, 5)) ax.plot( @@ -103,7 +112,6 @@ ax.set_ylabel("y-position [cm]", fontsize=18) ax.set_title("Head Position", fontsize=28) - # ## Specifying the track # @@ -113,10 +121,10 @@ # - `node_positions` (cm): the 2D positions of the graph # - `edges`: how the nodes are connected, as pairs of node indices, labeled by # their respective index in `node_positions`. -# - `linear_edge_order`: layout of edges in linear space in *order*, as tuples. +# - `linear_edge_order`: layout of edges in linear space in _order_, as tuples. # - `linear_edge_spacing`: spacing between each edge, as either a single number -# for all gaps or an array with a number for each gap. Gaps may be important for -# edges not connected in 2D space. +# for all gaps or an array with a number for each gap. Gaps may be important for +# edges not connected in 2D space. # # For example, (79.910, 216.720) is the 2D position of node 0 and (183.784, # 45.375) is the 2D position of node 8. Edge (0, 8) means there is an edge between @@ -126,6 +134,7 @@ # # For more examples, see # [this notebook](https://github.com/LorenFrankLab/track_linearization/blob/master/notebooks/). +# # + node_positions = np.array( @@ -173,6 +182,7 @@ # With these variables, we then add a `track_graph_name` and the corresponding # `environment`. +# # + sgpl.TrackGraph.insert1( @@ -193,6 +203,7 @@ # `TrackGraph` has several methods for visualizing in 2D and 1D space. # `plot_track_graph` plots in 2D to make sure our layout makes sense. +# fig, ax = plt.subplots(1, 1, figsize=(5, 5)) ax.plot( @@ -207,6 +218,7 @@ graph.plot_track_graph(ax=ax) # `plot_track_graph_as_1D` shows what this looks like in 1D. +# fig, ax = plt.subplots(1, 1, figsize=(10, 1)) graph.plot_track_graph_as_1D(ax=ax) @@ -214,7 +226,6 @@ # ## Parameters # -# # By default, linearization assigns each 2D position to its nearest point on the # track graph. This is then translated into 1D space. # @@ -223,6 +234,7 @@ # position from suddenly jumping to another. Position jumping like this may occur # at intersections or the head position swings closer to a non-target reward well # while on another edge. +# sgpl.LinearizationParameters.insert1( {"linearization_param_name": "default"}, skip_duplicates=True @@ -230,9 +242,11 @@ sgpl.LinearizationParameters() # ## Linearization +# # With linearization parameters, we specify the position interval we wish to # linearize from the `PositionOutput` table and create an entry in `LinearizationSelection` +# sgc.Session & {"nwb_file_name": nwb_copy_file_name} @@ -250,6 +264,7 @@ # - # And then run linearization by populating `LinearizedPositionV1`. +# sgpl.LinearizedPositionV1().populate() sgpl.LinearizedPositionV1() @@ -265,6 +280,7 @@ # - `linear_position`: 1D linearized position # - `track_segment_id`: index number of the edges given to track graph # - `projected_{x,y}_position`: 2D position projected to the track graph +# # + linear_key = { @@ -273,15 +289,15 @@ "linearization_param_name": "default", } -from spyglass.position_linearization import LinearizedPositionOutput +from spyglass.linearization.merge import LinearizedPositionOutput linear_position_df = (LinearizedPositionOutput & linear_key).fetch1_dataframe() linear_position_df - # - # We'll plot the 1D position over time, colored by edge, and use the 1D track # graph layout on the y-axis. +# # + fig, ax = plt.subplots(figsize=(20, 13)) @@ -301,6 +317,7 @@ # - # We can also plot the 2D position projected to the track graph +# fig, ax = plt.subplots(1, 1, figsize=(10, 10)) ax.plot(