Example #1
0
    for subject in subjects:
        sess = subjects[subject]
        # Loop over sessions (donepezil/placebo):
        for this_s in sess:
            ROI_files = [
                fmri_path + this_s[0] + '/Inplane/ROIs/R_V1.mat',
                fmri_path + this_s[0] + '/Inplane/ROIs/L_V1.mat'
            ]

            nifti_path = fmri_path + this_s[0] + '/%s_nifti/' % this_s[0]

            # Get the coordinates of the ROIs, while accounting for the
            # up-sampling:
            #
            ROI_coords = [
                tsv.upsample_coords(tsv.getROIcoords(f), up_samp)
                for f in ROI_files
            ]

            # Initialize lists for each behavioral condition:
            t_fix = []
            t_left = []
            t_right = []

            for this_fix in this_s[1]['fix_nii']:
                # Read data from each ROI, from each voxel. If you want to
                # average across voxels, set the "average" kwarg to True:

                # what's this filter? Does it average?
                t_fix.append(
                    load_nii(nifti_path + this_fix,
Example #2
0
    

    # Loop over subjects: 
    for subject in subjects:
        sess = subjects[subject]
        # Loop over sessions (donepezil/placebo):
        for this_s in sess:
            ROI_files=[fmri_path+this_s[0]+'/Inplane/ROIs/R_V1.mat',
                       fmri_path+this_s[0]+'/Inplane/ROIs/L_V1.mat']

            nifti_path = fmri_path + this_s[0] + '/%s_nifti/'%this_s[0]

            # Get the coordinates of the ROIs, while accounting for the
            # up-sampling:
            # 
            ROI_coords = [tsv.upsample_coords(tsv.getROIcoords(f),up_samp)
                               for f in ROI_files]

            # Initialize lists for each behavioral condition:
            t_fix = []
            t_left = []
            t_right = []

            for this_fix in this_s[1]['fix_nii']:
                # Read data from each ROI, from each voxel. If you want to
                # average across voxels, set the "average" kwarg to True:

                # what's this filter? Does it average?
                t_fix.append(load_nii(nifti_path+this_fix,
                                      ROI_coords,
                                      TR,
Example #3
0
def main():
    #inputs
    # scanner = sys.argv[1]
    # subject = sys.argv[2]
    # rois = ['LLGN_ecc0','LLGN_ecc14','LLGN_polar0','LLGN_polar5',
    # 'RLGN_ecc2','RLGN_ecc9','RLGN_polar2','RLGN_polar4']
    rois = [
        'LV1_ecc0-2', 'LV1_ecc10-18', 'LV1_polar602-026', 'LV1_polar474-526',
        'RV1_ecc1-3', 'RV1_ecc7-11', 'RV1_polar174-226', 'RV1_polar374-426'
    ]
    # rois = ['RV1','LV1','RV2v','LV2v','RV2d','LV2d','RV3v','LV3v','RV3d','LV3d']

    combine_early_vis_rois = False

    #paths
    # sdirs = sio.loadmat('/Volumes/Plata1/LGN/Group_Analyses/subjectDirs_20121103.mat')

    # if scanner=='3T':
    #     subject_dirs = sdirs['subjectDirs3T']
    # elif scanner=='7T':
    #     subject_dirs = sdirs['subjectDirs7T']
    #
    # data_dir = '/Volumes/Plata1/LGN/Scans/{0}/{1}/{2}/'.format(
    #     scanner, subject_dirs[subject,0][0], subject_dirs[subject,1][0])
    # nifti_dir = '{0}/{1}_nifti/'.format(data_dir, subject_dirs[subject,1][0])
    # roi_dir = '{0}/Inplane/ROIs/'.format(data_dir)
    # out_dir = '{0}/Masks/'.format(data_dir)

    data_dir = '/Volumes/Plata1/LGN/Scans/7T/JN_20120808_Session/JN_20120808_fslDC/'
    nifti_dir = os.path.join(data_dir, 'JN_20120808_fslDC_nifti/')
    roi_dir = os.path.join(data_dir, 'Inplane/ROIs/')
    out_dir = os.path.join(data_dir, 'Masks/')

    print data_dir

    #constants
    up_samp = [1.0000, 1.0000, 1.0000]  #upsample factor from EPI to GEM
    # up_samp = [2.5606,2.5606,1.0000]
    #usually epi: 2.220 x 2.220 x 2.300; gem: 0.867x0.867x2.300

    example_epi = nifti_dir + 'epi01_fix_fsldc.nii.gz'

    #Loop over ROIs
    for roi in rois:

        roi_file = roi_dir + roi + '.mat'

        #get the coordinates for the ROI, accounting for upsampling
        # upsample_coords takes into account 1 vs. 0-based indexing
        roi_coords = tsv.upsample_coords(tsv.getROIcoords(roi_file), up_samp)

        #save this to a text file for AFNI
        fout_coords = out_dir + roi + '_coords.txt'
        np.savetxt(fout_coords, roi_coords.transpose())

        #run afni 3dUndump command to make this into an ROI
        fout_nii = out_dir + roi + '_orig.nii'
        command = "3dUndump -prefix %s -master %s %s" % (fout_nii, example_epi,
                                                         fout_coords)
        util.remove_previous_files(fout_nii)  #remove file if it exists
        print command
        os.system(command)

        #Looks like this is R/L flipped? Flipping to normal
        print 'Flipping ROI'
        fout_nii_new = out_dir + roi + '.nii'
        command = "3dresample -orient LPI -prefix %s -inset %s" % (
            fout_nii_new, fout_nii)
        util.remove_previous_files(fout_nii_new)
        os.system(command)

        # we don't need the orig file anymore, it is L-R reversed
        # util.remove_previous_files(fout_nii)

    #Sum together ROI pairs to get a single V1,V2, and V3
    if combine_early_vis_rois:
        print 'Making large *all* ROIs'
        roi_final = ['V1', 'V2', 'V3']
        for roi in roi_final:
            fout = out_dir + '_all' + roi + '.nii.gz'
            fout_d = out_dir + '_all' + roi + 'd.nii.gz'
            fout_v = out_dirout_dir + '_all' + roi + 'v.nii.gz'

            if roi == 'V1':
                #note: ispositive is used to deal with potential overlap
                command = "3dcalc -a %s%s_L%s.nii.gz -b %s%s_R%s.nii.gz -expr 'ispositive(a+b)' -prefix %s" % (
                    out_dir, sub, roi, out_dir, sub, roi, fout)

                util.remove_previous_files(fout)
                os.system(command)

            else:
                command = "3dcalc -a %s%s_L%sd.nii.gz -b %s%s_R%sd.nii.gz -c  %s%s_L%sv.nii.gz -d %s%s_R%sv.nii.gz -expr 'ispositive(a+b+c+d)' -prefix %s" % (
                    out_dir, sub, roi, out_dir, sub, roi, out_dir, sub, roi,
                    out_dir, sub, roi, fout)
                util.remove_previous_files(fout)
                os.system(command)

                command = "3dcalc -a %s%s_L%sv.nii.gz -b %s%s_R%sv.nii.gz -expr 'ispositive(a+b)' -prefix %s" % (
                    out_dir, sub, roi, out_dir, sub, roi, fout_v)
                util.remove_previous_files(fout_v)
                os.system(command)

                command = "3dcalc -a %s%s_L%sd.nii.gz -b %s%s_R%sd.nii.gz -expr 'ispositive(a+b)' -prefix %s" % (
                    out_dir, sub, roi, out_dir, sub, roi, fout_d)
                util.remove_previous_files(fout_d)
                os.system(command)
for i_seed, seed_name in enumerate(seed_rois):
    print '\n', seed_name, seed_roi_type
    if seed_roi_type is 'mask':
        seed_file = os.path.join(mask_roi_dir, '{}.nii'.format(seed_name))

        # load roi mask
        seed_mask = nib.load(seed_file)
    
        # find the coordinates of the seed voxels
        seed_vals = seed_mask.get_data()
        seed_coords = np.array(np.where(seed_vals==1))
    elif seed_roi_type is 'mrvista':
        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'),

    for subject in subjects:
        # Get session
        for sess in session:
            sessName = subjects[subject][sess]

            # Get ROIs
            roi_names=np.array(rois[subject][sess][1])
            ROI_files=[]
            for roi in rois[subject][sess][1]:
                ROI_files.append(fmri_path+sessName[0]+'/Inplane/ROIs/' +roi +'.mat')

            # Get the coordinates of the ROIs, while accounting for the
            # up-sampling:
            ROI_coords = [tsv.upsample_coords(tsv.getROIcoords(f),up_samp) for f in ROI_files]

            nifti_path = fmri_path +sessName[0] + '/%s_nifti/' % sessName[0]
            reg_path=fmri_path+sessName[0]+'/regressors/'

            # Add filtering
            filterType='boxcar'

            #Go through each run and save out ROIs as nifti file
            for runName in allRuns:
                print 'Analyzing ' + runName

                # Initialize lists for each condition:
                t_fix = []
                print runName
                saveFile=base_path+ 'fmri/Results/timeseries/'+subject+sessionName[sess]+'_'+runName+'_%sROIts_%sReg_meanROI_stc.pck' % (len(roi_names), len(nuisReg))
def main():
    #inputs
    # scanner = sys.argv[1]
    # subject = sys.argv[2]
    # rois = ['LLGN_ecc0','LLGN_ecc14','LLGN_polar0','LLGN_polar5',
        # 'RLGN_ecc2','RLGN_ecc9','RLGN_polar2','RLGN_polar4']
    rois = ['LV1_ecc0-2', 'LV1_ecc10-18', 'LV1_polar602-026', 'LV1_polar474-526',
        'RV1_ecc1-3', 'RV1_ecc7-11', 'RV1_polar174-226', 'RV1_polar374-426']
    # rois = ['RV1','LV1','RV2v','LV2v','RV2d','LV2d','RV3v','LV3v','RV3d','LV3d']

    combine_early_vis_rois = False

    #paths
    # sdirs = sio.loadmat('/Volumes/Plata1/LGN/Group_Analyses/subjectDirs_20121103.mat')

    # if scanner=='3T':
    #     subject_dirs = sdirs['subjectDirs3T']
    # elif scanner=='7T':
    #     subject_dirs = sdirs['subjectDirs7T']
    # 
    # data_dir = '/Volumes/Plata1/LGN/Scans/{0}/{1}/{2}/'.format(
    #     scanner, subject_dirs[subject,0][0], subject_dirs[subject,1][0])
    # nifti_dir = '{0}/{1}_nifti/'.format(data_dir, subject_dirs[subject,1][0])
    # roi_dir = '{0}/Inplane/ROIs/'.format(data_dir)
    # out_dir = '{0}/Masks/'.format(data_dir)

    data_dir = '/Volumes/Plata1/LGN/Scans/7T/JN_20120808_Session/JN_20120808_fslDC/'
    nifti_dir = os.path.join(data_dir,'JN_20120808_fslDC_nifti/')
    roi_dir = os.path.join(data_dir,'Inplane/ROIs/')
    out_dir = os.path.join(data_dir,'Masks/')

    print data_dir

    #constants
    up_samp = [1.0000,1.0000,1.0000] #upsample factor from EPI to GEM
    # up_samp = [2.5606,2.5606,1.0000]
    #usually epi: 2.220 x 2.220 x 2.300; gem: 0.867x0.867x2.300

    example_epi = nifti_dir + 'epi01_fix_fsldc.nii.gz'

    #Loop over ROIs
    for roi in rois:

        roi_file = roi_dir + roi + '.mat'

        #get the coordinates for the ROI, accounting for upsampling
        # upsample_coords takes into account 1 vs. 0-based indexing
        roi_coords = tsv.upsample_coords(tsv.getROIcoords(roi_file),up_samp)

        #save this to a text file for AFNI
        fout_coords = out_dir + roi + '_coords.txt'
        np.savetxt(fout_coords,roi_coords.transpose())

        #run afni 3dUndump command to make this into an ROI
        fout_nii = out_dir + roi + '_orig.nii'
        command = "3dUndump -prefix %s -master %s %s" %(fout_nii,
                                                        example_epi,
                                                        fout_coords)
        util.remove_previous_files(fout_nii) #remove file if it exists
        print command
        os.system(command)

        #Looks like this is R/L flipped? Flipping to normal
        print 'Flipping ROI'
        fout_nii_new = out_dir +  roi + '.nii'
        command = "3dresample -orient LPI -prefix %s -inset %s" %(fout_nii_new,
                                                                  fout_nii)
        util.remove_previous_files(fout_nii_new)
        os.system(command)

        # we don't need the orig file anymore, it is L-R reversed
        # util.remove_previous_files(fout_nii)


    #Sum together ROI pairs to get a single V1,V2, and V3
    if combine_early_vis_rois:
        print 'Making large *all* ROIs'
        roi_final = ['V1','V2','V3']
        for roi in roi_final:
            fout = out_dir + '_all' + roi + '.nii.gz'
            fout_d = out_dir + '_all' + roi + 'd.nii.gz'
            fout_v = out_dirout_dir + '_all' + roi + 'v.nii.gz'

            if roi == 'V1':
                #note: ispositive is used to deal with potential overlap
                command = "3dcalc -a %s%s_L%s.nii.gz -b %s%s_R%s.nii.gz -expr 'ispositive(a+b)' -prefix %s" %(out_dir,sub,roi,out_dir,sub,roi,fout)

                util.remove_previous_files(fout)
                os.system(command)

            else:
                command = "3dcalc -a %s%s_L%sd.nii.gz -b %s%s_R%sd.nii.gz -c  %s%s_L%sv.nii.gz -d %s%s_R%sv.nii.gz -expr 'ispositive(a+b+c+d)' -prefix %s" %(out_dir,sub,roi,out_dir,sub,roi,out_dir,sub,roi,out_dir,sub,roi,fout)
                util.remove_previous_files(fout)
                os.system(command)

                command = "3dcalc -a %s%s_L%sv.nii.gz -b %s%s_R%sv.nii.gz -expr 'ispositive(a+b)' -prefix %s" %(out_dir,sub,roi,out_dir,sub,roi,fout_v)
                util.remove_previous_files(fout_v)
                os.system(command)
            
                command = "3dcalc -a %s%s_L%sd.nii.gz -b %s%s_R%sd.nii.gz -expr 'ispositive(a+b)' -prefix %s" %(out_dir,sub,roi,out_dir,sub,roi,fout_d)
                util.remove_previous_files(fout_d)
                os.system(command)
Example #7
0
# load data
data = nib.load(data_file)
volume_shape = data.shape[:-1]
n_TRs = data.shape[-1]

# Get seed data
# initialize seed time series
seed_ts = np.zeros((len(seed_rois), n_TRs))

for i_seed, seed_name in enumerate(seed_rois):
    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