示例#1
0
    def create_train_target_from_ext_infos(self):
        """
        Create training and target vectors from the
        given extensions
        :return:
        """
        # store training and correction
        train_nuclei = list()
        corr_nuclei = list()

        # go through infos
        for info in self.ext_infos:
            # load segmentation
            seg = Segmentation(info)
            seg.load()

            # add to training nuclei
            train_nuclei += seg.nuclei.copy()

            # load correction
            corr = Correction(seg)
            corr.load_corrections()

            # add to correction
            corr_nuclei += corr.corr_nonuc.copy()

        # load train params
        train_params = cfg.clf_train_params

        # create data and target vectors
        train_data = np.zeros(shape=(len(train_nuclei), len(train_params)))
        train_target = np.zeros(shape=(len(train_nuclei)))

        # go through nuclei and build target vector
        for i, nucleus in enumerate(train_nuclei):
            # get relevant params from nucleus
            for j, key in enumerate(train_params):
                train_data[i][j] = nucleus[key]

            # set target
            nucleus_value = 0

            # is it a nucleus?
            if Correction.is_correction_nonuc_with_list(nucleus, corr_nuclei) is False:
                nucleus_value = 1  # yes - it is

            train_target[i] = nucleus_value

        return train_data, train_target
示例#2
0
for info in loaded_infos:
    info_ID = info['ID']
    if info_ID in selected_info_IDs:
        infos.append(info)
        print('TEST APPEND', info)

img_maps = dict()
val_maps = dict()
tiling_count_map = dict()
tiling_maps = dict()
limits = dict()
limit_lists = dict()

