Exemple #1
0
    def normalize_model_data(self):
        # get the id of the slices by vertebral level
        id_by_level = {}
        for dic_slice in self.slices:
            level_int = int(round(dic_slice.level))
            if level_int not in id_by_level.keys():
                id_by_level[level_int] = [dic_slice.id]
            else:
                id_by_level[level_int].append(dic_slice.id)

        # get the average median values by level:
        list_gm_by_level = []
        list_wm_by_level = []
        list_min_by_level = []
        list_max_by_level = []
        list_indexes = []

        for level, list_id_slices in id_by_level.items():
            if level != 0:
                list_med_gm = []
                list_med_wm = []
                list_min = []
                list_max = []
                # get median GM and WM values for all slices of the same level:
                for id_slice in list_id_slices:
                    slice = self.slices[id_slice]
                    for gm in slice.gm_seg_M:
                        med_gm = np.median(slice.im_M[gm==1])
                        list_med_gm.append(med_gm)
                    for wm in slice.wm_seg_M:
                        med_wm = np.median(slice.im_M[wm == 1])
                        list_med_wm.append(med_wm)

                    list_min.append(min(slice.im_M.flatten()[slice.im_M.flatten().nonzero()]))
                    list_max.append(max(slice.im_M.flatten()))

                list_gm_by_level.append(np.mean(list_med_gm))
                list_wm_by_level.append(np.mean(list_med_wm))
                list_min_by_level.append(min(list_min))
                list_max_by_level.append(max(list_max))
                list_indexes.append(level)

        # add level 0 for images with no level (or level not in model)
        # average GM and WM for all slices, get min and max of all slices
        list_gm_by_level.append(np.mean(list_gm_by_level))
        list_wm_by_level.append(np.mean(list_wm_by_level))
        list_min_by_level.append(min(list_min_by_level))
        list_max_by_level.append(max(list_max_by_level))
        list_indexes.append(0)

        # save average median values in a Panda data frame
        data_intensities = {'GM': pd.Series(list_gm_by_level, index=list_indexes), 'WM': pd.Series(list_wm_by_level, index=list_indexes), 'MIN': pd.Series(list_min_by_level, index=list_indexes), 'MAX': pd.Series(list_max_by_level, index=list_indexes)}
        self.intensities = pd.DataFrame(data_intensities)

        # Normalize slices using dic values
        for dic_slice in self.slices:
            level_int = int(round(dic_slice.level))
            av_gm_slice, av_wm_slice = average_gm_wm([dic_slice], bin=True)
            norm_im_M = normalize_slice(dic_slice.im_M, av_gm_slice, av_wm_slice, self.intensities['GM'][level_int], self.intensities['WM'][level_int], val_min=self.intensities['MIN'][level_int], val_max=self.intensities['MAX'][level_int])
            dic_slice.set(im_m=norm_im_M)
    def normalize_model_data(self):
        # get the id of the slices by vertebral level
        id_by_level = {}
        for dic_slice in self.slices:
            level_int = int(round(dic_slice.level))
            if level_int not in id_by_level.keys():
                id_by_level[level_int] = [dic_slice.id]
            else:
                id_by_level[level_int].append(dic_slice.id)

        # get the average median values by level:
        list_gm_by_level = []
        list_wm_by_level = []
        list_min_by_level = []
        list_max_by_level = []
        list_indexes = []

        for level, list_id_slices in id_by_level.items():
            if level != 0:
                list_med_gm = []
                list_med_wm = []
                list_min = []
                list_max = []
                # get median GM and WM values for all slices of the same level:
                for id_slice in list_id_slices:
                    slice = self.slices[id_slice]
                    for gm in slice.gm_seg_M:
                        med_gm = np.median(slice.im_M[gm == 1])
                        list_med_gm.append(med_gm)
                    for wm in slice.wm_seg_M:
                        med_wm = np.median(slice.im_M[wm == 1])
                        list_med_wm.append(med_wm)

                    list_min.append(min(slice.im_M.flatten()[slice.im_M.flatten().nonzero()]))
                    list_max.append(max(slice.im_M.flatten()))

                list_gm_by_level.append(np.mean(list_med_gm))
                list_wm_by_level.append(np.mean(list_med_wm))
                list_min_by_level.append(min(list_min))
                list_max_by_level.append(max(list_max))
                list_indexes.append(level)

        # add level 0 for images with no level (or level not in model)
        # average GM and WM for all slices, get min and max of all slices
        list_gm_by_level.append(np.mean(list_gm_by_level))
        list_wm_by_level.append(np.mean(list_wm_by_level))
        list_min_by_level.append(min(list_min_by_level))
        list_max_by_level.append(max(list_max_by_level))
        list_indexes.append(0)

        # save average median values in a Panda data frame
        data_intensities = {'GM': pd.Series(list_gm_by_level, index=list_indexes), 'WM': pd.Series(list_wm_by_level, index=list_indexes), 'MIN': pd.Series(list_min_by_level, index=list_indexes), 'MAX': pd.Series(list_max_by_level, index=list_indexes)}
        self.intensities = pd.DataFrame(data_intensities)

        # Normalize slices using dic values
        for dic_slice in self.slices:
            level_int = int(round(dic_slice.level))
            av_gm_slice, av_wm_slice = average_gm_wm([dic_slice], bin=True)
            norm_im_M = normalize_slice(dic_slice.im_M, av_gm_slice, av_wm_slice, self.intensities['GM'][level_int], self.intensities['WM'][level_int], val_min=self.intensities['MIN'][level_int], val_max=self.intensities['MAX'][level_int])
            dic_slice.set(im_m=norm_im_M)
    def label_fusion(self, list_dic_indexes_by_slice):
        for target_slice in self.target_im:
            # get list of slices corresponding to the indexes
            list_dic_slices = [self.model.slices[j] for j in list_dic_indexes_by_slice[target_slice.id]]
            # average slices GM and WM
            # WM is not used anymore here, but the average_gm_wm() function is used in other parts of the code that need both the GM and WM averages
            data_mean_gm, data_mean_wm = average_gm_wm(list_dic_slices)
            # set negative values to 0
            data_mean_gm[data_mean_gm < 0] = 0

            # store segmentation into target_im
            target_slice.set(gm_seg_m=data_mean_gm)
    def label_fusion(self, list_dic_indexes_by_slice):
        for target_slice in self.target_im:
            # get list of slices corresponding to the indexes
            list_dic_slices = [self.model.slices[j] for j in list_dic_indexes_by_slice[target_slice.id]]
            # average slices GM and WM
            # WM is not used anymore here, but the average_gm_wm() function is used in other parts of the code that need both the GM and WM averages
            data_mean_gm, data_mean_wm = average_gm_wm(list_dic_slices)
            # set negative values to 0
            data_mean_gm[data_mean_gm < 0] = 0

            # store segmentation into target_im
            target_slice.set(gm_seg_m=data_mean_gm)
