def test_infomax(filename='./data/01-001.fmri.rest.1.volreg.band.pca_reduced.nii.gz',filename_mask='./data/01-001.fmri.rest.1.volreg.cortical_mask.nii.gz'): from Image import Image from sklearn.decomposition import FastICA import scipy.io as sio img = Image(img_filename=filename, mask_filename=filename_mask, fwhm=6.0) xmat = img.image_mat_in_mask_normalised.T from InfoMax import InfoMax infomax = InfoMax(30).fit(xmat) img.write_decomposition(maps=infomax.components_, filename='test_ica_infomax.nii.gz', normalise=True)
def test_ae( filename='/Volumes/VAULT/YOAD/all/01-001.fmri.rest.1.volreg.band.nii.gz', filename_mask='/Volumes/VAULT/YOAD/all/registrations/01-001.fmri.rest.1.volreg.cortical_mask.nii.gz' ): img = Image(img_filename=filename, mask_filename=filename_mask) xmat = img.image_mat_in_mask_normalised.T ae = Autoencoder(30, max_iter=200, second_nonlinear=True, bias=True).fit(xmat) img.write_decomposition(maps=ae.components_, filename='test_ae.nii.gz', normalise=False)
def test_infomax( filename='./data/01-001.fmri.rest.1.volreg.band.pca_reduced.nii.gz', filename_mask='./data/01-001.fmri.rest.1.volreg.cortical_mask.nii.gz'): from Image import Image from sklearn.decomposition import FastICA import scipy.io as sio img = Image(img_filename=filename, mask_filename=filename_mask, fwhm=6.0) xmat = img.image_mat_in_mask_normalised.T from InfoMax import InfoMax infomax = InfoMax(30).fit(xmat) img.write_decomposition(maps=infomax.components_, filename='test_ica_infomax.nii.gz', normalise=True)
def test_rica(filename='./data/01-001.fmri.rest.1.volreg.nii.gz', filename_mask='./data/01-001.fmri.rest.1.volreg.cortical_mask.nii.gz'): from Image import Image import scipy.io as sio img = Image(img_filename=filename, mask_filename=filename_mask) xmat = img.image_mat_in_mask_normalised.T from sklearn.decomposition import PCA, FastICA pca = PCA(30) xmat_red = pca.fit_transform(xmat.T) import matplotlib.pyplot as plt plt.plot(pca.components_.T) plt.show() import pdb; pdb.set_trace() rica = RICA(30).fit(xmat) img.write_decomposition(maps=rica.components_, filename='test_rica.nii.gz', normalise=False)
def test_rica( filename='./data/01-001.fmri.rest.1.volreg.nii.gz', filename_mask='./data/01-001.fmri.rest.1.volreg.cortical_mask.nii.gz'): from Image import Image import scipy.io as sio img = Image(img_filename=filename, mask_filename=filename_mask) xmat = img.image_mat_in_mask_normalised.T from sklearn.decomposition import PCA, FastICA pca = PCA(30) xmat_red = pca.fit_transform(xmat.T) import matplotlib.pyplot as plt plt.plot(pca.components_.T) plt.show() import pdb pdb.set_trace() rica = RICA(30).fit(xmat) img.write_decomposition(maps=rica.components_, filename='test_rica.nii.gz', normalise=False)
parser = argparse.ArgumentParser(description='matrix decomposition') parser.add_argument('-gae', metavar='gae', type=str, required=True) parser.add_argument('-gimg', metavar='gimg', type=str, required=True) parser.add_argument('-gmask', metavar='gmask', type=str, required=True) parser.add_argument('-reg', metavar='reg', type=str, nargs='+', required=True) parser.add_argument('-img', metavar='img', type=str, nargs='+', required=True) parser.add_argument('-mask', metavar='mask', type=str, nargs='+', required=True) parser.add_argument('-thr', metavar='thr', type=float, default=0.0) parser.add_argument('-s', metavar='s', type=int, default=None) args = parser.parse_args() # first compute individual autoencoders and move them into their group space representation for i,d in enumerate(args.reg): img = Image(img_filename=args.img[i], mask_filename=args.mask[i]) in_nii__2__mat = args.img[i] in_mask = args.mask[i] out_nii__2__mat = "{prefix}.mat".format(prefix=in_nii__2__mat[:-7]) out_autoencoder = "{prefix}.autoencoder.mat".format(prefix=in_nii__2__mat[:-7]) out_autoencoder_nifti = "{prefix}.autoencoder.nii.gz".format(prefix=in_nii__2__mat[:-7]) out_autoencoder_nifti_group = "{prefix}.autoencoder.group.nii.gz".format(prefix=in_nii__2__mat[:-7]) out_autoencoder_mat_group = "{prefix}.autoencoder.group.mat".format(prefix=in_nii__2__mat[:-7]) if not isfile(out_nii__2__mat): cmd = "nii__2__mat.py -img {img} -mask {mask} -mat {mat} -normalise".format(img=in_nii__2__mat, mat=out_nii__2__mat, mask=in_mask) print cmd call(cmd, shell=True) if not isfile(out_autoencoder): cmd = "ae.py -mat {mat} -c 200".format(mat=out_nii__2__mat) print cmd
def fmri(fmri_collection, t1_collection=None, seg_collections=None, atlas_collections=None, directory_reg=None, freq_band="0.01 0.1"): if directory_reg: call("mkdir -p {directory_reg}".format(directory_reg=directory_reg), shell=True) for index, fmri_scan in enumerate(fmri_collection): id_start = fmri_scan.rfind('/') + 1 id_end = fmri_scan.find('.') directory = fmri_scan[0:id_start] identifier = fmri_scan[id_start:id_end] # distortion correction if still required - deprecated VOLUMES = check_output('3dinfo -nv {scan}'.format(scan=fmri_scan), shell=True).rstrip() TR = check_output('3dinfo -tr {scan}'.format(scan=fmri_scan), shell=True).rstrip() print "scan: {scan} TR={TR} VOLUMES={VOLUMES}".format(scan=identifier, TR=TR, VOLUMES=VOLUMES) ########################################################################################################## ################################# FILE SETUP ############################################################# ########################################################################################################## ### 3ddespike in_3ddespike = fmri_scan out_3ddespike = "{directory}{fmri_id}.fmri.despike.nii.gz".format( fmri_id=identifier, directory=directory) ### 3dvolreg in_3dvolreg = out_3ddespike out_3dvolreg = "{prefix}.volreg.nii.gz".format(prefix=in_3dvolreg[:-7]) out_3dvolreg_txt = "{directory}{fmri_id}.motion.1D".format( fmri_id=identifier, directory=directory) out_motion = "{directory}{fmri_id}.motion.demean.1D".format( fmri_id=identifier, directory=directory) out_motion_derv = "{directory}{fmri_id}.motion.demean.derivative.1D".format( fmri_id=identifier, directory=directory) ### 3dvol to extract 4th volume in_3dvol = out_3dvolreg out_3dvol = "{prefix}.vol4.nii.gz".format(prefix=in_3dvol[:-7]) ### fmri brain mask in_3dAutomask = out_3dvolreg out_3dAutomask = "{prefix}.mask.nii.gz".format(prefix=out_3dvol[:-7]) ### 3dBandpass in_3dBandpass = out_3dvolreg out_3dBandpass = "******".format( prefix=in_3dBandpass[:-7]) out_3dBandpass_regressors = "{directory}{fmri_id}.bandpass.1D".format( fmri_id=identifier, directory=directory) ### 3dDeconvolve in_3dDeconvolve = out_3dvolreg out_3dDeconvolve_err = "{prefix}.deconvolve.err.nii.gz".format( prefix=in_3dDeconvolve[:-7]) out_3dDeconvolve_fit = "{prefix}.deconvolve.fit.nii.gz".format( prefix=in_3dDeconvolve[:-7]) out_3dDeconvolve_stats = "{prefix}.deconvolve.stats.nii.gz".format( prefix=in_3dDeconvolve[:-7]) ### final output fmri_preprocessed = out_3dDeconvolve_err fvol = out_3dvol matching_t1 = [s for s in t1_collection if identifier in s] matching_seg = [s for s in seg_collections if identifier in s] matching_atlas = [s for s in atlas_collections if identifier in s] if len(matching_t1) > 0: t1 = matching_t1[0] aff_t1_2_fmri = "{directory_reg}{fmri_id}.t1__2__{fmri_id}.fmri.despike.volreg.vol4.txt".format( fmri_id=identifier, directory_reg=directory_reg) aff_fmri_2_t1 = "{directory_reg}{fmri_id}.fmri.despike.volreg.vol4__2__{fmri_id}.t1.txt".format( fmri_id=identifier, directory_reg=directory_reg) aff_t1_in_fmri = "{directory_reg}{fmri_id}.t1__in__{fmri_id}.fmri.despike.volreg.vol4.nii.gz".format( fmri_id=identifier, directory_reg=directory_reg) aff_fmri_in_t1 = "{directory_reg}{fmri_id}.fmri.despike.volreg.vol4__in__{fmri_id}.t1.nii.gz".format( fmri_id=identifier, directory_reg=directory_reg) if len(matching_seg) > 0 and len(matching_atlas) > 0: seg = matching_seg[0] atlas = matching_atlas[0] fmri_seg = "{directory}{fmri_id}.fmri.despike.volreg.vol4.seg.nii.gz".format( directory=directory, fmri_id=identifier) fmri_atlas = "{directory}{fmri_id}.fmri.despike.volreg.vol4.atlas.nii.gz".format( directory=directory, fmri_id=identifier) ########################################################################################################## ################################# FMRI PREPROCESSING ##################################################### ########################################################################################################## # despiking if not isfile(out_3ddespike): cmd = "3dDespike -NEW -nomask -prefix {out_} {in_}".format( in_=in_3ddespike, out_=out_3ddespike) print cmd call(cmd, shell=True) # motion realignment if not isfile(out_3dvolreg): cmd = "3dvolreg -overwrite -zpad 1 -cubic -prefix {out_} -1Dfile {out_txt} {in_}".format( out_txt=out_3dvolreg_txt, in_=in_3dvolreg, out_=out_3dvolreg) print cmd call(cmd, shell=True) cmd = "1d_tool.py -infile {in_} -set_nruns 1 -demean -write {out_}".format( in_=out_3dvolreg_txt, out_=out_motion) print cmd call(cmd, shell=True) cmd = "1d_tool.py -infile {in_} -set_nruns 1 -derivative -demean -write {out_}".format( in_=out_3dvolreg_txt, out_=out_motion_derv) print cmd call(cmd, shell=True) # get one volume if not isfile(out_3dvol): cmd = "3dcalc -overwrite -a '{in_}[4]' -prefix {out_} -expr a".format( in_=in_3dvol, out_=out_3dvol) call(cmd, shell=True) # get brain mask for volume if not isfile(out_3dAutomask): cmd = "3dAutomask -overwrite -prefix {out_} {in_}".format( in_=in_3dAutomask, out_=out_3dAutomask) call(cmd, shell=True) # band passs filter signals if not isfile(out_3dBandpass_regressors): #cmd = "3dBandpass -overwrite -band 0.01 0.1 -prefix {out_} {in_}".format(in_=in_3dBandpass, out_=out_3dBandpass) cmd = "1dBport -nodata {VOLUMES} {TR} -band {band} -invert -nozero > {out_}".format( VOLUMES=VOLUMES, TR=TR, out_=out_3dBandpass_regressors, band=freq_band) print cmd call(cmd, shell=True) if not isfile(out_3dDeconvolve_err): cmd = """3dDeconvolve -input {in_} \ -ortvec {bandpass} bandpass \ -polort 3 \ -num_stimts 12 \ -stim_file 1 {motion}'[0]' -stim_base 1 -stim_label 1 roll_01 \ -stim_file 2 {motion}'[1]' -stim_base 2 -stim_label 2 pitch_01 \ -stim_file 3 {motion}'[2]' -stim_base 3 -stim_label 3 yaw_01 \ -stim_file 4 {motion}'[3]' -stim_base 4 -stim_label 4 dS_01 \ -stim_file 5 {motion}'[4]' -stim_base 5 -stim_label 5 dL_01 \ -stim_file 6 {motion}'[5]' -stim_base 6 -stim_label 6 dP_01 \ -stim_file 7 {motion_derv}'[0]' -stim_base 7 -stim_label 7 roll_02 \ -stim_file 8 {motion_derv}'[1]' -stim_base 8 -stim_label 8 pitch_02 \ -stim_file 9 {motion_derv}'[2]' -stim_base 9 -stim_label 9 yaw_02 \ -stim_file 10 {motion_derv}'[3]' -stim_base 10 -stim_label 10 dS_02 \ -stim_file 11 {motion_derv}'[4]' -stim_base 11 -stim_label 11 dL_02 \ -stim_file 12 {motion_derv}'[5]' -stim_base 12 -stim_label 12 dP_02 \ -fitts {out_fit} \ -errts {out_err} \ -bucket {out_stats} """.format(in_=in_3dDeconvolve, bandpass=out_3dBandpass_regressors, motion=out_motion, motion_derv=out_motion_derv, out_fit=out_3dDeconvolve_fit, out_err=out_3dDeconvolve_err, out_stats=out_3dDeconvolve_stats) call(cmd, shell=True) ######################################################################################################################################### ################################################### AFFINE REGISTRATIONS ################################################################ ######################################################################################################################################### #(i) fmri to t1 if t1 and atlas and not isfile(aff_fmri_in_t1): cmd = "reg_aladin -ref {ref} -rmask {rmask} -flo {flo} -fmask {fmask} -res {res} -aff {aff}".format( ref=t1, flo=fvol, res=aff_fmri_in_t1, aff=aff_fmri_2_t1, rmask=atlas, fmask=out_3dAutomask) print cmd call(cmd, shell=True) #(ii) t1 to fmri if t1 and atlas and not isfile(aff_t1_in_fmri): cmd = "reg_aladin -ref {ref} -rmask {rmask} -flo {flo} -fmask {fmask} -res {res} -aff {aff}".format( ref=fvol, flo=t1, res=aff_t1_in_fmri, aff=aff_t1_2_fmri, fmask=atlas, rmask=out_3dAutomask) print cmd call(cmd, shell=True) # resample seg in fmri space if seg and not isfile(fmri_seg): cmd = "reg_resample -ref {ref} -flo {flo} -psf -res {res} -trans {trans}".format( ref=fvol, flo=seg, res=fmri_seg, trans=aff_t1_2_fmri) print cmd call(cmd, shell=True) # resample atlas in fmri space if atlas and not isfile(fmri_atlas): cmd = "reg_resample -ref {ref} -flo {flo} -inter 0 -res {res} -trans {trans}".format( ref=fvol, flo=atlas, res=fmri_atlas, trans=aff_t1_2_fmri) print cmd call(cmd, shell=True) # build atlas based correlation matrices img = Image(img_filename=fmri_preprocessed, mask_filename=fmri_atlas, atlas_filename=fmri_atlas, atlas_thr=1) sio.savemat( "{prefix}.corr.mat".format(prefix=fmri_preprocessed[:-7]), {'CorrMatrix': img.atlas_corr}) sio.savemat( "{prefix}.rois.mat".format(prefix=fmri_preprocessed[:-7]), {'ROISignals': img.image_mat_in_mask_normalised_atlas}) labels = np.unique(img.atlas_v) labels = labels[labels > 0] sio.savemat( "{prefix}.rois_label.mat".format( prefix=fmri_preprocessed[:-7]), {'ROILabels': labels})
import argparse import numpy as np parser = argparse.ArgumentParser(description='build nifti file from mat file') parser.add_argument('-img', metavar='img', nargs='+', type=str, required=True) parser.add_argument('-mask', metavar='mask', nargs='+', type=str, required=True) parser.add_argument('-mat', metavar='mat', nargs='+', type=str, required=True) parser.add_argument('-normalise', dest='normalise', action='store_true') parser.set_defaults(normalise=False) args = parser.parse_args() if len(args.img)>1 and len(args.mask)>1: for i, mat in enumerate(args.mat): print args.img[i], args.mask[i], mat img = Image(img_filename=args.img[i], mask_filename=args.mask[i]) f = sio.loadmat(mat) hiddenlayer = f['h'] img.write_decomposition(maps=hiddenlayer, filename="{prefix}.nii.gz".format(prefix=mat[0:mat.find('.mat')]), normalise=args.normalise) else: img = Image(img_filename=args.img[0], mask_filename=args.mask[0]) mat = args.mat[0] f = sio.loadmat(mat) if 'h' in f.keys(): hiddenlayer = f['h'] if 'mat' in f.keys(): hiddenlayer = f['mat'] if 'x_tilde' in f.keys():
group = sio.loadmat(args.group_decomp) W1group=group['W1'] b1group=group['b1'] W2group=group['W2'] b2group=group['b2'] number_subjects = len(args.ind_decomp) number_individual_hiddenlayer = W1group.shape[1]/number_subjects number_group_hiddenlayer = W1group.shape[0] W1group=np.reshape(W1group,[number_subjects, number_group_hiddenlayer, number_individual_hiddenlayer]) W2group=np.reshape(W1group,[number_individual_hiddenlayer,number_subjects,number_group_hiddenlayer]) #W1group=np.reshape(W1group,[number_group_hiddenlayer, number_individual_hiddenlayer, number_subjects]) #bgroup=np.reshape(b1group, [number_subjects, number_individual_hiddenlayer]) W1group[np.abs(W1group)<args.thr]=0 for i,d in enumerate(args.ind_decomp): img = Image(img_filename=args.ind_fmri_nii[i], mask_filename=args.ind_mask[i]) individual = sio.loadmat(d) mat = sio.loadmat(args.ind_fmri_mat[i]) mat = mat['mat'] W1g = np.squeeze(W1group[i,:,:]) W2g = np.squeeze(W2group[:,i,:]) Wi = individual['W1'] bi = individual['b1'] #hi = individual['h'] pdb.set_trace() hi = Wi.dot(mat)+np.tile(bi, (mat.shape[1], 1)).transpose() hi=sigmoid(hi) img.write_decomposition(maps=hi, filename="{prefix}.hdummy.nii.gz".format(prefix=d[0:d.find('.mat')]), normalise=False) #pdb.set_trace() h = W1g.dot(hi)+np.tile(b1group, (hi.shape[1], 1)).transpose() h = sigmoid(h)
from niftybold.img.Image import Image import scipy.io as sio import argparse import numpy as np parser = argparse.ArgumentParser(description='build nifti file from mat file') parser.add_argument('-img', metavar='img', type=str, required=True) parser.add_argument('-mask', metavar='mask', type=str, required=True) parser.add_argument('-mat', metavar='mat', type=str, required=True) parser.add_argument('-normalise', metavar='normalises', type=bool, default=False) args = parser.parse_args() f = sio.loadmat(args.mat) hiddenlayer = f['h'] img = Image(img_filename=args.img, mask_filename=args.mask) img.write_decomposition(maps=hiddenlayer, filename="{prefix}.nii.gz".format( prefix=args.mat[0:args.mat.find('.mat')]), normalise=args.normalise)
def test_ae(filename='/Volumes/VAULT/YOAD/all/01-001.fmri.rest.1.volreg.band.nii.gz', filename_mask='/Volumes/VAULT/YOAD/all/registrations/01-001.fmri.rest.1.volreg.cortical_mask.nii.gz'): img = Image(img_filename=filename, mask_filename=filename_mask) xmat = img.image_mat_in_mask_normalised.T ae = Autoencoder(30, max_iter=200, second_nonlinear=True, bias=True).fit(xmat) img.write_decomposition(maps=ae.components_, filename='test_ae.nii.gz', normalise=False)
from niftybold.img.Image import Image import scipy.io as sio import argparse import numpy as np parser = argparse.ArgumentParser(description='build mat file from nifti file') parser.add_argument('-img', metavar='img', type=str, required=True) parser.add_argument('-mask', metavar='mask', type=str, required=True) parser.add_argument('-mat', metavar='mat', type=str, required=True) parser.add_argument('-normalise', metavar='normalises', type=bool, default=True) parser.add_argument('-asl', metavar='asl', type=bool, default=False) parser.add_argument('-fwhm', metavar='fwhm', type=float, default=0.0) args = parser.parse_args() img = Image(img_filename=args.img, mask_filename=args.mask, fwhm=args.fwhm, asl=args.asl) if args.normalise: _mat = img.image_mat_in_mask_normalised.T else: _mat = img.image_mat_in_mask.T sio.savemat(args.mat, {'mat': _mat})
import argparse import numpy as np parser = argparse.ArgumentParser( description='build mat file for matrix decomposition') parser.add_argument('-groupimg', metavar='groupimg', type=str, nargs='+', required=True) parser.add_argument('-groupmask', metavar='groupmask', type=str, required=True) parser.add_argument('-fwhm', metavar='fwhm', type=float, default=0.0) parser.add_argument('-mat', metavar='mat', type=str, default='ica_ready.mat') args = parser.parse_args() mat = None for index, scan in enumerate(args.groupimg): print scan img = Image(img_filename=scan, mask_filename=args.groupmask, fwhm=args.fwhm) _mat = img.image_mat_in_mask_normalised.T sio.savemat("{prefix}.mat".format(prefix=scan[0:scan.find('.nii')]), {'mat': _mat}) if index == 0: mat = _mat else: mat = np.concatenate((mat, _mat)) sio.savemat(args.mat, {'mat': mat})