algorithm/surf_tools.py
def nfold_location_overlap(imgdata1, imgdata2, labels, labelnum = None, index = 'dice', thr_meth = 'prob', prob_meth = 'part', n_fold=2, thr_range = [0,1,0.1], n_permutation=1, controlsize = False, actdata = None):
    """
    Decide the maximum threshold from raw image data.
    Here using the cross validation method to decide threhold using for getting the maximum probabilistic map
    
    Parameters:
    -----------
    imgdata1: A 2/4 dimensional data, used as train data
    imgdata2: A 2/4 dimensional data, used as test data
    labels: list, label number used to extract dice coefficient
    labelnum: by default is None, label number size. We recommend to provide label number here.
    index: 'dice' or 'percent'
    thr_meth: 'prob', threshold probabilistic map by probabilistic values (MPM)
              'number', threshold probabilistic map by numbers of vertex
    prob_meth: 'all' or 'part' subjects to use to compute probablistic map
    n_fold: split data into n_fold part, using first n_fold-1 part to get probabilistic map, then using rest part to evaluate overlap condition, by default is 2
    thr_range: pre-set threshold range to find the best maximum probabilistic threshold, the best threshold will search in this parameters, by default is [0,1,0.1], as the format of [start, stop, step]
    n_permuation: times of permutation, by default is 10
    controlsize: whether control label data size with template mpm label size or not, by default is False.
    actdata: if controlsize is True, please input actdata as a parameter. By default is None.

    Return:
    -------
    output_overlap: dice coefficient/percentage computed from function
                    output_dice consists of a 4 dimension array
                    permutation x threhold x subjects x regions
                    the first dimension permutation means the results of each permutation
                    the second dimension threhold means the results of pre-set threshold
                    the third dimension subjects means the results of each subject
                    the fourth dimension regions means the result of each region
    
    Example:
    --------
    >>> output_overlap = nfold_location_overlap(imgdata1, imgdata2, [2,4], labelnum = 4)
    """        
    if imgdata1.ndim == 4:
        imgdata1 = imgdata1.reshape((imgdata1.shape[0], imgdata1.shape[3]))
    if imgdata2.ndim == 4:
        imgdata2 = imgdata2.reshape((imgdata2.shape[0], imgdata2.shape[3]))
    if actdata is not None:
        if actdata.ndim == 4:
            actdata = actdata.reshape((actdata.shape[0], actdata.shape[3]))
    n_subj = imgdata1.shape[1]
    if labelnum is None:
        labelnum = int(np.max(np.unique(imgdata1)))
    assert (np.max(labels)<labelnum+1), "the maximum of labels should smaller than labelnum"
    output_overlap = []
    for n in range(n_permutation):
        print("permutation {} starts".format(n+1))
        test_subj = np.sort(np.random.choice(range(n_subj), n_subj-n_subj/n_fold, replace = False)).tolist()
        verify_subj = [val for val in range(n_subj) if val not in test_subj]
        test_data = imgdata1[:,test_subj]
        verify_data = imgdata2[:,verify_subj]
        if actdata is not None:
            verify_actdata = actdata[...,verify_subj]
        else:
            verify_actdata = None
        pm = make_pm(test_data, prob_meth, labelnum)
        pm_temp = cv_pm_overlap(pm, verify_data, labels, labels, thr_meth = thr_meth, thr_range = thr_range, index = index, cmpalllbl = False, controlsize = controlsize, actdata = verify_actdata)
        output_overlap.append(pm_temp)
    output_overlap = np.array(output_overlap)
    return output_overlap

    Decide the maximum threshold from raw image data.
    Here using the cross validation method to decide threhold using for getting the maximum probabilistic map
    
    Parameters:
    -----------
    imgdata1: A 2/4 dimensional data, used as train data
    imgdata2: A 2/4 dimensional data, used as test data
    labels: list, label number used to extract dice coefficient
    labelnum: by default is None, label number size. We recommend to provide label number here.
    index: 'dice' or 'percent'
    thr_meth: 'prob', threshold probabilistic map by probabilistic values (MPM)
              'number', threshold probabilistic map by numbers of vertex
    prob_meth: 'all' or 'part' subjects to use to compute probablistic map
    n_fold: split data into n_fold part, using first n_fold-1 part to get probabilistic map, then using rest part to evaluate overlap condition, by default is 2
    thr_range: pre-set threshold range to find the best maximum probabilistic threshold, the best threshold will search in this parameters, by default is [0,1,0.1], as the format of [start, stop, step]
    n_permuation: times of permutation, by default is 10
    controlsize: whether control label data size with template mpm label size or not, by default is False.
    actdata: if controlsize is True, please input actdata as a parameter. By default is None.

    Return:
    -------
    output_overlap: dice coefficient/percentage computed from function
                    output_dice consists of a 4 dimension array
                    permutation x threhold x subjects x regions
                    the first dimension permutation means the results of each permutation
                    the second dimension threhold means the results of pre-set threshold
                    the third dimension subjects means the results of each subject
                    the fourth dimension regions means the result of each region
    
    Example:
    --------
    >>> output_overlap = nfold_location_overlap(imgdata1, imgdata2, [2,4], labelnum = 4)
    
