예제 #1
0
파일: scenarios.py 프로젝트: pyhrf/pyhrf
 def save_time_series(k):
     if simulation.has_key(k):
         fn_stim_induced = add_prefix(
             op.join(output_dir, k + '.nii'), prefix)
         logger.info('%s flat shape %s', k, str(simulation[k].shape))
         vol = expand_array_in_mask(simulation[k], mask_vol, flat_axis=1)
         write_volume(np.rollaxis(vol, 0, 4), fn_stim_induced)
예제 #2
0
def split_big_parcels(parcel_file, output_file, max_size=400):
    print 'split_big_parcels ...'
    roiMask, roiHeader = read_volume(parcel_file)
    roiIds = np.unique(roiMask)
    background = roiIds.min()
    labels = roiMask[np.where(roiMask > background)].astype(int)
    if (np.bincount(labels) <= max_size).all():
        logger.info('no parcel to split')
        return

    graphs = parcels_to_graphs(roiMask, kerMask3D_6n)
    for roiId in roiIds:
        if roiId != background:
            roi_size = (roiMask == roiId).sum()
            if roi_size > max_size:
                print 'roi %d, size = %d' % (roiId, roi_size)
                nparcels = int(np.ceil(roi_size * 1. / max_size))
                print 'split into %d parcels ...' % (nparcels)
                split_parcel(labels,
                             graphs,
                             roiId,
                             nparcels,
                             inplace=True,
                             verbosity=1)

    final_roi_mask = np.zeros_like(roiMask)
    final_roi_mask[np.where(roiMask > background)] = labels
    assert (np.bincount(labels) <= max_size).all()
    write_volume(final_roi_mask, output_file, roiHeader)
예제 #3
0
def split_big_parcels(parcel_file, output_file, max_size=400):
    print 'split_big_parcels ...'
    roiMask, roiHeader = read_volume(parcel_file)
    roiIds = np.unique(roiMask)
    background = roiIds.min()
    labels = roiMask[np.where(roiMask>background)].astype(int)
    if (np.bincount(labels) <= max_size).all():
        pyhrf.verbose(1, 'no parcel to split')
        return

    graphs = parcels_to_graphs(roiMask, kerMask3D_6n)
    for roiId in roiIds:
        if roiId != background:
            roi_size = (roiMask==roiId).sum()
            if roi_size > max_size:
                print 'roi %d, size = %d' %(roiId, roi_size)
                nparcels = int(np.ceil(roi_size*1./max_size))
                print 'split into %d parcels ...' %(nparcels)
                split_parcel(labels, graphs, roiId, nparcels, inplace=True,
                             verbosity=1)

    final_roi_mask = np.zeros_like(roiMask)
    final_roi_mask[np.where(roiMask>background)] = labels
    #print np.bincount(labels)
    assert (np.bincount(labels) <= max_size).all()
    write_volume(final_roi_mask, output_file, roiHeader)
예제 #4
0
    def save(self, output_dir):
        """
        Save paradigm to output_dir/paradigm.csv,
        BOLD to output_dir/bold.nii, mask to output_dir/mask.nii
        #TODO: handle multi-session

        Return: tuple of file names in this order: (paradigm, bold, mask)
        """
        from pyhrf.tools._io import write_volume, write_texture
        paradigm_file = op.join(output_dir, 'paradigm.csv')
        self.paradigm.save_csv(paradigm_file)
        if self.data_type == 'volume':
            # unflatten bold
            bold_vol = expand_array_in_mask(self.bold, self.roiMask, 1)
            bold_vol = np.rollaxis(bold_vol, 0, 4)
            bold_file = op.join(output_dir, 'bold.nii')
            write_volume(bold_vol, bold_file, self.meta_obj)

            mask_file = op.join(output_dir, 'mask.nii')
            write_volume(self.roiMask, mask_file, self.meta_obj)

        elif self.data_type == 'surface':  # TODO surface
            bold_file = op.join(output_dir, 'bold.gii')
            write_texture(self.bold_vol, bold_file, self.meta_obj)
            pass

        return paradigm_file, bold_file, mask_file
예제 #5
0
 def save_time_series(k):
     if simulation.has_key(k):
         fn_stim_induced = add_prefix(op.join(output_dir, k + '.nii'),
                                      prefix)
         logger.info('%s flat shape %s', k, str(simulation[k].shape))
         vol = expand_array_in_mask(simulation[k], mask_vol, flat_axis=1)
         write_volume(np.rollaxis(vol, 0, 4), fn_stim_induced)
예제 #6
0
파일: glm.py 프로젝트: pmesejo/pyhrf
    def glm_matlab_from_files(bold_file, tr, paradigm_csv_file, output_dir,
                              mask_file, hf_cut=128, hack_mask=False):
        """
        Only mono-session
        #TODO: compute mask if mask_file does not exist
        #TODO: handle contrasts
        """

        # Functional mask
        # if not op.exists(mask_file):
        #     pyhrf.verbose(1, 'Mask file does not exist. Computing mask from '\
        #                       'BOLD data')
        #     compute_mask_files(bold_files, mask_file, False, 0.4, 0.9)


        #split BOLD into 3D vols:
        bold, hbold = read_volume(bold_file)
        bold_files = []
        tmp_path = tempfile.mkdtemp(dir=pyhrf.cfg['global']['tmp_path'])
        for iscan, bscan in enumerate(bold):
            f = op.join(tmp_path, 'bold_%06d.nii' %iscan)
            write_volume(bscan, f, hbold)
            bold_files.append(f)
        bold_files = ';'.join(bold_files)

        script_path = op.join(op.dirname(pyhrf.__file__),'../../script/SPM')
        spm_path = pyhrf.cfg['global']['spm_path']
        matlab_code = "cd %s;paradigm_file='%s';TR=%f;mask_file='%s';" \
            "bold_files='%s';output_path='%s';" \
            "HF_cut=%f;spm_path='%s';api=1;hack_mask=%d;glm_intra_subj;exit" \
            %(script_path,paradigm_csv_file,tr,mask_file,bold_files,output_dir,
              hf_cut,spm_path,hack_mask)

        matlab_cmd = 'matlab -nosplash -nodesktop -r "%s"'%matlab_code

        if op.exists(op.join(output_dir,'SPM.mat')):
            #remove SPM.mat so that SPM won't ask over ask overwriting
            os.remove(op.join(output_dir,'SPM.mat'))

        #print 'matlab cmd:'
        #print matlab_cmd
        os.system(matlab_cmd)

        # Fix shape of outputs if necessary
        # eg if input data has shape (n,m,1) then SPM will write outputs of
        # shape (n,m) so that they are not consistent with their QForm
        input_shape = bscan.shape
        for foutput in glob.glob(op.join(output_dir, '*.img')):
            data, h = read_volume(foutput)
            if data.ndim < 3:
                sm = ','.join([ [':','np.newaxis'][d==1] \
                                    for d in input_shape ] )
                exec('data = data[%s]' %sm)
                assert data.shape == input_shape
                write_volume(data, foutput, h)

        shutil.rmtree(tmp_path)
        #TODO: maybe find a better way to grab beta file names
        beta_files = sorted(glob.glob(op.join(output_dir,'beta_*.img')))
        return beta_files
예제 #7
0
파일: core.py 프로젝트: rcherbonnier/pyhrf
    def save(self, output_dir):
        """
        Save paradigm to output_dir/paradigm.csv,
        BOLD to output_dir/bold.nii, mask to output_dir/mask.nii
        #TODO: handle multi-session

        Return: tuple of file names in this order: (paradigm, bold, mask)
        """
        from pyhrf.tools._io import write_volume, write_texture
        paradigm_file = op.join(output_dir, 'paradigm.csv')
        self.paradigm.save_csv(paradigm_file)
        if self.data_type == 'volume':
            # unflatten bold
            bold_vol = expand_array_in_mask(self.bold, self.roiMask, 1)
            bold_vol = np.rollaxis(bold_vol, 0, 4)
            bold_file = op.join(output_dir, 'bold.nii')
            write_volume(bold_vol, bold_file, self.meta_obj)

            mask_file = op.join(output_dir, 'mask.nii')
            write_volume(self.roiMask, mask_file, self.meta_obj)

        elif self.data_type == 'surface':  # TODO surface
            bold_file = op.join(output_dir, 'bold.gii')
            write_texture(self.bold_vol, bold_file, self.meta_obj)
            pass

        return paradigm_file, bold_file, mask_file
예제 #8
0
파일: glm.py 프로젝트: zddzxxsmile/pyhrf
    def glm_matlab_from_files(bold_file, tr, paradigm_csv_file, output_dir,
                              mask_file, hf_cut=128, hack_mask=False):
        """
        Only mono-session
        #TODO: compute mask if mask_file does not exist
        #TODO: handle contrasts
        """

        # Functional mask
        # if not op.exists(mask_file):
        #     logger.info('Mask file does not exist. Computing mask from '
        #                 'BOLD data')
        #     compute_mask_files(bold_files, mask_file, False, 0.4, 0.9)

        # split BOLD into 3D vols:
        bold, hbold = read_volume(bold_file)
        bold_files = []
        tmp_path = tempfile.mkdtemp(dir=pyhrf.cfg['global']['tmp_path'])
        for iscan, bscan in enumerate(bold):
            f = op.join(tmp_path, 'bold_%06d.nii' % iscan)
            write_volume(bscan, f, hbold)
            bold_files.append(f)
        bold_files = ';'.join(bold_files)

        script_path = op.join(op.dirname(pyhrf.__file__), '../../script/SPM')
        spm_path = pyhrf.cfg['global']['spm_path']
        matlab_code = "cd %s;paradigm_file='%s';TR=%f;mask_file='%s';" \
            "bold_files='%s';output_path='%s';" \
            "HF_cut=%f;spm_path='%s';api=1;hack_mask=%d;glm_intra_subj;exit" \
            % (script_path, paradigm_csv_file, tr, mask_file, bold_files, output_dir,
               hf_cut, spm_path, hack_mask)

        matlab_cmd = 'matlab -nosplash -nodesktop -r "%s"' % matlab_code

        if op.exists(op.join(output_dir, 'SPM.mat')):
            # remove SPM.mat so that SPM won't ask over ask overwriting
            os.remove(op.join(output_dir, 'SPM.mat'))

        # print 'matlab cmd:'
        # print matlab_cmd
        os.system(matlab_cmd)

        # Fix shape of outputs if necessary
        # eg if input data has shape (n,m,1) then SPM will write outputs of
        # shape (n,m) so that they are not consistent with their QForm
        input_shape = bscan.shape
        for foutput in glob.glob(op.join(output_dir, '*.img')):
            data, h = read_volume(foutput)
            if data.ndim < 3:
                sm = ','.join([[':', 'np.newaxis'][d == 1]
                               for d in input_shape])
                exec('data = data[%s]' % sm)
                assert data.shape == input_shape
                write_volume(data, foutput, h)

        shutil.rmtree(tmp_path)
        # TODO: maybe find a better way to grab beta file names
        beta_files = sorted(glob.glob(op.join(output_dir, 'beta_*.img')))
        return beta_files
