Exemplo n.º 1
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)
Exemplo n.º 2
0
 def _compute_graph(self):
     if self.data_type != 'volume':
         raise Exception('Can only compute graph for volume data')
     pyhrf.verbose(6, 'FmriData._compute_graph() ...')
     to_discard = [self.backgroundLabel]
     self._graph = parcels_to_graphs(self.roiMask, kerMask3D_6n,
                                     toDiscard=to_discard)
Exemplo n.º 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():
        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)
Exemplo n.º 4
0
 def build_graphs(self, force=False):
     logger.debug('FmriData.build_graphs (self.graphs is None ? %s ) ...',
                  str(self.graphs is None))
     logger.debug('data_type: %s', self.data_type)
     if self.graphs is None or force:
         if self.data_type == 'volume':
             logger.info('Building graph from volume ...')
             to_discard = [self.backgroundLabel]
             self.graphs = parcels_to_graphs(self.roiMask,
                                             kerMask3D_6n,
                                             toDiscard=to_discard)
             logger.info('Graph built (%d rois)!', len(self.graphs.keys()))
             self.edge_lentghs = dict([(i, [[1] * len(nl) for nl in g])
                                       for i, g in self.graphs.items()])
         elif self.data_type == 'surface':
             if self.graphs is not None:
                 return self.graphs
             else:
                 raise Exception('Graph is not set for surface data!')
Exemplo n.º 5
0
 def build_graphs(self, force=False):
     logger.debug('FmriData.build_graphs (self.graphs is None ? %s ) ...',
                  str(self.graphs is None))
     logger.debug('data_type: %s', self.data_type)
     if self.graphs is None or force:
         if self.data_type == 'volume':
             logger.info('Building graph from volume ...')
             to_discard = [self.backgroundLabel]
             self.graphs = parcels_to_graphs(self.roiMask,
                                             kerMask3D_6n,
                                             toDiscard=to_discard)
             logger.info('Graph built (%d rois)!', len(self.graphs.keys()))
             self.edge_lentghs = dict([(i, [[1] * len(nl) for nl in g])
                                       for i, g in self.graphs.items()])
         elif self.data_type == 'surface':
             if self.graphs is not None:
                 return self.graphs
             else:
                 raise Exception('Graph is not set for surface data!')
Exemplo n.º 6
0
        if (TIME_AXIS == 0 and mshape != b.shape[1:]) or \
                (TIME_AXIS == 3 and mshape != b.shape[:-1]):
            raise Exception('Error: BOLD shape is different from mask shape')

        sessionScans.append(np.arange(lastScan, lastScan+b.shape[TIME_AXIS],
                                      dtype=int))
        lastScan += b.shape[TIME_AXIS]
    bold = np.concatenate(tuple(bolds), axis=TIME_AXIS)

    discard_bad_data(bold, roiMask)

    if build_graph:
        # Build graph

        if len(np.unique(roiMask))==1 or keepBackground:
            graphs = parcels_to_graphs(roiMask.astype(int), kerMask3D_6n)
        else:
            if (roiMask==0).any():
                toDiscard = [0]
                pyhrf.verbose(1,'Discarding background (label=0)')
            else:
                toDiscard = None
            graphs = parcels_to_graphs(roiMask.astype(int), kerMask3D_6n,
                                       toDiscard=toDiscard)
        pyhrf.verbose(1,'Graph built !')
    else:
        graphs = None

    #Split bold into rois:
    roiBold = {}
    for roiId in np.unique(roiMask):
Exemplo n.º 7
0
 def setUp(self):
     pf = 'subj0_parcellation.nii.gz'
     fnm = pyhrf.get_data_file_name(pf)
     m, mh = read_volume(fnm)
     self.graph = parcels_to_graphs(m.astype(int), kerMask3D_6n,
                                    toDiscard=[0])[1]
Exemplo n.º 8
0
        if (TIME_AXIS == 0 and mshape != b.shape[1:]) or \
                (TIME_AXIS == 3 and mshape != b.shape[:-1]):
            raise Exception('Error: BOLD shape is different from mask shape')

        sessionScans.append(np.arange(lastScan, lastScan + b.shape[TIME_AXIS],
                                      dtype=int))
        lastScan += b.shape[TIME_AXIS]
    bold = np.concatenate(tuple(bolds), axis=TIME_AXIS)

    discard_bad_data(bold, roiMask)

    if build_graph:
        # Build graph

        if len(np.unique(roiMask)) == 1 or keepBackground:
            graphs = parcels_to_graphs(roiMask.astype(int), kerMask3D_6n)
        else:
            if (roiMask == 0).any():
                toDiscard = [0]
                logger.info('Discarding background (label=0)')
            else:
                toDiscard = None
            graphs = parcels_to_graphs(roiMask.astype(int), kerMask3D_6n,
                                       toDiscard=toDiscard)
        logger.info('Graph built !')
    else:
        graphs = None

    # Split bold into rois:
    roiBold = {}
    for roiId in np.unique(roiMask):