def extract_metric_from_slice_set(slices_set, seg_to_use=None, metric='Mean', gm_percentile=0.03, wm_percentile=0.05, save=False, output='metric_in_dictionary.txt'):
    """
    uses the registereg images and GM segmentation (or another segmentation)to extract mean intensity values in the WM dn GM
    :param slices_set:
    :param seg_to_use: must be of GM not WM
    :param gm_percentile:
    :param wm_percentile:
    :param save:
    :return:
    """
    import copy
    level_label = {0: '', 1: 'C1', 2: 'C2', 3: 'C3', 4: 'C4', 5: 'C5', 6: 'C6', 7: 'C7', 8: 'T1', 9: 'T2', 10: 'T3', 11: 'T4', 12: 'T5', 13: 'T6'}
    if save:
        f = open(output, 'w')
        f.write('Slice id - Slice level - '+metric+' in WM - '+metric+' in GM - Std in WM - Std in GM\n')
    else:
        f = None
    slice_set_metric = {}
    for i, slice_i in enumerate(slices_set):
        gm_dat = copy.deepcopy(slice_i.im_M)
        wm_dat = copy.deepcopy(slice_i.im_M)

        # mask with the gray matter segmentation
        if seg_to_use is None:
            gm_dat[slice_i.gm_seg_M == 0] = 0
            wm_dat[slice_i.gm_seg_M == 1] = 0
        else:
            gm_dat[seg_to_use[i] == 0] = 0
            wm_dat[seg_to_use[i] == 1] = 0

        # threshold to 0.1 to get rid of the (almost) null values
        gm_dat = gm_dat[gm_dat > 0.1]
        wm_dat = wm_dat[wm_dat > 0.1]

        # metric in GM
        if gm_percentile != 0:
            # removing outliers with a percentile
            gm_dat = sorted(gm_dat.flatten())
            n_gm_outliers = int(round(gm_percentile*2.0/len(gm_dat)))
            if n_gm_outliers != 0 and n_gm_outliers*2 < len(gm_dat):
                gm_dat = gm_dat[n_gm_outliers:-n_gm_outliers]

        if gm_dat == []:
            gm_met = 0
            gm_std = 0
        else:
            if metric.lower() == 'mean':
                gm_met = np.mean(gm_dat)
            elif metric.lower() == 'median':
                gm_met = np.median(gm_dat)
            gm_std = np.std(gm_dat)

        # metric in WM
        if wm_percentile != 0:
            # removing outliers with a percentile
            wm_dat = sorted(wm_dat.flatten())
            n_wm_outliers = int(round(wm_percentile*len(wm_dat)/2.0))
            if n_wm_outliers != 0 and n_wm_outliers*2 < len(wm_dat):
                wm_dat = wm_dat[n_wm_outliers:-n_wm_outliers]
        if wm_dat == []:
            wm_met = 0
            wm_std = 0
        else:
            if metric.lower() == 'mean':
                wm_met = np.mean(wm_dat)
            elif metric.lower() == 'median':
                wm_met = np.median(wm_dat)
            wm_std = np.std(wm_dat)

        slice_set_metric[slice_i.id] = (wm_met, gm_met, wm_std, gm_std)
        if save:
            f.write(str(slice_i.id) + ' - ' + level_label[int(slice_i.level)] + ' - ' + str(wm_met) + ' - ' + str(gm_met) + ' - ' + str(wm_std) + ' - ' + str(gm_std) + '\n')
    if save:
        f.close()
    return slice_set_metric



########################################################################################################################
# -------------------------------------------------- PRETREATMENTS --------------------------------------------------- #
########################################################################################################################

# ------------------------------------------------------------------------------------------------------------------
dataset/ETHPy150Open neuropoly/spinalcordtoolbox/scripts/msct_gmseg_utils.py/extract_metric_from_slice_set/swapped operands of `/`