예제 #9
0
    def test_pyhrf_extract_cc_vol(self):
        test_mask = np.array(
            [[[1, 1, 0, 1, 1], [1, 1, 0, 1, 1], [0, 0, 0, 0, 0],
              [1, 0, 1, 1, 0], [0, 0, 1, 1, 0]],
             [[1, 1, 0, 1, 1], [1, 1, 0, 1, 1], [0, 0, 0, 0, 0],
              [0, 0, 1, 1, 0], [0, 0, 1, 1, 0]]],
            dtype=int)
        mask_file = op.join(self.tmp_dir, 'test_mask.nii')
        write_volume(test_mask, mask_file)

        cmd = 'pyhrf_extract_cc_vol -v0 -m 2 %s' % mask_file
        if os.system(cmd) != 0:
            raise Exception('Command %s failed' % cmd)
        assert len(os.listdir(self.tmp_dir)) == 4
예제 #10
0
파일: graphtest.py 프로젝트: pyhrf/pyhrf
    def test_pyhrf_extract_cc_vol(self):
        test_mask = np.array([[[1, 1, 0, 1, 1],
                               [1, 1, 0, 1, 1],
                               [0, 0, 0, 0, 0],
                               [1, 0, 1, 1, 0],
                               [0, 0, 1, 1, 0]],
                              [[1, 1, 0, 1, 1],
                               [1, 1, 0, 1, 1],
                               [0, 0, 0, 0, 0],
                               [0, 0, 1, 1, 0],
                               [0, 0, 1, 1, 0]]],
                             dtype=int)
        mask_file = op.join(self.tmp_dir, 'test_mask.nii')
        write_volume(test_mask, mask_file)

        cmd = 'pyhrf_extract_cc_vol -v0 -m 2 %s' % mask_file
        if os.system(cmd) != 0:
            raise Exception('Command %s failed' % cmd)
        assert len(os.listdir(self.tmp_dir)) == 4
예제 #11
0
def make_parcellation_cubed_blobs_from_file(parcellation_file,
                                            output_path,
                                            roi_ids=None,
                                            bg_parcel=0,
                                            skip_existing=False):

    p, mp = read_volume(parcellation_file)
    p = p.astype(np.int32)
    if bg_parcel == 0 and p.min() == -1:
        p += 1  # set background to 0

    if roi_ids is None:
        roi_ids = np.unique(p)

    logger.info('%d rois to extract', (len(roi_ids) - 1))

    tmp_dir = pyhrf.get_tmp_path('blob_parcellation')
    tmp_parcel_mask_file = op.join(tmp_dir, 'parcel_for_blob.nii')

    out_files = []
    for roi_id in roi_ids:
        if roi_id != bg_parcel:  # discard background
            output_blob_file = op.join(output_path,
                                       'parcel_%d_cubed_blob.arg' % roi_id)
            out_files.append(output_blob_file)
            if skip_existing and os.path.exists(output_blob_file):
                continue
            parcel_mask = (p == roi_id).astype(np.int32)
            write_volume(parcel_mask, tmp_parcel_mask_file, mp)
            logger.info('Extract ROI %d -> %s', roi_id, output_blob_file)
            cmd = 'AimsGraphConvert -i %s -o %s --bucket' \
                % (tmp_parcel_mask_file, output_blob_file)
            logger.info('Cmd: %s', cmd)
            os.system(cmd)
    if op.exists(tmp_parcel_mask_file):
        os.remove(tmp_parcel_mask_file)

    return out_files
예제 #12
0
def make_parcellation_cubed_blobs_from_file(parcellation_file, output_path,
                                            roi_ids=None, bg_parcel=0,
                                            skip_existing=False):


    p,mp = read_volume(parcellation_file)
    p = p.astype(np.int32)
    if bg_parcel==0 and p.min() == -1:
        p += 1 #set background to 0

    if roi_ids is None:
        roi_ids = np.unique(p)

    pyhrf.verbose(1,'%d rois to extract' %(len(roi_ids)-1))

    tmp_dir = pyhrf.get_tmp_path('blob_parcellation')
    tmp_parcel_mask_file = op.join(tmp_dir, 'parcel_for_blob.nii')

    out_files = []
    for roi_id in roi_ids:
        if roi_id != bg_parcel: #discard background
            output_blob_file = op.join(output_path, 'parcel_%d_cubed_blob.arg'\
                                           %roi_id)
            out_files.append(output_blob_file)
            if skip_existing and os.path.exists(output_blob_file):
                continue
            parcel_mask = (p==roi_id).astype(np.int32)
            write_volume(parcel_mask, tmp_parcel_mask_file, mp)
            pyhrf.verbose(3,'Extract ROI %d -> %s' %(roi_id,output_blob_file))
            cmd = 'AimsGraphConvert -i %s -o %s --bucket' \
                %(tmp_parcel_mask_file, output_blob_file)
            pyhrf.verbose(3,'Cmd: %s' %(cmd))
            os.system(cmd)
    if op.exists(tmp_parcel_mask_file):
        os.remove(tmp_parcel_mask_file)

    return out_files
예제 #13
0
파일: surface.py 프로젝트: pmesejo/pyhrf
def project_fmri_from_kernels(input_mesh, kernels_file, fmri_data_file,
                              output_tex, bin_threshold=None, ):

    pyhrf.verbose(2,'Project data onto mesh using kernels ...')

    if 0:
        print 'Projecting ...'
        print 'func data:', fmri_data_file
        print 'Mesh file:', input_mesh
        print 'Save as:', output_tex

    pyhrf.verbose(2,'Call AimsFunctionProjection -op 1 ...')    

    data_files = []
    output_texs = []
    p_ids = None
    if bin_threshold is not None:
        d,h = read_volume(fmri_data_file)
        if np.allclose(d.astype(int), d):
            tmp_dir = pyhrf.get_tmp_path()
            p_ids = np.unique(d)
            pyhrf.verbose(2, 'bin threshold: %f' %bin_threshold)
            pyhrf.verbose(2, 'pids(n=%d): %d...%d' \
                              %(len(p_ids),min(p_ids),max(p_ids)))
            for i,p_id in enumerate(p_ids):
                if p_id != 0:
                    new_p = np.zeros_like(d)
                    new_p[np.where(d==p_id)] = i + 1 #0 is background
                    ifn = op.join(tmp_dir,'pmask_%d.nii'%p_id)
                    write_volume(new_p, ifn, h)
                    data_files.append(ifn)
                    ofn = op.join(tmp_dir,'ptex_%d.gii'%p_id)
                    output_texs.append(ofn)
        else:
            data_files.append(fmri_data_file)
            output_texs.append(output_tex)
    else:
        data_files.append(fmri_data_file)
        output_texs.append(output_tex)

    pyhrf.verbose(3, 'input data files: %s' %str(data_files))
    pyhrf.verbose(3, 'output data files: %s' %str(output_texs))

    for data_file, o_tex in zip(data_files, output_texs):
        projection = [ 
            'AimsFunctionProjection', 
            '-op', '1',
            '-d', kernels_file,
            '-d1', data_file,
            '-m', input_mesh,
            '-o', o_tex
            ]

        cmd = ' '.join(map(str,projection))
        pyhrf.verbose(3, 'cmd: %s' %cmd)
        os.system(cmd)

    if bin_threshold is not None:
        pyhrf.verbose(2, 'Binary threshold of texture at %f' %bin_threshold)
        o_tex = output_texs[0]
        data,data_gii = read_texture(o_tex)
        data = (data>bin_threshold).astype(np.int32)
        print 'data:', data.dtype
        if p_ids is not None:
            for pid, o_tex in zip(p_ids[1:], output_texs[1:]):
                pdata,pdata_gii = read_texture(o_tex)
                data += (pdata>bin_threshold).astype(np.int32) * pid

        #assert (np.unique(data) == p_ids).all()
        write_texture(data, output_tex, intent='NIFTI_INTENT_LABEL')
