Skip to content

Commit d7fb868

Browse files
authored
Merge pull request #446 from int-brain-lab/more_docs
More docs
2 parents 8dbbe98 + d644ea8 commit d7fb868

18 files changed

+2319
-114
lines changed

brainbox/ephys_plots.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,3 +496,30 @@ def histc(x, bins):
496496
return data.convert2dict(), fig, ax
497497

498498
return data
499+
500+
501+
def image_raw_data(raw, fs, chn_coords=None, cmap='bone', title=None, display=False, gain=-90, **kwargs):
502+
503+
def gain2level(gain):
504+
return 10 ** (gain / 20) * 4 * np.array([-1, 1])
505+
506+
ylabel = 'Channel index' if chn_coords is None else 'Distance from probe tip (um)'
507+
title = title or 'Raw data'
508+
509+
y = np.arange(raw.shape[1]) if chn_coords is None else chn_coords[:, 1]
510+
511+
x = np.array([0, raw.shape[0] - 1]) / fs * 1e3
512+
513+
data = ImagePlot(raw, y=y, cmap=cmap)
514+
data.set_labels(title=title, xlabel='Time (ms)',
515+
ylabel=ylabel, clabel='Power (uV)')
516+
clim = gain2level(gain)
517+
data.set_clim(clim=clim)
518+
data.set_xlim(xlim=x)
519+
data.set_ylim()
520+
521+
if display:
522+
ax, fig = plot_image(data.convert2dict(), **kwargs)
523+
return data.convert2dict(), fig, ax
524+
525+
return data

brainbox/io/one.py

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88
import pandas as pd
99
from scipy.interpolate import interp1d
1010

11-
from one.api import ONE
11+
from one.api import ONE, One
1212
import one.alf.io as alfio
13+
from one.alf import cache
1314

1415
from iblutil.util import Bunch
15-
1616
from ibllib.io import spikeglx
1717
from ibllib.io.extractors.training_wheel import extract_wheel_moves, extract_first_movement_times
1818
from ibllib.ephys.neuropixel import SITES_COORDINATES, TIP_SIZE_UM, trace_header
@@ -872,10 +872,15 @@ def load_channels_from_insertion(ins, depths=None, one=None, ba=None):
872872
class SpikeSortingLoader:
873873
"""
874874
Object that will load spike sorting data for a given probe insertion.
875-
876-
875+
This class can be instantiated in several manners
876+
- With Alyx database probe id:
877+
SpikeSortingLoader(pid=pid, one=one)
878+
- With Alyx database eic and probe name:
879+
SpikeSortingLoader(eid=eid, pname='probe00', one=one)
880+
- From a local session and probe name:
881+
SpikeSortingLoader(session_path=session_path, pname='probe00')
877882
"""
878-
one: ONE
883+
one: ONE = None
879884
atlas: None = None
880885
pid: str = None
881886
eid: str = ''
@@ -891,14 +896,26 @@ class SpikeSortingLoader:
891896
spike_sorting_path: Path = None
892897

893898
def __post_init__(self):
899+
# pid gets precedence
894900
if self.pid is not None:
895901
self.eid, self.pname = self.one.pid2eid(self.pid)
896-
if self.atlas is None:
897-
self.atlas = AllenAtlas()
898-
self.session_path = self.one.eid2path(self.eid)
902+
self.session_path = self.one.eid2path(self.eid)
903+
# then eid / pname combination
904+
elif self.session_path is None or self.session_path == '':
905+
self.session_path = self.one.eid2path(self.eid)
906+
# fully local providing a session path
907+
else:
908+
self.one = One(cache_dir=self.session_path.parents[2], mode='local')
909+
df_sessions = cache._make_sessions_df(self.session_path)
910+
self.one._cache['sessions'] = df_sessions.set_index('id')
911+
self.one._cache['datasets'] = cache._make_datasets_df(self.session_path, hash_files=False)
912+
self.eid = str(self.session_path.relative_to(self.session_path.parents[2]))
913+
# populates default properties
899914
self.collections = self.one.list_collections(
900915
self.eid, filename='spikes*', collection=f"alf/{self.pname}*")
901916
self.datasets = self.one.list_datasets(self.eid)
917+
if self.atlas is None:
918+
self.atlas = AllenAtlas()
902919
self.files = {}
903920

904921
@staticmethod