for info in infos:
    seg = Segmentation(info)
    seg.load()

    # preparations for params
    if 'between_space' in params_to_get:
        # get binary stack without nuclei
        filled_minus_nuclei = ImageHandler.load_tiff_as_stack(
            seg.get_results_dir().tmp + 'filled_minus_nuclei.tif')

        if filled_minus_nuclei is None:
            filled_stack = np.full(
                (seg.stacks.lamin.shape[0], seg.stacks.lamin.shape[1],
                 seg.stacks.lamin.shape[2]),
                1,
                dtype=np.uint8)

            filled_minus_nuclei = seg.nuclei.add_nuclei_to_stack(
示例#3
0
class CriteriaSelection(QtGui.QDialog):

    def __init__(self, image_info, force_nuclei_load=False, parent=None):
        super(CriteriaSelection, self).__init__(parent)

        # nuclei selection?
        if not hasattr(self, 'is_nuclei_selection'):
            self.is_nuclei_selection = False

        # load image infos
        self.image_info = image_info

        # prepare criteria
        self.criteria = dict()

        # init segmentation
        self.segmentation = Segmentation(self.image_info)

        # load segmentation
        self.segmentation.load(force_nuclei_load=force_nuclei_load)

        # buttons
        self.btn_reset = QtGui.QPushButton(gui_labels.btn_reset)
        self.btn_load = QtGui.QPushButton(gui_labels.btn_load)
        self.btn_save = QtGui.QPushButton(gui_labels.btn_save)
        self.btn_close = QtGui.QPushButton(gui_labels.btn_close)

        # listeners
        self.btn_reset.clicked.connect(self.reset_criteria)
        self.btn_load.clicked.connect(self.load_criteria)
        self.btn_save.clicked.connect(self.save_criteria)
        self.btn_close.clicked.connect(self.close)

    def prep_ctn_criteria_select(self, lookup_nucleus):
        """
        Prepare main container for criteria selection

        :return:
        """
        container = QtGui.QGridLayout()

        # select criteria
        criteria = cfg.filter_criteria_labels

        if lookup_nucleus is True:
            criteria = cfg.filter_criteria_nuclei

        counter = 0
        for param in criteria:
            # get container
            param_ctn = self.prep_criteria(param.lower(), lookup_nucleus=lookup_nucleus)

            # add container
            if param_ctn is not None:
                container.addLayout(param_ctn, counter, 0)

                counter += 1

        # submit buttons
        container.addLayout(self.prep_ctn_submit(), counter, 0)

        return container

    def reset_criteria(self):
        """
        Reset edit boxes

        :return:
        """
        # go through edit boxes and empty
        for criteria_raw in self.criteria:
            self.criteria[criteria_raw]['edt_min'].setText('')
            self.criteria[criteria_raw]['edt_max'].setText('')

    def load_criteria(self):
        """
        Load selection criteria

        :return:
        """
        # load criteria
        self.loaded_criteria = self.segmentation.get_nuclei_criteria(True)

        # set min/max fields for critieria
        for criteria_raw in self.criteria:
            # edit boxes
            if self.loaded_criteria is not None and criteria_raw in self.loaded_criteria.keys():
                # set value in edit box
                if self.loaded_criteria[criteria_raw]['MIN'] is not None:
                    self.criteria[criteria_raw]['edt_min'].setText(str(self.loaded_criteria[criteria_raw]['MIN']))

                if self.loaded_criteria[criteria_raw]['MAX'] is not None:
                    self.criteria[criteria_raw]['edt_max'].setText(str(self.loaded_criteria[criteria_raw]['MAX']))

    def save_criteria(self):
        """
        Save selection criteria for labels

        :return:
        """
        # create criteria mapping
        criteria = dict()

        # go through criteria and get the values
        for param in self.loaded_criteria.keys():

            # is criteria on mask?
            if param.lower() in self.criteria.keys():
                # get min and max
                min_input = self.criteria[param.lower()]['edt_min'].text()
                max_input = self.criteria[param.lower()]['edt_max'].text()

                min_value = None
                if min_input:
                    min_value = '%.1f' % float(min_input)

                max_value = None
                if max_input:
                    max_value = '%.1f' % float(max_input)
            else:
                min_value = self.loaded_criteria[param]['MIN']
                max_value = self.loaded_criteria[param]['MAX']

            # write to mapping
            criteria[param] = dict()
            criteria[param]['MIN'] = min_value
            criteria[param]['MAX'] = max_value

        # set criteria
        self.loaded_criteria = criteria

        # save to file
        self.segmentation.save_nuclei_criteria(criteria)

    def close(self):
        """
        Close criteria selection and show the main window again

        :return:
        """
        # show parent
        self.parent().showNormal()

        # close processing
        super(QtGui.QDialog, self).close()

    def prep_criteria(self, param, lookup_nucleus):
        """
        Prepare a container with the parameter

        :param param:
        :return:
        """
        container = None

        prep_ctn = True

        # is param part of the nuclei?
        if lookup_nucleus is True:
            if self.segmentation.nuclei.is_param_in_nuclei(param) is False:
                prep_ctn = False

        if prep_ctn is True:
            # prepare dictionary
            self.prep_criteria_dict(param)

            # prepare container
            container = self.prep_criteria_ctn(param)

        return container

    def prep_criteria_dict(self, param):
        """
        Prepare dictionary for param

        :param param:
        :return:
        """
        self.criteria[param] = dict()

        # edit boxes
        self.criteria[param]['edt_min'] = QtGui.QLineEdit()
        self.criteria[param]['edt_max'] = QtGui.QLineEdit()

        # figures
        self.criteria[param]['fig_hist'] = plt.figure(figsize=(5, 2))
        self.criteria[param]['fig_example'] = plt.figure(figsize=(5, 2))

        # canvases
        self.criteria[param]['cnv_hist'] = FigureCanvas(self.criteria[param]['fig_hist'])
        self.criteria[param]['cnv_example'] = FigureCanvas(self.criteria[param]['fig_example'])

        # misc widgets
        self.criteria[param]['sld_hist'] = QtGui.QSlider(QtCore.Qt.Horizontal, self)

    def prep_criteria_ctn(self, param):
        """
        Container to set criteria param

        :return:
        """
        container = QtGui.QGridLayout()

        # labels
        container.addWidget(QtGui.QLabel(getattr(gui_labels, 'crit_sel_label_' + param)), 0, 0)
        container.addWidget(QtGui.QLabel(gui_labels.crit_sel_min), 1, 0)
        container.addWidget(QtGui.QLabel(gui_labels.crit_sel_max), 2, 0)
        container.setRowStretch(3, 1)

        # edit boxes
        container.addWidget(self.criteria[param]['edt_min'], 1, 1)
        container.addWidget(self.criteria[param]['edt_max'], 2, 1)

        # sort nuclei
        if self.is_nuclei_selection:
            # sort nuclei
            self.segmentation.nuclei.sort_nuclei(param)

            # get sorted list
            # TODO transform the props into a dict
            self.criteria[param]['sorted_labels'] = self.segmentation.nuclei.get_param_list_from_nuclei(param)
            self.criteria[param]['sorted_props'] = self.segmentation.nuclei.get_param_list_from_nuclei(create_dict=True)
        else:
            # sort labels
            self.criteria[param]['sorted_labels'] = self.segmentation.get_sorted_prop_list(param)
            self.criteria[param]['sorted_props'] = self.segmentation.sorted_probs.copy()

        # show histogram
        Plot.view_histogram_of_value_list(self.criteria[param]['fig_hist'],
                                          self.criteria[param]['sorted_labels'],
                                          cfg.criteria_select_hist_bins)

        # add to container
        container.addWidget(self.criteria[param]['cnv_hist'], 0, 2, 3, 1)
        container.addWidget(self.criteria[param]['cnv_example'], 0, 3, 4, 1)

        # slider
        self.criteria[param]['sld_hist'].valueChanged[int].connect(self.make_change_criteria_example(param))
        self.criteria[param]['sld_hist'].setMinimum(0)
        self.criteria[param]['sld_hist'].setMaximum(len(self.criteria[param]['sorted_props']))
        container.addWidget(self.criteria[param]['sld_hist'], 3, 2)

        return container

    def make_change_criteria_example(self, param):
        """
        Make function to call change criteria

        :param param:
        :return:
        """
        def change_criteria_example_param(example_selected):
            self.change_criteria_example(param, example_selected)

        return change_criteria_example_param

    def change_criteria_example(self, param, example_selected):
        """
        Change example in +/- range

        :param param:
        :param example_selected:
        :return:
        """
        min_range = example_selected - cfg.criteria_select_eg_range

        if min_range < 0:
            min_range = 0

        max_range = example_selected + cfg.criteria_select_eg_range

        if max_range > len(self.criteria[param]['sorted_props']):
            max_range = len(self.criteria[param]['sorted_props'])

        # prepare examples
        examples = list()

        # show examples from each part
        for i in range(min_range, max_range):
            example_title = '%i\n%.2f' % (self.criteria[param]['sorted_props'][i]['nID'],
                                            self.criteria[param]['sorted_props'][i][param])

            # prepare image
            examples = Plot.prepare_output(examples,
                                           self.criteria[param]['sorted_props'][i]['img'],
                                           example_title,
                                           cfg.criteria_select_eg_colour)

        # clear figure
        if len(examples[0]) < (cfg.criteria_select_eg_range * 2):
            self.criteria[param]['fig_example'].clear()

        # show examples
        Plot.show_images(self.criteria[param]['fig_example'], examples, cols=cfg.criteria_select_eg_cols)

        # update canvas
        self.criteria[param]['cnv_example'].draw()
示例#4
0
    # merge
    if processing['merge'] == 1:
        test_window = MergeCriteria(selected_info)
        test_window.show()
        test_window.raise_()
        test_window.activateWindow()
        sys.exit(app.exec_())
    elif processing['merge'] >= 2:
        force_reload = False
        if processing['merge'] == 3:
            force_reload = True

        print('=== Merge ===')
        seg = Segmentation(selected_info)
        seg.load(force_props_load=force_reload)
        seg.segment(process=False, merge=True, filter=False)
        seg.save(force_nuclei_raw=True)
        del(seg)

    # nuclei
    if processing['nuclei'] == 1:
        test_window = NucleiCriteria(selected_info)
        test_window.show()
        test_window.raise_()
        test_window.activateWindow()
        sys.exit(app.exec_())
    elif processing['nuclei'] >= 2:
        force_reload = False
        if processing['nuclei'] == 3:
            force_reload = True
示例#5
0
# import classes
from storage.image import ImageHandler
from processing.segmentation import Segmentation

# load image infos
image_infos = ImageHandler.load_image_infos()

revs_to_merge = list()

# apply defined filters to images
for image_info in image_infos:
    # revision? then skip
    if Segmentation.is_revision_by_ID(image_info['ID']):
        revs_to_merge.append(Segmentation.extract_rev_from_ID(image_info['ID']))
    else:
        # initialise segmentation
        segmentation = Segmentation(image_info)

        # load results
        segmentation.load()

        # merge
        for rev in revs_to_merge:
            segmentation.merge_parent_with_rev(rev)

        # save merge
        segmentation.save_merge_segmentation()

        # reset revs to merge
        revs_to_merge = list()