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)
예제 #2
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_target(self):
        # get gm seg from model by level
        gm_seg_model, wm_seg_model = self.model.get_gm_wm_by_level()

        # for each target slice: normalize
        for target_slice in self.target_im:
            level_int = int(round(target_slice.level))
            if level_int not in self.model.intensities.index:
                level_int = 0
            norm_im_M = normalize_slice(target_slice.im_M, gm_seg_model[level_int], wm_seg_model[level_int], self.model.intensities['GM'][level_int], self.model.intensities['WM'][level_int], val_min=self.model.intensities['MIN'][level_int], val_max=self.model.intensities['MAX'][level_int])
            target_slice.set(im_m=norm_im_M)
    def normalize_target(self):
        # get gm seg from model by level
        gm_seg_model, wm_seg_model = self.model.get_gm_wm_by_level()

        # for each target slice: normalize
        for target_slice in self.target_im:
            level_int = int(np.round(target_slice.level))
            if level_int not in self.model.intensities.index:
                level_int = 0
            norm_im_M = normalize_slice(target_slice.im_M, gm_seg_model[level_int], wm_seg_model[level_int], self.model.intensities['GM'][level_int], self.model.intensities['WM'][level_int], val_min=self.model.intensities['MIN'][level_int], val_max=self.model.intensities['MAX'][level_int])
            target_slice.set(im_m=norm_im_M)