Decide the maximum threshold from raw image data.
Here using the cross validation method to decide threhold using for getting the maximum probabilistic map



A 2/4 dimensional data, used as train data
imgdata2: A 2/4 dimensional data, used as test data
labels: list, label number used to extract dice coefficient
labelnum: by default is None, label number size. We recommend to provide label number here.



dice coefficient/percentage computed from function
output_dice consists of a 4 dimension array
permutation x threhold x subjects x regions
the first dimension permutation means the results of each permutation
the second dimension threhold means the results of pre-set threshold
the third dimension subjects means the results of each subject
the fourth dimension regions means the result of each region



>>> output_overlap = nfold_location_overlap(imgdata1, imgdata2, [2,4], labelnum = 4)
Decide the maximum threshold from raw image data . Here using the cross validation method to decide threhold using for getting the maximum probabilistic map A 2 / 4 dimensional data used as train data imgdata2 : A 2 / 4 dimensional data used as test data labels : list label number used to extract dice coefficient labelnum : by default is None label number size . We recommend to provide label number here . dice coefficient / percentage computed from function output_dice consists of a 4 dimension array permutation x threhold x subjects x regions the first dimension permutation means the results of each permutation the second dimension threhold means the results of pre - set threshold the third dimension subjects means the results of each subject the fourth dimension regions means the result of each region >>> output_overlap = nfold_location_overlap ( imgdata1 imgdata2 [ 2 4 ] labelnum = 4 )
def nfold_location_overlap(imgdata1, imgdata2, labels, labelnum = None, index = 'dice', thr_meth = 'prob', prob_meth = 'part', n_fold=2, thr_range = [0,1,0.1], n_permutation=1, controlsize = False, actdata = None):
    if imgdata1.ndim == 4:
        imgdata1 = imgdata1.reshape((imgdata1.shape[0], imgdata1.shape[3]))
    if imgdata2.ndim == 4:
        imgdata2 = imgdata2.reshape((imgdata2.shape[0], imgdata2.shape[3]))
    if actdata is not None:
        if actdata.ndim == 4:
            actdata = actdata.reshape((actdata.shape[0], actdata.shape[3]))
    n_subj = imgdata1.shape[1]
    if labelnum is None:
        labelnum = int(np.max(np.unique(imgdata1)))
    assert (np.max(labels)<labelnum+1), "the maximum of labels should smaller than labelnum"
    output_overlap = []
    for n in range(n_permutation):
        print("permutation {} starts".format(n+1))
        test_subj = np.sort(np.random.choice(range(n_subj), n_subj-n_subj/n_fold, replace = False)).tolist()
        verify_subj = [val for val in range(n_subj) if val not in test_subj]
        test_data = imgdata1[:,test_subj]
        verify_data = imgdata2[:,verify_subj]
        if actdata is not None:
            verify_actdata = actdata[...,verify_subj]
        else:
            verify_actdata = None
        pm = make_pm(test_data, prob_meth, labelnum)
        pm_temp = cv_pm_overlap(pm, verify_data, labels, labels, thr_meth = thr_meth, thr_range = thr_range, index = index, cmpalllbl = False, controlsize = controlsize, actdata = verify_actdata)
        output_overlap.append(pm_temp)
    output_overlap = np.array(output_overlap)
    return output_overlap
