Skip to content

Commit

Permalink
added ProbabilityMapper to GUI
Browse files Browse the repository at this point in the history
  • Loading branch information
haesleinhuepf committed Dec 3, 2021
1 parent 9aeb3c7 commit 0c20968
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 7 deletions.
31 changes: 25 additions & 6 deletions napari_accelerated_pixel_and_object_classification/_dock_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import numpy as np
import napari

from apoc import PredefinedFeatureSet, ObjectSegmenter, PixelClassifier
from apoc import PredefinedFeatureSet, ObjectSegmenter, PixelClassifier, ProbabilityMapper
from napari_tools_menu import register_dock_widget

@register_dock_widget(menu="Segmentation > Object segmentation (APOC)")
Expand Down Expand Up @@ -61,7 +61,13 @@ def __init__(self, napari_viewer, classifier_class=ObjectSegmenter):
training_widget.setLayout(QVBoxLayout())

# Annotation
training_widget.layout().addWidget(QLabel("Select ground truth annotation / object label"))
if self.classifier_class == ObjectSegmentation:
suffix = " + object class"
elif self.classifier_class == ProbabilityMapper:
suffix = " + class for probability output"
else:
suffix = ""
training_widget.layout().addWidget(QLabel("Select ground truth annotation" + suffix))
self.label_list = QComboBox()
self.update_label_list()

Expand All @@ -74,9 +80,11 @@ def __init__(self, napari_viewer, classifier_class=ObjectSegmenter):
num_object_annotation_spinner.setMaximumWidth(40)
num_object_annotation_spinner.setMinimum(1)
num_object_annotation_spinner.setValue(2)
if self.classifier_class != PixelClassifier:
print("classifier ", self.classifier_class)
if self.classifier_class == ObjectSegmentation:
temp.layout().addWidget(num_object_annotation_spinner)
elif self.classifier_class == ProbabilityMapper:
temp.layout().addWidget(num_object_annotation_spinner)

training_widget.layout().addWidget(temp)

# Features
Expand Down Expand Up @@ -217,8 +225,10 @@ def train(self, images, annotation, object_annotation_value, feature_definition,
num_ensembles=num_trees,
max_depth=num_max_depth)

if hasattr(clf, "positive_class_identifier"):
if self.classifier_class == ObjectSegmenter:
clf.positive_class_identifier = object_annotation_value
elif self.classifier_class == ProbabilityMapper:
clf.output_probability_of_class = object_annotation_value

print("annotation shape", annotation.shape)

Expand All @@ -238,7 +248,10 @@ def _add_to_viewer(self, name, data):
self.viewer.layers[name].data = data
self.viewer.layers[name].visible = True
except KeyError:
self.viewer.add_labels(data, name=name)
if self.classifier_class == ProbabilityMapper:
self.viewer.add_image(data, name=name)
else:
self.viewer.add_labels(data, name=name)

def predict(self, images, filename):
print("predict")
Expand Down Expand Up @@ -375,6 +388,12 @@ class SemanticSegmentation(ObjectSegmentation):
def __init__(self, napari_viewer):
super().__init__(napari_viewer, classifier_class=PixelClassifier)

@register_dock_widget(menu="Filtering > Probability mapper (APOC)")
class ProbabilityMapping(ObjectSegmentation):
def __init__(self, napari_viewer):
super().__init__(napari_viewer, classifier_class=ProbabilityMapper)


class FeatureSelector(QWidget):
def __init__(self, feature_definition:str):
super().__init__()
Expand Down
13 changes: 12 additions & 1 deletion napari_accelerated_pixel_and_object_classification/_function.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from apoc import PredefinedFeatureSet, PixelClassifier, ObjectSegmenter, ObjectClassifier
from apoc import PredefinedFeatureSet, PixelClassifier, ObjectSegmenter, ObjectClassifier, ProbabilityMapper

import numpy as np
from napari_plugin_engine import napari_hook_implementation
Expand All @@ -12,6 +12,7 @@ def napari_experimental_provide_function():
return [
Train_object_segmentation,
Apply_object_segmentation,
Apply_probability_mapper,
Train_object_segmentation_from_visible_image_layers,
Apply_object_segmentation_to_visible_image_layers,
Train_pixel_classifier,
Expand Down Expand Up @@ -101,6 +102,16 @@ def Train_object_segmentation(
return result


@register_function(menu="Filtering > Probability Mapper (apply pretrained, APOC)")
@time_slicer
def Apply_probability_mapper(image: "napari.types.ImageData",
model_filename : str = "ProbabilityMapper.cl",
viewer: napari.Viewer = None) -> "napari.types.ImageData":
clf = ProbabilityMapper(opencl_filename=model_filename)
result = clf.predict(image=image)
return result


@register_function(menu="Segmentation > Object segmentation (apply pretrained, APOC)")
@time_slicer
def Apply_object_segmentation(image: "napari.types.ImageData",
Expand Down

0 comments on commit 0c20968

Please sign in to comment.