예제 #14
0
def simulation_save_vol_outputs(simulation,
                                output_dir,
                                bold_3D_vols_dir=None,
                                simulation_graph_output=None,
                                prefix=None,
                                vol_meta=None):
    """ simulation_graph_output : None, 'simple', 'thumbnails' #TODO
    """

    if simulation.has_key('paradigm'):
        fn = add_prefix(op.join(output_dir, 'paradigm.csv'), prefix)
        simulation['paradigm'].save_csv(fn)

    # Save all volumes in nifti format:
    if simulation.has_key('labels_vol'):
        mask_vol = np.ones_like(simulation['labels_vol'][0])
    elif simulation.has_key('mask'):
        mask_vol = simulation.get('mask', None)
    elif simulation.has_key('labels'):
        mask_vol = np.ones_like(simulation['labels'][0])
    else:
        raise Exception('Dunno where to get mask')

    logger.info('Vol mask of shape %s', str(mask_vol.shape))

    fn_mask = add_prefix(op.join(output_dir, 'mask.nii'), prefix)
    write_volume(mask_vol.astype(np.int32), fn_mask, vol_meta)

    if simulation.has_key('hrf_territories'):
        fn_h_territories = add_prefix(
            op.join(output_dir, 'hrf_territories.nii'), prefix)

        ht = expand_array_in_mask(simulation['hrf_territories'] + 1, mask_vol)
        write_volume(ht, fn_h_territories, vol_meta)

    if simulation.has_key('hrf'):
        from pyhrf.ndarray import MRI3Daxes
        fn_hrf = add_prefix(op.join(output_dir, 'hrf.nii'), prefix)
        logger.info('hrf flat shape %s', str(simulation['hrf'].shape))
        if simulation['hrf'].ndim == 1:
            hrf = (np.ones(mask_vol.size) * simulation['hrf'][:, np.newaxis])
        else:
            hrf = simulation['hrf']

        hrfs_vol = expand_array_in_mask(hrf, mask_vol, flat_axis=1)
        dt = simulation['dt']
        chrfs = xndarray(
            hrfs_vol,
            axes_names=[
                'time',
            ] + MRI3Daxes,
            axes_domains={'time': np.arange(hrfs_vol.shape[0]) * dt})
        chrfs.save(fn_hrf, vol_meta)

        ttp_vol = hrfs_vol.argmax(0)
        fn_ttp = add_prefix(op.join(output_dir, 'ttp.nii'), prefix)
        write_volume(ttp_vol, fn_ttp, vol_meta)

    if simulation.has_key('brf'):
        from pyhrf.ndarray import MRI3Daxes
        fn_brf = add_prefix(op.join(output_dir, 'brf.nii'), prefix)
        logger.info('brf flat shape %s', str(simulation['brf'].shape))
        brfs_vol = expand_array_in_mask(simulation['brf'],
                                        mask_vol,
                                        flat_axis=1)
        dt = simulation['dt']
        cbrfs = xndarray(
            brfs_vol,
            axes_names=[
                'time',
            ] + MRI3Daxes,
            axes_domains={'time': np.arange(brfs_vol.shape[0]) * dt})
        cbrfs.save(fn_brf, vol_meta)

    if simulation.has_key('prf'):
        from pyhrf.ndarray import MRI3Daxes
        fn_brf = add_prefix(op.join(output_dir, 'prf.nii'), prefix)
        logger.info('prf flat shape %s', str(simulation['prf'].shape))
        brfs_vol = expand_array_in_mask(simulation['prf'],
                                        mask_vol,
                                        flat_axis=1)
        dt = simulation['dt']
        cbrfs = xndarray(
            brfs_vol,
            axes_names=[
                'time',
            ] + MRI3Daxes,
            axes_domains={'time': np.arange(brfs_vol.shape[0]) * dt})
        cbrfs.save(fn_brf, vol_meta)

    if simulation.has_key('drift'):
        fn_drift = add_prefix(op.join(output_dir, 'drift.nii'), prefix)
        logger.info('drift flat shape %s', str(simulation['drift'].shape))
        drift_vol = expand_array_in_mask(simulation['drift'],
                                         mask_vol,
                                         flat_axis=1)
        write_volume(np.rollaxis(drift_vol, 0, 4), fn_drift)

    if simulation.has_key('drift_coeffs'):
        fn_drift = add_prefix(op.join(output_dir, 'drift_coeffs.nii'), prefix)
        logger.info('drift flat shape %s',
                    str(simulation['drift_coeffs'].shape))
        drift_vol = expand_array_in_mask(simulation['drift'],
                                         mask_vol,
                                         flat_axis=1)
        write_volume(np.rollaxis(drift_vol, 0, 4), fn_drift)

    if simulation.has_key('noise'):
        fn_noise = add_prefix(op.join(output_dir, 'noise.nii'), prefix)
        logger.info('noise flat shape %s', str(simulation['noise'].shape))
        noise_vol = expand_array_in_mask(simulation['noise'],
                                         mask_vol,
                                         flat_axis=1)
        write_volume(np.rollaxis(noise_vol, 0, 4), fn_noise, vol_meta)

        fn_noise = add_prefix(op.join(output_dir, 'noise_emp_var.nii'), prefix)
        noise_vol = expand_array_in_mask(simulation['noise'].var(0), mask_vol)
        write_volume(noise_vol, fn_noise, vol_meta)

    if simulation.has_key('noise_var'):
        fn_noise_var = add_prefix(op.join(output_dir, 'noise_var.nii'), prefix)
        logger.info('noise_var flat shape %s',
                    str(simulation['noise_var'].shape))
        noise_var_vol = expand_array_in_mask(simulation['noise_var'], mask_vol)
        write_volume(noise_var_vol, fn_noise_var, vol_meta)

    if simulation.has_key('stim_induced_signal'):
        fn_stim_induced = add_prefix(op.join(output_dir, 'stim_induced.nii'),
                                     prefix)
        logger.info('stim_induced flat shape %s',
                    str(simulation['stim_induced_signal'].shape))
        stim_induced_vol = expand_array_in_mask(
            simulation['stim_induced_signal'], mask_vol, flat_axis=1)
        write_volume(np.rollaxis(stim_induced_vol, 0, 4), fn_stim_induced)

    if simulation.has_key('perf_stim_induced'):
        fn_stim_induced = add_prefix(
            op.join(output_dir, 'perf_stim_induced.nii'), prefix)
        logger.info('asl_stim_induced flat shape %s',
                    str(simulation['perf_stim_induced'].shape))
        stim_induced_vol = expand_array_in_mask(
            simulation['perf_stim_induced'], mask_vol, flat_axis=1)
        write_volume(np.rollaxis(stim_induced_vol, 0, 4), fn_stim_induced)

        fn_stim_induced = add_prefix(
            op.join(output_dir, 'perf_stim_induced_ct.nii'), prefix)
        logger.info('asl_stim_induced flat shape %s',
                    str(simulation['perf_stim_induced'].shape))

        dsf = simulation['dsf']
        perf = np.dot(simulation['ctrl_tag_mat'],
                      simulation['perf_stim_induced'][0:-1:dsf])
        stim_induced_vol = expand_array_in_mask(perf, mask_vol, flat_axis=1)
        write_volume(np.rollaxis(stim_induced_vol, 0, 4), fn_stim_induced)

    if simulation.has_key('perf_baseline'):
        fn = add_prefix(op.join(output_dir, 'perf_baseline.nii'), prefix)
        pb = np.zeros_like(simulation['bold']) + simulation['perf_baseline']
        write_volume(expand_array_in_mask(pb[0], mask_vol), fn, vol_meta)

    if simulation.has_key('bold_stim_induced'):
        fn_stim_induced = add_prefix(
            op.join(output_dir, 'bold_stim_induced.nii'), prefix)
        logger.info('asl_stim_induced flat shape %s',
                    str(simulation['bold_stim_induced'].shape))
        stim_induced_vol = expand_array_in_mask(
            simulation['bold_stim_induced'], mask_vol, flat_axis=1)
        write_volume(np.rollaxis(stim_induced_vol, 0, 4), fn_stim_induced)

    m = np.where(mask_vol)
    labels_and_mask = mask_vol.copy()[m]

    for ic in xrange(simulation['labels'].shape[0]):
        if simulation.has_key('condition_defs'):
            c_name = simulation['condition_defs'][ic].name
        else:
            c_name = 'cond%d' % ic
        fn_labels = add_prefix(op.join(output_dir, 'labels_%s.nii' % c_name),
                               prefix)
        if simulation.has_key('labels'):
            labels_c = simulation['labels'][ic]
            labels_and_mask[np.where(labels_c)] = ic + 2
            write_volume(
                expand_array_in_mask(labels_c, mask_vol).astype(np.int32),
                fn_labels, vol_meta)
        elif simulation.has_key('labels_vol'):
            labels_c = simulation['labels_vol'][ic]
            labels_and_mask[np.where(labels_c[m])] = ic + 2
            write_volume(labels_c.astype(np.int32), fn_labels, vol_meta)

        if simulation.has_key('nrls'):
            nrls_c = simulation['nrls'][ic]
            fn = add_prefix(op.join(output_dir, 'nrls_%s.nii' % c_name),
                            prefix)
            write_volume(expand_array_in_mask(nrls_c, mask_vol), fn, vol_meta)
        if simulation.has_key('nrls_session'):
            nrls_session_c = simulation['nrls_session'][ic]
            fn = add_prefix(
                op.join(output_dir, 'nrls_session_%s.nii' % (c_name)), prefix)
            write_volume(expand_array_in_mask(nrls_session_c, mask_vol), fn,
                         vol_meta)

        if simulation.has_key('brls'):
            brls_c = simulation['brls'][ic]
            fn = add_prefix(op.join(output_dir, 'brls_%s.nii' % c_name),
                            prefix)
            write_volume(expand_array_in_mask(brls_c, mask_vol), fn, vol_meta)
        if simulation.has_key('prls'):
            prls_c = simulation['prls'][ic]
            fn = add_prefix(op.join(output_dir, 'prls_%s.nii' % c_name),
                            prefix)
            write_volume(expand_array_in_mask(prls_c, mask_vol), fn, vol_meta)

        if simulation.has_key('neural_efficacies'):
            ne_c = simulation['neural_efficacies'][ic]
            fn = add_prefix(
                op.join(output_dir, 'neural_efficacies_%s.nii' % c_name),
                prefix)
            write_volume(expand_array_in_mask(ne_c, mask_vol), fn, vol_meta)

    fn_labels_and_mask = add_prefix(op.join(output_dir, 'mask_and_labels.nii'),
                                    prefix)

    write_volume(
        expand_array_in_mask(labels_and_mask, mask_vol).astype(int),
        fn_labels_and_mask, vol_meta)

    if simulation.has_key('bold_full_vol') or simulation.has_key('bold'):
        fn = add_prefix(op.join(output_dir, 'bold.nii'), prefix)
        if simulation.has_key('bold_full_vol'):
            bold4D = simulation['bold_full_vol']
        else:
            bold = simulation['bold']
            bold4D = expand_array_in_mask(bold, mask_vol, flat_axis=1)

        write_volume(np.rollaxis(bold4D, 0, 4), fn, vol_meta)

    def save_time_series(k):
        if simulation.has_key(k):
            fn_stim_induced = add_prefix(op.join(output_dir, k + '.nii'),
                                         prefix)
            logger.info('%s flat shape %s', k, str(simulation[k].shape))
            vol = expand_array_in_mask(simulation[k], mask_vol, flat_axis=1)
            write_volume(np.rollaxis(vol, 0, 4), fn_stim_induced)

    save_time_series('flow_induction')
    save_time_series('cbv')
    save_time_series('hbr')
    save_time_series('bold_stim_induced_rescaled')

    if simulation.has_key('asl'):
        fn = add_prefix(op.join(output_dir, 'asl.nii'), prefix)
        asl4D = expand_array_in_mask(simulation['asl'], mask_vol, flat_axis=1)
        write_volume(np.rollaxis(asl4D, 0, 4), fn, vol_meta)

    if simulation.has_key('outliers'):
        fn = add_prefix(op.join(output_dir, 'outliers.nii'), prefix)
        outliers = expand_array_in_mask(simulation['outliers'],
                                        mask_vol,
                                        flat_axis=1)

        write_volume(np.rollaxis(outliers, 0, 4), fn, vol_meta)

    if simulation.has_key('hrf_group'):
        hrfgroup = simulation['hrf_group']
        nb_vox = mask_vol.size
        fn_hrf = add_prefix(op.join(output_dir, 'hrf_group.nii'), prefix)
        logger.info('hrf group shape %s', str(simulation['hrf_group'].shape))
        hrfGd = duplicate_hrf(nb_vox, hrfgroup)
        hrfs_vol = expand_array_in_mask(hrfGd, mask_vol, flat_axis=1)
        dt = simulation['dt']
        chrfs = xndarray(
            hrfs_vol,
            axes_names=[
                'time',
            ] + MRI3Daxes,
            axes_domains={'time': np.arange(hrfs_vol.shape[0]) * dt})
        chrfs.save(fn_hrf, vol_meta)

    if bold_3D_vols_dir is not None:
        assert op.exists(bold_3D_vols_dir)
        for iscan, bscan in enumerate(bold4D):
            fnout = add_prefix('bold_%06d.nii' % (iscan), prefix)
            write_volume(bscan, op.join(bold_3D_vols_dir, fnout), vol_meta)
