Skip to content

Instantly share code, notes, and snippets.

@cosacog
Last active January 26, 2016 16:08
Show Gist options
  • Save cosacog/b0adf0e8dea9f0ec37ac to your computer and use it in GitHub Desktop.
Save cosacog/b0adf0e8dea9f0ec37ac to your computer and use it in GitHub Desktop.
mne pythonで作ったcon (connectivityのデータ)とlabelからconnectivityの図の動画を作るfunction
#!/usr/bin/env python
# _*_ coding: utf-8 -*-
def make_movie_circle_plot(con,n_seed_label,labels, times, con_method, vmin, vmax):
"""
mne pythonで作ったcon(connectivity)のデータから時系列でfigureを作る
# all-to-allかseed basedのデータにするか
#n_seed_label = 9 # number or None: e.g. labels[9].name=entorhinal-rh
#n_seed_label = None
"""
import numpy as np
if n_seed_label is None:
# pattern 1: all to all
con_res = con # 特に意味なし。チュートリアルではmethodを2つ計算しており、それを格納するのに使っている
sTitle = 'All-to-All Connectivity '
#vmin,vmax=0.9,1.0
movie_name_part = 'all2all_'
else:
## pattern 2: 1 to all
con_res = np.zeros(shape=con.shape)
con_res[n_seed_label,:,0,:]=con[n_seed_label,:,0,:]
con_res[:,n_seed_label,0,:]=con[:,n_seed_label,0,:]
sTitle = 'seed-to-All Connectivity '+ labels[n_seed_label].name+ ' '
#vmin,vmax=0.5,1.0
movie_name_part = labels[n_seed_label].name+'_'
label_names = [label.name for label in labels]
label_colors = [label.color for label in labels]
lh_labels = [name for name in label_names if name.endswith('lh')]
# Get the y-location of the label
label_ypos = list()
for name in lh_labels:
idx = label_names.index(name)
ypos = np.mean(labels[idx].pos[:, 1])
label_ypos.append(ypos)
# Reorder the labels based on their location
lh_labels = [label for (ypos, label) in sorted(zip(label_ypos, lh_labels))]
# For the right hemi
rh_labels = [label[:-2] + 'rh' for label in lh_labels]
# Save the plot order and create a circular layout
node_order = list()
node_order.extend(lh_labels[::-1]) # reverse the order
node_order.extend(rh_labels)
from mne.viz import circular_layout, plot_connectivity_circle
node_angles = circular_layout(label_names, node_order, start_pos=90,
group_boundaries=[0, len(label_names) / 2])
# Plot the graph using node colors from the FreeSurfer parcellation. We only
# show the 300 strongest connections.
import matplotlib.pyplot as plt
#times_title = evoked.times*1000
times_title = times*1000
import datetime
png_dir = 'png'+ datetime.datetime.now().strftime('%y%m%d%H%M%S')
## movieのためのpngをたくさん作る
import os
os.mkdir(png_dir)
os.chdir(png_dir)
# plot
for ii in np.arange(len(times_title)):
print(ii)
fig=plt.figure(num=None, figsize=(8,8),facecolor='black')
plot_connectivity_circle(con_res[:,:,0,ii], label_names,
n_lines=300,
node_angles=node_angles, node_colors=label_colors,
title= sTitle + con_method +' '+ str(times_title[ii]),
vmin = vmin, vmax =vmax,
fig=fig)
plt.savefig('%s%04d%s' %('connectivity_',ii,'.png'), facecolor='black')
fig.clf()
plt.close()
## ffmpeg: png->mp4
#import os
movie_fname = 'movie_'+ movie_name_part +fname+'_'+ str(event_time)+'_'+str(fmin)+'_'+str(fmax)+'Hz.mp4'
os.chdir('../')
os.system('ffmpeg -framerate 10 -i png/connectivity_%04d.png '+ movie_fname) #https://goo.gl/GW6LXi
os.system('rm -rf '+png_dir)
# subprocess.call('ffmpeg', shell=True) #is also possible
@cosacog
Copy link
Author

cosacog commented Dec 19, 2015

Readmeの代わり

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment