-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathbrain_plots.py
More file actions
762 lines (669 loc) · 28.1 KB
/
brain_plots.py
File metadata and controls
762 lines (669 loc) · 28.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
import plotly.graph_objs as go
import plotly
from matplotlib.cm import ScalarMappable
from matplotlib.colors import Normalize
from ..localization import Brain, find_closest_vertices
from ..utils import surfdist_viz
def _view(hemi, mode: str = "lateral", backend: str = "mpl"):
"""
Appropriate azimuth for displaying the specified hemisphere in specified view.
Arguments
---------
hemi : {'lh','rh'}
Hemisphere
mode : {'lateral','medial','frontal','occipital',top','bottom',best'}, default='lateral'
What view to return azimuth and elevation for. One of 'lateral',
'best', 'medial','frontal','occipital',top','bottom'
backend : {'mpl','plotly'}, defualt='mpl'
Plotting backend, either 'mpl' or 'plotly'
Returns
-------
elevation : float, elevation of view, if backend='mpl'
azimuth : float, azimuth of view, if backend='mpl'
eye : dict, for plotly.graph_objects.layout.scene.camera.eye, if backend='plotly'
center : dict, for plotly.graph_objects.layout.scene.camera.center, if backend='plotly'
"""
if mode == "lateral":
if backend == "plotly":
eye = dict(x=-1, y=0, z=0) if hemi == "lh" else dict(x=1, y=0, z=0)
center = dict(x=0, y=0, z=0)
return eye, center
else:
return (0, 180) if hemi == "lh" else (0, 0)
elif mode == "medial":
if backend == "plotly":
eye = dict(x=1, y=0, z=0) if hemi == "lh" else dict(x=-1, y=0, z=0)
center = dict(x=0, y=0, z=0)
return eye, center
else:
return (0, 0) if hemi == "lh" else (0, 180)
elif mode == "frontal":
if backend == "plotly":
eye = dict(x=0, y=1, z=0)
center = dict(x=0, y=0, z=0)
return eye, center
else:
return (0, 90)
elif mode == "occipital":
if backend == "plotly":
eye = dict(x=0, y=-1, z=0)
center = dict(x=0, y=0, z=0)
return eye, center
else:
return (0, 270)
elif mode == "top":
if backend == "plotly":
eye = dict(x=0, y=0, z=1)
center = dict(x=0, y=0, z=0)
return eye, center
else:
return (90, 270)
elif mode == "bottom":
if backend == "plotly":
eye = dict(x=0, y=0, z=-1)
center = dict(x=0, y=0, z=0)
return eye, center
else:
return (270, 270)
elif mode == "best":
if backend == "plotly":
eye = dict(x=-1, y=0.1, z=0.1) if hemi == "lh" else dict(x=1, y=0.1, z=0.1)
center = dict(x=0, y=0, z=0)
return eye, center
else:
return (10, 170) if hemi == "lh" else (10, 10)
raise ValueError(f"Unknown `mode`: {mode}.")
def _plot_hemi(hemi, cmap="coolwarm", ax=None, view="best", threshold=None, vmin=None, vmax=None):
if isinstance(view, tuple):
elev, azim = view
else:
elev, azim = _view(hemi.hemi, mode=view)
surfdist_viz(
*hemi.surf,
hemi.overlay,
elev, azim,
cmap=cmap,
threshold=threshold,
alpha=hemi.alpha,
bg_map=hemi.sulc,
bg_on_stat=True,
ax=ax,
vmin=vmin,
vmax=vmax
)
ax.axes.set_axis_off()
ax.grid(False)
def plot_brain_overlay(
brain, cmap="coolwarm", ax=None, hemi='both', view="best", vmin=None, vmax=None, cmap_quantile=1.0, threshold=None, **kwargs
):
"""
Plot brain overlay on the 3D cortical surface using matplotlib.
If certain regions have been set as visible using
brain.set_visible(), only those regions will be shown.
Parameters
----------
brain : nl.localization.Brain
Brain instance to plot on.
cmap : str, default='coolwarm'
Colormap to use.
ax : list | tuple of matplotlib Axes
2 Axes to plot the left and right hemispheres with.
hemi : {'both', 'lh', 'rh'}, default='both'
Hemisphere(s) to plot. If 'both', then 2 subplots are created, one for each hemisphere.
Otherwise only one hemisphere is displayed with its overlay.
view : {'lateral','medial','frontal','occipital',top','bottom',best'} | tuple, default='best'
View of the brain to display. A tuple can specify the (elevation, azimuth) for matplotlib backend,
or a tuple of dicts for (eye, center), which are the plotly.graph_objects.layout.scene.camera.eye and
plotly.graph_objects.layout.scene.camera.center for plotly backend.
vmin : float, optional
Minimum value for colormap. If not given, will use cmap_quantile or range or overlay values.
vmax : float, optional
Maximum value for colormap. If not given, will use cmap_quantile or range or overlay values.
cmap_quantile : float | tuple of floats (optional), default=1.0
If a single float less than 1, will only use the central ``cmap_quantile`` portion of the range
of values to create the vmin and vmax for the colormap. For example, if set to 0.95,
then only the middle 95% of the values will be used to set the range of the colormap. If a tuple,
then it should specify 2 quantiles, one for the vmin and one for the vmax, such as (0.025, 0.975),
which would be equivalent to passing a single value of 0.95.
threshold : positive float, optional
If given, then only values on the overlay which are less -threshold or greater than threshold will
be shown.
**kwargs : kwargs
Any other kwargs to pass to matplotlib.pyplot.figure (such as figsize)
Returns
-------
fig : matplotlib Figure
axes : tuple of matplotlib Axes
"""
fig = plt.figure(**kwargs)
if ax is None:
if hemi in ['both', 'b']:
ax1 = fig.add_subplot(1, 2, 1, projection="3d")
ax2 = fig.add_subplot(1, 2, 2, projection="3d")
ax = (ax1, ax2)
else:
ax = fig.add_subplot(1, 1, 1, projection="3d")
if hemi in ['left','lh']:
ax = [ax, None]
elif hemi in ['right','rh']:
ax = [None, ax]
else:
if hemi in ['both', 'b']:
assert len(ax) == 2
if vmin is None or vmax is None:
if cmap_quantile is not None:
if isinstance(cmap_quantile, float):
assert cmap_quantile <= 1 and cmap_quantile > 0
cmap_diff = (1.0 - cmap_quantile) / 2.
vmin_l = np.quantile(brain.lh.overlay[brain.lh.overlay!=0], cmap_diff)
vmax_l = np.quantile(brain.lh.overlay[brain.lh.overlay!=0], 1.0 - cmap_diff)
vmin_r = np.quantile(brain.rh.overlay[brain.rh.overlay!=0], cmap_diff)
vmax_r = np.quantile(brain.rh.overlay[brain.rh.overlay!=0], 1.0 - cmap_diff)
elif isinstance(cmap_quantile, tuple):
vmin_l = np.quantile(brain.lh.overlay[brain.lh.overlay!=0], cmap_quantile[0])
vmax_l = np.quantile(brain.lh.overlay[brain.lh.overlay!=0], cmap_quantile[1])
vmin_r = np.quantile(brain.rh.overlay[brain.rh.overlay!=0], cmap_quantile[0])
vmax_r = np.quantile(brain.rh.overlay[brain.rh.overlay!=0], cmap_quantile[1])
else:
raise ValueError('cmap_quantile must be either a float or a tuple')
else:
vmin_l = brain.lh.overlay[brain.lh.overlay!=0].min()
vmax_l = brain.lh.overlay[brain.lh.overlay!=0].max()
vmin_r = brain.rh.overlay[brain.rh.overlay!=0].min()
vmax_r = brain.rh.overlay[brain.rh.overlay!=0].max()
# determine vmin and vmax
if hemi in ['both', 'b']:
if vmin is None:
vmin = min([vmin_l, vmin_r])
if vmax is None:
vmax = max([vmax_l, vmax_r])
elif hemi in ['left','lh']:
if vmin is None:
vmin = vmin_l
if vmax is None:
vmax = vmax_l
elif hemi in ['right','rh']:
if vmin is None:
vmin = vmin_r
if vmax is None:
vmax = vmax_r
if ax[0] is not None:
_plot_hemi(brain.lh, cmap, ax[0], view=view, vmin=vmin, vmax=vmax, threshold=threshold)
if ax[1] is not None:
_plot_hemi(brain.rh, cmap, ax[1], view=view, vmin=vmin, vmax=vmax, threshold=threshold)
return fig, ax
def _tri_indices(simplices):
# simplices is a numpy array defining the simplices of the triangularization
# returns the lists of indices i, j, k
return ([triplet[c] for triplet in simplices] for c in range(3))
def _plotly_trisurf(points3D, simplices, facecolor, opacity=1, name=""):
# points3D are coordinates of the triangle vertices
# simplices are the simplices that define the triangularization;
# simplices is a numpy array of shape (no_triangles, 3)
I, J, K = _tri_indices(simplices)
triangles = go.Mesh3d(
x=points3D[:, 0],
y=points3D[:, 1],
z=points3D[:, 2],
facecolor=facecolor,
i=I,
j=J,
k=K,
name=name,
opacity=opacity,
)
return triangles
def _plotly_scatter3d(coords, elec_colors, elec_alpha, name=""):
marker = go.scatter3d.Marker(color=elec_colors)
if not isinstance(elec_alpha, (np.ndarray, list)):
elec_alpha = np.asarray([elec_alpha] * len(coords))
scatter = go.Scatter3d(
x=coords[:, 0],
y=coords[:, 1],
z=coords[:, 2],
mode="markers",
marker=marker,
name=name,
customdata=elec_alpha,
)
return scatter
def _set_opacity(trace):
"""https://community.plotly.com/t/varying-opacity-in-scatter-3d/75505/5"""
if hasattr(trace, 'customdata') and isinstance(trace.customdata, float):
opacities = trace.customdata
r, g, b = plotly.colors.hex_to_rgb(trace.marker.color)
trace.marker.color = [
f'rgba({r}, {g}, {b}, {a})'
for a in map(lambda x: x[0], opacities)]
def plot_brain_elecs(
brain,
elecs,
isleft=None,
values=None,
colors="k",
hemi="both",
view="lateral",
snap_to_surface=None,
elec_size=4,
cortex="classic",
cmap="cool",
alpha=1,
vmin=None,
vmax=None,
brain_alpha=None,
figsize=6,
backend="mpl",
**kwargs,
):
"""
Plot electrodes on the brain using a simple matplotlib backend, or an interactive
3D figure using the plotly backend.
Due to the limitation of matplotlib being unable to render 3D surfaces
in order as they would truly be seen by the camera angle, electrodes
which are behind the cortical surface will still be visible as if
they were in front of it.
Parameters
----------
brain : nl.localization.Brain
Brain instance to plot on.
elecs : np.ndarray
Array of shape (num_elecs, 3) of electrode coordinates in pial space.
isleft : np.ndarray, optional
Boolean array of length (num_elecs) indicating whether a given electrode belongs
to the left hemisphere. If not given, they are assumed based on the sign of the first
component of the `elecs` coordinates (negative is left, positive is right)
values : np.ndarray, optional
Float values of length (num_elecs) which will be converted by the colormap colors
for each electrode.
colors : np.ndarray | list[str] | str, default='k'
Colors to plot for each electrode. Ignored if values is not None. This can be a single string
specifiying a color for all electrodes, a list of strings of the same length as
elecs, or a numpy array of shape (num_elecs, 4) specifying the RGBA value for each electrode.
hemi : {'both', 'lh', 'rh'}, default='both'
Hemisphere(s) to plot. If 'both', then 2 subplots are created, one for each hemisphere.
Otherwise only one hemisphere is displayed with its electrodes.
view : {'lateral','frontal','occipital',medial','top','bottom',best'} | tuple, default='lateral'
View of the brain to display. A tuple can specify the (elevation, azimuth) for matplotlib backend,
or a tuple of dicts for (eye, center), which are the plotly.graph_objects.layout.scene.camera.eye and
plotly.graph_objects.layout.scene.camera.center for plotly backend.
snap_to_surface : bool, optional
Whether to snap electrodes to the nearest point on the pial cortical surface. If plotting
an 'inflated' brain, this should be set to True (default) to map through the pial surface,
since coordinates are assumed to represent coordinates in the pial space. If plotting pial,
then this can be set to False (default) to show true electrode placement, or True to map
to the surface.
elec_size : int | np.ndarray, default=4
Size of the markers representing electrodes. If an array, should give the size for each electrode.
cortex : {'classic','high_contrast','mid_contrast','low_contrast','bone'}, default='classic'
How to map the sulci to greyscale. 'classic' will leave sulci untouched, which may be
better for plotting the pial surface, but 'high_contrast' will enhance the contrast between
gyri and sulci, which may be better for inflated surfaces.
cmap : str, default='cool'
Colormap for electrode values if values are provided.
alpha : float | np.ndarray, optional, default=1
Opacity of the electrodes. Either a single float or an array of same length as number of electrodes.
If None, then the colors provided should be an array of RGBA values, not just RGB.
vmin : float, optional
Minimum value for colormap normalization. If None, uses the min of valus.
vmax : float, optional
Maximum value for colormap normalization. If None, uses the max of values.
brain_alpha : float, optional
Opacity of the brain surface. The default sets this to a reasonable value based on
both the surface type ('pial' or not) and the backend ('plotly' vs 'mpl')
figsize : int, default=6
Size of the figure to display. This will be multiplied by the number of hemispheres
to plot to specify the width.
backend : {'mpl','plotly'}, default='mpl'
Backend used for plotting. Matplotlib produces a figure and list of axes, each with a hemisphere
as a static image. Plotly produces a 3D plot figure widget which can be saved as an image or interacted
with in HTML form.
**kwargs
Additional keyword arguments to be passed to the matplotlib.pyplot.scatter function.
Returns
-------
fig : matplotlib Figure or plotly Figure, depending on backend
axes : list of matplotlib Axes, only if using mpl backend, otherwise None
Notes
-----
When using within a jupyter notebook, it may be required to run these lines in this specific order before
doing any plotting with the plotly backend.
>>> import plotly.offline as pyo
>>> pyo.init_notebook_mode(connected=True)
>>> import plotly.io as pio
>>> pio.renderers.default = 'iframe' # or possibly 'notebook'
If plots still don't show in the notebook, you may need to install nbformat if you do not have it, or
you may need to enable certain ipywidgets for displaying plotly in notebooks.
Regardless, it should still be possible to save the the figure as either HTML or
static image file. See `plotly examples documentation <https://plotly.com/python/interactive-html-export/>`_ for
details on saving figures.
Examples
--------
>>> import numpy as np
>>> import matplotlib.pyplot as plt
>>> from naplib.localization import Brain
>>> from naplib.visualization import plot_brain_elecs, plot_brain_overlay
>>> brain = Brain('pial', subject_dir='path/to/subjects/').split_hg('midpoint').split_stg().simplify_labels()
>>> coords = np.array([[-47.281147 , 17.026093 , -21.833099 ],
[-48.273964 , 16.155487 , -20.162935 ],
[-51.101261 , 13.711058 , -16.258459 ]])
>>> values = np.array([1, 1.5, 3]) # one value per electrode for color
>>> # plot with matplotlib
>>> fig, axes = plot_brain_elecs(brain, coords, values=values, hemi='lh', view='lateral')
>>> plt.show()
>>> # plot interactive figure with plotly
>>> fig, axes = plot_brain_elecs(brain, coords, isleft, colors=colors, backend='plotly')
>>> fig.write_html("interactive_brain_plot.html") # save as an interactive html plot
>>> fig.show() # show the interactive plot in the notebook
"""
if hemi == "both":
surfs = {"lh": brain.lh.surf, "rh": brain.rh.surf}
sulci = {"lh": brain.lh.sulc, "rh": brain.rh.sulc}
elif hemi == "lh":
surfs = {"lh": brain.lh.surf}
sulci = {"lh": brain.lh.sulc}
elif hemi == "rh":
surfs = {"rh": brain.rh.surf}
sulci = {"rh": brain.rh.sulc}
else:
raise ValueError(f"hemi must be either both, lh, or rh, but got {hemi}")
if isleft is None:
isleft = elecs[:, 0] < 0
if backend not in ["mpl", "plotly"]:
raise ValueError(f"backend must be either mpl or plotly but got {backend}")
if snap_to_surface is None:
if brain.surf_type == "pial":
snap_to_surface = False
else:
snap_to_surface = True
if brain_alpha is None:
if brain.surf_type == "pial":
brain_alpha = 0.3
else:
if backend == "plotly":
brain_alpha = 1
else:
brain_alpha = 0.45
if backend == "mpl":
kwargs.setdefault("edgecolors", "none")
kwargs.setdefault("depthshade", False)
fig, axes = _plot_brain_elecs_standalone(
brain,
surfs,
sulci,
elecs=elecs,
elec_isleft=isleft,
elec_values=values,
snap_to_surface=snap_to_surface,
colors=colors,
elec_size=elec_size,
view=view,
cortex=cortex,
cmap=cmap,
elec_alpha=alpha,
vmin=vmin,
vmax=vmax,
brain_alpha=brain_alpha,
figsize=figsize,
backend=backend,
**kwargs,
)
return fig, axes
def _plot_brain_elecs_standalone(
brain,
surfs,
sulci=None,
elecs=None,
elec_isleft=None,
elec_values=None,
snap_to_surface=True,
colors="k",
elec_size=4,
cortex="classic",
cmap="cool",
elec_alpha=1,
view="lateral",
brain_alpha=0.3,
vmin=None,
vmax=None,
figsize=8,
backend="mpl",
**kwargs,
):
colormap_map = dict(
classic=(dict(colormap="Greys", vmin=-1, vmax=2), lambda x: x),
high_contrast=(dict(colormap="Greys", vmin=-0.2, vmax=1.3), lambda x: x),
mid_contrast=(dict(colormap="Greys", vmin=-1.3, vmax=1.3), np.tanh),
low_contrast=(dict(colormap="Greys", vmin=-4, vmax=4), lambda x: x),
grey_binary=(
dict(colormap="Greys", vmin=-0.9, vmax=2),
lambda x: np.where(x < 0.5, np.tanh(x - 0.2), np.tanh(x + 0.2)),
),
bone=(dict(colormap="bone", vmin=-0.2, vmax=2), lambda x: x),
)
assert isinstance(surfs, dict)
if isinstance(elec_size, list):
elec_size = np.asarray(elec_size)
if cortex not in colormap_map:
raise ValueError(
f"Invalid cortex. Must be one of {'classic','high_contrast','low_contrast','bone_r'} but got {cortex}"
)
if isinstance(elec_alpha, list) or isinstance(elec_alpha, np.ndarray):
update_opacity_per_elec = True
else:
update_opacity_per_elec = False
sulci_cmap_kwargs, sulci_cmap_nonlinearity = colormap_map[cortex]
hemi_keys = sorted(list(surfs.keys()))
for k in hemi_keys:
assert k in ["lh", "rh"]
cmap_sulci = plt.colormaps[sulci_cmap_kwargs["colormap"]]
vmin_sulci = sulci_cmap_kwargs["vmin"]
vmax_sulci = sulci_cmap_kwargs["vmax"]
norm_sulci = Normalize(vmin=vmin_sulci, vmax=vmax_sulci)
cmap_sulci_func = lambda x: cmap_sulci(norm_sulci(sulci_cmap_nonlinearity(x)))
# create figure
if backend == "mpl":
if not isinstance(figsize, tuple):
figsize = (figsize * len(hemi_keys), int(figsize * 1.2))
fig = plt.figure(figsize=figsize)
else:
trace_list = []
if elecs is not None:
if elec_isleft is None:
elec_isleft = np.ones((len(elecs),)).astype("bool")
if elec_values is not None:
# if plotting electrodes, that overrides the colormap for overlay
cmap_overlay = plt.colormaps[cmap]
vmin = elec_values.min() if vmin is None else vmin
vmax = elec_values.max() if vmax is None else vmax
norm = Normalize(vmin=vmin, vmax=vmax)
cmap_func = lambda x: cmap_overlay(norm(x))
cmap_mappable = ScalarMappable(cmap=cmap_overlay, norm=norm)
num_subfigs = len(hemi_keys)
# do plotting on each axis if mpl, otherwise together
axes = []
for i, hemi in enumerate(hemi_keys):
verts = surfs[hemi][0]
triangles = surfs[hemi][1]
if sulci[hemi] is not None:
sulc = sulci[hemi]
if isinstance(view, str):
elev, azim = _view(hemi, mode=view, backend=backend)
elif isinstance(view, tuple):
elev, azim = view
else:
raise ValueError("Argument `view` should be a string or tuple.")
# color by sulci
if sulci[hemi] is not None:
triangle_values_sulci = np.array(
[[sulc[nn] for nn in triangles[i]] for i in range(len(triangles))]
).mean(1)
colors_sulci = cmap_sulci_func(triangle_values_sulci)
else:
colors_sulci = np.ones((len(triangles),4))
colors_sulci[:,:3] = 0.5
if backend == "plotly":
colors_sulci *= 255
colors_sulci = colors_sulci.astype("int")
if len(hemi_keys) == 2:
# add some offset between hemispheres
# if plotting both hemispheres on brain, need to offset since
# inflated coords are centered at zero for each hemisphere in x axis
if hemi == "lh":
vert_x_offset = np.array([verts[:, 0].max() + 3, 0, 0])
offset_verts = verts - vert_x_offset
else:
vert_x_offset = np.array([verts[:, 0].min() - 3, 0, 0])
offset_verts = verts - vert_x_offset
else:
vert_x_offset = np.array([0, 0, 0])
offset_verts = verts
mesh = _plotly_trisurf(
offset_verts,
triangles,
facecolor=colors_sulci,
name=f"hemi-{hemi}",
opacity=brain_alpha,
)
trace_list.append(mesh)
else: # mpl
ax = fig.add_subplot(
1, num_subfigs, i + 1, projection="3d", azim=azim, elev=elev
)
ax.set_axis_off()
axes.append(ax)
p3dc = ax.plot_trisurf(
verts[:, 0], verts[:, 1], verts[:, 2], triangles=triangles
)
if sulci[hemi] is not None:
# set the face colors of the Poly3DCollection
colors_sulci[:, -1] = brain_alpha
p3dc.set_fc(colors_sulci)
else:
p3dc.set_alpha(brain_alpha)
if elecs is None:
continue
# snap elecs to pial surface
if snap_to_surface:
if hemi == "lh":
map_surf = brain.lh.surf_pial
else:
map_surf = brain.rh.surf_pial
nearest_vert_indices, _ = find_closest_vertices(map_surf[0], elecs)
if backend == "plotly":
coords = offset_verts[nearest_vert_indices]
else:
coords = surfs[hemi][0][nearest_vert_indices]
else:
if backend == "plotly":
coords = elecs - vert_x_offset
else:
coords = elecs
# if no elec_values specified, use given colors
if elec_values is not None:
elec_colors = cmap_func(elec_values)
elif colors is None:
elec_colors = np.zeros((len(coords), 4))
elec_colors[:, -1] = elec_alpha
elif isinstance(colors, str):
if isinstance(elec_alpha, (float, int)):
elec_colors = np.asarray(
[mpl.colors.to_rgba(colors, elec_alpha)] * len(elec_isleft)
)
else:
elec_colors = np.asarray(
[mpl.colors.to_rgba(colors, alph) for alph in elec_alpha]
)
elif isinstance(colors, list):
if isinstance(elec_alpha, (float, int)):
if isinstance(elec_alpha, list) or isinstance(elec_alpha, np.ndarray):
elec_colors = np.asarray(
[mpl.colors.to_rgba(cc, alph) for cc, alph in zip(colors, elec_alpha)]
)
else:
elec_colors = np.asarray(
[mpl.colors.to_rgba(cc, elec_alpha) for cc in colors]
)
else:
elec_colors = np.asarray(
[
mpl.colors.to_rgba(cc, alph)
for cc, alph in zip(colors, elec_alpha)
]
)
elif isinstance(colors, np.ndarray):
elec_colors = colors.copy()
if elec_colors.shape[1] == 4 and elec_alpha is not None:
elec_colors[:, 3] = elec_alpha
else:
raise TypeError(
"no values given, and colors could not be interpreted as either numpy array, single color string, or list of strings"
)
# restrict to only this hemisphere
if hemi == "lh":
coords = coords[elec_isleft]
elec_colors = elec_colors[elec_isleft]
if isinstance(elec_size, (np.ndarray)):
elec_size_hemi = elec_size[elec_isleft]
else:
elec_size_hemi = elec_size
else:
coords = coords[~elec_isleft]
elec_colors = elec_colors[~elec_isleft]
if isinstance(elec_size, (np.ndarray)):
elec_size_hemi = elec_size[~elec_isleft]
else:
elec_size_hemi = elec_size
if backend == "plotly":
elec_colors *= 255
elec_colors = elec_colors.astype("int")
scatter = _plotly_scatter3d(coords, elec_colors, elec_alpha=elec_alpha, name=f"elecs-{hemi}")
trace_list.append(scatter)
else: # mpl
x, y, z = coords.T
ax.scatter(x, y, z, s=elec_size_hemi, c=elec_colors, **kwargs)
if backend == "plotly":
axis = dict(
showbackground=False,
showgrid=False, # thin lines in the background
zeroline=False, # thick line at x=0
visible=False, # numbers below
)
scene = dict(
camera=dict(
eye=elev,
center=azim,
projection=dict(
type="orthographic", # perspective
),
),
xaxis=dict(axis),
yaxis=dict(axis),
zaxis=dict(axis),
aspectratio=dict(x=0.8 * len(hemi_keys), y=1.3, z=1),
)
layout = go.Layout(
title="Brain plot",
width=figsize * 120,
height=figsize * 120,
scene=scene,
)
fig = go.Figure(data=trace_list, layout=layout)
if update_opacity_per_elec:
fig.for_each_trace(_set_opacity)
# change electrode size to custom size if specified
if elec_size is not None:
fig = fig.for_each_trace(
lambda trace: trace.update(marker_size=elec_size)
if "elecs" in trace.name
else (),
)
return fig, None
if elec_values is not None: # mpl
fig.subplots_adjust(right=0.95) # create space on the right hand side
ax_cbar = plt.axes([0.96, 0.55, 0.02, 0.3]) # add a small custom axis
fig.colorbar(mappable=cmap_mappable, cax=ax_cbar)
axes.append((ax_cbar))
return fig, axes