In [55]:
# Imports
import numpy as np
import nilearn as nil
import nibabel as nib
from matplotlib import gridspec
from nilearn import plotting as nlp
from matplotlib import pyplot as plt
import matplotlib.animation as animation
In [56]:
%pylab inline
In [3]:
from tempfile import NamedTemporaryFile
VIDEO_TAG = """<video controls>
<source src="data:video/x-m4v;base64,{0}" type="video/mp4">
Your browser does not support the video tag.
</video>"""
def anim_to_html(anim):
if not hasattr(anim, '_encoded_video'):
with NamedTemporaryFile(suffix='.mp4') as f:
anim.save(f.name, fps=20, extra_args=['-vcodec', 'libx264'])
video = open(f.name, "rb").read()
anim._encoded_video = video.encode("base64")
return VIDEO_TAG.format(anim._encoded_video)
In [7]:
from IPython.display import HTML
def display_animation(anim):
plt.close(anim._fig)
return HTML(anim_to_html(anim))
In [22]:
window = np.arange(0,200-50)
In [23]:
def visu(i, tmp, ax, ay):
ay.plot([window[i], window[i]+50], [2, 2], color='blue', linewidth=5, alpha=0.5)
ax.plot([window[i], window[i]+50], [2, 2], color='red', linewidth=5, alpha=0.5)
In [54]:
fig = plt.figure(figsize=(7,4))
ax = fig.add_subplot(121)
ax.set_xlim([0, 200])
ay = fig.add_subplot(122)
ay.set_xlim([0, 200])
iml = list()
for i in np.arange(50):
a = ay.axvspan(window[i], window[i]+50, color='blue', alpha=0.5)
#b = ax.axvspan(window[i], window[i]+50, color='red', alpha=0.5)
iml.append((a,))
ani = animation.ArtistAnimation(fig, iml, interval=50, blit=True,
repeat_delay=1000)
#ani = animation.FuncAnimation(fig, visu, interval=50, blit=False, fargs=(np.arange(50), ax, ay))
display_animation(ani)
Out[54]:
In [16]:
ani.save('dynamic_images.mp4')
In [39]:
a, = ('hello', 'huhu')
In [57]:
# Paths
mask_path = '/data1/abide/Mask/mask_data_specific.nii.gz'
prior_path = '/data1/cambridge/template/template_cambridge_basc_multiscale_sym_scale012.nii.gz'
pheno_path = '/data1/abide/Pheno/merged_pheno.csv'
sub_path = '/data1/abide/Full/abide_release_sym_gsc0_lp01/Stanford/fmri_0051198_session_1_run1.nii.gz'
In [59]:
# Get the subject
s_img = nib.load(sub_path)
In [64]:
fig = plt.figure(figsize=(7,4))
ax = fig.add_subplot(121)
ax.set_xlim([0, 200])
ay = fig.add_subplot(122)
iml = list()
for i in np.arange(50):
a = ax.axvspan(window[i], window[i]+50, color='blue', alpha=0.5)
b = nlp.plot_stat_map(nil.image.index_img(s_img, i), axes=ax)
iml.append((a,b))
ani = animation.ArtistAniTruemation(fig, iml, interval=50, blit=False,
repeat_delay=1000)
#ani = animation.FuncAnimation(fig, visu, interval=50, blit=False, fargs=(np.arange(50), ax, ay))
display_animation(ani)
In [72]:
fig = plt.figure(figsize=(7,4))
ax = fig.add_subplot(121)
ax.set_xlim([0, 200])
ax.axvspan(window[i], window[i]+50, color='blue', alpha=0.5)
ay = fig.add_subplot(122)
nlp.plot_stat_map(nil.image.index_img(s_img, i), axes=ay, colorbar=False, display_mode='x', cut_coords=(0,))
Out[72]:
In [76]:
def show_things(i, window, ax, ay):
ax.axvspan(window[i], window[i]+50, color='blue', alpha=0.5)
nlp.plot_stat_map(nil.image.index_img(s_img, i), axes=ay, colorbar=False, display_mode='x', cut_coords=(0,))
In [77]:
fig = plt.figure(figsize=(7,4))
ax = fig.add_subplot(121)
ax.set_xlim([0, 200])
ay = fig.add_subplot(122)
ani = animation.FuncAnimation(fig, show_things, interval=10, blit=False, fargs=(window, ax, ay))
display_animation(ani)
Out[77]:
In [ ]: