예제 #1
0
def mask_fmri_process(fmri_path, masks, sys_use='unix'):
    ### 1. Load and mask the data
    fmri_path = ub_wind_path(fmri_path, system=sys_use) #change the path format wind-unix
    
    mask_img_rh= masks[0] #right hemisphere mask
    mask_img_rh = ub_wind_path(mask_img_rh, system=sys_use)
    mask_img_lh= masks[1] #left hemisphere mask
    mask_img_lh = ub_wind_path(mask_img_lh, system=sys_use)
    
    #Apply the masks and concatenate   
    masked_data_rh = apply_mask(fmri_path, mask_img_rh)
    masked_data_lh = apply_mask(fmri_path, mask_img_lh)    
    masked_data=np.hstack([masked_data_rh, masked_data_lh])
    
    ### 2. Filter ####and zscore
    n_voxels = np.shape(masked_data)[1]
    for voxel in range(0, n_voxels):
        data_to_filter = masked_data[:,voxel]                        
        #apply the filter 
        data_to_filter = TimeSeries(data_to_filter, sampling_interval=2.335)
        F = FilterAnalyzer(data_to_filter, ub=0.15, lb=0.02)
        data_filtered=F.filtered_boxcar.data
        masked_data[:,voxel] = data_filtered                        
        #Z score
        masked_data[:,voxel] = np.array( stats.zscore( masked_data[:,voxel]  ) ) ; ## zscore + 5 just to get + values
    
    #append it and save the data
    return masked_data    
예제 #2
0
def process_enc_timestamps(masked_data, timestamp_run, TR=2.335):
    n_voxels = np.shape(masked_data)[1]
    ####   2. Apply a filter for each voxel
    data_filtered = np.zeros(np.shape(masked_data))
    for voxel in range(0, n_voxels):
        data_to_filter = masked_data[:,
                                     voxel]  #data of the voxel along the session
        #apply the filter
        data_to_filter = TimeSeries(data_to_filter, sampling_interval=TR)
        F = FilterAnalyzer(data_to_filter, ub=0.15,
                           lb=0.02)  ##upper and lower boundaries
        data_filt = F.filtered_boxcar.data
        data_filtered[:, voxel] = data_filt

    ####   3. Subset of data corresponding to the delay times (all voxels)
    encoding_delay_activity = np.zeros(
        (len(timestamp_run), n_voxels))  ## emply matrix (n_trials, n_voxels)
    for idx, t in enumerate(timestamp_run):  #in each trial
        delay_TRs = data_filtered[
            t:t + 2, :]  #take the first scan of the delay and the next
        delay_TRs_mean = np.mean(delay_TRs,
                                 axis=0)  #make the mean in each voxel of 2TR
        encoding_delay_activity[
            idx, :] = delay_TRs_mean  #index the line in the matrix

    ###   4. zscore  in each voxel in the temporal dimension (with the other 2TR of the same session)
    for voxel in range(0, n_voxels):  # by voxel
        vx_act = encoding_delay_activity[:, voxel]
        vx_act_zs = np.array(stats.zscore(vx_act))
        ## zscore
        encoding_delay_activity[:,
                                voxel] = vx_act_zs  ## replace previos activity

    return encoding_delay_activity
예제 #3
0
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
예제 #4
0
# ax01.plot(S_original.spectrum_multi_taper[0],
#           S_original.spectrum_multi_taper[1][9],
#           label='Multi-taper')

ax01.set_xlabel('Frequency (Hz)')
ax01.set_ylabel('Power')
plt.ylim((0,25))

ax01.legend()
plt.savefig("../figure/FFT.jpg")


# In[33]:

F = FilterAnalyzer(T, ub=0.15, lb=0.02)

# Initialize a figure to display the results:
fig02 = plt.figure(3)
ax02 = fig02.add_subplot(1, 1, 1)

# Plot the original, unfiltered data:
ax02.plot(F.data[87440], label='unfiltered')

# ax02.plot(F.filtered_boxcar.data[89], label='Boxcar filter')

# ax02.plot(F.fir.data[89], label='FIR')

# ax02.plot(F.iir.data[89], label='IIR')

ax02.plot(F.filtered_fourier.data[87440], label='Fourier')