예제 #15
0
def glm_nipy_from_files(bold_file,
                        tr,
                        paradigm_csv_file,
                        output_dir,
                        mask_file,
                        session=0,
                        contrasts=None,
                        con_test_baseline=0.0,
                        hrf_model='Canonical',
                        drift_model='Cosine',
                        hfcut=128,
                        residuals_model='spherical',
                        fit_method='ols',
                        fir_delays=[0]):
    """
    #TODO: handle surface data
    hrf_model : Canonical | Canonical with Derivative | FIR

    """

    fdata = FmriData.from_vol_files(mask_file, paradigm_csv_file, [bold_file],
                                    tr)
    g, dm, cons = glm_nipy(fdata,
                           contrasts=contrasts,
                           hrf_model=hrf_model,
                           hfcut=hfcut,
                           drift_model=drift_model,
                           residuals_model=residuals_model,
                           fit_method=fit_method,
                           fir_delays=fir_delays)

    ns, nr = dm.matrix.shape
    cdesign_matrix = xndarray(dm.matrix,
                              axes_names=['time', 'regressor'],
                              axes_domains={
                                  'time': np.arange(ns) * tr,
                                  'regressor': dm.names
                              })

    cdesign_matrix.save(op.join(output_dir, 'design_matrix.nii'))

    beta_files = []
    beta_values = dict.fromkeys(dm.names)
    beta_vars = dict.fromkeys(dm.names)
    beta_vars_voxels = dict.fromkeys(dm.names)
    for ib, bname in enumerate(dm.names):
        # beta values
        beta_vol = expand_array_in_mask(g.beta[ib], fdata.roiMask > 0)
        beta_fn = op.join(output_dir, 'beta_%s.nii' % bname)
        write_volume(beta_vol, beta_fn, fdata.meta_obj)
        beta_files.append(beta_fn)
        beta_values[bname] = beta_vol

        # normalized variance of betas
        # variance: diag of cov matrix
        beta_vars[bname] = sp.diag(g.nvbeta)[ib]
        # sig2 = g.s2 #ResMS
        # variance for all voxels, condition ib
        var_cond = sp.diag(g.nvbeta)[ib] * g.s2
        beta_vars_voxels[bname] = var_cond
        #beta_var_fn = op.join(output_dir, 'var_beta_%s.nii' %bname)
        #write_volume(beta_var, beta_var_fn, fdata.meta_obj)
        # beta_var_files.append(beta_var_fn)

    if cons is not None:
        con_files = []
        pval_files = []
        for cname, con in cons.iteritems():
            con_vol = expand_array_in_mask(con.effect, fdata.roiMask > 0)
            con_fn = op.join(output_dir, 'con_effect_%s.nii' % cname)
            write_volume(con_vol, con_fn, fdata.meta_obj)
            con_files.append(con_fn)

            pval_vol = expand_array_in_mask(con.pvalue(con_test_baseline),
                                            fdata.roiMask > 0)
            pval_fn = op.join(output_dir, 'con_pvalue_%s.nii' % cname)
            write_volume(pval_vol, pval_fn, fdata.meta_obj)
            pval_files.append(pval_fn)
    else:
        con_files = None
        pval_files = None

    dof = g.dof
    # if do_ppm:
    # for

    # TODO: FIR stuffs
    # , con_files, pval_files
    return beta_files, beta_values, beta_vars_voxels, dof
예제 #16
0
def project_fmri_from_kernels(
    input_mesh,
    kernels_file,
    fmri_data_file,
    output_tex,
    bin_threshold=None,
):

    logger.info('Project data onto mesh using kernels ...')

    if 0:
        print 'Projecting ...'
        print 'func data:', fmri_data_file
        print 'Mesh file:', input_mesh
        print 'Save as:', output_tex

    logger.info('Call AimsFunctionProjection -op 1 ...')

    data_files = []
    output_texs = []
    p_ids = None
    if bin_threshold is not None:
        d, h = read_volume(fmri_data_file)
        if np.allclose(d.astype(int), d):
            tmp_dir = pyhrf.get_tmp_path()
            p_ids = np.unique(d)
            logger.info('bin threshold: %f', bin_threshold)
            logger.info('pids(n=%d): %d...%d', len(p_ids), min(p_ids),
                        max(p_ids))
            for i, p_id in enumerate(p_ids):
                if p_id != 0:
                    new_p = np.zeros_like(d)
                    new_p[np.where(d == p_id)] = i + 1  # 0 is background
                    ifn = op.join(tmp_dir, 'pmask_%d.nii' % p_id)
                    write_volume(new_p, ifn, h)
                    data_files.append(ifn)
                    ofn = op.join(tmp_dir, 'ptex_%d.gii' % p_id)
                    output_texs.append(ofn)
        else:
            data_files.append(fmri_data_file)
            output_texs.append(output_tex)
    else:
        data_files.append(fmri_data_file)
        output_texs.append(output_tex)

    logger.info('input data files: %s', str(data_files))
    logger.info('output data files: %s', str(output_texs))

    for data_file, o_tex in zip(data_files, output_texs):
        projection = [
            'AimsFunctionProjection', '-op', '1', '-d', kernels_file, '-d1',
            data_file, '-m', input_mesh, '-o', o_tex
        ]

        cmd = ' '.join(map(str, projection))
        logger.info('cmd: %s', cmd)
        os.system(cmd)

    if bin_threshold is not None:
        logger.info('Binary threshold of texture at %f', bin_threshold)
        o_tex = output_texs[0]
        data, data_gii = read_texture(o_tex)
        data = (data > bin_threshold).astype(np.int32)
        print 'data:', data.dtype
        if p_ids is not None:
            for pid, o_tex in zip(p_ids[1:], output_texs[1:]):
                pdata, pdata_gii = read_texture(o_tex)
                data += (pdata > bin_threshold).astype(np.int32) * pid

        #assert (np.unique(data) == p_ids).all()
        write_texture(data, output_tex, intent='NIFTI_INTENT_LABEL')
예제 #17
0
def parcellation_for_jde(fmri_data, avg_parcel_size=250, output_dir=None,
                         method='gkm', glm_drift='Cosine', glm_hfcut=128):
    """
    method: gkm, ward, ward_and_gkm
    """

    if output_dir is None:
        output_dir = tempfile.mkdtemp(prefix='pyhrf_JDE_parcellation_GLM',
                                      dir=pyhrf.cfg['global']['tmp_path'])
    glm_output_dir = op.join(output_dir, 'GLM_for_parcellation')
    if not op.exists(glm_output_dir): os.makedirs(glm_output_dir)

    pyhrf.verbose(1, 'GLM for parcellation')

    # if fmri_data.data_type == 'volume':
    #     paradigm_file, bold_file, mask_file = fmri_data.save(glm_output_dir)
    #     beta_files = glm_nipy_from_files(bold_file, fmri_data.tr, paradigm_file,
    #                                      glm_output_dir, mask_file,
    #                                      drift_model=glm_drift, hfcut=glm_hfcut)
    # elif fmri_data.data_type == 'surface':
    #     beta_files = glm_nipy(fmri_data, glm_output_dir,
    #                           drift_model=glm_drift, hfcut=glm_hfcut)

    g, dm, cons = glm_nipy(fmri_data, drift_model=glm_drift, hfcut=glm_hfcut)

    pval_files = []
    if cons is not None:
        func_data = [('con_pval_%s' %cname, con.pvalue()) \
                         for cname, con in cons.iteritems()]
    else:
        reg_cst_drift = re.compile(".*constant.*|.*drift.*")
        func_data = [('beta_%s' %reg_name, g.beta[ir]) \
                         for ir,reg_name in enumerate(dm.names) \
                         if not reg_cst_drift.match(reg_name)]

    for name, data in func_data:
        val_vol = expand_array_in_mask(data, fmri_data.roiMask>0)
        val_fn = op.join(glm_output_dir, '%s.nii' %name)
        write_volume(val_vol, val_fn, fmri_data.meta_obj)
        pval_files.append(val_fn)

    mask_file = op.join(glm_output_dir,'mask.nii')
    write_volume(fmri_data.roiMask>0, mask_file, fmri_data.meta_obj)

    nvox = fmri_data.get_nb_vox_in_mask()
    nparcels = round_nb_parcels(nvox * 1. / avg_parcel_size)

    pyhrf.verbose(1, 'Parcellation from GLM outputs, method: %s, ' \
                      'nb parcels: %d' %(method, nparcels))

    if fmri_data.data_type == 'volume':
        parcellation_file = op.join(output_dir, 'parcellation_%s_np%d.nii'
                                    %(method, nparcels))

        make_parcellation_from_files(pval_files, mask_file, parcellation_file,
                                     nparcels, method)
        parcellation,_ = read_volume(parcellation_file)
    else:
        mesh_file = fmri_data.data_files[-1]
        parcellation_file = op.join(output_dir, 'parcellation_%s_np%d.gii'
                                    %(method, nparcels))
        make_parcellation_surf_from_files(pval_files, mesh_file,
                                          parcellation_file, nparcels, method,
                                          verbose=1)
        parcellation,_ = read_texture(parcellation_file)
    #print parcellation_file


    pyhrf.verbose(1, parcellation_report(parcellation))

    return parcellation, parcellation_file
