Ejemplo n.º 1
0
def make_mask_from_points(pois, mask_file, new_mask_file):

    m = xndarray.load(mask_file)
    new_m = xndarray.xndarray_like(m)
    for poi in pois:
        i,j,k = poi['sagittal'], poi['coronal'], poi['axial']
        new_m.data[i,j,k] = 1

    new_m.save(new_mask_file)
Ejemplo n.º 2
0
    def test_voronoi_with_seeds(self):

        import os.path as op
        from pyhrf.ndarray import xndarray
        import pyhrf
        fn = 'subj0_parcellation.nii.gz'
        mask_file = pyhrf.get_data_file_name(fn)

        orientation = ['axial', 'coronal', 'sagittal']
        seeds = xndarray.xndarray_like(
            xndarray.load(mask_file)).reorient(orientation)

        seed_coords = np.array([[24, 35, 8],  # axial, coronal, sagittal
                                [27, 35, 5],
                                [27, 29, 46],
                                [31, 28, 46]])

        seeds.data[:] = 0
        seeds.data[tuple(seed_coords.T)] = 1

        seed_file = op.join(self.tmp_dir, 'voronoi_seeds.nii')
        seeds.save(seed_file, set_MRI_orientation=True)

        output_file = op.join(self.tmp_dir, 'voronoi_parcellation.nii')
        cmd = 'pyhrf_parcellate_spatial %s -m voronoi -c %s -o %s -v %d' \
            % (mask_file, seed_file, output_file, logger.getEffectiveLevel())

        if os.system(cmd) != 0:
            raise Exception('"' + cmd + '" did not execute correctly')

        logger.info('cmd: %s', cmd)

        assert op.exists(output_file)
        parcellation = xndarray.load(output_file)

        n_parcels = len(np.unique(parcellation.data)) - 1

        self.assertEqual(n_parcels, len(seed_coords))
Ejemplo n.º 3
0
def make_parcellation(subject, dest_dir="parcellation", roi_mask_file=None):
    """
    Perform a functional parcellation from input fmri data
    
    Return: parcellation file name (str)
    """
    # Loading names for folders and files
    # - T maps (input)
    # func_files = glob(op.join(op.join(op.join('./', subject), \
    #                    't_maps'), 'BOLD*nii'))
    # func_files = glob(op.join('./', subject, 'ASLf', 'spm_analysis', \
    #                            'Tmaps*img'))
    func_files = glob(op.join("./", subject, "ASLf", "spm_analysis", "spmT*img"))
    print "Tmap files: ", func_files

    # - Mask (input)
    # spm_mask_file = op.join(spm_maps_dir, 'mask.img')
    mask_dir = op.join("./", subject, "preprocessed_data")
    if not op.exists(mask_dir):
        os.makedirs(mask_dir)
    mask_file = op.join(mask_dir, "mask.nii")
    mask = op.join(mask_dir, "rcut_tissue_mask.nii")
    volume = op.join("./", subject, "ASLf", "funct", "coregister", "mean" + subject + "_ASLf_correctionT1_0001.nii")
    make_mask(mask, volume, mask_file)

    # - parcellation (output)
    parcellation_dir = op.join("./", subject, dest_dir)
    if not op.exists(parcellation_dir):
        os.makedirs(parcellation_dir)
    pfile = op.join(parcellation_dir, "parcellation_func.nii")

    # Parcellation
    from pyhrf.parcellation import make_parcellation_from_files

    # make_parcellation_from_files(func_files, mask_file, pfile,
    #                             nparcels=200, method='ward_and_gkm')

    # Masking with a ROI so we just consider parcels inside
    # a certain area of the brain
    # if roi_mask_file is not None:
    if 0:  # for ip in np.array([11, 51, 131, 194]):
        # ip = 200

        # print 'Masking parcellation with roi_mask_file: ', roi_mask_file
        print "Masking ROI: ", ip
        pfile_masked = op.join(parcellation_dir, "parcellation_func_masked_roi" + str(ip) + ".nii")

        from pyhrf.ndarray import xndarray

        parcellation = xndarray.load(pfile)
        # m = xndarray.load(roi_mask_file)
        # parcels_to_keep = np.unique(parcellation.data * m.data)
        masked_parcellation = xndarray.xndarray_like(parcellation)
        # for ip in parcels_to_keep:
        #    masked_parcellation.data[np.where(parcellation.data==ip)] = ip

        masked_parcellation.data[np.where(parcellation.data == ip)] = ip
        masked_parcellation.save(pfile_masked)

    from pyhrf.ndarray import xndarray

    for tmap in func_files:
        func_file_i = xndarray.load(tmap)
        func_data = func_file_i.data
        # func_file_i.data[np.where(func_data<0.1*func_data.max())] = 0

        parcellation = xndarray.load(pfile)
        print func_data.max()
        print func_data.argmax()
        # ip = parcellation.data[func_data.argmax()]
        ip = parcellation.data[np.unravel_index(func_data.argmax(), parcellation.data.shape)]
        print ip.shape

        masked_parcellation = xndarray.xndarray_like(parcellation)
        print masked_parcellation.data.shape
        print parcellation.data.shape
        masked_parcellation.data[np.where(parcellation.data == ip)] = ip
        masked_parcellation.save(tmap[:-4] + "_parcelmax.nii")

    return pfile
