Skip to content

Commit

Permalink
Merge branch 'main' into fix_tests
Browse files Browse the repository at this point in the history
  • Loading branch information
haesleinhuepf authored Dec 28, 2022
2 parents 8ccfe4e + 75771ba commit 0c4edb7
Show file tree
Hide file tree
Showing 9 changed files with 437 additions and 20 deletions.
Binary file modified images/object_classifier_gui.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
try:
from ._version import version as __version__
except ImportError:
__version__ = "0.11.1"
__version__ = "0.12.0"



Expand Down
26 changes: 14 additions & 12 deletions napari_accelerated_pixel_and_object_classification/_dock_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,22 +294,14 @@ def train(self,
print("Applying / prediction done.")

short_filename = filename.split("/")[-1]
self._add_to_viewer("Result of " + short_filename, result, scale)
_add_to_viewer(self.viewer, self.classifier_class == ProbabilityMapper, "Result of " + short_filename, result, scale)

if show_classifier_statistics and self.viewer is not None:
table = QTableWidget()
update_model_analysis(table, classifier)
self.viewer.window.add_dock_widget(table)

def _add_to_viewer(self, name, data, scale=None):
try:
self.viewer.layers[name].data = data.astype(int)
self.viewer.layers[name].visible = True
except KeyError:
if self.classifier_class == ProbabilityMapper:
self.viewer.add_image(data, name=name)
else:
self.viewer.add_labels(data.astype(int), name=name, scale=scale)


def predict(self, images, filename, scale=None):
print("predict")
Expand All @@ -332,7 +324,7 @@ def predict(self, images, filename, scale=None):

short_filename = filename.split("/")[-1]

self._add_to_viewer("Result of " + short_filename, result, scale=scale)
_add_to_viewer(self.viewer, self.classifier_class == ProbabilityMapper, "Result of " + short_filename, result, scale)

def update_memory_consumption(self):
number_of_pixels = np.sum(tuple([np.prod(i.shape) for i in self.get_selected_images_data()]))
Expand Down Expand Up @@ -555,9 +547,10 @@ def getFeatures(self):
def napari_experimental_provide_dock_widget():
# you can return either a single widget, or a sequence of widgets
from ._function import Train_object_classifier
from ._object_classification_widget import ObjectClassification
from ._custom_table_row_classifier import CustomObjectClassifierWidget
from ._object_merger import Train_object_merger
return [ObjectSegmentation, SemanticSegmentation, Train_object_classifier, CustomObjectClassifierWidget, Train_object_merger]
return [ObjectSegmentation, SemanticSegmentation, CustomObjectClassifierWidget, Train_object_merger, ObjectClassification]


def set_border(widget:QWidget, spacing=2, margin=0):
Expand All @@ -566,3 +559,12 @@ def set_border(widget:QWidget, spacing=2, margin=0):
if hasattr(widget.layout(), "setSpacing"):
widget.layout().setSpacing(spacing)

def _add_to_viewer(viewer, as_image, name, data, scale=None):
try:
viewer.layers[name].data = data.astype(int)
viewer.layers[name].visible = True
except KeyError:
if as_image:
viewer.add_image(data, name=name, scale=scale)
else:
viewer.add_labels(data.astype(int), name=name, scale=scale)
19 changes: 16 additions & 3 deletions napari_accelerated_pixel_and_object_classification/_function.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from apoc import PredefinedFeatureSet, PixelClassifier, ObjectSegmenter, ObjectClassifier, ProbabilityMapper
import warnings

from apoc import PredefinedFeatureSet, PixelClassifier, ObjectSegmenter, ObjectClassifier, ObjectSelector, ProbabilityMapper

import numpy as np
from napari_plugin_engine import napari_hook_implementation
Expand Down Expand Up @@ -191,7 +193,7 @@ def Connected_component_labeling(labels: "napari.types.LabelsData", object_class
instances = cle.connected_components_labeling_box(binary)
return instances

@register_dock_widget(menu="Segmentation post-processing > Object classification (APOC)")
@register_dock_widget(menu="Segmentation post-processing > Object classification (deprecated, APOC)")
@magic_factory(
model_filename=dict(widget_type='FileEdit', mode='w'),
shape_extension_ratio=dict(label='shape (extension ratio)')
Expand Down Expand Up @@ -223,7 +225,7 @@ def Train_object_classifier(image: "napari.types.ImageData",
show_feature_correlation_matrix=False,
viewer : "napari.Viewer" = None
) -> "napari.types.LabelsData":

warnings.warn("This function is deprecated. Use the classes in the APOC package instead.", DeprecationWarning)
features = ","
if pixel_count:
features = features + "area,"
Expand Down Expand Up @@ -318,6 +320,17 @@ def Apply_object_classification(image: "napari.types.ImageData",
result = clf.predict(labels, image)
return result

@register_function(menu="Segmentation post-processing > Object selection (apply pretrained, APOC)")
@time_slicer
def Apply_object_classification(image: "napari.types.ImageData",
labels: "napari.types.LabelsData",
model_filename : str = "ObjectSelector.cl",
viewer: napari.Viewer = None) -> "napari.types.LabelsData":

clf = ObjectSelector(opencl_filename=model_filename)
result = clf.predict(labels, image)
return result

@register_function(menu="Measurement > Annotation to cluster ID column (APOC)")
def annotation_to_cluster_id(labels: "napari.types.LabelsData", annotation: "napari.types.LabelsData", name:str="ANNOTATION", viewer:napari.Viewer=None):
import pyclesperanto_prototype as cle
Expand Down
Loading

0 comments on commit 0c4edb7

Please sign in to comment.