Exemple #5
0
 def label_fusion(self, list_dic_indexes_by_slice):
     for target_slice in self.target_im:
         # get list of slices corresponding to the indexes
         list_dic_slices = [self.model.slices[j] for j in list_dic_indexes_by_slice[target_slice.id]]
         # average slices GM and WM
         data_mean_gm, data_mean_wm = average_gm_wm(list_dic_slices)
         if self.param_seg.type_seg == 'bin':
             # binarize GM seg
             data_mean_gm[data_mean_gm >= 0.5] = 1
             data_mean_gm[data_mean_gm < 0.5] = 0
             # binarize WM seg
             data_mean_wm[data_mean_wm >= 0.5] = 1
             data_mean_wm[data_mean_wm < 0.5] = 0
         # store segmentation into target_im
         target_slice.set(gm_seg_m=data_mean_gm, wm_seg_m=data_mean_wm)
    def get_gm_wm_by_level(self):
        gm_seg_model = {}  # dic of mean gm seg by vertebral level
        wm_seg_model = {}  # dic of mean wm seg by vertebral level
        # get id of the slices by level
        slices_by_level = {}
        for dic_slice in self.slices:
            level_int = int(round(dic_slice.level))
            if level_int not in slices_by_level.keys():
                slices_by_level[level_int] = [dic_slice]
            else:
                slices_by_level[level_int].append(dic_slice)
        # get average gm and wm by level
        for level, list_slices in slices_by_level.items():
            data_mean_gm, data_mean_wm = average_gm_wm(list_slices)
            gm_seg_model[level] = data_mean_gm
            wm_seg_model[level] = data_mean_wm
        # for level=0 (no leve or level not in model) output average GM and WM seg across all model data
        gm_seg_model[0] = np.mean(gm_seg_model.values(), axis=0)
        wm_seg_model[0] = np.mean(wm_seg_model.values(), axis=0)

        return gm_seg_model, wm_seg_model
    def get_gm_wm_by_level(self):
        gm_seg_model = {}  # dic of mean gm seg by vertebral level
        wm_seg_model = {}  # dic of mean wm seg by vertebral level
        # get id of the slices by level
        slices_by_level = {}
        for dic_slice in self.slices:
            level_int = int(np.round(dic_slice.level))
            if level_int not in set(slices_by_level.keys()):
                slices_by_level[level_int] = [dic_slice]
            else:
                slices_by_level[level_int].append(dic_slice)
        # get average gm and wm by level
        for level, list_slices in slices_by_level.items():
            data_mean_gm, data_mean_wm = average_gm_wm(list_slices)
            gm_seg_model[level] = data_mean_gm
            wm_seg_model[level] = data_mean_wm
        # for level=0 (no leve or level not in model) output average GM and WM seg across all model data
        gm_seg_model[0] = np.mean(list(gm_seg_model.values()), axis=0)
        wm_seg_model[0] = np.mean(list(wm_seg_model.values()), axis=0)

        return gm_seg_model, wm_seg_model