def BMA_consensus_cluster_parallel(cfg, remote_path, remote_BOLD_fn, remote_mask_fn, Y, nifti_masker, \
                        num_vox, K_clus, K_clusters, \
                        parc, alpha, prop, nbItRFIR, onsets, durations,\
                        output_sub_parc, rescale=True, averg_bold=False):
    '''
    Performs all steps for one clustering case (Kclus given, number l of the parcellation given)
    remote_path: path on the cluster, where results will be stored
    '''
    import os
    import sys
    sys.path.append("/home/pc174679/pyhrf/pyhrf-tree_trunk/script/WIP/Scripts_IRMf_BB/Parcellations/")
    sys.path.append("/home/pc174679/pyhrf/pyhrf-tree_trunk/script/WIP/Scripts_IRMf_Adultes_Solv/")
    sys.path.append("/home/pc174679/pyhrf/pyhrf-tree_trunk/script/WIP/Scripts_IRMf_Adultes_Solv/Scripts_divers_utiles/Scripts_utiles/")
    sys.path.append('/home/pc174679/local/installations/consensus-cluster-0.6')
    
    from Random_parcellations import random_parcellations, subsample_data_on_time
    from Divers_parcellations_test import *
    
    from RFIR_evaluation_parcellations import JDE_estim, RFIR_estim, clustering_from_RFIR
    
    from Random_parcellations import hrf_roi_to_vox
    from pyhrf.tools._io import remote_copy, remote_mkdir
    from nisl import io
    
    #nifti_masker.mask=remote_mask_fn
    
    # Creation of the necessary paths --> do not do here
    parc_name = 'Subsampled_data_with_' + str(K_clus) + 'clusters' 
    parc_name_clus = parc_name + 'rnd_number_' + str(parc+1)
    
    remote_sub = os.sep.join((remote_path, parc_name))   
    #if not os.path.exists(remote_sub):
        #os.path.exists(remote_sub)
        #print 'remote_sub:', remote_sub
        #os.makedirs(remote_sub)
    remote_sub_parc = os.sep.join((remote_sub,parc_name_clus))   
    #if not os.path.exists(remote_sub_parc):
        #os.makedirs(remote_sub_parc)
    
    output_RFIR_parc = os.sep.join((output_sub_parc,'RFIR_estim'))
    
    ###################################
    ## 1st STEP: SUBSAMPLING
    print '--- Subsample data ---'
    Ysub = subsample_data_on_time(Y, remote_mask_fn, K_clus, alpha, prop, \
                    nifti_masker, rescale=rescale)
    print 'Ysub:', Ysub
    print 'remote_sub_prc:', remote_sub_parc
    Ysub_name = 'Y_sub_'+ str(K_clus) + 'clusters_' + 'rnd_number_' + str(parc+1) +'.nii'
    Ysub_fn = os.sep.join((remote_sub_parc, Ysub_name))
    Ysub_masked = nifti_masker.inverse_transform(Ysub).get_data()
    write_volume(Ysub_masked, Ysub_fn)                        
    
    
    
    ###################################
    ## 2D STEP: RFIR
    print '--- Performs RFIR estimation ---'

    
    remote_RFIR_parc_clus = os.sep.join((remote_sub_parc, 'RFIR_estim'))
    #if not os.path.exists(remote_RFIR_parc):os.makedirs(remote_RFIR_parc)
    #remote_RFIR_parc_clus = os.sep.join((remote_RFIR_parc, parc_name_clus))
    #if not os.path.exists(remote_RFIR_parc_clus):os.makedirs(remote_RFIR_parc_clus)
    
    print '  * output path for RFIR ', remote_RFIR_parc_clus
    print '  * RFIR for subsampling nb ', str(parc+1), ' with ', K_clus, ' clusters' 
    RFIR_estim(nbItRFIR, onsets, durations, Ysub_fn, remote_mask_fn, \
                remote_RFIR_parc, avg_bold=averg_bold) 
                  
    hrf_fn = os.sep.join((remote_RFIR_parc_clus, 'rfir_ehrf.nii'))
    #remote_copy([hrf_fn], remote_host, 
                #remote_user, remote_path)[0]
    
    ###################################
    ## 3D STEP: CLUSTERING FROM RFIR RESULTS
    name_hrf = 'rfir_ehrf.nii'
    
    from pyhrf.tools._io import write_volume, read_volume
    from pyhrf.tools._io import read_volume, write_volume
    import nisl.io as ionisl
    from sklearn.feature_extraction import image
    from sklearn.cluster import WardAgglomeration
    from scipy.spatial.distance import cdist, pdist
    
    hrf_fn = os.sep.join((remote_RFIR_parc_clus,name_hrf))
    hrf=read_volume(hrf_fn)[0]
    hrf_t_fn = add_suffix(hrf_fn, 'transpose')
    #taking only 1st condition to parcellate
    write_volume(hrf[:,:,:,:,0], hrf_t_fn)
    
    nifti_masker = ionisl.NiftiMasker(remote_mask_fn)
    Nm = nifti_masker.fit(hrf_t_fn)
    
    #features: coeff of the HRF
    HRF = Nm.fit_transform(hrf_t_fn)
    
    mask, meta_data = read_volume(remote_mask_fn)
    shape = mask.shape
    connectivity = image.grid_to_graph(n_x=shape[0], n_y=shape[1],
            n_z=shape[2], mask=mask)
            
    #features used for clustering
    features = HRF.transpose()

    ward = WardAgglomeration(n_clusters=K_clus, connectivity=connectivity,
                                memory='nisl_cache')
    ward.fit(HRF)
    labels_tot = ward.labels_+1 
        
        
    #Kelbow, Perc_WSS, all_parc_from_RFIR_fns, all_parc_RFIR = \
    #clustering_from_RFIR(K_clusters, remote_RFIR_parc_clus, remote_mask_fn, name_hrf, plots=False)
    #labels_tot = all_parc_RFIR[str(Kelbow)]
    
    #to retrieve clustering with as many clusters as determined in K_clusters
    #labels_tot = all_parc_RFIR[str(K_clus)]
    #Parcellation retrieved: for K=Kelbow
    #clusters_RFIR_fn = all_parc_from_RFIR[str(Kelbow)]
    #clustering_rfir_fn = os.path.join(remote_RFIR_parc_clus, 'output_clustering_elbow.nii')
    #write_volume(read_volume(clusters_RFIR_fn)[0], clustering_rfir_fn, meta_bold)

    #labels_tot = nifti_masker.fit_transform([clusters_RFIR_fn])[0]
    #labels_tot = read_volume(clusters_RFIR_fn)[0]
    
    #labels_name='labels_' + str(int(K_clus)) + '_' + str(parc+1) + '.pck'
    #name_f = os.sep.join((remote_sub_parc, labels_name))
    #pickle_labels=open(name_f, 'w')
    #cPickle.dump(labels_tot,f)
    #pickle_labels.close()
    
    #remote_copy(pickle_labels, remote_user, 
            #remote_host, output_sub_parc)
    
    #################################
    ## Prepare consensus clustering
    print 'Prepare consensus clustering'
    clustcount, totalcount = upd_similarity_matrix(labels_tot)
    print 'results:', clustcount
    
    return clustcount.astype(np.bool)
예제 #19
0
def parcellation_for_jde(fmri_data,
                         avg_parcel_size=250,
                         output_dir=None,
                         method='gkm',
                         glm_drift='Cosine',
                         glm_hfcut=128):
    """
    method: gkm, ward, ward_and_gkm
    """

    if output_dir is None:
        output_dir = tempfile.mkdtemp(prefix='pyhrf_JDE_parcellation_GLM',
                                      dir=pyhrf.cfg['global']['tmp_path'])
    glm_output_dir = op.join(output_dir, 'GLM_for_parcellation')
    if not op.exists(glm_output_dir):
        os.makedirs(glm_output_dir)

    logger.info('GLM for parcellation')

    g, dm, cons = glm_nipy(fmri_data, drift_model=glm_drift, hfcut=glm_hfcut)

    pval_files = []
    if cons is not None:
        func_data = [('con_pval_%s' % cname, con.pvalue())
                     for cname, con in cons.iteritems()]
    else:
        reg_cst_drift = re.compile(".*constant.*|.*drift.*")
        func_data = [('beta_%s' % reg_name, g.beta[ir])
                     for ir, reg_name in enumerate(dm.names)
                     if not reg_cst_drift.match(reg_name)]

    for name, data in func_data:
        val_vol = expand_array_in_mask(data, fmri_data.roiMask > 0)
        val_fn = op.join(glm_output_dir, '%s.nii' % name)
        write_volume(val_vol, val_fn, fmri_data.meta_obj)
        pval_files.append(val_fn)

    mask_file = op.join(glm_output_dir, 'mask.nii')
    write_volume(fmri_data.roiMask > 0, mask_file, fmri_data.meta_obj)

    nvox = fmri_data.get_nb_vox_in_mask()
    nparcels = round_nb_parcels(nvox * 1. / avg_parcel_size)

    logger.info('Parcellation from GLM outputs, method: %s, nb parcels: %d',
                method, nparcels)

    if fmri_data.data_type == 'volume':
        parcellation_file = op.join(
            output_dir, 'parcellation_%s_np%d.nii' % (method, nparcels))

        make_parcellation_from_files(pval_files, mask_file, parcellation_file,
                                     nparcels, method)
        parcellation, _ = read_volume(parcellation_file)
    else:
        mesh_file = fmri_data.data_files[-1]
        parcellation_file = op.join(
            output_dir, 'parcellation_%s_np%d.gii' % (method, nparcels))
        make_parcellation_surf_from_files(pval_files,
                                          mesh_file,
                                          parcellation_file,
                                          nparcels,
                                          method,
                                          verbose=1)
        parcellation, _ = read_texture(parcellation_file)

    logger.info(parcellation_report(parcellation))

    return parcellation, parcellation_file
