Skip to content

Commit be2d939

Browse files
authored
Merge pull request #847 from int-brain-lab/docs
Docs
2 parents e1eb551 + d63dd65 commit be2d939

File tree

1 file changed

+279
-0
lines changed

1 file changed

+279
-0
lines changed
Lines changed: 279 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,279 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"id": "c1fdd473",
6+
"metadata": {},
7+
"source": [
8+
"# Loading Fiber Photometry Data\n",
9+
"\n",
10+
"Calcium activity recorded using a fiber photometry."
11+
]
12+
},
13+
{
14+
"cell_type": "code",
15+
"execution_count": null,
16+
"id": "6e1ba7b2",
17+
"metadata": {
18+
"nbsphinx": "hidden"
19+
},
20+
"outputs": [],
21+
"source": [
22+
"# Turn off logging and disable tqdm this is a hidden cell on docs page\n",
23+
"import logging\n",
24+
"import os\n",
25+
"\n",
26+
"logger = logging.getLogger('ibllib')\n",
27+
"logger.setLevel(logging.CRITICAL)\n",
28+
"\n",
29+
"os.environ[\"TQDM_DISABLE\"] = \"1\""
30+
]
31+
},
32+
{
33+
"cell_type": "markdown",
34+
"id": "a406ed50",
35+
"metadata": {},
36+
"source": [
37+
"## Relevant ALF objects\n",
38+
"* photometry\n",
39+
"* photometryROI\n",
40+
"\n",
41+
"\n",
42+
"## More details\n",
43+
"* [Description of photometry datasets](https://docs.google.com/document/d/1OqIqqakPakHXRAwceYLwFY9gOrm8_P62XIfCTnHwstg/edit#heading=h.3o4nwo63tny)"
44+
]
45+
},
46+
{
47+
"cell_type": "markdown",
48+
"id": "02482b24",
49+
"metadata": {},
50+
"source": [
51+
"## Finding sessions with photometry data\n",
52+
"Sessions that contain photometry data can be found by searching for sessions with a corresponding photometry dataset"
53+
]
54+
},
55+
{
56+
"cell_type": "code",
57+
"execution_count": null,
58+
"id": "06f43a20",
59+
"metadata": {},
60+
"outputs": [],
61+
"source": [
62+
"from one.api import ONE\n",
63+
"one = ONE()\n",
64+
"sessions = one.search(dataset='photometry.signal.pqt')\n",
65+
"print(f'{len(sessions)} sessions with photometry data found')"
66+
]
67+
},
68+
{
69+
"cell_type": "markdown",
70+
"id": "dea30e9f",
71+
"metadata": {},
72+
"source": [
73+
"## Loading photometry data\n",
74+
"The photometry data for a single session can be loaded in the following way"
75+
]
76+
},
77+
{
78+
"cell_type": "code",
79+
"execution_count": null,
80+
"id": "41fae7ad",
81+
"metadata": {},
82+
"outputs": [],
83+
"source": [
84+
"# Get the first returned sessions with photometry data\n",
85+
"eid = sessions[0]\n",
86+
"# Load the photometry signal dataset\n",
87+
"photometry = one.load_dataset(eid, 'photometry.signal.pqt')\n",
88+
"print(photometry.columns)"
89+
]
90+
},
91+
{
92+
"cell_type": "markdown",
93+
"id": "5a19d768",
94+
"metadata": {},
95+
"source": [
96+
"The data returned is a table that contains photometry data for all ROIS (Region0G, Region1G, ...) recorded simultaneously in a single session. The number of rows in the table give the number of imaging frames in the dataset. The timestamps for each frame is stored in the `times` column are in seconds from session start and are aligned to other times from the session, e.g behavioral or video events.\n",
97+
"\n",
98+
"The wavelength of light used to collect each imaging frame can be found using either the `wavelength` or the `name` column. For example if we want to limit our table to only frames collected at 470 nm we can do the following"
99+
]
100+
},
101+
{
102+
"cell_type": "code",
103+
"execution_count": null,
104+
"id": "62f0bc6c",
105+
"metadata": {},
106+
"outputs": [],
107+
"source": [
108+
"# Limit signal to frames collected at 470 nm\n",
109+
"photometry = photometry[photometry['wavelength'] == 470]"
110+
]
111+
},
112+
{
113+
"cell_type": "markdown",
114+
"id": "d544f651",
115+
"metadata": {},
116+
"source": [
117+
"The photometry data also contains a column called `include` which contains a manually selected interval of the signal that is free from artefacts"
118+
]
119+
},
120+
{
121+
"cell_type": "code",
122+
"execution_count": null,
123+
"id": "acc946cf",
124+
"metadata": {},
125+
"outputs": [],
126+
"source": [
127+
"# Restrict signal to artefact free intervals\n",
128+
"photometry = photometry[photometry['include']]"
129+
]
130+
},
131+
{
132+
"cell_type": "markdown",
133+
"id": "a9f91adb",
134+
"metadata": {},
135+
"source": [
136+
"## Associating ROIs to Brain Regions"
137+
]
138+
},
139+
{
140+
"cell_type": "markdown",
141+
"id": "462c2242",
142+
"metadata": {},
143+
"source": [
144+
"We can associate each Region with a brain region by loading in the photometryROI dataset. This contains a lookup table from `ROI` to a `fiber` stored on the openalyx database and a `brain_region`"
145+
]
146+
},
147+
{
148+
"cell_type": "code",
149+
"execution_count": null,
150+
"id": "5ff081d7",
151+
"metadata": {},
152+
"outputs": [],
153+
"source": [
154+
"rois = one.load_dataset(eid, 'photometryROI.locations.pqt')\n",
155+
"rois"
156+
]
157+
},
158+
{
159+
"cell_type": "markdown",
160+
"id": "bbdfb2fc",
161+
"metadata": {},
162+
"source": [
163+
"We can rename our columns in our photometry data with the brain regions"
164+
]
165+
},
166+
{
167+
"cell_type": "code",
168+
"execution_count": null,
169+
"id": "4420a657",
170+
"metadata": {},
171+
"outputs": [],
172+
"source": [
173+
"photometry = photometry.rename(columns=rois.to_dict()['brain_region'])\n",
174+
"print(photometry.columns)"
175+
]
176+
},
177+
{
178+
"cell_type": "markdown",
179+
"id": "9c3978e5",
180+
"metadata": {},
181+
"source": [
182+
"Please see the associated [publication](https://doi.org/10.1101/2024.02.26.582199) for these datasets for more information about the definition of the given brain regions."
183+
]
184+
},
185+
{
186+
"cell_type": "markdown",
187+
"id": "d2116ab7",
188+
"metadata": {},
189+
"source": [
190+
"## QC of the ROIs"
191+
]
192+
},
193+
{
194+
"cell_type": "markdown",
195+
"id": "a4cc1d09",
196+
"metadata": {},
197+
"source": [
198+
"Each ROI has an associated fiber insertion registered on the openalyx database. The fiber contains information about the brain region targeted and also a `QC` value indicating if the signal is good or not. The associated [publication](https://doi.org/10.1101/2024.02.26.582199) contains more information about the defintion of a passing QC value.\n",
199+
"\n",
200+
"For a session we can find the QC for each ROI in the following way"
201+
]
202+
},
203+
{
204+
"cell_type": "code",
205+
"execution_count": null,
206+
"id": "4937fd8c",
207+
"metadata": {},
208+
"outputs": [],
209+
"source": [
210+
"from iblutil.util import Bunch\n",
211+
"\n",
212+
"QC = Bunch()\n",
213+
"for roi, info in rois.iterrows():\n",
214+
" fiber = one.alyx.rest('insertions', 'list', session=eid, name=info.fiber)[0]\n",
215+
" QC[info.brain_region] = fiber['json']['qc']\n",
216+
"\n",
217+
"print(QC)"
218+
]
219+
},
220+
{
221+
"cell_type": "markdown",
222+
"id": "eb061d87",
223+
"metadata": {},
224+
"source": [
225+
"## Computing dF / F"
226+
]
227+
},
228+
{
229+
"cell_type": "markdown",
230+
"id": "b36aef53",
231+
"metadata": {},
232+
"source": [
233+
"Here we show an example of how to compute the dF/F signal from the photometry data using the defintion in associated [publication](https://doi.org/10.1101/2024.02.26.582199)"
234+
]
235+
},
236+
{
237+
"cell_type": "code",
238+
"execution_count": null,
239+
"id": "5fa5ca08",
240+
"metadata": {},
241+
"outputs": [],
242+
"source": [
243+
"# Compute df/F signal for brain region DMS\n",
244+
"# Baseline signal is the +- 30s rolling average of the raw signal\n",
245+
"\n",
246+
"# Get the frame rate of the data\n",
247+
"fr = (1 / photometry.times.diff().mean()).round()\n",
248+
"# Define rolling average window of 30 s\n",
249+
"window = 30\n",
250+
"\n",
251+
"F = photometry['DMS']\n",
252+
"F0 = F.rolling(int(fr * window), center=True).mean()\n",
253+
"dF = (F - F0) / F0\n"
254+
]
255+
}
256+
],
257+
"metadata": {
258+
"celltoolbar": "Edit Metadata",
259+
"kernelspec": {
260+
"display_name": "Python 3 (ipykernel)",
261+
"language": "python",
262+
"name": "python3"
263+
},
264+
"language_info": {
265+
"codemirror_mode": {
266+
"name": "ipython",
267+
"version": 3
268+
},
269+
"file_extension": ".py",
270+
"mimetype": "text/x-python",
271+
"name": "python",
272+
"nbconvert_exporter": "python",
273+
"pygments_lexer": "ipython3",
274+
"version": "3.9.16"
275+
}
276+
},
277+
"nbformat": 4,
278+
"nbformat_minor": 5
279+
}

0 commit comments

Comments
 (0)