def discrete_to_roi_features(self, fid, method='average'): """ Compute an ROI-level feature given the discrete features Parameters ---------- fid(string) the discrete feature under consideration method='average' the assessment method Results ------ the computed roi-feature is returned """ if method not in['min','max','average','cumulated_average']: raise ValueError, 'unknown method' if method=='cumulated_average': df = self.discrete_features[fid] data = self.discrete_features[fid] d0 = data[0] if np.size(d0) == np.shape(d0)[0]: np.reshape(d0,(np.size(d0),1)) fdim = d0.shape[1] ldata = np.zeros((self.k,fdim)) for k in range(self.k): dk = self.get_descendents(k) card = np.sum(self.get_size()[dk]) for ch in dk: ldata[k] += np.sum(data[ch],0) ldata[k]/=card self.set_roi_feature(fid,ldata) else: ldata = MultipleROI.discrete_to_roi_features(self, fid, method) return ldata
def clean(self, valid): """ remove the rois for which valid==0 and update the hierarchy accordingly In case sum(valid)==0, 0 is returned """ if np.sum(valid)==0: return None # fixme: is None a correct way of dealing with k=0 ? # first clean as a forest sf = self.subforest(valid) Forest.__init__(self, sf.V, sf.parents) # then clean as a multiple ROI MultipleROI.clean(self, valid) return self.V
def test_mroi2(verbose=0): nim = load(RefImage) mroi = MultipleROI(affine=nim.get_affine(), shape=nim.get_shape()) pos = 1.0*np.array([[10,10,10],[0,0,0],[20,0,20],[0,0,35]]) rad = np.array([5.,6.,7.,8.0]) mroi.as_multiple_balls(pos,rad) mroi.append_balls(np.array([[-10.,0.,10.]]),np.array([7.0])) mroi.set_roi_feature_from_image('T1_signal',RefImage) avt1 = mroi.get_roi_feature('T1_signal') assert(np.size(avt1)==5)
def test_mroi1(verbose=0): nim = load(RefImage) mroi = MultipleROI(affine=nim.get_affine(), shape=nim.get_shape()) pos = 1.0*np.array([[10,10,10],[0,0,0],[20,0,20],[0,0,35]]) rad = np.array([5.,6.,7.,8.0]) mroi.as_multiple_balls(pos,rad) mroi.append_balls(np.array([[-10.,0.,10.]]),np.array([7.0])) roiPath = os.path.join(WriteDir,"mroi.nii") mroi.make_image(roiPath) assert(os.path.isfile(roiPath))
def __init__(self, parents=None, affine=np.eye(4), shape=None, xyz=None, id='nroi'): """ Building the NROI Parameters ---------- parents=None: array of shape(k) providing the hierachical structure if parent==None, None is returned affine=np.eye(4), array of shape(4,4), coordinate-defining affine transformation shape=None, tuple of length 3 defining the size of the grid implicit to the discrete ROI definition xyz=None, list of arrays of shape (3, nvox) that yield the grid position of each grid guy. id='nroi', string, region identifier """ if parents==None: return None k = np.size(parents) Forest.__init__(self,k,parents) MultipleROI.__init__(self,id, k, affine, shape, xyz)
save(wim, blobPath) # --- 2.b take blob labelled "1" as an ROI roi = DiscreteROI( affine=affine, shape=shape) roi.from_labelled_image(blobPath, 1) roiPath2 = os.path.join(swd, "roi_blob_1.nii") roi.make_image(roiPath2) # --- 2.c take the blob closest to 'position as an ROI' roiPath3 = os.path.join(swd, "blob_closest_to_%d_%d_%d.nii")%\ (position[0][0], position[0][1], position[0][2]) roi.from_position_and_image(blobPath, np.array(position)) roi.make_image(roiPath3) # --- 2.d make a set of ROIs from all the blobs mroi = MultipleROI( affine=affine, shape=shape) mroi.from_labelled_image(blobPath) roiPath4 = os.path.join(swd, "roi_all_blobs.nii") mroi.make_image(roiPath4) mroi.set_discrete_feature_from_image('activ', input_image) mroi.discrete_to_roi_features('activ') mroi.plot_roi_feature('activ') # ---- 2.e the same, a bit more complex mroi = MultipleROI( affine=affine, shape=shape) mroi.as_multiple_balls(np.array([[-10.,0.,10.]]),np.array([7.0])) mroi.from_labelled_image(blobPath,np.arange(1,20)) mroi.from_labelled_image(blobPath,np.arange(31,50)) roiPath5 = os.path.join(swd,"roi_some_blobs.nii") mroi.set_discrete_feature_from_image('activ',input_image) mroi.discrete_to_roi_features('activ')
zmap = mask.get_data().astype(np.float) zmap[zmap>0] = zmap[zmap>0]*zvals contrast_image = Nifti1Image(zmap, mask.get_affine()) # if you want to save the contrast as an image # contrast_path = op.join(swd, 'zmap.nii') # save(contrast_image, contrast_path) ######################################## # Create ROIs ######################################## positions = np.array([[60, -30, 5],[50, 27, 5]]) # in mm (here in the MNI space) radii = np.array([8,6]) mroi = MultipleROI( affine=mask.get_affine(), shape=mask.get_shape()) mroi.as_multiple_balls(positions, radii) # to save an image of the ROIs mroi.make_image((op.join(swd, "roi.nii"))) # exact the time courses with ROIs mroi.set_discrete_feature_from_image('signal', image=fmri_data) # ROI average time courses mroi.discrete_to_roi_features('signal') # roi-level contrast average mroi.set_discrete_feature_from_image('contrast', image=contrast_image) mroi.discrete_to_roi_features('contrast')