예제 #20
0
파일: surface.py 프로젝트: pyhrf/pyhrf
def project_fmri_from_kernels(input_mesh, kernels_file, fmri_data_file, output_tex, bin_threshold=None):

    logger.info("Project data onto mesh using kernels ...")

    if 0:
        print "Projecting ..."
        print "func data:", fmri_data_file
        print "Mesh file:", input_mesh
        print "Save as:", output_tex

    logger.info("Call AimsFunctionProjection -op 1 ...")

    data_files = []
    output_texs = []
    p_ids = None
    if bin_threshold is not None:
        d, h = read_volume(fmri_data_file)
        if np.allclose(d.astype(int), d):
            tmp_dir = pyhrf.get_tmp_path()
            p_ids = np.unique(d)
            logger.info("bin threshold: %f", bin_threshold)
            logger.info("pids(n=%d): %d...%d", len(p_ids), min(p_ids), max(p_ids))
            for i, p_id in enumerate(p_ids):
                if p_id != 0:
                    new_p = np.zeros_like(d)
                    new_p[np.where(d == p_id)] = i + 1  # 0 is background
                    ifn = op.join(tmp_dir, "pmask_%d.nii" % p_id)
                    write_volume(new_p, ifn, h)
                    data_files.append(ifn)
                    ofn = op.join(tmp_dir, "ptex_%d.gii" % p_id)
                    output_texs.append(ofn)
        else:
            data_files.append(fmri_data_file)
            output_texs.append(output_tex)
    else:
        data_files.append(fmri_data_file)
        output_texs.append(output_tex)

    logger.info("input data files: %s", str(data_files))
    logger.info("output data files: %s", str(output_texs))

    for data_file, o_tex in zip(data_files, output_texs):
        projection = [
            "AimsFunctionProjection",
            "-op",
            "1",
            "-d",
            kernels_file,
            "-d1",
            data_file,
            "-m",
            input_mesh,
            "-o",
            o_tex,
        ]

        cmd = " ".join(map(str, projection))
        logger.info("cmd: %s", cmd)
        os.system(cmd)

    if bin_threshold is not None:
        logger.info("Binary threshold of texture at %f", bin_threshold)
        o_tex = output_texs[0]
        data, data_gii = read_texture(o_tex)
        data = (data > bin_threshold).astype(np.int32)
        print "data:", data.dtype
        if p_ids is not None:
            for pid, o_tex in zip(p_ids[1:], output_texs[1:]):
                pdata, pdata_gii = read_texture(o_tex)
                data += (pdata > bin_threshold).astype(np.int32) * pid

        # assert (np.unique(data) == p_ids).all()
        write_texture(data, output_tex, intent="NIFTI_INTENT_LABEL")
예제 #21
0
파일: scenarios.py 프로젝트: pyhrf/pyhrf
def simulation_save_vol_outputs(
    simulation, output_dir, bold_3D_vols_dir=None, simulation_graph_output=None, prefix=None, vol_meta=None
):
    """ simulation_graph_output : None, 'simple', 'thumbnails' #TODO
    """

    if simulation.has_key("paradigm"):
        fn = add_prefix(op.join(output_dir, "paradigm.csv"), prefix)
        simulation["paradigm"].save_csv(fn)

    # Save all volumes in nifti format:
    if simulation.has_key("labels_vol"):
        mask_vol = np.ones_like(simulation["labels_vol"][0])
    elif simulation.has_key("mask"):
        mask_vol = simulation.get("mask", None)
    elif simulation.has_key("labels"):
        mask_vol = np.ones_like(simulation["labels"][0])
    else:
        raise Exception("Dunno where to get mask")

    logger.info("Vol mask of shape %s", str(mask_vol.shape))

    fn_mask = add_prefix(op.join(output_dir, "mask.nii"), prefix)
    write_volume(mask_vol.astype(np.int32), fn_mask, vol_meta)

    if simulation.has_key("hrf_territories"):
        fn_h_territories = add_prefix(op.join(output_dir, "hrf_territories.nii"), prefix)

        ht = expand_array_in_mask(simulation["hrf_territories"] + 1, mask_vol)
        write_volume(ht, fn_h_territories, vol_meta)

    if simulation.has_key("hrf"):
        from pyhrf.ndarray import MRI3Daxes

        fn_hrf = add_prefix(op.join(output_dir, "hrf.nii"), prefix)
        logger.info("hrf flat shape %s", str(simulation["hrf"].shape))
        if simulation["hrf"].ndim == 1:
            hrf = np.ones(mask_vol.size) * simulation["hrf"][:, np.newaxis]
        else:
            hrf = simulation["hrf"]

        hrfs_vol = expand_array_in_mask(hrf, mask_vol, flat_axis=1)
        dt = simulation["dt"]
        chrfs = xndarray(
            hrfs_vol, axes_names=["time"] + MRI3Daxes, axes_domains={"time": np.arange(hrfs_vol.shape[0]) * dt}
        )
        chrfs.save(fn_hrf, vol_meta)

        ttp_vol = hrfs_vol.argmax(0)
        fn_ttp = add_prefix(op.join(output_dir, "ttp.nii"), prefix)
        write_volume(ttp_vol, fn_ttp, vol_meta)

    if simulation.has_key("brf"):
        from pyhrf.ndarray import MRI3Daxes

        fn_brf = add_prefix(op.join(output_dir, "brf.nii"), prefix)
        logger.info("brf flat shape %s", str(simulation["brf"].shape))
        brfs_vol = expand_array_in_mask(simulation["brf"], mask_vol, flat_axis=1)
        dt = simulation["dt"]
        cbrfs = xndarray(
            brfs_vol, axes_names=["time"] + MRI3Daxes, axes_domains={"time": np.arange(brfs_vol.shape[0]) * dt}
        )
        cbrfs.save(fn_brf, vol_meta)

    if simulation.has_key("prf"):
        from pyhrf.ndarray import MRI3Daxes

        fn_brf = add_prefix(op.join(output_dir, "prf.nii"), prefix)
        logger.info("prf flat shape %s", str(simulation["prf"].shape))
        brfs_vol = expand_array_in_mask(simulation["prf"], mask_vol, flat_axis=1)
        dt = simulation["dt"]
        cbrfs = xndarray(
            brfs_vol, axes_names=["time"] + MRI3Daxes, axes_domains={"time": np.arange(brfs_vol.shape[0]) * dt}
        )
        cbrfs.save(fn_brf, vol_meta)

    if simulation.has_key("drift"):
        fn_drift = add_prefix(op.join(output_dir, "drift.nii"), prefix)
        logger.info("drift flat shape %s", str(simulation["drift"].shape))
        drift_vol = expand_array_in_mask(simulation["drift"], mask_vol, flat_axis=1)
        write_volume(np.rollaxis(drift_vol, 0, 4), fn_drift)

    if simulation.has_key("drift_coeffs"):
        fn_drift = add_prefix(op.join(output_dir, "drift_coeffs.nii"), prefix)
        logger.info("drift flat shape %s", str(simulation["drift_coeffs"].shape))
        drift_vol = expand_array_in_mask(simulation["drift"], mask_vol, flat_axis=1)
        write_volume(np.rollaxis(drift_vol, 0, 4), fn_drift)

    if simulation.has_key("noise"):
        fn_noise = add_prefix(op.join(output_dir, "noise.nii"), prefix)
        logger.info("noise flat shape %s", str(simulation["noise"].shape))
        noise_vol = expand_array_in_mask(simulation["noise"], mask_vol, flat_axis=1)
        write_volume(np.rollaxis(noise_vol, 0, 4), fn_noise, vol_meta)

        fn_noise = add_prefix(op.join(output_dir, "noise_emp_var.nii"), prefix)
        noise_vol = expand_array_in_mask(simulation["noise"].var(0), mask_vol)
        write_volume(noise_vol, fn_noise, vol_meta)

    if simulation.has_key("noise_var"):
        fn_noise_var = add_prefix(op.join(output_dir, "noise_var.nii"), prefix)
        logger.info("noise_var flat shape %s", str(simulation["noise_var"].shape))
        noise_var_vol = expand_array_in_mask(simulation["noise_var"], mask_vol)
        write_volume(noise_var_vol, fn_noise_var, vol_meta)

    if simulation.has_key("stim_induced_signal"):
        fn_stim_induced = add_prefix(op.join(output_dir, "stim_induced.nii"), prefix)
        logger.info("stim_induced flat shape %s", str(simulation["stim_induced_signal"].shape))
        stim_induced_vol = expand_array_in_mask(simulation["stim_induced_signal"], mask_vol, flat_axis=1)
        write_volume(np.rollaxis(stim_induced_vol, 0, 4), fn_stim_induced)

    if simulation.has_key("perf_stim_induced"):
        fn_stim_induced = add_prefix(op.join(output_dir, "perf_stim_induced.nii"), prefix)
        logger.info("asl_stim_induced flat shape %s", str(simulation["perf_stim_induced"].shape))
        stim_induced_vol = expand_array_in_mask(simulation["perf_stim_induced"], mask_vol, flat_axis=1)
        write_volume(np.rollaxis(stim_induced_vol, 0, 4), fn_stim_induced)

        fn_stim_induced = add_prefix(op.join(output_dir, "perf_stim_induced_ct.nii"), prefix)
        logger.info("asl_stim_induced flat shape %s", str(simulation["perf_stim_induced"].shape))

        dsf = simulation["dsf"]
        perf = np.dot(simulation["ctrl_tag_mat"], simulation["perf_stim_induced"][0:-1:dsf])
        stim_induced_vol = expand_array_in_mask(perf, mask_vol, flat_axis=1)
        write_volume(np.rollaxis(stim_induced_vol, 0, 4), fn_stim_induced)

    if simulation.has_key("perf_baseline"):
        fn = add_prefix(op.join(output_dir, "perf_baseline.nii"), prefix)
        pb = np.zeros_like(simulation["bold"]) + simulation["perf_baseline"]
        write_volume(expand_array_in_mask(pb[0], mask_vol), fn, vol_meta)

    if simulation.has_key("bold_stim_induced"):
        fn_stim_induced = add_prefix(op.join(output_dir, "bold_stim_induced.nii"), prefix)
        logger.info("asl_stim_induced flat shape %s", str(simulation["bold_stim_induced"].shape))
        stim_induced_vol = expand_array_in_mask(simulation["bold_stim_induced"], mask_vol, flat_axis=1)
        write_volume(np.rollaxis(stim_induced_vol, 0, 4), fn_stim_induced)

    m = np.where(mask_vol)
    labels_and_mask = mask_vol.copy()[m]

    for ic in xrange(simulation["labels"].shape[0]):
        if simulation.has_key("condition_defs"):
            c_name = simulation["condition_defs"][ic].name
        else:
            c_name = "cond%d" % ic
        fn_labels = add_prefix(op.join(output_dir, "labels_%s.nii" % c_name), prefix)
        if simulation.has_key("labels"):
            labels_c = simulation["labels"][ic]
            labels_and_mask[np.where(labels_c)] = ic + 2
            write_volume(expand_array_in_mask(labels_c, mask_vol).astype(np.int32), fn_labels, vol_meta)
        elif simulation.has_key("labels_vol"):
            labels_c = simulation["labels_vol"][ic]
            labels_and_mask[np.where(labels_c[m])] = ic + 2
            write_volume(labels_c.astype(np.int32), fn_labels, vol_meta)

        if simulation.has_key("nrls"):
            nrls_c = simulation["nrls"][ic]
            fn = add_prefix(op.join(output_dir, "nrls_%s.nii" % c_name), prefix)
            write_volume(expand_array_in_mask(nrls_c, mask_vol), fn, vol_meta)
        if simulation.has_key("nrls_session"):
            nrls_session_c = simulation["nrls_session"][ic]
            fn = add_prefix(op.join(output_dir, "nrls_session_%s.nii" % (c_name)), prefix)
            write_volume(expand_array_in_mask(nrls_session_c, mask_vol), fn, vol_meta)

        if simulation.has_key("brls"):
            brls_c = simulation["brls"][ic]
            fn = add_prefix(op.join(output_dir, "brls_%s.nii" % c_name), prefix)
            write_volume(expand_array_in_mask(brls_c, mask_vol), fn, vol_meta)
        if simulation.has_key("prls"):
            prls_c = simulation["prls"][ic]
            fn = add_prefix(op.join(output_dir, "prls_%s.nii" % c_name), prefix)
            write_volume(expand_array_in_mask(prls_c, mask_vol), fn, vol_meta)

        if simulation.has_key("neural_efficacies"):
            ne_c = simulation["neural_efficacies"][ic]
            fn = add_prefix(op.join(output_dir, "neural_efficacies_%s.nii" % c_name), prefix)
            write_volume(expand_array_in_mask(ne_c, mask_vol), fn, vol_meta)

    fn_labels_and_mask = add_prefix(op.join(output_dir, "mask_and_labels.nii"), prefix)

    write_volume(expand_array_in_mask(labels_and_mask, mask_vol).astype(int), fn_labels_and_mask, vol_meta)

    if simulation.has_key("bold_full_vol") or simulation.has_key("bold"):
        fn = add_prefix(op.join(output_dir, "bold.nii"), prefix)
        if simulation.has_key("bold_full_vol"):
            bold4D = simulation["bold_full_vol"]
        else:
            bold = simulation["bold"]
            bold4D = expand_array_in_mask(bold, mask_vol, flat_axis=1)

        write_volume(np.rollaxis(bold4D, 0, 4), fn, vol_meta)

    def save_time_series(k):
        if simulation.has_key(k):
            fn_stim_induced = add_prefix(op.join(output_dir, k + ".nii"), prefix)
            logger.info("%s flat shape %s", k, str(simulation[k].shape))
            vol = expand_array_in_mask(simulation[k], mask_vol, flat_axis=1)
            write_volume(np.rollaxis(vol, 0, 4), fn_stim_induced)

    save_time_series("flow_induction")
    save_time_series("cbv")
    save_time_series("hbr")
    save_time_series("bold_stim_induced_rescaled")

    if simulation.has_key("asl"):
        fn = add_prefix(op.join(output_dir, "asl.nii"), prefix)
        asl4D = expand_array_in_mask(simulation["asl"], mask_vol, flat_axis=1)
        write_volume(np.rollaxis(asl4D, 0, 4), fn, vol_meta)

    if simulation.has_key("outliers"):
        fn = add_prefix(op.join(output_dir, "outliers.nii"), prefix)
        outliers = expand_array_in_mask(simulation["outliers"], mask_vol, flat_axis=1)

        write_volume(np.rollaxis(outliers, 0, 4), fn, vol_meta)

    if simulation.has_key("hrf_group"):
        hrfgroup = simulation["hrf_group"]
        nb_vox = mask_vol.size
        fn_hrf = add_prefix(op.join(output_dir, "hrf_group.nii"), prefix)
        logger.info("hrf group shape %s", str(simulation["hrf_group"].shape))
        hrfGd = duplicate_hrf(nb_vox, hrfgroup)
        hrfs_vol = expand_array_in_mask(hrfGd, mask_vol, flat_axis=1)
        dt = simulation["dt"]
        chrfs = xndarray(
            hrfs_vol, axes_names=["time"] + MRI3Daxes, axes_domains={"time": np.arange(hrfs_vol.shape[0]) * dt}
        )
        chrfs.save(fn_hrf, vol_meta)

    if bold_3D_vols_dir is not None:
        assert op.exists(bold_3D_vols_dir)
        for iscan, bscan in enumerate(bold4D):
            fnout = add_prefix("bold_%06d.nii" % (iscan), prefix)
            write_volume(bscan, op.join(bold_3D_vols_dir, fnout), vol_meta)