def nfold_location_overlap ( imgdata1 , imgdata2 , labels , labelnum = None , index = 'dice' , thr_meth = 'prob' , prob_meth = 'part' , n_fold = 2 , thr_range = [ 0 , 1 , 0.1 ] , n_permutation = 1 , controlsize = False , actdata = None ) : if imgdata1 . ndim == 4 : imgdata1 = imgdata1 . reshape ( ( imgdata1 . shape [ 0 ] , imgdata1 . shape [ 3 ] ) ) if imgdata2 . ndim == 4 : imgdata2 = imgdata2 . reshape ( ( imgdata2 . shape [ 0 ] , imgdata2 . shape [ 3 ] ) ) if actdata is not None : if actdata . ndim == 4 : actdata = actdata . reshape ( ( actdata . shape [ 0 ] , actdata . shape [ 3 ] ) ) n_subj = imgdata1 . shape [ 1 ] if labelnum is None : labelnum = int ( np . max ( np . unique ( imgdata1 ) ) ) assert ( np . max ( labels ) < labelnum + 1 ) , "the maximum of labels should smaller than labelnum" output_overlap = [ ] for n in range ( n_permutation ) : print ( "permutation {} starts" . format ( n + 1 ) ) test_subj = np . sort ( np . random . choice ( range ( n_subj ) , n_subj - n_subj / n_fold , replace = False ) ) . tolist ( ) verify_subj = [ val for val in range ( n_subj ) if val not in test_subj ] test_data = imgdata1 [ : , test_subj ] verify_data = imgdata2 [ : , verify_subj ] if actdata is not None : verify_actdata = actdata [ ... , verify_subj ] else : verify_actdata = None pm = make_pm ( test_data , prob_meth , labelnum ) pm_temp = cv_pm_overlap ( pm , verify_data , labels , labels , thr_meth = thr_meth , thr_range = thr_range , index = index , cmpalllbl = False , controlsize = controlsize , actdata = verify_actdata ) output_overlap . append ( pm_temp ) output_overlap = np . array ( output_overlap ) return output_overlap
Decide the maximum threshold from raw image data.
Decide the maximum threshold from raw image data .
"""
    Decide the maximum threshold from raw image data.
    Here using the cross validation method to decide threhold using for getting the maximum probabilistic map
    
    Parameters:
    -----------
    imgdata1: A 2/4 dimensional data, used as train data
    imgdata2: A 2/4 dimensional data, used as test data
    labels: list, label number used to extract dice coefficient
    labelnum: by default is None, label number size. We recommend to provide label number here.
    index: 'dice' or 'percent'
    thr_meth: 'prob', threshold probabilistic map by probabilistic values (MPM)
              'number', threshold probabilistic map by numbers of vertex
    prob_meth: 'all' or 'part' subjects to use to compute probablistic map
    n_fold: split data into n_fold part, using first n_fold-1 part to get probabilistic map, then using rest part to evaluate overlap condition, by default is 2
    thr_range: pre-set threshold range to find the best maximum probabilistic threshold, the best threshold will search in this parameters, by default is [0,1,0.1], as the format of [start, stop, step]
    n_permuation: times of permutation, by default is 10
    controlsize: whether control label data size with template mpm label size or not, by default is False.
    actdata: if controlsize is True, please input actdata as a parameter. By default is None.

    Return:
    -------
    output_overlap: dice coefficient/percentage computed from function
                    output_dice consists of a 4 dimension array
                    permutation x threhold x subjects x regions
                    the first dimension permutation means the results of each permutation
                    the second dimension threhold means the results of pre-set threshold
                    the third dimension subjects means the results of each subject
                    the fourth dimension regions means the result of each region
    
    Example:
    --------
    >>> output_overlap = nfold_location_overlap(imgdata1, imgdata2, [2,4], labelnum = 4)
    """
imgdata1
imgdata2
labels
labelnum
index
thr_meth
prob_meth
n_fold
thr_range
n_permutation
controlsize
actdata