brainbox/plot_base.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,11 @@ def _get_scale(self, axis):
185185
:param axis: 'x' or 'y'
186186
:return:
187187
"""
188-
lim = self._set_lim(axis)
188+
if axis == 'x':
189+
lim = self.xlim
190+
else:
191+
lim = self.ylim
192+
lim = self._set_lim(axis, lim=lim)
189193
scale = (lim[1] - lim[0]) / self.data['c'].shape[axis_dict[axis]]
190194
return scale
191195

examples/atlas/Working with ibllib atlas.ipynb

Lines changed: 217 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 248 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,248 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"id": "f199bec2",
6+
"metadata": {},
7+
"source": [
8+
"# Plotting brain region values on circular flatmap"
9+
]
10+
},
11+
{
12+
"cell_type": "markdown",
13+
"id": "94c08e66",
14+
"metadata": {},
15+
"source": [
16+
"This example walks through various ways to overlay brain region values on a circular flatmap"
17+
]
18+
},
19+
{
20+
"cell_type": "markdown",
21+
"id": "17fd07ec",
22+
"metadata": {},
23+
"source": [
24+
"## The circular flatmap"
25+
]
26+
},
27+
{
28+
"cell_type": "markdown",
29+
"id": "3ca88864",
30+
"metadata": {},
31+
"source": [
32+
"The circular flatmap is obtained by sampling the volume using concentric circles through the brain."
33+
]
34+
},
35+
{
36+
"cell_type": "code",
37+
"execution_count": null,
38+
"id": "1178246b",
39+
"metadata": {},
40+
"outputs": [],
41+
"source": [
42+
"from ibllib.atlas import FlatMap\n",
43+
"flmap_cr = FlatMap(flatmap='circles')"
44+
]
45+
},
46+
{
47+
"cell_type": "code",
48+
"execution_count": null,
49+
"id": "490614c3",
50+
"metadata": {},
51+
"outputs": [],
52+
"source": [
53+
"# Display the concentric circles used in flatmap\n",
54+
"ax = flmap_cr.plot_top(volume='image')\n",
55+
"ax.plot(flmap_cr.ml_scale * 1e6, flmap_cr.ap_scale * 1e6)"
56+
]
57+
},
58+
{
59+
"cell_type": "markdown",
60+
"id": "135dd187",
61+
"metadata": {},
62+
"source": [
63+
"This results in a flatmap that can be displayed in the following way"
64+
]
65+
},
66+
{
67+
"cell_type": "code",
68+
"execution_count": null,
69+
"id": "8b8c4223",
70+
"metadata": {},
71+
"outputs": [],
72+
"source": [
73+
"import matplotlib.pyplot as plt\n",
74+
"fig, ax = plt.subplots(figsize=(18,4))\n",
75+
"flmap_cr.plot_flatmap(ax)"
76+
]
77+
},
78+
{
79+
"cell_type": "markdown",
80+
"id": "ec15f88c",
81+
"metadata": {},
82+
"source": [
83+
"It is also possible to display this flatmap such that each circle is stacked on top of eachother. For this, the **pyramid** flatmap should be used"
84+
]
85+
},
86+
{
87+
"cell_type": "code",
88+
"execution_count": null,
89+
"id": "7461e3f8",
90+
"metadata": {},
91+
"outputs": [],
92+
"source": [
93+
"# Instantiate flatmap with circles arranged vetically on top of eachother\n",
94+
"flmap_py = FlatMap(flatmap='pyramid')"
95+
]
96+
},
97+
{
98+
"cell_type": "code",
99+
"execution_count": null,
100+
"id": "1f78b2ab",
101+
"metadata": {},
102+
"outputs": [],
103+
"source": [
104+
"fig, ax = plt.subplots(figsize=(8, 8))\n",
105+
"flmap_py.plot_flatmap(ax=ax)"
106+
]
107+
},
108+
{
109+
"cell_type": "markdown",
110+
"id": "e7af738a",
111+
"metadata": {},
112+
"source": [
113+
"## Data preparation"
114+
]
115+
},
116+
{
117+
"cell_type": "markdown",
118+
"id": "40fa09d0",
119+
"metadata": {},
120+
"source": [
121+
"In order to plot brain regions values on the flatmap an array of acronyms and an array of values corresponding to each acronym must be provided. A detailed overview of how to prepare your data can be found [here](https://int-brain-lab.github.io/iblenv/notebooks_external/atlas_plotting_scalar_on_slice.html#Data-preparation)"
122+
]
123+
},
124+
{
125+
"cell_type": "code",
126+
"execution_count": null,
127+
"id": "20a1db83",
128+
"metadata": {},
129+
"outputs": [],
130+
"source": [
131+
"import numpy as np\n",
132+
"# prepare array of acronyms\n",
133+
"acronyms = np.array(['VPM', 'PO', 'LP', 'CA1', 'DG-mo', 'VISa5', 'SSs5'])\n",
134+
"# assign data to each acronym\n",
135+
"values = np.arange(acronyms.size)"
136+
]
137+
},
138+
{
139+
"cell_type": "code",
140+
"execution_count": null,
141+
"id": "e6ae51d0",
142+
"metadata": {},
143+
"outputs": [],
144+
"source": [
145+
"from ibllib.atlas.regions import BrainRegions\n",
146+
"br = BrainRegions()\n",
147+
"# prepare array of acronyms with beryl mapping\n",
148+
"acronyms_beryl = np.unique(br.acronym2acronym(acronyms, mapping='Beryl'))\n",
149+
"values_beryl = np.arange(acronyms_beryl.size)"
150+
]
151+
},
152+
{
153+
"cell_type": "code",
154+
"execution_count": null,
155+
"id": "3724b968",
156+
"metadata": {},
157+
"outputs": [],
158+
"source": [
159+
"# prepare different values for left and right hemipshere for Beryl acronyms\n",
160+
"values_beryl_lh = np.random.randint(0, 10, acronyms_beryl.size)\n",
161+
"values_beryl_rh = np.random.randint(0, 10, acronyms_beryl.size)\n",
162+
"values_beryl_lr = np.c_[values_beryl_lh, values_beryl_rh]"
163+
]
164+
},
165+
{
166+
"cell_type": "markdown",
167+
"id": "74fe528a",
168+
"metadata": {},
169+
"source": [
170+
"## Examples"
171+
]
172+
},
173+
{
174+
"cell_type": "code",
175+
"execution_count": null,
176+
"id": "dfa2d623",
177+
"metadata": {},
178+
"outputs": [],
179+
"source": [
180+
"from ibllib.atlas.plots import plot_scalar_on_flatmap\n",
181+
"# Plot region values on the left hemisphere of circle flatmap overlaid on brain region boundaries using Allen mapping\n",
182+
"fig, ax = plt.subplots(figsize=(18,4))\n",
183+
"fig, ax = plot_scalar_on_flatmap(acronyms, values, hemisphere='left', mapping='Allen', flmap_atlas=flmap_cr, ax=ax)"
184+
]
185+
},
186+
{
187+
"cell_type": "code",
188+
"execution_count": null,
189+
"id": "cc78a1c7",
190+
"metadata": {},
191+
"outputs": [],
192+
"source": [
193+
"# Plot region values on the both hemispheres of circle flatmap overlaid on the dwi Allen image using Beryl mapping\n",
194+
"fig, ax = plt.subplots(figsize=(18,4))\n",
195+
"fig, ax = plot_scalar_on_flatmap(acronyms_beryl, values_beryl, hemisphere='both', mapping='Beryl', background='image', \n",
196+
" cmap='Reds', flmap_atlas=flmap_cr, ax=ax)"
197+
]
198+
},
199+
{
200+
"cell_type": "code",
201+
"execution_count": null,
202+
"id": "37bf7bd8",
203+
"metadata": {},
204+
"outputs": [],
205+
"source": [
206+
"# Plot region values on the right hemisphere of pyramidal flatmap overlaid on the dwi Allen image using Allen mapping\n",
207+
"fig, ax = plt.subplots(figsize=(8,8))\n",
208+
"fig, ax = plot_scalar_on_flatmap(acronyms, values, hemisphere='right', mapping='Allen', background='image', \n",
209+
" cmap='Reds', flmap_atlas=flmap_py, ax=ax)"
210+
]
211+
},
212+
{
213+
"cell_type": "code",
214+
"execution_count": null,
215+
"id": "28f7f30c",
216+
"metadata": {},
217+
"outputs": [],
218+
"source": [
219+
"# Plot two column region values on the both hemispheres of pyramidal flatmap overlaid on brain region boundaries \n",
220+
"# using Beryl mapping\n",
221+
"fig, ax = plt.subplots(figsize=(8,8))\n",
222+
"fig, ax = plot_scalar_on_flatmap(acronyms_beryl, values_beryl_lr, hemisphere='both', mapping='Beryl', \n",
223+
" background='boundary', cmap='Blues', flmap_atlas=flmap_py, ax=ax)"
224+
]
225+
}
226+
],
227+
"metadata": {
228+
"kernelspec": {
229+
"display_name": "Python [conda env:iblenv] *",
230+
"language": "python",
231+
"name": "conda-env-iblenv-py"
232+
},
233+
"language_info": {
234+
"codemirror_mode": {
235+
"name": "ipython",
236+
"version": 3
237+
},
238+
"file_extension": ".py",
239+
"mimetype": "text/x-python",
240+
"name": "python",
241+
"nbconvert_exporter": "python",
242+
"pygments_lexer": "ipython3",
243+
"version": "3.9.7"
244+
}
245+
},
246+
"nbformat": 4,
247+
"nbformat_minor": 5
248+
}

0 commit comments

Comments
 (0)