diff --git a/notebooks/43_Decoding_SortedSpikes.ipynb b/notebooks/43_Decoding_SortedSpikes.ipynb index b779bfac1..1adbe3ba2 100644 --- a/notebooks/43_Decoding_SortedSpikes.ipynb +++ b/notebooks/43_Decoding_SortedSpikes.ipynb @@ -16,14 +16,12 @@ "- `decoding_interval`\n", "\n", "This time, instead of extracting waveform features, we can proceed directly from the SpikeSortingOutput table to specify which units we want to decode. The rest of the decoding process is the same as before.\n", - "\n", - "\n", - "## SortedSpikesGroup" + "\n" ] }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 9, "metadata": {}, "outputs": [], "source": [ @@ -35,20 +33,158 @@ ") # load config for database connection info" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## SortedSpikesGroup\n", + "\n", + "`SortedSpikesGroup` is a child table of `SpikeSortingOutput` in the spikesorting pipeline. It allows us to group the spikesorting results from multiple \n", + "sources (e.g. multiple terode groups or intervals) into a single entry. Here we will group together the spiking of multiple tetrode groups to use for decoding.\n", + "\n", + "\n", + "This table allows us filter units by their annotation labels from curation (e.g only include units labeled \"good\", exclude units labeld \"noise\") by defining parameters from `UnitSelectionParams`. When accessing data through `SortedSpikesGroup` the table will include only units with at least one label in `include_labels` and no labels in `exclude_labels`. We can look at those here:\n" + ] + }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ - "[2024-01-29 10:31:51,696][INFO]: Connecting root@localhost:3306\n", - "[2024-01-29 10:31:51,746][INFO]: Connected root@localhost:3306\n", - "[10:31:52][WARNING] Spyglass: Please update position_tools to >= 0.1.0\n" + "[2024-02-02 12:06:04,725][INFO]: Connecting sambray@lmf-db.cin.ucsf.edu:3306\n", + "[2024-02-02 12:06:04,762][INFO]: Connected sambray@lmf-db.cin.ucsf.edu:3306\n", + "[12:06:05][WARNING] Spyglass: Please update position_tools to >= 0.1.0\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'unit_filter_params_name': 'default_exclusion', 'include_labels': [], 'exclude_labels': ['noise', 'mua']}\n" ] }, + { + "data": { + "text/html": [ + "\n", + " \n", + " \n", + " \n", + " \n", + "
\n",
+ " unit_filter_params_name \n", + " \n", + " | \n",
+ " include_labels \n", + " \n", + " | \n",
+ " exclude_labels \n", + " \n", + " |
---|---|---|
all_units | \n", + "=BLOB= | \n", + "=BLOB= |
default_exclusion | \n", + "=BLOB= | \n", + "=BLOB= |
exclude_noise | \n", + "=BLOB= | \n", + "=BLOB= |
Total: 3
curation_id
\n", " \n", " \n", - "...
\n", - "Total: 23
\n", + " \n", + "Total: 3
\n", " " ], "text/plain": [ "*sorting_id *merge_id recording_id sorter sorter_param_n nwb_file_name interval_list_ curation_id \n", "+------------+ +------------+ +------------+ +------------+ +------------+ +------------+ +------------+ +------------+\n", - "08a302b6-5505- 485a4ddf-332d- 449b64e3-db0b- clusterless_th default_cluste mediumnwb20230 45f6b9a1-eef3- 0 \n", - "0ca508ee-af4c- 6acb99b8-6a0c- 328da21c-1d9c- clusterless_th default_cluste mediumnwb20230 686d9951-1c0f- 0 \n", - "209dc048-6fae- f7237e18-4e73- aff78f2f-2ba0- clusterless_th default_cluste mediumnwb20230 719e8a86-fcf1- 0 \n", - "21a9a593-f6f3- 7e3fa66e-727e- 2402805a-04f9- clusterless_th default_cluste mediumnwb20230 d581b117-160e- 0 \n", - "406a20e3-5a9f- 6d039a63-17ad- f1427e00-2974- clusterless_th default_cluste mediumnwb20230 0e848c38-9105- 0 \n", - "4131c51b-c56d- e0e9133a-7a4e- 9e332d82-1daf- clusterless_th default_cluste mediumnwb20230 9ed11db5-c42e- 0 \n", - "4c5a629a-71d9- 9959b614-2318- 3a2c3eed-413a- clusterless_th default_cluste mediumnwb20230 2b9fbf14-74a0- 0 \n", - "4d629c07-1931- c0eb6455-fc41- f07bc0b0-de6b- clusterless_th default_cluste mediumnwb20230 5c68f0f0-f577- 0 \n", - "554a9a3c-0461- 912e250e-56d8- 7f128981-6868- clusterless_th default_cluste mediumnwb20230 f4b9301f-bc91- 0 \n", - "7bb007f2-26d3- d7d2c97a-0e6e- a9b7cec0-1256- clusterless_th default_cluste mediumnwb20230 74270cba-36ee- 0 \n", - "80e1f37f-48a7- abb92dce-4410- 3c40ebdc-0b61- clusterless_th default_cluste mediumnwb20230 0f91197e-bebb- 0 \n", - "8848c4a8-a2f2- 74e10781-1228- 257c077b-8f3b- clusterless_th default_cluste mediumnwb20230 e289e03d-32ad- 0 \n", - " ...\n", - " (Total: 23)" + "642242ff-5f0e- 75286bf3-f876- 01c5b8e9-933d- mountainsort4 franklab_tetro mediumnwb20230 0a6611b3-c593- 1 \n", + "a4b5a94d-ba41- 143dff79-3779- a8a1d29d-ffdf- mountainsort4 franklab_tetro mediumnwb20230 3d782852-a56b- 1 \n", + "874775be-df0f- a900c1c8-909d- 747f4eea-6df3- mountainsort4 franklab_tetro mediumnwb20230 9cf9e3cd-7115- 1 \n", + " (Total: 3)" ] }, "execution_count": 2, @@ -250,67 +313,20 @@ "from spyglass.spikesorting.spikesorting_merge import SpikeSortingOutput\n", "import spyglass.spikesorting.v1 as sgs\n", "\n", - "\n", "nwb_copy_file_name = \"mediumnwb20230802_.nwb\"\n", "\n", "sorter_keys = {\n", " \"nwb_file_name\": nwb_copy_file_name,\n", - " \"sorter\": \"clusterless_thresholder\",\n", - " \"sorter_param_name\": \"default_clusterless\",\n", + " \"sorter\": \"mountainsort4\",\n", + " \"curation_id\": 1\n", "}\n", - "\n", + "# check the set of sorting's we'll use\n", "(sgs.SpikeSortingSelection & sorter_keys) * SpikeSortingOutput.CurationV1" ] }, { "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "array([UUID('485a4ddf-332d-35b5-3ad4-0561736c1844'),\n", - " UUID('6acb99b8-6a0c-eb83-1141-5f603c5895e0'),\n", - " UUID('f7237e18-4e73-4aee-805b-90735e9147de'),\n", - " UUID('7e3fa66e-727e-1541-819a-b01309bb30ae'),\n", - " UUID('6d039a63-17ad-0b78-4b1e-f02d5f3dbbc5'),\n", - " UUID('e0e9133a-7a4e-1321-a43a-e8afcb2f25da'),\n", - " UUID('9959b614-2318-f597-6651-a3a82124d28a'),\n", - " UUID('c0eb6455-fc41-c200-b62e-e3ca81b9a3f7'),\n", - " UUID('912e250e-56d8-ee33-4525-c844d810971b'),\n", - " UUID('d7d2c97a-0e6e-d1b8-735c-d55dc66a30e1'),\n", - " UUID('abb92dce-4410-8f17-a501-a4104bda0dcf'),\n", - " UUID('74e10781-1228-4075-0870-af224024ffdc'),\n", - " UUID('8bbddc0f-d6ae-6260-9400-f884a6e25ae8'),\n", - " UUID('614d796c-0b95-6364-aaa0-b6cb1e7bbb83'),\n", - " UUID('b332482b-e430-169d-8ac0-0a73ce968ed7'),\n", - " UUID('86897349-ff68-ac72-02eb-739dd88936e6'),\n", - " UUID('4a712103-c223-864f-82e0-6c23de79cc14'),\n", - " UUID('cf858380-e8a3-49de-c2a9-1a277e307a68'),\n", - " UUID('cc4ee561-f974-f8e5-0ea4-83185263ac67'),\n", - " UUID('4a72c253-b3ca-8c13-e615-736a7ebff35c'),\n", - " UUID('b92a94d8-ee1e-2097-a81f-5c1e1556ed24'),\n", - " UUID('5c53bd33-d57c-fbba-e0fb-55e0bcb85d03'),\n", - " UUID('0751a1e1-a406-7f87-ae6f-ce4ffc60621c')], dtype=object)" - ] - }, - "execution_count": 3, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "spikesorting_merge_ids = (\n", - " (sgs.SpikeSortingSelection & sorter_keys) * SpikeSortingOutput.CurationV1\n", - ").fetch(\"merge_id\")\n", - "\n", - "spikesorting_merge_ids" - ] - }, - { - "cell_type": "code", - "execution_count": 4, + "execution_count": 6, "metadata": {}, "outputs": [ { @@ -372,85 +388,44 @@ " \n",
- " merge_id \n", - " \n", + "nwb_file_name \n", + " name of the NWB file\n", " | \n",
- " spikesorting_merge_id \n", + "unit_filter_params_name \n", " \n", " | \n",
- " unit_group_name \n", - " Name of unit group\n", + "sorted_spikes_group_name \n", + " \n", " |
---|---|---|
39b22078-84fa-d3aa-8ca9-2368603d6a47 | \n", - "0751a1e1-a406-7f87-ae6f-ce4ffc60621c | \n", - "all units |
7bc69ef8-ae89-f30f-c236-56a87a49ed63 | \n", - "485a4ddf-332d-35b5-3ad4-0561736c1844 | \n", - "all units |
2f6295c6-ad14-dde0-6b14-ee29bc2ed849 | \n", - "4a712103-c223-864f-82e0-6c23de79cc14 | \n", - "all units |
7fa3c0ad-36a8-c14c-d84a-890c6002a457 | \n", - "4a72c253-b3ca-8c13-e615-736a7ebff35c | \n", - "all units |
4b025478-ab06-8e65-ef09-7e32be5a624c | \n", - "5c53bd33-d57c-fbba-e0fb-55e0bcb85d03 | \n", - "all units |
9658d944-1f1f-d467-1f56-bcc32d63e6e8 | \n", - "614d796c-0b95-6364-aaa0-b6cb1e7bbb83 | \n", - "all units |
76cb7574-a687-37c9-1045-8e4e5d13a0b0 | \n", - "6acb99b8-6a0c-eb83-1141-5f603c5895e0 | \n", - "all units |
87af74fa-a77e-f29b-99f7-f5720aa4816f | \n", - "6d039a63-17ad-0b78-4b1e-f02d5f3dbbc5 | \n", - "all units |
3cccfce5-dad9-61c5-7eee-1c7081837100 | \n", - "74e10781-1228-4075-0870-af224024ffdc | \n", - "all units |
bbddf25f-d810-f586-e3b8-cb96350fbbde | \n", - "7e3fa66e-727e-1541-819a-b01309bb30ae | \n", - "all units |
7887e5db-da1d-d261-d58a-a51757593965 | \n", - "86897349-ff68-ac72-02eb-739dd88936e6 | \n", - "all units |
1b7ba376-89f9-e0ac-9104-fbd1bd984e41 | \n", - "8bbddc0f-d6ae-6260-9400-f884a6e25ae8 | \n", - "all units |
mediumnwb20230802_.nwb | \n", + "all_units | \n", + "test_group |
...
\n", - "Total: 23
Total: 1
\n", " " ], "text/plain": [ - "*merge_id spikesorting_m unit_group_nam\n", + "*nwb_file_name *unit_filter_p *sorted_spikes\n", "+------------+ +------------+ +------------+\n", - "39b22078-84fa- 0751a1e1-a406- all units \n", - "7bc69ef8-ae89- 485a4ddf-332d- all units \n", - "2f6295c6-ad14- 4a712103-c223- all units \n", - "7fa3c0ad-36a8- 4a72c253-b3ca- all units \n", - "4b025478-ab06- 5c53bd33-d57c- all units \n", - "9658d944-1f1f- 614d796c-0b95- all units \n", - "76cb7574-a687- 6acb99b8-6a0c- all units \n", - "87af74fa-a77e- 6d039a63-17ad- all units \n", - "3cccfce5-dad9- 74e10781-1228- all units \n", - "bbddf25f-d810- 7e3fa66e-727e- all units \n", - "7887e5db-da1d- 86897349-ff68- all units \n", - "1b7ba376-89f9- 8bbddc0f-d6ae- all units \n", - " ...\n", - " (Total: 23)" + "mediumnwb20230 all_units test_group \n", + " (Total: 1)" ] }, - "execution_count": 4, + "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "from spyglass.spikesorting.unit_inclusion_merge import (\n", - " ImportedUnitInclusionV1,\n", - " UnitInclusionOutput,\n", - ")\n", + "from spyglass.decoding.v1.sorted_spikes import SortedSpikesGroup\n", "\n", - "ImportedUnitInclusionV1().insert_all_units(spikesorting_merge_ids)\n", - "\n", - "UnitInclusionOutput.ImportedUnitInclusionV1() & [\n", - " {\"spikesorting_merge_id\": id} for id in spikesorting_merge_ids\n", - "]" + "SortedSpikesGroup()" ] }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 7, "metadata": {}, "outputs": [ { @@ -515,42 +490,53 @@ "nwb_file_name
\n", " name of the NWB file\n", "unit_filter_params_name
\n", + " \n", + "sorted_spikes_group_name
\n", " \n", "Total: 1
\n", + "Total: 2
\n", " " ], "text/plain": [ - "*nwb_file_name *sorted_spikes\n", - "+------------+ +------------+\n", - "mediumnwb20230 test_group \n", - " (Total: 1)" + "*nwb_file_name *unit_filter_p *sorted_spikes\n", + "+------------+ +------------+ +------------+\n", + "mediumnwb20230 all_units test_group \n", + "mediumnwb20230 default_exclus test_group \n", + " (Total: 2)" ] }, - "execution_count": 5, + "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "from spyglass.spikesorting.analysis.v1.group import SortedSpikesGroup\n", - "\n", - "unit_inclusion_merge_ids = (\n", - " UnitInclusionOutput.ImportedUnitInclusionV1\n", - " & [{\"spikesorting_merge_id\": id} for id in spikesorting_merge_ids]\n", + "# get the merge_ids for the selected sortings\n", + "spikesorting_merge_ids = (\n", + " (sgs.SpikeSortingSelection & sorter_keys) * SpikeSortingOutput.CurationV1\n", ").fetch(\"merge_id\")\n", "\n", + "# create a new sorted spikes group\n", + "unit_filter_params_name = \"default_exclusion\"\n", "SortedSpikesGroup().create_group(\n", " group_name=\"test_group\",\n", " nwb_file_name=nwb_copy_file_name,\n", - " unit_inclusion_merge_ids=unit_inclusion_merge_ids,\n", + " keys=[\n", + " {\"spikesorting_merge_id\": merge_id}\n", + " for merge_id in spikesorting_merge_ids\n", + " ],\n", + " unit_filter_params_name=unit_filter_params_name,\n", ")\n", - "\n", + "# check the new group\n", "SortedSpikesGroup & {\n", " \"nwb_file_name\": nwb_copy_file_name,\n", " \"sorted_spikes_group_name\": \"test_group\",\n", @@ -559,7 +545,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 8, "metadata": {}, "outputs": [ { @@ -624,70 +610,50 @@ "nwb_file_name
\n", " name of the NWB file\n", "unit_filter_params_name
\n", + " \n", + "sorted_spikes_group_name
\n", " \n", "unit_inclusion_merge_id
\n", + "spikesorting_merge_id
\n", " \n", "...
\n", - "Total: 23
\n", + " \n", + "Total: 3
\n", " " ], "text/plain": [ - "*nwb_file_name *sorted_spikes *unit_inclusio\n", - "+------------+ +------------+ +------------+\n", - "mediumnwb20230 test_group 0363fb94-c5de-\n", - "mediumnwb20230 test_group 12da7476-bb70-\n", - "mediumnwb20230 test_group 151976b9-9dc6-\n", - "mediumnwb20230 test_group 1b7ba376-89f9-\n", - "mediumnwb20230 test_group 2f6295c6-ad14-\n", - "mediumnwb20230 test_group 357a0267-874e-\n", - "mediumnwb20230 test_group 39b22078-84fa-\n", - "mediumnwb20230 test_group 3cccfce5-dad9-\n", - "mediumnwb20230 test_group 4b025478-ab06-\n", - "mediumnwb20230 test_group 7591b69d-a902-\n", - "mediumnwb20230 test_group 76cb7574-a687-\n", - "mediumnwb20230 test_group 7887e5db-da1d-\n", - " ...\n", - " (Total: 23)" + "*nwb_file_name *unit_filter_p *sorted_spikes *spikesorting_\n", + "+------------+ +------------+ +------------+ +------------+\n", + "mediumnwb20230 default_exclus test_group 143dff79-3779-\n", + "mediumnwb20230 default_exclus test_group 75286bf3-f876-\n", + "mediumnwb20230 default_exclus test_group a900c1c8-909d-\n", + " (Total: 3)" ] }, - "execution_count": 6, + "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "SortedSpikesGroup.Units & {\n", + "# look at the sortings within the group we just made\n", + "SortedSpikesGroup.SortGroup & {\n", " \"nwb_file_name\": nwb_copy_file_name,\n", " \"sorted_spikes_group_name\": \"test_group\",\n", + " \"unit_filter_params_name\": unit_filter_params_name,\n", "}" ] }, @@ -702,7 +668,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 9, "metadata": {}, "outputs": [ { @@ -777,18 +743,30 @@ "Total: 6
\n", + "...
\n", + "Total: 15
\n", " " ], "text/plain": [ @@ -796,14 +774,21 @@ "+------------+ +--------+ +--------+\n", "contfrag_clust =BLOB= =BLOB= \n", "contfrag_clust =BLOB= =BLOB= \n", + "contfrag_clust =BLOB= =BLOB= \n", "contfrag_sorte =BLOB= =BLOB= \n", "contfrag_sorte =BLOB= =BLOB= \n", - "nonlocal_clust =BLOB= =BLOB= \n", - "nonlocal_sorte =BLOB= =BLOB= \n", - " (Total: 6)" + "j1620210710_co =BLOB= =BLOB= \n", + "j1620210710_te =BLOB= =BLOB= \n", + "MS2220180629_c =BLOB= =BLOB= \n", + "ms_lineartrack =BLOB= =BLOB= \n", + "ms_lineartrack =BLOB= =BLOB= \n", + "ms_lineartrack =BLOB= =BLOB= \n", + "ms_wtrack_2023 =BLOB= =BLOB= \n", + " ...\n", + " (Total: 15)" ] }, - "execution_count": 7, + "execution_count": 9, "metadata": {}, "output_type": "execute_result" } @@ -845,121 +830,13 @@ }, { "cell_type": "code", - "execution_count": 8, - "metadata": {}, - "outputs": [ - { - "data": { - "text/html": [ - "\n", - " \n", - " \n", - " \n", - " \n", - " \n",
- " nwb_file_name \n", - " name of the NWB file\n", - " | \n",
- " sorted_spikes_group_name \n", - " \n", - " | \n",
- " position_group_name \n", - " \n", - " | \n",
- " decoding_param_name \n", - " a name for this set of parameters\n", - " | \n",
- " encoding_interval \n", - " descriptive name of this interval list\n", - " | \n",
- " decoding_interval \n", - " descriptive name of this interval list\n", - " | \n",
- " estimate_decoding_params \n", - " whether to estimate the decoding parameters\n", - " |
---|---|---|---|---|---|---|
Total: 0
nwb_file_name
\n", " name of the NWB file\n", "unit_filter_params_name
\n", + " \n", + "sorted_spikes_group_name
\n", " \n", "estimate_decoding_params
\n", " whether to estimate the decoding parameters\n", "<xarray.Dataset>\n", - "Dimensions: (state_ind: 26668, dim_0: 26668, time: 3750,\n", - " states: 2, intervals: 1, state_bins: 26668)\n", + "Dimensions: (state_ind: 25752, dim_0: 25752, time: 5001,\n", + " states: 2, intervals: 1, state_bins: 25752)\n", "Coordinates:\n", " * state_ind (state_ind) int32 0 0 0 0 0 0 0 ... 1 1 1 1 1 1\n", " * time (time) float64 1.626e+09 ... 1.626e+09\n", @@ -1581,8 +1419,8 @@ " encoding_groups (states) int32 ...\n", " * state_bins (state_bins) object MultiIndex\n", " * state (state_bins) object 'Continuous' ... 'Fragme...\n", - " * x_position (state_bins) float64 29.02 29.02 ... 262.7\n", - " * y_position (state_bins) float64 0.5211 2.516 ... 224.0\n", + " * x_position (state_bins) float64 29.02 29.02 ... 258.8\n", + " * y_position (state_bins) float64 5.828 7.811 ... 224.0\n", "Dimensions without coordinates: dim_0, intervals\n", "Data variables:\n", " initial_conditions (dim_0) float64 ...\n", @@ -1590,54 +1428,54 @@ " acausal_posterior (intervals, time, state_bins) float32 ...\n", " acausal_state_probabilities (intervals, time, states) float64 ...\n", "Attributes:\n", - " marginal_log_likelihoods: -22323.148