예제 #22
0
파일: scenarios.py 프로젝트: pyhrf/pyhrf
def simulation_save_vol_outputs(simulation, output_dir, bold_3D_vols_dir=None,
                                simulation_graph_output=None, prefix=None,
                                vol_meta=None):
    """ simulation_graph_output : None, 'simple', 'thumbnails' #TODO
    """

    if simulation.has_key('paradigm'):
        fn = add_prefix(op.join(output_dir, 'paradigm.csv'), prefix)
        simulation['paradigm'].save_csv(fn)

    # Save all volumes in nifti format:
    if simulation.has_key('labels_vol'):
        mask_vol = np.ones_like(simulation['labels_vol'][0])
    elif simulation.has_key('mask'):
        mask_vol = simulation.get('mask', None)
    elif simulation.has_key('labels'):
        mask_vol = np.ones_like(simulation['labels'][0])
    else:
        raise Exception('Dunno where to get mask')

    logger.info('Vol mask of shape %s', str(mask_vol.shape))

    fn_mask = add_prefix(op.join(output_dir, 'mask.nii'), prefix)
    write_volume(mask_vol.astype(np.int32), fn_mask, vol_meta)

    if simulation.has_key('hrf_territories'):
        fn_h_territories = add_prefix(op.join(output_dir, 'hrf_territories.nii'),
                                      prefix)

        ht = expand_array_in_mask(simulation['hrf_territories'] + 1, mask_vol)
        write_volume(ht, fn_h_territories, vol_meta)

    if simulation.has_key('hrf'):
        from pyhrf.ndarray import MRI3Daxes
        fn_hrf = add_prefix(op.join(output_dir, 'hrf.nii'), prefix)
        logger.info('hrf flat shape %s', str(simulation['hrf'].shape))
        if simulation['hrf'].ndim == 1:
            hrf = (np.ones(mask_vol.size) * simulation['hrf'][:, np.newaxis])
        else:
            hrf = simulation['hrf']

        hrfs_vol = expand_array_in_mask(hrf, mask_vol, flat_axis=1)
        dt = simulation['dt']
        chrfs = xndarray(hrfs_vol, axes_names=['time', ] + MRI3Daxes,
                         axes_domains={'time': np.arange(hrfs_vol.shape[0]) * dt})
        chrfs.save(fn_hrf, vol_meta)

        ttp_vol = hrfs_vol.argmax(0)
        fn_ttp = add_prefix(op.join(output_dir, 'ttp.nii'), prefix)
        write_volume(ttp_vol, fn_ttp, vol_meta)

    if simulation.has_key('brf'):
        from pyhrf.ndarray import MRI3Daxes
        fn_brf = add_prefix(op.join(output_dir, 'brf.nii'), prefix)
        logger.info('brf flat shape %s', str(simulation['brf'].shape))
        brfs_vol = expand_array_in_mask(
            simulation['brf'], mask_vol, flat_axis=1)
        dt = simulation['dt']
        cbrfs = xndarray(brfs_vol, axes_names=['time', ] + MRI3Daxes,
                         axes_domains={'time': np.arange(brfs_vol.shape[0]) * dt})
        cbrfs.save(fn_brf, vol_meta)

    if simulation.has_key('prf'):
        from pyhrf.ndarray import MRI3Daxes
        fn_brf = add_prefix(op.join(output_dir, 'prf.nii'), prefix)
        logger.info('prf flat shape %s', str(simulation['prf'].shape))
        brfs_vol = expand_array_in_mask(
            simulation['prf'], mask_vol, flat_axis=1)
        dt = simulation['dt']
        cbrfs = xndarray(brfs_vol, axes_names=['time', ] + MRI3Daxes,
                         axes_domains={'time': np.arange(brfs_vol.shape[0]) * dt})
        cbrfs.save(fn_brf, vol_meta)

    if simulation.has_key('drift'):
        fn_drift = add_prefix(op.join(output_dir, 'drift.nii'), prefix)
        logger.info('drift flat shape %s', str(simulation['drift'].shape))
        drift_vol = expand_array_in_mask(simulation['drift'], mask_vol,
                                         flat_axis=1)
        write_volume(np.rollaxis(drift_vol, 0, 4), fn_drift)

    if simulation.has_key('drift_coeffs'):
        fn_drift = add_prefix(op.join(output_dir, 'drift_coeffs.nii'), prefix)
        logger.info(
            'drift flat shape %s', str(simulation['drift_coeffs'].shape))
        drift_vol = expand_array_in_mask(simulation['drift'], mask_vol,
                                         flat_axis=1)
        write_volume(np.rollaxis(drift_vol, 0, 4), fn_drift)

    if simulation.has_key('noise'):
        fn_noise = add_prefix(op.join(output_dir, 'noise.nii'), prefix)
        logger.info('noise flat shape %s', str(simulation['noise'].shape))
        noise_vol = expand_array_in_mask(simulation['noise'], mask_vol,
                                         flat_axis=1)
        write_volume(np.rollaxis(noise_vol, 0, 4), fn_noise, vol_meta)

        fn_noise = add_prefix(op.join(output_dir, 'noise_emp_var.nii'), prefix)
        noise_vol = expand_array_in_mask(simulation['noise'].var(0), mask_vol)
        write_volume(noise_vol, fn_noise, vol_meta)

    if simulation.has_key('noise_var'):
        fn_noise_var = add_prefix(op.join(output_dir, 'noise_var.nii'), prefix)
        logger.info(
            'noise_var flat shape %s', str(simulation['noise_var'].shape))
        noise_var_vol = expand_array_in_mask(simulation['noise_var'], mask_vol)
        write_volume(noise_var_vol, fn_noise_var, vol_meta)

    if simulation.has_key('stim_induced_signal'):
        fn_stim_induced = add_prefix(op.join(output_dir, 'stim_induced.nii'),
                                     prefix)
        logger.info('stim_induced flat shape %s',
                    str(simulation['stim_induced_signal'].shape))
        stim_induced_vol = expand_array_in_mask(simulation['stim_induced_signal'],
                                                mask_vol, flat_axis=1)
        write_volume(np.rollaxis(stim_induced_vol, 0, 4), fn_stim_induced)

    if simulation.has_key('perf_stim_induced'):
        fn_stim_induced = add_prefix(op.join(output_dir, 'perf_stim_induced.nii'),
                                     prefix)
        logger.info('asl_stim_induced flat shape %s',
                    str(simulation['perf_stim_induced'].shape))
        stim_induced_vol = expand_array_in_mask(simulation['perf_stim_induced'],
                                                mask_vol, flat_axis=1)
        write_volume(np.rollaxis(stim_induced_vol, 0, 4), fn_stim_induced)

        fn_stim_induced = add_prefix(op.join(output_dir,
                                             'perf_stim_induced_ct.nii'),
                                     prefix)
        logger.info('asl_stim_induced flat shape %s',
                    str(simulation['perf_stim_induced'].shape))

        dsf = simulation['dsf']
        perf = np.dot(simulation['ctrl_tag_mat'],
                      simulation['perf_stim_induced'][0:-1:dsf])
        stim_induced_vol = expand_array_in_mask(perf, mask_vol, flat_axis=1)
        write_volume(np.rollaxis(stim_induced_vol, 0, 4), fn_stim_induced)

    if simulation.has_key('perf_baseline'):
        fn = add_prefix(op.join(output_dir, 'perf_baseline.nii'), prefix)
        pb = np.zeros_like(simulation['bold']) + simulation['perf_baseline']
        write_volume(expand_array_in_mask(pb[0], mask_vol), fn, vol_meta)

    if simulation.has_key('bold_stim_induced'):
        fn_stim_induced = add_prefix(op.join(output_dir, 'bold_stim_induced.nii'),
                                     prefix)
        logger.info('asl_stim_induced flat shape %s',
                    str(simulation['bold_stim_induced'].shape))
        stim_induced_vol = expand_array_in_mask(simulation['bold_stim_induced'],
                                                mask_vol, flat_axis=1)
        write_volume(np.rollaxis(stim_induced_vol, 0, 4), fn_stim_induced)

    m = np.where(mask_vol)
    labels_and_mask = mask_vol.copy()[m]

    for ic in xrange(simulation['labels'].shape[0]):
        if simulation.has_key('condition_defs'):
            c_name = simulation['condition_defs'][ic].name
        else:
            c_name = 'cond%d' % ic
        fn_labels = add_prefix(op.join(output_dir, 'labels_%s.nii' % c_name),
                               prefix)
        if simulation.has_key('labels'):
            labels_c = simulation['labels'][ic]
            labels_and_mask[np.where(labels_c)] = ic + 2
            write_volume(expand_array_in_mask(labels_c, mask_vol).astype(np.int32),
                         fn_labels, vol_meta)
        elif simulation.has_key('labels_vol'):
            labels_c = simulation['labels_vol'][ic]
            labels_and_mask[np.where(labels_c[m])] = ic + 2
            write_volume(labels_c.astype(np.int32), fn_labels, vol_meta)

        if simulation.has_key('nrls'):
            nrls_c = simulation['nrls'][ic]
            fn = add_prefix(
                op.join(output_dir, 'nrls_%s.nii' % c_name), prefix)
            write_volume(expand_array_in_mask(nrls_c, mask_vol), fn, vol_meta)
        if simulation.has_key('nrls_session'):
            nrls_session_c = simulation['nrls_session'][ic]
            fn = add_prefix(op.join(output_dir, 'nrls_session_%s.nii'
                                    % (c_name)), prefix)
            write_volume(expand_array_in_mask(nrls_session_c, mask_vol),
                         fn, vol_meta)

        if simulation.has_key('brls'):
            brls_c = simulation['brls'][ic]
            fn = add_prefix(
                op.join(output_dir, 'brls_%s.nii' % c_name), prefix)
            write_volume(expand_array_in_mask(brls_c, mask_vol), fn, vol_meta)
        if simulation.has_key('prls'):
            prls_c = simulation['prls'][ic]
            fn = add_prefix(
                op.join(output_dir, 'prls_%s.nii' % c_name), prefix)
            write_volume(expand_array_in_mask(prls_c, mask_vol), fn, vol_meta)

        if simulation.has_key('neural_efficacies'):
            ne_c = simulation['neural_efficacies'][ic]
            fn = add_prefix(op.join(output_dir, 'neural_efficacies_%s.nii'
                                    % c_name), prefix)
            write_volume(expand_array_in_mask(ne_c, mask_vol), fn, vol_meta)

    fn_labels_and_mask = add_prefix(op.join(output_dir, 'mask_and_labels.nii'),
                                    prefix)

    write_volume(expand_array_in_mask(labels_and_mask, mask_vol).astype(int),
                 fn_labels_and_mask, vol_meta)

    if simulation.has_key('bold_full_vol') or simulation.has_key('bold'):
        fn = add_prefix(op.join(output_dir, 'bold.nii'), prefix)
        if simulation.has_key('bold_full_vol'):
            bold4D = simulation['bold_full_vol']
        else:
            bold = simulation['bold']
            bold4D = expand_array_in_mask(bold, mask_vol, flat_axis=1)

        write_volume(np.rollaxis(bold4D, 0, 4), fn, vol_meta)

    def save_time_series(k):
        if simulation.has_key(k):
            fn_stim_induced = add_prefix(
                op.join(output_dir, k + '.nii'), prefix)
            logger.info('%s flat shape %s', k, str(simulation[k].shape))
            vol = expand_array_in_mask(simulation[k], mask_vol, flat_axis=1)
            write_volume(np.rollaxis(vol, 0, 4), fn_stim_induced)
    save_time_series('flow_induction')
    save_time_series('cbv')
    save_time_series('hbr')
    save_time_series('bold_stim_induced_rescaled')

    if simulation.has_key('asl'):
        fn = add_prefix(op.join(output_dir, 'asl.nii'), prefix)
        asl4D = expand_array_in_mask(simulation['asl'], mask_vol, flat_axis=1)
        write_volume(np.rollaxis(asl4D, 0, 4), fn, vol_meta)

    if simulation.has_key('outliers'):
        fn = add_prefix(op.join(output_dir, 'outliers.nii'), prefix)
        outliers = expand_array_in_mask(simulation['outliers'], mask_vol,
                                        flat_axis=1)

        write_volume(np.rollaxis(outliers, 0, 4), fn, vol_meta)

    if simulation.has_key('hrf_group'):
        hrfgroup = simulation['hrf_group']
        nb_vox = mask_vol.size
        fn_hrf = add_prefix(op.join(output_dir, 'hrf_group.nii'), prefix)
        logger.info('hrf group shape %s', str(simulation['hrf_group'].shape))
        hrfGd = duplicate_hrf(nb_vox, hrfgroup)
        hrfs_vol = expand_array_in_mask(hrfGd, mask_vol, flat_axis=1)
        dt = simulation['dt']
        chrfs = xndarray(hrfs_vol, axes_names=['time', ] + MRI3Daxes,
                         axes_domains={'time': np.arange(hrfs_vol.shape[0]) * dt})
        chrfs.save(fn_hrf, vol_meta)

    if bold_3D_vols_dir is not None:
        assert op.exists(bold_3D_vols_dir)
        for iscan, bscan in enumerate(bold4D):
            fnout = add_prefix('bold_%06d.nii' % (iscan), prefix)
            write_volume(bscan, op.join(bold_3D_vols_dir, fnout), vol_meta)