def generate_noise(stim_induced_signal, noise_var):
    noise = np.random.randn(*stim_induced_signal.data.shape) * noise_var**.5
    return xndarray.xndarray_like(stim_induced_signal, data=noise)
Ejemplo n.º 5
0
def make_parcellation(subject, dest_dir='parcellation', roi_mask_file=None):
    """
    Perform a functional parcellation from input fmri data
    
    Return: parcellation file name (str)
    """
    # Loading names for folders and files
    # - T maps (input)
    #func_files = glob(op.join(op.join(op.join('./', subject), \
    #                    't_maps'), 'BOLD*nii'))
    #func_files = glob(op.join('./', subject, 'ASLf', 'spm_analysis', \
    #                            'Tmaps*img'))
    func_files = glob(op.join('./', subject, 'ASLf', 'spm_analysis', \
                                'spmT*img'))
    print 'Tmap files: ', func_files 

    # - Mask (input)
    #spm_mask_file = op.join(spm_maps_dir, 'mask.img')
    mask_dir = op.join('./', subject, 'preprocessed_data')
    if not op.exists(mask_dir): os.makedirs(mask_dir)
    mask_file = op.join(mask_dir, 'mask.nii')
    mask = op.join(mask_dir, 'rcut_tissue_mask.nii')
    volume = op.join('./', subject, 'ASLf', 'funct', 'coregister', \
                     'mean' + subject + '_ASLf_correctionT1_0001.nii')
    make_mask(mask, volume, mask_file)

    # - parcellation (output)
    parcellation_dir = op.join('./', subject, dest_dir)
    if not op.exists(parcellation_dir): os.makedirs(parcellation_dir)
    pfile = op.join(parcellation_dir, 'parcellation_func.nii')

    # Parcellation
    from pyhrf.parcellation import make_parcellation_from_files 
    #make_parcellation_from_files(func_files, mask_file, pfile, 
    #                             nparcels=200, method='ward_and_gkm')

    # Masking with a ROI so we just consider parcels inside 
    # a certain area of the brain
    #if roi_mask_file is not None:   
    if 0: #for ip in np.array([11, 51, 131, 194]):
        #ip = 200

        #print 'Masking parcellation with roi_mask_file: ', roi_mask_file
        print 'Masking ROI: ', ip
        pfile_masked = op.join(parcellation_dir, 'parcellation_func_masked_roi' + str(ip) + '.nii')

        from pyhrf.ndarray import xndarray
        parcellation = xndarray.load(pfile)
        #m = xndarray.load(roi_mask_file)
        #parcels_to_keep = np.unique(parcellation.data * m.data)
        masked_parcellation = xndarray.xndarray_like(parcellation)
        #for ip in parcels_to_keep:
        #    masked_parcellation.data[np.where(parcellation.data==ip)] = ip
        
        masked_parcellation.data[np.where(parcellation.data==ip)] = ip
        masked_parcellation.save(pfile_masked)

    from pyhrf.ndarray import xndarray
    for tmap in func_files:
        func_file_i = xndarray.load(tmap)
        func_data = func_file_i.data
        #func_file_i.data[np.where(func_data<0.1*func_data.max())] = 0

        parcellation = xndarray.load(pfile)
        print func_data.max()
        print func_data.argmax()
        #ip = parcellation.data[func_data.argmax()]
        ip = parcellation.data[np.unravel_index(func_data.argmax(), parcellation.data.shape)]
        print ip.shape

        masked_parcellation = xndarray.xndarray_like(parcellation)
        print masked_parcellation.data.shape
        print parcellation.data.shape
        masked_parcellation.data[np.where(parcellation.data==ip)] = ip
        masked_parcellation.save(tmap[:-4] + '_parcelmax.nii')

    return pfile