def bpfilter(in_files,lp,hp,TR): from nibabel import load import nitime.fmri.io as io out_files = [] path, name, ext = split_filename(in_files) out_file = os.path.join(os.getcwd(), name + '_bp' + ext) fmri_data = load(in_files) volume_shape = fmri_data.shape[:-1] coords = list(np.ndindex(volume_shape)) coords2 = np.array(coords).T T_fourier = io.time_series_from_file(in_files, coords2, TR=TR, filter=dict(lb=lp, ub=hp, mports = ['import os', 'import nibabel as nb', 'import numpy as np', 'import scipy as sp', ('from nipype.utils.filemanip import filename_to_list, ' 'list_to_filename, split_filename'), 'from scipy.special import legendre' ] method='fourier'))
def mod_filter(in_file, algorithm, lowpass_freq, highpass_freq, tr): import os from nipype.utils.filemanip import fname_presuffix if algorithm == 'fsl': import nipype.interfaces.fsl as fsl filter = fsl.TemporalFilter() filter.inputs.in_file = in_file if highpass_freq < 0: filter.inputs.highpass_sigma = -1 else: filter.inputs.highpass_sigma = 1 / (2 * tr * highpass_freq) if lowpass_freq < 0: filter.inputs.lowpass_sigma = -1 else: filter.inputs.lowpass_sigma = 1 / (2 * tr * lowpass_freq) res = filter.run() out_file = res.outputs.out_file else: import nitime.fmri.io as io from nitime.analysis import FilterAnalyzer import nibabel as nib import numpy as np T = io.time_series_from_file(in_file, TR=tr) if highpass_freq < 0: highpass_freq = 0 if lowpass_freq < 0: lowpass_freq = None filt_order = np.floor(T.shape[3] / 3) if filt_order % 2 == 1: filt_order -= 1 F = FilterAnalyzer(T, ub=lowpass_freq, lb=highpass_freq, filt_order=filt_order) if algorithm == 'IIR': Filtered_data = F.iir.data suffix = '_iir_filt' elif algorithm == 'Boxcar': Filtered_data = F.filtered_boxcar.data suffix = '_boxcar_filt' elif algorithm == 'Fourier': Filtered_data = F.filtered_fourier.data suffix = '_fourier_filt' elif algorithm == 'FIR': Filtered_data = F.fir.data suffix = '_fir_filt' else: raise ValueError('Unknown Nitime filtering algorithm: %s' % algorithm) out_file = fname_presuffix(in_file, suffix=suffix, newpath=os.getcwd()) out_img = nib.Nifti1Image(Filtered_data, nib.load(in_file).get_affine()) out_img.to_filename(out_file) return out_file
def mod_filter(in_file, algorithm, lowpass_freq, highpass_freq, tr): import os from nipype.utils.filemanip import fname_presuffix if algorithm == 'fsl': import nipype.interfaces.fsl as fsl filter = fsl.TemporalFilter() filter.inputs.in_file = in_file if highpass_freq < 0: filter.inputs.highpass_sigma = -1 else: filter.inputs.highpass_sigma = 1 / (2 * tr * highpass_freq) if lowpass_freq < 0: filter.inputs.lowpass_sigma = -1 else: filter.inputs.lowpass_sigma = 1 / (2 * tr * lowpass_freq) res = filter.run() out_file = res.outputs.out_file else: import nitime.fmri.io as io from nitime.analysis import FilterAnalyzer import nibabel as nib import numpy as np T = io.time_series_from_file(in_file, TR=tr) if highpass_freq < 0: highpass_freq = 0 if lowpass_freq < 0: lowpass_freq = None filt_order = np.floor(T.shape[3]/3) if filt_order % 2 == 1: filt_order -= 1 F = FilterAnalyzer(T, ub=lowpass_freq, lb=highpass_freq, filt_order=filt_order) if algorithm == 'IIR': Filtered_data = F.iir.data suffix = '_iir_filt' elif algorithm == 'Boxcar': Filtered_data = F.filtered_boxcar.data suffix = '_boxcar_filt' elif algorithm == 'Fourier': Filtered_data = F.filtered_fourier.data suffix = '_fourier_filt' elif algorithm == 'FIR': Filtered_data = F.fir.data suffix = '_fir_filt' else: raise ValueError('Unknown Nitime filtering algorithm: %s' % algorithm) out_file = fname_presuffix(in_file, suffix=suffix, newpath=os.getcwd()) out_img = nib.Nifti1Image(Filtered_data, nib.load(in_file).get_affine()) out_img.to_filename(out_file) return out_file
def analyze_connectivity(imagelist, path_roi, roi_names, ts_param, **kwargs): TR = 1. for arg in kwargs: if arg == 'TR': TR = np.float(kwargs[arg]) roi_list = os.listdir(path_roi) roi_list = [r for r in roi_list if r.find('.hdr') != -1 \ or r.find('nii.gz') != -1] ''' roi_list = ['lower_left_new_.nii.gz', 'lower_right_new_.nii.gz', 'upper_left_new_.nii.gz', 'upper_right_new_.nii.gz', 'roi_attention_all.4dfp.hdr', 'roi_default_all.4dfp.hdr', 'searchlight_separated.nii.gz' ] ''' #roi_names = np.loadtxt('/media/DATA/fmri/learning/roi_labels', dtype=np.str) print 'Length of image list is '+str(len(imagelist)) volume_shape = imagelist[0].get_data().shape[:-1] n_shape = list(volume_shape) n_shape.append(-1) coords = list(np.ndindex(volume_shape)) coords = np.array(coords).reshape(n_shape) data_arr = [] for roi in roi_list: #print roi r_mask = ni.load(os.path.join(path_roi, roi)) mask = r_mask.get_data().squeeze() roi_filt = roi_names[roi_names.T[1] == roi] for label in np.unique(mask)[1:]: roi_m = np.int_(roi_filt.T[2]) == label if roi_m.any(): print 'Loading voxels from '+roi_filt[roi_m].T[0][0] time_serie = io.time_series_from_file([f.get_filename() for f in imagelist], \ coords[mask==label].T, \ TR=float(TR), \ normalize=ts_param['normalize'], \ average=ts_param['average'], \ filter=ts_param['filter']) data_arr.append(time_serie) del r_mask data = np.array(data_arr) ts = TimeSeries(data, sampling_interval=float(TR)) del imagelist, time_serie C = nta.CorrelationAnalyzer(ts) return C, ts
def analyze_connectivity(imagelist, path_roi, roi_names, ts_param, **kwargs): TR = 1. for arg in kwargs: if arg == 'TR': TR = np.float(kwargs[arg]) roi_list = os.listdir(path_roi) roi_list = [r for r in roi_list if r.find('.hdr') != -1 \ or r.find('nii.gz') != -1] ''' roi_list = ['lower_left_new_.nii.gz', 'lower_right_new_.nii.gz', 'upper_left_new_.nii.gz', 'upper_right_new_.nii.gz', 'roi_attention_all.4dfp.hdr', 'roi_default_all.4dfp.hdr', 'searchlight_separated.nii.gz' ] ''' #roi_names = np.loadtxt('/media/DATA/fmri/learning/roi_labels', dtype=np.str) print('Length of image list is '+str(len(imagelist))) volume_shape = imagelist[0].get_data().shape[:-1] n_shape = list(volume_shape) n_shape.append(-1) coords = list(np.ndindex(volume_shape)) coords = np.array(coords).reshape(n_shape) data_arr = [] for roi in roi_list: r_mask = ni.load(os.path.join(path_roi, roi)) mask = r_mask.get_data().squeeze() roi_filt = roi_names[roi_names.T[1] == roi] for label in np.unique(mask)[1:]: roi_m = np.int_(roi_filt.T[2]) == label if roi_m.any(): print('Loading voxels from '+roi_filt[roi_m].T[0][0]) time_serie = io.time_series_from_file([f.get_filename() for f in imagelist], \ coords[mask==label].T, \ TR=float(TR), \ normalize=ts_param['normalize'], \ average=ts_param['average'], \ filter=ts_param['filter']) data_arr.append(time_serie) del r_mask data = np.array(data_arr) ts = TimeSeries(data, sampling_interval=float(TR)) del imagelist, time_serie C = nta.CorrelationAnalyzer(ts) return C, ts
def time_normalizer(in_file, tr): ''' Mean centering and variance normalizing a time series ''' import os import nitime.fmri.io as io import nibabel as nib from nipype.utils.filemanip import fname_presuffix T= io.time_series_from_file(in_file, normalize=None, TR=tr) #previously: normalize="zscore" (percent) as Allen states that variance normalisation degrades inter-subject amplitude and shapes of the components normalized_data = T.data out_img = nib.Nifti1Image(normalized_data,nib.load(in_file).get_affine()) out_file = fname_presuffix(in_file, suffix='_norm',newpath=os.getcwd()) out_img.to_filename(out_file) return out_file
def time_normalizer(in_file, tr): ''' Mean centering and variance normalizing a time series ''' import os import nitime.fmri.io as io import nibabel as nib from nipype.utils.filemanip import fname_presuffix T = io.time_series_from_file(in_file, normalize='zscore', TR=tr) normalized_data = T.data out_img = nib.Nifti1Image(normalized_data, nib.load(in_file).get_affine()) out_file = fname_presuffix(in_file, suffix='_norm', newpath=os.getcwd()) out_img.to_filename(out_file) return out_file
def time_normalizer(in_file, tr): ''' Mean centering and variance normalizing a time series ''' import os import nitime.fmri.io as io import nibabel as nib from nipype.utils.filemanip import fname_presuffix T = io.time_series_from_file( in_file, normalize=None, TR=tr ) #previously: normalize="zscore" (percent) as Allen states that variance normalisation degrades inter-subject amplitude and shapes of the components normalized_data = T.data out_img = nib.Nifti1Image(normalized_data, nib.load(in_file).get_affine()) out_file = fname_presuffix(in_file, suffix='_norm', newpath=os.getcwd()) out_img.to_filename(out_file) return out_file
seed_file = os.path.join(mrvista_roi_dir, '{}.mat'.format(seed_name)) # get the coordinates of the seed voxels seed_coords = tsv.upsample_coords(tsv.getROIcoords(seed_file), upsample_factor) if flip_mrvista_x: seed_coords[0] = (volume_shape[0]-1) - (seed_coords[0]) else: print 'seed roi type not recognized' # make the seed time series (mean of roi time series) seed_ts[i_seed] = ntio.time_series_from_file(data_file, coords=seed_coords, TR=TR, normalize='percent', average=True, filter=dict(lb=f_lb, ub=f_ub, method='boxcar'), verbose=True).data # seed_ts[i_seed] = ntio.time_series_from_file(data_file, # coords=seed_coords, # TR=TR, # average=True, # verbose=True).data seed_T = ntts.TimeSeries(seed_ts, sampling_interval=TR) fig = viz.plot_tseries(seed_T) seed_Cor = nta.CorrelationAnalyzer(seed_T)
fmri_file = os.path.join(data_path, 'fmri1.nii.gz') fmri_data = load(fmri_file) volume_shape = fmri_data.shape[:-1] coords = list(np.ndindex(volume_shape)) n_seeds = 3 seeds = np.random.randint(0, len(coords), n_seeds) coords_seeds = np.array(coords)[seeds].T coords_target = np.array(coords).T time_series_seed = io.time_series_from_file(fmri_file, coords_seeds, TR=TR, normalize='percent', filter=dict(lb=f_lb, ub=f_ub, method='boxcar')) time_series_target = io.time_series_from_file(fmri_file, coords_target, TR=TR, normalize='percent',filter=dict(lb=f_lb, ub=f_ub, method='boxcar')) A = nta.SeedCoherenceAnalyzer(time_series_seed, time_series_target, method=dict(NFFT=20)) B = nta.SeedCorrelationAnalyzer(time_series_seed, time_series_target) freq_idx = np.where((A.frequencies > f_lb) * (A.frequencies < f_ub))[0] cor = [] coh = [] for this_seed in range(n_seeds): coh.append(np.mean(A.coherence[this_seed][:, freq_idx], -1)) cor.append(B.corrcoef[this_seed])
coords_target = np.array(coords).T """ We use nitime.fmri.io in order to generate TimeSeries objects from spatial coordinates in the data file: """ #Make the seed time series: time_series_seed = io.time_series_from_file(fmri_file, coords_seeds, TR=TR, normalize='percent', filter=dict(lb=f_lb, ub=f_ub, method='boxcar')) #Make the target time series: time_series_target = io.time_series_from_file(fmri_file, coords_target, TR=TR, normalize='percent', filter=dict(lb=f_lb, ub=f_ub, method='boxcar')) """
f_lb = 0.02 f_ub = 0.15 data_file_path = test_dir_path = os.path.join(nitime.__path__[0], 'data') fmri_file = os.path.join(data_file_path, 'fmri1.nii.gz') fmri_data = load(fmri_file) volume_shape = fmri_data.shape[:-1] coords = list(np.ndindex(volume_shape)) print (coords) coords = np.array(coords).T T_unfiltered = io.time_series_from_file(fmri_file, coords, TR=TR, normalize='percent') T_fir = io.time_series_from_file(fmri_file, coords, TR=TR, normalize='percent', filter=dict(lb=f_lb, ub=f_ub, method='fir', filt_order=10)) T_iir = io.time_series_from_file(fmri_file, coords, TR=TR, normalize='percent',
def get_bold_timeserie(imagelist, path_roi, roi_names, ts_param, detrend=False, **kwargs): ''' To be modified Enhancement: - create a file/class for the rois with all the informations about it (network, long_name, name, pathfile, mask_value) - figure out how to ''' TR = 1. for arg in kwargs: if arg == 'tr': TR = np.float(kwargs[arg]) #print TR #roi_list = os.listdir(path_roi) #roi_list = [r for r in roi_list if r.find('.hdr') != -1 or r.find('nii.gz') != -1] print 'Length of image list is '+str(len(imagelist)) volume_shape = imagelist[0].get_data().shape[:-1] n_shape = list(volume_shape) n_shape.append(-1) coords = list(np.ndindex(volume_shape)) coords = np.array(coords).reshape(n_shape) n_runs = len(imagelist) data_arr = [] for roi in np.unique(roi_names.T[-2]): #print roi # roi_names.T[1][i] roi_name = roi #print roi_name #network_name = roi[0] r_mask = ni.load(os.path.join(path_roi, roi_name, roi_name+'_separated_3mm.nii.gz')) mask = r_mask.get_data().squeeze() roi_filt = roi_names[roi_names.T[-2] == roi_name] #print roi_filt for label in np.int_(np.unique(mask)[1:]): roi_m = np.int_(roi_filt.T[-1]) == label if roi_m.any(): print 'Loading voxels from '+roi_filt[roi_m].T[2][0] time_serie = io.time_series_from_file([f.get_filename() for f in imagelist], \ coords[mask==label].T, \ TR=float(TR), \ normalize=ts_param['normalize'], \ average=ts_param['average'], \ filter=ts_param['filter']) data_new = [] #print time_serie.data.shape if detrend == True: i = 0 for vt in time_serie.data: run_split = np.split(vt, n_runs) i = i + 1 #Detrending d_vt = np.hstack([detrend(r) for r in run_split]) z_vt = (d_vt - np.mean(d_vt))/np.std(d_vt) z_vt[np.isnan(z_vt)] = 0 data_new.append(z_vt) time_serie.data = np.vstack(data_new) ''' if ts_param['average'] == True: ts_new = np.mean(time_serie.data, axis=0) ''' data_arr.append(time_serie.data) del r_mask data = np.vstack(data_arr) data[np.isnan(data)] = 0 ts = TimeSeries(data, sampling_interval=float(TR)) del imagelist, time_serie #C = nta.CorrelationAnalyzer(ts) return ts
volume_shape = fmri_data.shape[:-1] coords = list(np.ndindex(volume_shape)) coords = np.array(coords).T """ We use :mod:`nitime.fmri.io` in order to generate a TimeSeries object from spatial coordinates in the data file. Notice that normalization method is provided as a string input to the keyword argument 'normalize' and the filter and its properties are provided as a dict to the keyword argument 'filter': """ T_unfiltered = io.time_series_from_file(fmri_file, coords, TR=TR, normalize='percent') T_fir = io.time_series_from_file(fmri_file, coords, TR=TR, normalize='percent', filter=dict(lb=f_lb, ub=f_ub, method='fir', filt_order=10)) T_iir = io.time_series_from_file(fmri_file, coords, TR=TR, normalize='percent',
# find the coordinates of the roi voxels roi_vals = roi_data.get_data() roi_coords = np.array(np.where(roi_vals == 1)) coords_seed = roi_coords # find the coordinates of cortex voxels mask_vals = mask_data.get_data() cortex_coords = np.array(np.where(mask_vals == 1)) coords_target = cortex_coords # make the seed time series (mean of roi time series) time_series_seed = ntio.time_series_from_file(fmri_file, coords_seed, TR=TR, normalize=None, average=True, filter=dict(lb=f_lb, ub=f_ub, method='boxcar'), verbose=True) # make the target time series (all voxels in cortex) time_series_target = ntio.time_series_from_file(fmri_file, coords_target, TR=TR, normalize=None, filter=dict( lb=f_lb, ub=f_ub, method='boxcar'), verbose=True)
def get_bold_timeserie(imagelist, path_roi, roi_names, ts_param, detrend=False, **kwargs): ''' To be modified Enhancement: - create a file/class for the rois with all the informations about it (network, long_name, name, pathfile, mask_value) - figure out how to ''' TR = 1. for arg in kwargs: if arg == 'tr': TR = np.float(kwargs[arg]) #print TR #roi_list = os.listdir(path_roi) #roi_list = [r for r in roi_list if r.find('.hdr') != -1 or r.find('nii.gz') != -1] print('Length of image list is '+str(len(imagelist))) volume_shape = imagelist[0].get_data().shape[:-1] n_shape = list(volume_shape) n_shape.append(-1) coords = list(np.ndindex(volume_shape)) coords = np.array(coords).reshape(n_shape) n_runs = len(imagelist) data_arr = [] for roi in np.unique(roi_names.T[-2]): roi_name = roi r_mask = ni.load(os.path.join(path_roi, roi_name, roi_name+'_separated_3mm.nii.gz')) mask = r_mask.get_data().squeeze() roi_filt = roi_names[roi_names.T[-2] == roi_name] for label in np.int_(np.unique(mask)[1:]): roi_m = np.int_(roi_filt.T[-1]) == label if roi_m.any(): print('Loading voxels from '+roi_filt[roi_m].T[2][0]) time_serie = io.time_series_from_file([f.get_filename() for f in imagelist], \ coords[mask==label].T, \ TR=float(TR), \ normalize=ts_param['normalize'], \ average=ts_param['average'], \ filter=ts_param['filter']) data_new = [] #print time_serie.data.shape if detrend == True: i = 0 for vt in time_serie.data: run_split = np.split(vt, n_runs) i = i + 1 #Detrending d_vt = np.hstack([detrend(r) for r in run_split]) z_vt = (d_vt - np.mean(d_vt))/np.std(d_vt) z_vt[np.isnan(z_vt)] = 0 data_new.append(z_vt) time_serie.data = np.vstack(data_new) data_arr.append(time_serie.data) del r_mask data = np.vstack(data_arr) data[np.isnan(data)] = 0 ts = TimeSeries(data, sampling_interval=float(TR)) del imagelist, time_serie #C = nta.CorrelationAnalyzer(ts) return ts
print '\n', seed_name seed_file = os.path.join(roi_dir, '{}.mat'.format(seed_name)) # get the coordinates of the seed voxels seed_coords = tsv.upsample_coords(tsv.getROIcoords(seed_file), upsample_factor) if flip_x: seed_coords[0] = (volume_shape[0]-1) - (seed_coords[0]) # make the seed time series (mean of roi time series) # this is a little odd - reads the data as a TimeSeries, then just takes the data ... seed_ts[i_seed] = ntio.time_series_from_file(data_file, coords=seed_coords, TR=TR, normalize=None, average=True, filter=dict(lb=f_lb, ub=f_ub, method='boxcar'), verbose=True).data # normalize='percent' # seed_ts[i_seed] = ntio.time_series_from_file(data_file, # coords=seed_coords, # TR=TR, # average=True, # verbose=True).data seed_T = ntts.TimeSeries(seed_ts, sampling_interval=TR) fig_seed_tseries = viz.plot_tseries(seed_T) plt.title('seed tseries, {}'.format(fmri_file))
# find the coordinates of the roi voxels roi_vals = roi_data.get_data() roi_coords = np.array(np.where(roi_vals==1)) coords_seed = roi_coords # find the coordinates of cortex voxels mask_vals = mask_data.get_data() cortex_coords = np.array(np.where(mask_vals==1)) coords_target = cortex_coords # make the seed time series (mean of roi time series) time_series_seed = ntio.time_series_from_file(fmri_file, coords_seed, TR=TR, normalize=None, average=True, filter=dict(lb=f_lb, ub=f_ub, method='boxcar'), verbose=True) # make the target time series (all voxels in cortex) time_series_target = ntio.time_series_from_file(fmri_file, coords_target, TR=TR, normalize=None, filter=dict(lb=f_lb, ub=f_ub, method='boxcar'), verbose=True)