예제 #23
0
파일: glm.py 프로젝트: ainafp/pyhrf
def glm_nipy_from_files(bold_file, tr, paradigm_csv_file, output_dir,
                        mask_file, session=0, contrasts=None,
                        con_test_baseline=0.0,
                        hrf_model='Canonical',
                        drift_model='Cosine', hfcut=128,
                        residuals_model='spherical', fit_method='ols',
                        fir_delays=[0]):
    """
    #TODO: handle surface data
    hrf_model : Canonical | Canonical with Derivative | FIR

    """

    fdata = FmriData.from_vol_files(mask_file, paradigm_csv_file,
                                    [bold_file], tr)
    g, dm, cons = glm_nipy(fdata, contrasts=contrasts, hrf_model=hrf_model,
                           hfcut=hfcut, drift_model=drift_model,
                           residuals_model=residuals_model,
                           fit_method=fit_method, fir_delays=fir_delays)

    ns, nr = dm.matrix.shape
    cdesign_matrix = xndarray(dm.matrix, axes_names=['time', 'regressor'],
                              axes_domains={'time': np.arange(ns) * tr,
                                            'regressor': dm.names})

    cdesign_matrix.save(op.join(output_dir, 'design_matrix.nii'))

    beta_files = []
    beta_values = dict.fromkeys(dm.names)
    beta_vars = dict.fromkeys(dm.names)
    beta_vars_voxels = dict.fromkeys(dm.names)
    for ib, bname in enumerate(dm.names):
        # beta values
        beta_vol = expand_array_in_mask(g.beta[ib], fdata.roiMask > 0)
        beta_fn = op.join(output_dir, 'beta_%s.nii' % bname)
        write_volume(beta_vol, beta_fn, fdata.meta_obj)
        beta_files.append(beta_fn)
        beta_values[bname] = beta_vol

        # normalized variance of betas
        # variance: diag of cov matrix
        beta_vars[bname] = sp.diag(g.nvbeta)[ib]
        # sig2 = g.s2 #ResMS
        # variance for all voxels, condition ib
        var_cond = sp.diag(g.nvbeta)[ib] * g.s2
        beta_vars_voxels[bname] = var_cond
        #beta_var_fn = op.join(output_dir, 'var_beta_%s.nii' %bname)
        #write_volume(beta_var, beta_var_fn, fdata.meta_obj)
        # beta_var_files.append(beta_var_fn)

    if cons is not None:
        con_files = []
        pval_files = []
        for cname, con in cons.iteritems():
            con_vol = expand_array_in_mask(con.effect, fdata.roiMask > 0)
            con_fn = op.join(output_dir, 'con_effect_%s.nii' % cname)
            write_volume(con_vol, con_fn, fdata.meta_obj)
            con_files.append(con_fn)

            pval_vol = expand_array_in_mask(con.pvalue(con_test_baseline),
                                            fdata.roiMask > 0)
            pval_fn = op.join(output_dir, 'con_pvalue_%s.nii' % cname)
            write_volume(pval_vol, pval_fn, fdata.meta_obj)
            pval_files.append(pval_fn)
    else:
        con_files = None
        pval_files = None

    dof = g.dof
    # if do_ppm:
    # for

    # TODO: FIR stuffs
    # , con_files, pval_files
    return beta_files, beta_values, beta_vars_voxels, dof