In [1]:
%matplotlib inline
import numpy as np
import nibabel as nib
import matplotlib.pyplot as plt
from matplotlib import colors
import nilearn
import nilearn.plotting
from __future__ import print_function,division
In [2]:
#copied from Richard's notebook
def rebuild_nii(num):
data = np.load('Mean_Vec.npy')
a = data[:,num].copy()
nim = nib.load('cc400_roi_atlas.nii')
imdat=nim.get_data()
imdat_new = imdat.copy()
for n, i in enumerate(np.unique(imdat)):
if i != 0:
imdat_new[imdat == i] = a[n-1] * 100000 # scaling factor. Could also try to get float values in nifti...
nim_out = nib.Nifti1Image(imdat_new, nim.get_affine(), nim.get_header())
nim_out.set_data_dtype('float32')
# to save:
nim_out.to_filename('Gradient_'+ str(num) +'_res.nii')
nilearn.plotting.plot_epi(nim_out)
return(nim_out)
In [3]:
for i in range(10):
nims = rebuild_nii(i)
In [ ]:
actual brain visualisation widget
In [21]:
from ipywidgets import widgets
from ipywidgets import interact,fixed
from IPython.display import display
In [5]:
def rebuild_nii_compute(data,nim,num):
"""
precompute to make widget faster
"""
#data = np.load('Mean_Vec.npy')
a = data[:,num].copy()
#nim = nib.load('cc400_roi_atlas.nii')
imdat=nim.get_data()
imdat_new = imdat.copy()
for n, i in enumerate(np.unique(imdat)):
if i != 0:
imdat_new[imdat == i] = a[n-1] * 100000 # scaling factor. Could also try to get float values in nifti...
nim_out = nib.Nifti1Image(imdat_new, nim.get_affine(), nim.get_header())
nim_out.set_data_dtype('float32')
# to save:
#nim_out.to_filename('Gradient_'+ str(num) +'_res.nii')
#nilearn.plotting.plot_epi(nim_out)
return nim_out,imdat_new
def rebuild_nii_plot(num,nims,cutc_x,cutc_y,cutc_z):
"""
simple plotting function for widget
"""
cut_coords = (cutc_x,cutc_y,cutc_z)
fig = nilearn.plotting.plot_epi(nims[num],cut_coords=cut_coords)
plt.show()
def rebuild_nii_plot_new(num,imdats_new,cutc_x,cutc_y,cutc_z,cmap,figsize):
"""
maybe using matplotlib is faster
"""
cut_coords = (cutc_x,cutc_y,cutc_z)
fig,axes=plt.subplots(1,3,figsize=figsize)
imdat = imdats_new[num]
axes[0].imshow(imdat[:,cutc_y,:].T,cmap=cmap,origin='lower')
axes[0].set_title('y')
axes[1].imshow(imdat[cutc_x,:,:].T,cmap=cmap,origin='lower')
axes[1].set_title('x')
axes[2].imshow(imdat[:,:,cutc_z].T,cmap=cmap,origin='lower')
axes[2].set_title('z')
plt.show()
In [6]:
#prepare data
data = np.load('Mean_Vec.npy')
nim = nib.load('cc400_roi_atlas.nii')
imdat=nim.get_data()
print(data.shape,imdat.shape)
In [7]:
#precompute to speed up widget
nims = []
imdats = []
for j in range(data.shape[1]):
nim_new,imdat_new = rebuild_nii_compute(data,nim,j)
nims.append(nim_new)
imdats.append(imdat_new)
In [8]:
#testing plotting function
nilearn.plotting.plot_epi(nims[0]) #automatic choice of cut_coords
nilearn.plotting.plot_epi(nims[0],cut_coords=(58,-15,-20)) #manual choice
Out[8]:
In [9]:
#build widget
coords_max = 60
i = interact(rebuild_nii_plot,
num=(0,9),
nims=fixed(nims),
cutc_x=(-coords_max,coords_max),
cutc_y=(-coords_max,coords_max),
cutc_z=(-coords_max,coords_max),
)
In [10]:
coords_max = imdat.shape
figsize=(15,10)
i = interact(rebuild_nii_plot_new,
num=(0,9),
imdats_new=fixed(imdats),
cutc_x=(0,coords_max[0]-1),
cutc_y=(0,coords_max[1]-1),
cutc_z=(0,coords_max[2]-1),
cmap = ['jet','viridis','gray'],
figsize=fixed(figsize)
)
dataset from https://github.com/datacarpentry/python-neuroimaging-lesson/tree/master/neuroimaging/data
In [11]:
epi_img = nib.load('someones_epi.nii.gz')
epi_data = epi_img.get_data()
In [12]:
def plot_projection(data,axis,pos0,pos1,pos2,cmap,figsize):
if axis=='x':
imdat = data[pos0,:,:]
elif axis=='y':
imdat = data[:,pos1,:]
elif axis=='z':
imdat = data[:,:,pos2]
fig,ax = plt.subplots(figsize=figsize)
ax.imshow(imdat,cmap=cmap, origin="lower")
plt.show()
In [13]:
#some settings for widget
data = epi_data
data.shape
figsize=(6,6)
In [14]:
i = interact(plot_projection,
data=fixed(data),
axis=['x','y','z'],
pos0=(0,data.shape[0]-1),
pos1=(0,data.shape[1]-1),
pos2=(0,data.shape[2]-1),
cmap = ['gray','jet','viridis'],
figsize=fixed(figsize))
In [15]:
def show_slices_grid(slices,gridsize):
""" Function to display row of image slices """
fig, axes = plt.subplots(gridsize, len(slices)//gridsize+1,figsize=(12,12))
for i, slice in enumerate(slices):
row = i//gridsize
column = i%gridsize
axes[row][column].imshow(slice.T, cmap="gray", origin="lower")
fig.tight_layout()
slices = [epi_data[:,:,i] for i in range(epi_data.shape[2])]
show_slices_grid(slices,6)
In [ ]:
In [16]:
#from nilearn examples
localizer_dataset = nilearn.datasets.fetch_localizer_button_task()
localizer_tmap_filename = localizer_dataset.tmaps[0]
In [17]:
def plot_glass_threshold(fname,threshold,display_mode):
nilearn.plotting.plot_glass_brain(fname, threshold=threshold, display_mode=display_mode)
plt.show()
In [22]:
widget_glass = interact(plot_glass_threshold,
fname=fixed(localizer_tmap_filename),
threshold=(0,5),
display_mode=['ortho','xz'])
display(widget_glass)
In [23]:
%%html
<script src="https://npmcdn.com/jupyter-js-widgets@~1.2.0/dist/embed.js"></script><script type="application/vnd.jupyter-embedded-widgets">{
"c5ceb95d056c49758d1ea9be16b25364": {
"model_name": "LayoutModel",
"model_module": "jupyter-js-widgets",
"state": {},
"views": []
},
"f82e57ae90354086a2ce4f7c7fcd6da7": {
"model_name": "BoxModel",
"model_module": "jupyter-js-widgets",
"state": {
"children": [
"IPY_MODEL_035ccb3d8c47496c9c04721b9bdd6808",
"IPY_MODEL_4941cffe81cd407695b2a543413e9069",
"IPY_MODEL_0091b4c3a150409abb5b2469d949df87",
"IPY_MODEL_7ab198b46daf4a998e6ba5c6bd17c54f"
],
"layout": "IPY_MODEL_c5ceb95d056c49758d1ea9be16b25364",
"_dom_classes": [
"widget-interact"
]
},
"views": [
{
"cell_index": 12
}
]
},
"ad42da5418184595a70eeb6be09b1ca5": {
"model_name": "LayoutModel",
"model_module": "jupyter-js-widgets",
"state": {},
"views": []
},
"035ccb3d8c47496c9c04721b9bdd6808": {
"model_name": "IntSliderModel",
"model_module": "jupyter-js-widgets",
"state": {
"layout": "IPY_MODEL_ad42da5418184595a70eeb6be09b1ca5",
"description": "num",
"max": 9,
"value": 4
},
"views": []
},
"7d1203c93f32490ea00b485ee898c13e": {
"model_name": "LayoutModel",
"model_module": "jupyter-js-widgets",
"state": {},
"views": []
},
"4941cffe81cd407695b2a543413e9069": {
"model_name": "IntSliderModel",
"model_module": "jupyter-js-widgets",
"state": {
"layout": "IPY_MODEL_7d1203c93f32490ea00b485ee898c13e",
"min": -60,
"description": "cutc_x",
"max": 60
},
"views": []
},
"8b3d0c998552429ba96af1e82051085e": {
"model_name": "LayoutModel",
"model_module": "jupyter-js-widgets",
"state": {},
"views": []
},
"0091b4c3a150409abb5b2469d949df87": {
"model_name": "IntSliderModel",
"model_module": "jupyter-js-widgets",
"state": {
"layout": "IPY_MODEL_8b3d0c998552429ba96af1e82051085e",
"min": -60,
"description": "cutc_y",
"max": 60
},
"views": []
},
"6405e4ba51ab411c892030a25c8e013b": {
"model_name": "LayoutModel",
"model_module": "jupyter-js-widgets",
"state": {},
"views": []
},
"7ab198b46daf4a998e6ba5c6bd17c54f": {
"model_name": "IntSliderModel",
"model_module": "jupyter-js-widgets",
"state": {
"layout": "IPY_MODEL_6405e4ba51ab411c892030a25c8e013b",
"min": -60,
"description": "cutc_z",
"max": 60
},
"views": []
},
"9090c26c1dbb4d3abdf769b434e1bf5f": {
"model_name": "LayoutModel",
"model_module": "jupyter-js-widgets",
"state": {},
"views": []
},
"622ae7db273f48bda444bcf0a815e3dc": {
"model_name": "BoxModel",
"model_module": "jupyter-js-widgets",
"state": {
"children": [
"IPY_MODEL_774200a559f143acbd4c245a7d7d3cdc",
"IPY_MODEL_f81dbe2115324c1885b19048c9fe7258",
"IPY_MODEL_54d773c8fde34957a8c8ec732215cc51",
"IPY_MODEL_6a3f756de009491a97416f66decd2847",
"IPY_MODEL_05a8dfbff68d460dabb4432145477371"
],
"layout": "IPY_MODEL_9090c26c1dbb4d3abdf769b434e1bf5f",
"_dom_classes": [
"widget-interact"
]
},
"views": [
{
"cell_index": 13
}
]
},
"3cc2dcfe2bd742bc9e9d61bcbcca36a1": {
"model_name": "LayoutModel",
"model_module": "jupyter-js-widgets",
"state": {},
"views": []
},
"774200a559f143acbd4c245a7d7d3cdc": {
"model_name": "IntSliderModel",
"model_module": "jupyter-js-widgets",
"state": {
"layout": "IPY_MODEL_3cc2dcfe2bd742bc9e9d61bcbcca36a1",
"description": "num",
"max": 9,
"value": 4
},
"views": []
},
"638bc2fdafee43348e7087327ab25423": {
"model_name": "LayoutModel",
"model_module": "jupyter-js-widgets",
"state": {},
"views": []
},
"f81dbe2115324c1885b19048c9fe7258": {
"model_name": "IntSliderModel",
"model_module": "jupyter-js-widgets",
"state": {
"layout": "IPY_MODEL_638bc2fdafee43348e7087327ab25423",
"description": "cutc_x",
"max": 62,
"value": 31
},
"views": []
},
"e20d17520edb4ec2a274b4c21298fb5f": {
"model_name": "LayoutModel",
"model_module": "jupyter-js-widgets",
"state": {},
"views": []
},
"54d773c8fde34957a8c8ec732215cc51": {
"model_name": "IntSliderModel",
"model_module": "jupyter-js-widgets",
"state": {
"layout": "IPY_MODEL_e20d17520edb4ec2a274b4c21298fb5f",
"description": "cutc_y",
"max": 74,
"value": 58
},
"views": []
},
"0f67135234fe482a8f9b474ed07cacfb": {
"model_name": "LayoutModel",
"model_module": "jupyter-js-widgets",
"state": {},
"views": []
},
"6a3f756de009491a97416f66decd2847": {
"model_name": "IntSliderModel",
"model_module": "jupyter-js-widgets",
"state": {
"layout": "IPY_MODEL_0f67135234fe482a8f9b474ed07cacfb",
"description": "cutc_z",
"max": 60,
"value": 30
},
"views": []
},
"42e7f2bddb724ef69f6f36762567aced": {
"model_name": "LayoutModel",
"model_module": "jupyter-js-widgets",
"state": {},
"views": []
},
"05a8dfbff68d460dabb4432145477371": {
"model_name": "DropdownModel",
"model_module": "jupyter-js-widgets",
"state": {
"_options_labels": [
"jet",
"viridis",
"gray"
],
"layout": "IPY_MODEL_42e7f2bddb724ef69f6f36762567aced",
"description": "cmap",
"value": "jet"
},
"views": []
},
"4e6f7de4f3114143ba8a4e0a521c140b": {
"model_name": "LayoutModel",
"model_module": "jupyter-js-widgets",
"state": {},
"views": []
},
"00f33e2f2656426f9dd579ba0647eaa0": {
"model_name": "BoxModel",
"model_module": "jupyter-js-widgets",
"state": {
"children": [
"IPY_MODEL_79e8eb0e98404475a1c91ba03617902a",
"IPY_MODEL_b451d69ac90249f3941d82ea5834b000",
"IPY_MODEL_9121523756c94de29b2b1aa5ed1aa987",
"IPY_MODEL_011cb272ca0b4b5787ec75f7c1c1842a",
"IPY_MODEL_d4696498b6294066ab2743d5c4f9b68b"
],
"layout": "IPY_MODEL_4e6f7de4f3114143ba8a4e0a521c140b",
"_dom_classes": [
"widget-interact"
]
},
"views": [
{
"cell_index": 18
}
]
},
"8ebe200f40974e68bd2d74b21467c79c": {
"model_name": "LayoutModel",
"model_module": "jupyter-js-widgets",
"state": {},
"views": []
},
"79e8eb0e98404475a1c91ba03617902a": {
"model_name": "DropdownModel",
"model_module": "jupyter-js-widgets",
"state": {
"_options_labels": [
"x",
"y",
"z"
],
"layout": "IPY_MODEL_8ebe200f40974e68bd2d74b21467c79c",
"description": "axis",
"value": "z"
},
"views": []
},
"b9f19962b7d344e4b8fdeaf13f668765": {
"model_name": "LayoutModel",
"model_module": "jupyter-js-widgets",
"state": {},
"views": []
},
"b451d69ac90249f3941d82ea5834b000": {
"model_name": "IntSliderModel",
"model_module": "jupyter-js-widgets",
"state": {
"layout": "IPY_MODEL_b9f19962b7d344e4b8fdeaf13f668765",
"description": "pos0",
"max": 52,
"value": 26
},
"views": []
},
"5792afd05f664f4e82f9b29176c2fd29": {
"model_name": "LayoutModel",
"model_module": "jupyter-js-widgets",
"state": {},
"views": []
},
"9121523756c94de29b2b1aa5ed1aa987": {
"model_name": "IntSliderModel",
"model_module": "jupyter-js-widgets",
"state": {
"layout": "IPY_MODEL_5792afd05f664f4e82f9b29176c2fd29",
"description": "pos1",
"max": 60,
"value": 30
},
"views": []
},
"06b42a73c9cd4664b2e0b0b62d353032": {
"model_name": "LayoutModel",
"model_module": "jupyter-js-widgets",
"state": {},
"views": []
},
"011cb272ca0b4b5787ec75f7c1c1842a": {
"model_name": "IntSliderModel",
"model_module": "jupyter-js-widgets",
"state": {
"layout": "IPY_MODEL_06b42a73c9cd4664b2e0b0b62d353032",
"description": "pos2",
"max": 32,
"value": 27
},
"views": []
},
"7381fe00643449c7b18b23ad5c595230": {
"model_name": "LayoutModel",
"model_module": "jupyter-js-widgets",
"state": {},
"views": []
},
"d4696498b6294066ab2743d5c4f9b68b": {
"model_name": "DropdownModel",
"model_module": "jupyter-js-widgets",
"state": {
"_options_labels": [
"gray",
"jet",
"viridis"
],
"layout": "IPY_MODEL_7381fe00643449c7b18b23ad5c595230",
"description": "cmap",
"value": "gray"
},
"views": []
},
"c4e4fd61cb78444b8405d5107e6a3ad6": {
"model_name": "LayoutModel",
"model_module": "jupyter-js-widgets",
"state": {},
"views": []
},
"8e71b225afaa46f2b083d47cfa486241": {
"model_name": "BoxModel",
"model_module": "jupyter-js-widgets",
"state": {
"children": [
"IPY_MODEL_96a8985d6f7f4dbb83da4d668bff874f",
"IPY_MODEL_5877d83a07984ada89699fe528531962"
],
"layout": "IPY_MODEL_c4e4fd61cb78444b8405d5107e6a3ad6",
"_dom_classes": [
"widget-interact"
]
},
"views": []
},
"a6ee993971ad464db78c7c021685d973": {
"model_name": "LayoutModel",
"model_module": "jupyter-js-widgets",
"state": {},
"views": []
},
"96a8985d6f7f4dbb83da4d668bff874f": {
"model_name": "IntSliderModel",
"model_module": "jupyter-js-widgets",
"state": {
"layout": "IPY_MODEL_a6ee993971ad464db78c7c021685d973",
"description": "threshold",
"max": 5,
"value": 3
},
"views": []
},
"0ddb19094c8444379ae4b4c52d19d282": {
"model_name": "LayoutModel",
"model_module": "jupyter-js-widgets",
"state": {},
"views": []
},
"5877d83a07984ada89699fe528531962": {
"model_name": "DropdownModel",
"model_module": "jupyter-js-widgets",
"state": {
"_options_labels": [
"ortho",
"xz"
],
"layout": "IPY_MODEL_0ddb19094c8444379ae4b4c52d19d282",
"description": "display_mode",
"value": "xz"
},
"views": []
},
"9e75c166c3c5457fb827ca8abc8cc10a": {
"model_name": "LayoutModel",
"model_module": "jupyter-js-widgets",
"state": {},
"views": []
},
"cc11fe5c04fd40198f719b9b00d375b5": {
"model_name": "BoxModel",
"model_module": "jupyter-js-widgets",
"state": {
"children": [
"IPY_MODEL_24597bb8755445e196d7e5c221fde35b",
"IPY_MODEL_d04274ec459b41cc83235fa341a50126"
],
"layout": "IPY_MODEL_9e75c166c3c5457fb827ca8abc8cc10a",
"_dom_classes": [
"widget-interact"
]
},
"views": []
},
"148e966428984ec99b94c2e9e3ce80d0": {
"model_name": "LayoutModel",
"model_module": "jupyter-js-widgets",
"state": {},
"views": []
},
"24597bb8755445e196d7e5c221fde35b": {
"model_name": "IntSliderModel",
"model_module": "jupyter-js-widgets",
"state": {
"layout": "IPY_MODEL_148e966428984ec99b94c2e9e3ce80d0",
"description": "threshold",
"max": 5,
"value": 2
},
"views": []
},
"4003aff12fa54ce7900bb427436c987c": {
"model_name": "LayoutModel",
"model_module": "jupyter-js-widgets",
"state": {},
"views": []
},
"d04274ec459b41cc83235fa341a50126": {
"model_name": "DropdownModel",
"model_module": "jupyter-js-widgets",
"state": {
"_options_labels": [
"ortho",
"xz"
],
"layout": "IPY_MODEL_4003aff12fa54ce7900bb427436c987c",
"description": "display_mode",
"value": "ortho"
},
"views": []
},
"fac7d1c40e7f4b8fba373862dbd1cf38": {
"model_name": "LayoutModel",
"model_module": "jupyter-js-widgets",
"state": {},
"views": []
},
"a7fd165c4c9e40f1ba4de58ec6f4bf05": {
"model_name": "BoxModel",
"model_module": "jupyter-js-widgets",
"state": {
"children": [
"IPY_MODEL_72cc29026e4844f5a06fe6f549e9a534",
"IPY_MODEL_45185c3bc2344f00b9291ca00a2d72c3"
],
"layout": "IPY_MODEL_fac7d1c40e7f4b8fba373862dbd1cf38",
"_dom_classes": [
"widget-interact"
]
},
"views": [
{
"cell_index": 25
}
]
},
"8bef201fc5bf42b9ba1801bcea2bdb33": {
"model_name": "LayoutModel",
"model_module": "jupyter-js-widgets",
"state": {},
"views": []
},
"72cc29026e4844f5a06fe6f549e9a534": {
"model_name": "IntSliderModel",
"model_module": "jupyter-js-widgets",
"state": {
"layout": "IPY_MODEL_8bef201fc5bf42b9ba1801bcea2bdb33",
"description": "threshold",
"max": 5,
"value": 1
},
"views": []
},
"d6c126cc60e14f48b929328ec5e631e8": {
"model_name": "LayoutModel",
"model_module": "jupyter-js-widgets",
"state": {},
"views": []
},
"45185c3bc2344f00b9291ca00a2d72c3": {
"model_name": "DropdownModel",
"model_module": "jupyter-js-widgets",
"state": {
"_options_labels": [
"ortho",
"xz"
],
"layout": "IPY_MODEL_d6c126cc60e14f48b929328ec5e631e8",
"description": "display_mode",
"value": "ortho"
},
"views": []
}
}</script>
In [ ]: