Skip to content

Commit 8835395

Browse files
committed
Fix imports
1 parent 2bca8ef commit 8835395

File tree

1 file changed

+106
-74
lines changed

1 file changed

+106
-74
lines changed

ibllib/atlas/plots.py

Lines changed: 106 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@
77
import numpy as np
88
from scipy.ndimage import gaussian_filter
99
from scipy.stats import binned_statistic
10-
import matplotlib
1110
import matplotlib.pyplot as plt
12-
from matplotlib import cm
11+
from matplotlib import cm, colors
1312
from matplotlib.patches import Polygon, PathPatch
1413
import matplotlib.path as mpath
1514
from iblutil.io.hashfile import md5
@@ -208,20 +207,22 @@ def reorder_data(acronyms, values, brain_regions=None):
208207

209208
def load_slice_files(slice, mapping):
210209
"""
211-
Function to load in set of vectorised atlas slices for a given atlas axis and mapping. If the data does not
212-
exist locally, it will download the files automatically stored in a AWS s3 bucket.
210+
Function to load in set of vectorised atlas slices for a given atlas axis and mapping.
211+
212+
If the data does not exist locally, it will download the files automatically stored in a AWS S3
213+
bucket.
213214
214215
Parameters
215216
----------
216-
slice: {'coronal', 'sagittal', 'horizontal', 'top'}
217-
The axis of the atlas to load
218-
mapping: {'Allen', 'Beryl', 'Cosmos'}
219-
The mapping to load
217+
slice : {'coronal', 'sagittal', 'horizontal', 'top'}
218+
The axis of the atlas to load.
219+
mapping : {'Allen', 'Beryl', 'Cosmos'}
220+
The mapping to load.
220221
221222
Returns
222223
-------
223-
slice_data: json
224-
A json containing the vertices to draw each region for each slice in the Allen annotation volume
224+
slice_data : numpy.array
225+
A json containing the vertices to draw each region for each slice in the Allen annotation volume.
225226
226227
"""
227228
OLD_MD5 = {
@@ -246,43 +247,43 @@ def _plot_slice_vector(coords, slice, values, mapping, empty_color='silver', cle
246247
ba=None, ax=None, slice_json=None, **kwargs):
247248
"""
248249
Function to plot scalar value per allen region on vectorised version of histology slice. Do not use directly but use
249-
through plot_scalar_on_slice function with vector=True
250+
through plot_scalar_on_slice function with vector=True.
250251
251252
Parameters
252253
----------
253254
coords: float
254-
coordinate of slice in um (not needed when slice='top')
255+
Coordinate of slice in um (not needed when slice='top').
255256
slice: {'coronal', 'sagittal', 'horizontal', 'top'}
256-
the axis through the atlas volume to display
257+
The axis through the atlas volume to display.
257258
values: numpy.array
258259
Array of values for each of the lateralised Allen regions found using BrainRegions().acronym. If no
259-
value is assigned to the acronym, the value at corresponding to that index should be nan
260+
value is assigned to the acronym, the value at corresponding to that index should be NaN.
260261
mapping: {'Allen', 'Beryl', 'Cosmos'}
261-
the mapping to use
262+
The mapping to use.
262263
empty_color: str, tuple of int, default='silver'
263-
The color used to fill the regions that do not have any values assigned (regions with nan)
264+
The color used to fill the regions that do not have any values assigned (regions with NaN).
264265
clevels: numpy.array, list or tuple
265-
The min and max values to use for the colormap
266+
The min and max values to use for the colormap.
266267
cmap: string
267-
Colormap to use
268+
Colormap to use.
268269
show_cbar: bool, default=False
269-
Whether or not to display a colorbar
270+
Whether to display a colorbar.
270271
ba : ibllib.atlas.AllenAtlas
271-
A brain atlas object
272+
A brain atlas object.
272273
ax : matplotlib.pyplot.Axes
273274
An axis object to plot onto.
274-
slice_json: json
275-
The set of vectorised slices for this slice, obtained using load_slice_files(slice, mapping)
276-
kwargs
277-
Set of kwargs passed into matplotlib.patches.Polygon
275+
slice_json: numpy.array
276+
The set of vectorised slices for this slice, obtained using load_slice_files(slice, mapping).
277+
**kwargs
278+
Set of kwargs passed into matplotlib.patches.Polygon.
278279
279280
Returns
280281
-------
281282
fig: matplotlib.figure.Figure
282-
The plotted figure
283+
The plotted figure.
283284
ax: matplotlib.pyplot.Axes
284285
The plotted axes.
285-
cbar: matplotlib.pyplot.colorbar
286+
cbar: matplotlib.pyplot.colorbar, optional
286287
matplotlib colorbar object, only returned if show_cbar=True
287288
288289
"""
@@ -303,7 +304,7 @@ def _plot_slice_vector(coords, slice, values, mapping, empty_color='silver', cle
303304
fig = ax.get_figure()
304305

305306
colormap = cm.get_cmap(cmap)
306-
norm = matplotlib.colors.Normalize(vmin=clevels[0], vmax=clevels[1])
307+
norm = colors.Normalize(vmin=clevels[0], vmax=clevels[1])
307308
nan_vals = np.isnan(values)
308309
rgba_color = np.full((values.size, 4), fill_value=np.nan)
309310
rgba_color[~nan_vals] = colormap(norm(values[~nan_vals]), bytes=True)
@@ -354,40 +355,71 @@ def _plot_slice_vector(coords, slice, values, mapping, empty_color='silver', cle
354355
ax.invert_yaxis()
355356

356357
if show_cbar:
357-
cbar = fig.colorbar(matplotlib.cm.ScalarMappable(norm=norm, cmap=cmap), ax=ax)
358+
cbar = fig.colorbar(cm.ScalarMappable(norm=norm, cmap=cmap), ax=ax)
358359
return fig, ax, cbar
359360
else:
360361
return fig, ax
361362

362363

363-
def plot_scalar_on_slice(regions, values, coord=-1000, slice='coronal', mapping='Allen', hemisphere='left',
364+
def plot_scalar_on_slice(regions, values, coord=-1000, slice='coronal', mapping=None, hemisphere='left',
364365
background='image', cmap='viridis', clevels=None, show_cbar=False, empty_color='silver',
365366
brain_atlas=None, ax=None, vector=False, slice_files=None, **kwargs):
366367
"""
367-
Function to plot scalar value per allen region on histology slice
368+
Function to plot scalar value per region on histology slice.
368369
369-
:param regions: array of acronyms of Allen regions
370-
:param values: array of scalar value per acronym. If hemisphere is 'both' and different values want to be shown on each
371-
hemispheres, values should contain 2 columns, 1st column for LH values, 2nd column for RH values
372-
:param coord: coordinate of slice in um (not needed when slice='top')
373-
:param slice: orientation of slice, options are 'coronal', 'sagittal', 'horizontal', 'top' (top view of brain)
374-
:param mapping: atlas mapping to use, options are 'Allen', 'Beryl' or 'Cosmos'
375-
:param hemisphere: hemisphere to display, options are 'left', 'right', 'both'
376-
:param background: background slice to overlay onto, options are 'image' or 'boundary' (only used when vector = False)
377-
:param cmap: colormap to use
378-
:param clevels: min max color levels [cmin, cmax]
379-
:param show_cbar: whether or not to add colorbar to axis
380-
:param empty_color: color to use for regions without any values (only used when vector = True)
381-
:param brain_atlas: AllenAtlas object
382-
:param ax: optional axis object to plot on
383-
:param vector: whether to show as bitmap of vector graphic
384-
:param slice_files: slice files for
385-
:param **kwargs: kwargs to pass to matplotlib polygon e.g linewidth=2, edgecolor='none' (only used when vector = True)
386-
:return:
370+
Parameters
371+
----------
372+
regions : array_like
373+
An array of brain region acronyms.
374+
values : numpy.array
375+
An array of scalar value per acronym. If hemisphere is 'both' and different values want to
376+
be shown on each hemisphere, values should contain 2 columns, 1st column for LH values, 2nd
377+
column for RH values.
378+
coord : float
379+
Coordinate of slice in um (not needed when slice='top').
380+
slice : {'coronal', 'sagittal', 'horizontal', 'top'}, default='coronal'
381+
Orientation of slice.
382+
mapping : str, optional
383+
Atlas mapping to use, options are depend on atlas used (see `ibllib.atlas.BrainRegions`).
384+
If None, the atlas default mapping is used.
385+
hemisphere : {'left', 'right', 'both'}, default='left'
386+
The hemisphere to display.
387+
background : {image', 'boundary'}, default='image'
388+
Background slice to overlay onto, options are 'image' or 'boundary'. If `vector` is false,
389+
this argument is ignored.
390+
cmap: str, default='viridis'
391+
Colormap to use.
392+
clevels : array_like
393+
The min and max color levels to use.
394+
show_cbar: bool, default=False
395+
Whether to display a colorbar.
396+
empty_color : str, default='silver'
397+
Color to use for regions without any values (only used when `vector` is true).
398+
brain_atlas : ibllib.atlas.AllenAtlas
399+
A brain atlas object.
400+
ax : matplotlib.pyplot.Axes
401+
An axis object to plot onto.
402+
vector : bool, default=False
403+
Whether to show as bitmap or vector graphic.
404+
slice_files: numpy.array
405+
The set of vectorised slices for this slice, obtained using `load_slice_files(slice, mapping)`.
406+
**kwargs
407+
Set of kwargs passed into matplotlib.patches.Polygon, e.g. linewidth=2, edgecolor='None'
408+
(only used when vector = True).
409+
410+
Returns
411+
-------
412+
fig: matplotlib.figure.Figure
413+
The plotted figure.
414+
ax: matplotlib.pyplot.Axes
415+
The plotted axes.
416+
cbar: matplotlib.pyplot.colorbar, optional
417+
matplotlib colorbar object, only returned if show_cbar=True.
387418
"""
388419

389420
ba = brain_atlas or AllenAtlas()
390421
br = ba.regions
422+
mapping = mapping or br.default_mapping
391423

392424
if clevels is None:
393425
clevels = (np.nanmin(values), np.nanmax(values))
@@ -489,7 +521,7 @@ def plot_scalar_on_flatmap(regions, values, depth=0, flatmap='dorsal_cortex', ma
489521
d_idx = int(np.round(depth / ba.res_um)) # need to find nearest to 25
490522

491523
if background == 'boundary':
492-
cmap_bound = matplotlib.cm.get_cmap("bone_r").copy()
524+
cmap_bound = cm.get_cmap("bone_r").copy()
493525
cmap_bound.set_under([1, 1, 1], 0)
494526

495527
if ax:
@@ -514,8 +546,8 @@ def plot_scalar_on_flatmap(regions, values, depth=0, flatmap='dorsal_cortex', ma
514546
ax.set_xlim(np.ceil(ba.flatmap.shape[1] / 2), ba.flatmap.shape[1])
515547

516548
if show_cbar:
517-
norm = matplotlib.colors.Normalize(vmin=clevels[0], vmax=clevels[1], clip=False)
518-
cbar = fig.colorbar(matplotlib.cm.ScalarMappable(norm=norm, cmap=cmap), ax=ax)
549+
norm = colors.Normalize(vmin=clevels[0], vmax=clevels[1], clip=False)
550+
cbar = fig.colorbar(cm.ScalarMappable(norm=norm, cmap=cmap), ax=ax)
519551
return fig, ax, cbar
520552
else:
521553
return fig, ax
@@ -533,7 +565,7 @@ def plot_volume_on_slice(volume, coord=-1000, slice='coronal', mapping='Allen',
533565
:param background: background slice to overlay onto, options are 'image' or 'boundary'
534566
:param cmap: colormap to use
535567
:param clevels: min max color levels [cmin, cmax]
536-
:param show_cbar: whether or not to add colorbar to axis
568+
:param show_cbar: whether to add colorbar to axis
537569
:param brain_atlas: AllenAtlas object
538570
:param ax: optional axis object to plot on
539571
:return:
@@ -573,7 +605,7 @@ def plot_points_on_slice(xyz, values=None, coord=-1000, slice='coronal', mapping
573605
:param background: background slice to overlay onto, options are 'image' or 'boundary'
574606
:param cmap: colormap to use
575607
:param clevels: min max color levels [cmin, cmax]
576-
:param show_cbar: whether or not to add colorbar to axis
608+
:param show_cbar: whether to add colorbar to axis
577609
:param aggr: aggregation method. Options are sum, count, mean, std, median, min and max.
578610
Can also give in custom function (https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.binned_statistic.html)
579611
:param fwhm: fwhm distance of gaussian kernel in um
@@ -648,33 +680,34 @@ def compute_volume_from_points(xyz, values=None, aggr='sum', fwhm=100, ba=None):
648680
def _plot_slice(coord, slice, region_values, vol_type, background='boundary', map='Allen', clevels=None, cmap='viridis',
649681
show_cbar=False, ba=None, ax=None):
650682
"""
651-
Function to plot scalar value per allen region on histology slice. Do not use directly but use
652-
through plot_scalar_on_slice function
683+
Function to plot scalar value per allen region on histology slice.
684+
685+
Do not use directly but use through plot_scalar_on_slice function.
653686
654687
Parameters
655688
----------
656689
coord: float
657-
coordinate of slice in um (not needed when slice='top')
690+
coordinate of slice in um (not needed when slice='top').
658691
slice: {'coronal', 'sagittal', 'horizontal', 'top'}
659-
the axis through the atlas volume to display
692+
the axis through the atlas volume to display.
660693
region_values: numpy.array
661694
Array of values for each of the lateralised Allen regions found using BrainRegions().acronym. If no
662-
value is assigned to the acronym, the value at corresponding to that index should be nan
695+
value is assigned to the acronym, the value at corresponding to that index should be nan.
663696
vol_type: 'value'
664-
The type of volume to be displayed, should alwasy be 'value' if values want to be displayed
697+
The type of volume to be displayed, should always be 'value' if values want to be displayed.
665698
background: {'image', 'boundary'}
666699
The background slice to overlay the values onto. When 'image' it uses the Allen dwi image, when
667-
'boundary' it displays the boundaries between regions
700+
'boundary' it displays the boundaries between regions.
668701
map: {'Allen', 'Beryl', 'Cosmos'}
669-
the mapping to use
702+
the mapping to use.
670703
clevels: numpy.array, list or tuple
671-
The min and max values to use for the colormap
672-
cmap: string
673-
Colormap to use
704+
The min and max values to use for the colormap.
705+
cmap: str, default='viridis'
706+
Colormap to use.
674707
show_cbar: bool, default=False
675-
Whether or not to display a colorbar
708+
Whether to display a colorbar.
676709
ba : ibllib.atlas.AllenAtlas
677-
A brain atlas object
710+
A brain atlas object.
678711
ax : matplotlib.pyplot.Axes
679712
An axis object to plot onto.
680713
@@ -685,7 +718,7 @@ def _plot_slice(coord, slice, region_values, vol_type, background='boundary', ma
685718
ax: matplotlib.pyplot.Axes
686719
The plotted axes.
687720
cbar: matplotlib.pyplot.colorbar
688-
matplotlib colorbar object, only returned if show_cbar=True
721+
matplotlib colorbar object, only returned if show_cbar=True.
689722
690723
"""
691724
ba = ba or AllenAtlas()
@@ -739,8 +772,8 @@ def _plot_slice(coord, slice, region_values, vol_type, background='boundary', ma
739772
ba.plot_top(volume='boundary', mapping=map, ax=ax)
740773

741774
if show_cbar:
742-
norm = matplotlib.colors.Normalize(vmin=clevels[0], vmax=clevels[1], clip=False)
743-
cbar = fig.colorbar(matplotlib.cm.ScalarMappable(norm=norm, cmap=cmap), ax=ax)
775+
norm = colors.Normalize(vmin=clevels[0], vmax=clevels[1], clip=False)
776+
cbar = fig.colorbar(cm.ScalarMappable(norm=norm, cmap=cmap), ax=ax)
744777
return fig, ax, cbar
745778
else:
746779
return fig, ax
@@ -760,7 +793,7 @@ def plot_scalar_on_barplot(acronyms, values, errors=None, order=True, ax=None, b
760793
errors: numpy.array
761794
A 1D array of error values corresponding to each acronym in the acronyms array
762795
order: bool, default=True
763-
Whether or not to order the acronyms according to the order defined by the Allen structure tree
796+
Whether to order the acronyms according to the order defined by the Allen structure tree
764797
ax : matplotlib.pyplot.Axes
765798
An axis object to plot onto.
766799
brain_regions : ibllib.atlas.regions.BrainRegions
@@ -860,10 +893,10 @@ def plot_swanson_vector(acronyms=None, values=None, ax=None, hemisphere=None, br
860893

861894
if acronyms is not None:
862895
ibr, vals = br.propagate_down(acronyms, values)
863-
colormap = matplotlib.colormaps.get_cmap(cmap)
896+
colormap = cm.get_cmap(cmap)
864897
vmin = vmin or np.nanmin(vals)
865898
vmax = vmax or np.nanmax(vals)
866-
norm = matplotlib.colors.Normalize(vmin=vmin, vmax=vmax)
899+
norm = colors.Normalize(vmin=vmin, vmax=vmax)
867900
rgba_color = colormap(norm(vals), bytes=True)
868901

869902
if mask is not None:
@@ -1072,7 +1105,6 @@ def plot_swanson(acronyms=None, values=None, ax=None, hemisphere=None, br=None,
10721105
br = BrainRegions() if br is None else br
10731106
br.compute_hierarchy()
10741107
s2a = swanson()
1075-
# both hemishpere
10761108
if hemisphere == 'both':
10771109
_s2a = s2a + np.sum(br.id > 0)
10781110
_s2a[s2a == 0] = 0
@@ -1102,7 +1134,7 @@ def plot_swanson(acronyms=None, values=None, ax=None, hemisphere=None, br=None,
11021134
imb = np.zeros((*s2a.shape[:2], 4), dtype=np.uint8)
11031135
# fill in the empty regions with the blank regions colours if necessary
11041136
if iswan is not None:
1105-
imb[~iswan] = (np.array(matplotlib.colors.to_rgba(empty_color)) * 255).astype('uint8')
1137+
imb[~iswan] = (np.array(colors.to_rgba(empty_color)) * 255).astype('uint8')
11061138
imb[s2a == 0] = 255
11071139
# imb[s2a == 1] = np.array([167, 169, 172, 255])
11081140
imb[s2a == 1] = np.array([0, 0, 0, 255])

0 commit comments

Comments
 (0)