algorithm/vol_tools.py
def template_overlap(self, template, para='percent', tempnumber = None):
        """
        Compute overlap between target data and template 
        -----------------------------------------
        Parameters:
            template: template image, 
            para: index call for computing
                  'percent', overlap #voxels/target region #voxels
                  'amount', overlap #voxels
                  'dice', 2*(intersection)/union
            tempnumber: template label number, set in case miss label in specific subjects
        Output:
            overlaparray, overlap array(matrix) in two images(target & template)
            uni_tempextlbl, overlap label within template(Note that label not within target) 
        """
        try:
            template.shape
        except AttributeError:
            template = nib.load(template).get_data()
            print('Template should be an array')
        if template.shape != self._roimask.shape:
            raise Exception('template should have the same shape with target data')
        templabel = np.unique(template)[1:]
        if tempnumber is not None:
            templabel = np.array(range(1,tempnumber+1))
        overlaparray = np.empty((templabel.size, self._roinumber))
        
        roiloc = np.transpose(np.nonzero(self._roimask))
        tup_roiloc = map(tuple, roiloc)
        tempextlabel_all = np.array([template[i] for i in tup_roiloc])
        roiextlabel_all = np.array([self._roimask[i] for i in tup_roiloc])
        tempextlabel = np.delete(tempextlabel_all, np.where(tempextlabel_all==0))
        roiextlabel = np.delete(roiextlabel_all, np.where(tempextlabel_all==0))
        uni_tempextlbl = np.unique(tempextlabel)
        for i, vali in enumerate(templabel):
            for j, valj in enumerate(range(1, 1+self._roinumber)):
                if para == 'percent':
                    try:
                        overlaparray[i,j] = 1.0*tempextlabel[(tempextlabel == vali)*(roiextlabel == valj)].size/self._roimask[self._roimask == valj].size
                    except ZeroDivisionError:
                        overlaparray[i,j] = np.nan
                elif para == 'amount':
                    overlaparray[i,j] = tempextlabel[(tempextlabel == vali)*(roiextlabel == valj)].size
                elif para == 'dice':
                    try:
                        overlaparray[i,j] = 2.0*tempextlabel[(tempextlabel == vali)*(roiextlabel == valj)].size/(template[template == vali].size + self._roimask[self._roimask == valj].size)
                    except ZeroDivisionError:
                        overlaparray[i,j] = np.nan
                else:
                    raise Exception("para should be 'percent', 'amount' or 'dice', please retype")
        return overlaparray, uni_tempextlbl

        Compute overlap between target data and template 
        -----------------------------------------
        Parameters:
            template: template image, 
            para: index call for computing
                  'percent', overlap #voxels/target region #voxels
                  'amount', overlap #voxels
                  'dice', 2*(intersection)/union
            tempnumber: template label number, set in case miss label in specific subjects
        Output:
            overlaparray, overlap array(matrix) in two images(target & template)
            uni_tempextlbl, overlap label within template(Note that label not within target) 
        
Compute overlap between target data and template
Compute overlap between target data and template
def template_overlap(self, template, para='percent', tempnumber = None):
        try:
            template.shape
        except AttributeError:
            template = nib.load(template).get_data()
            print('Template should be an array')
        if template.shape != self._roimask.shape:
            raise Exception('template should have the same shape with target data')
        templabel = np.unique(template)[1:]
        if tempnumber is not None:
            templabel = np.array(range(1,tempnumber+1))
        overlaparray = np.empty((templabel.size, self._roinumber))
        roiloc = np.transpose(np.nonzero(self._roimask))
        tup_roiloc = map(tuple, roiloc)
        tempextlabel_all = np.array([template[i] for i in tup_roiloc])
        roiextlabel_all = np.array([self._roimask[i] for i in tup_roiloc])
        tempextlabel = np.delete(tempextlabel_all, np.where(tempextlabel_all==0))
        roiextlabel = np.delete(roiextlabel_all, np.where(tempextlabel_all==0))
        uni_tempextlbl = np.unique(tempextlabel)
        for i, vali in enumerate(templabel):
            for j, valj in enumerate(range(1, 1+self._roinumber)):
                if para == 'percent':
                    try:
                        overlaparray[i,j] = 1.0*tempextlabel[(tempextlabel == vali)*(roiextlabel == valj)].size/self._roimask[self._roimask == valj].size
                    except ZeroDivisionError:
                        overlaparray[i,j] = np.nan
                elif para == 'amount':
                    overlaparray[i,j] = tempextlabel[(tempextlabel == vali)*(roiextlabel == valj)].size
                elif para == 'dice':
                    try:
                        overlaparray[i,j] = 2.0*tempextlabel[(tempextlabel == vali)*(roiextlabel == valj)].size/(template[template == vali].size + self._roimask[self._roimask == valj].size)
                    except ZeroDivisionError:
                        overlaparray[i,j] = np.nan
                else:
                    raise Exception("para should be 'percent', 'amount' or 'dice', please retype")
        return overlaparray, uni_tempextlbl
def template_overlap ( self , template , para = 'percent' , tempnumber = None ) : try : template . shape except AttributeError : template = nib . load ( template ) . get_data ( ) print ( 'Template should be an array' ) if template . shape != self . _roimask . shape : raise Exception ( 'template should have the same shape with target data' ) templabel = np . unique ( template ) [ 1 : ] if tempnumber is not None : templabel = np . array ( range ( 1 , tempnumber + 1 ) ) overlaparray = np . empty ( ( templabel . size , self . _roinumber ) ) roiloc = np . transpose ( np . nonzero ( self . _roimask ) ) tup_roiloc = map ( tuple , roiloc ) tempextlabel_all = np . array ( [ template [ i ] for i in tup_roiloc ] ) roiextlabel_all = np . array ( [ self . _roimask [ i ] for i in tup_roiloc ] ) tempextlabel = np . delete ( tempextlabel_all , np . where ( tempextlabel_all == 0 ) ) roiextlabel = np . delete ( roiextlabel_all , np . where ( tempextlabel_all == 0 ) ) uni_tempextlbl = np . unique ( tempextlabel ) for i , vali in enumerate ( templabel ) : for j , valj in enumerate ( range ( 1 , 1 + self . _roinumber ) ) : if para == 'percent' : try : overlaparray [ i , j ] = 1.0 * tempextlabel [ ( tempextlabel == vali ) * ( roiextlabel == valj ) ] . size / self . _roimask [ self . _roimask == valj ] . size except ZeroDivisionError : overlaparray [ i , j ] = np . nan elif para == 'amount' : overlaparray [ i , j ] = tempextlabel [ ( tempextlabel == vali ) * ( roiextlabel == valj ) ] . size elif para == 'dice' : try : overlaparray [ i , j ] = 2.0 * tempextlabel [ ( tempextlabel == vali ) * ( roiextlabel == valj ) ] . size / ( template [ template == vali ] . size + self . _roimask [ self . _roimask == valj ] . size ) except ZeroDivisionError : overlaparray [ i , j ] = np . nan else : raise Exception ( "para should be 'percent', 'amount' or 'dice', please retype" ) return overlaparray , uni_tempextlbl
Compute overlap between target data and template
Compute overlap between target data and template
"""
        Compute overlap between target data and template 
        -----------------------------------------
        Parameters:
            template: template image, 
            para: index call for computing
                  'percent', overlap #voxels/target region #voxels
                  'amount', overlap #voxels
                  'dice', 2*(intersection)/union
            tempnumber: template label number, set in case miss label in specific subjects
        Output:
            overlaparray, overlap array(matrix) in two images(target & template)
            uni_tempextlbl, overlap label within template(Note that label not within target) 
        """
self
template
None
para
index call for computing
'percent', overlap #voxels/target region #voxels
'amount', overlap #voxels
'dice', 2*(intersection)/union
index call for computing ' percent ' overlap #voxels / target region #voxels ' amount ' overlap #voxels ' dice ' 2 * ( intersection ) / union
tempnumber
template label number, set in case miss label in specific subjects
template label number set in case miss label in specific subjects