Skip to content

Commit

Permalink
Merge pull request #29 from fractal-napari-plugins-collection/scikit-…
Browse files Browse the repository at this point in the history
…learn-dependency

Update scikit learn dependency & test no roi_id code path
  • Loading branch information
jluethi authored May 17, 2023
2 parents 5f2f5b0 + cc6e8fe commit 15aee5c
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 10 deletions.
4 changes: 2 additions & 2 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = napari-feature-classifier
version = 0.1.0
version = 0.1.1
author = Joel Luethi and Max Hess
author_email = joel.luethi@uzh.ch
url = https://github.com/fractal-napari-plugins-collection/napari-feature-classifier
Expand Down Expand Up @@ -43,7 +43,7 @@ install_requires =
matplotlib
magicgui
pandas
scikit-learn
scikit-learn >= 1.2.2
pandera
xxhash
hypothesis
Expand Down
23 changes: 18 additions & 5 deletions src/napari_feature_classifier/_tests/test_classifier_widget.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
""" Tests for classifier widget initialization"""
import numpy as np
import pandas as pd
import pytest
import os

import imageio
from pathlib import Path
Expand Down Expand Up @@ -31,17 +33,19 @@ def test_classifier_widgets_initialization_no_features_selected(make_napari_view
assert classifier_widget._run_container is None



features = make_features(np.unique(lbl_img_np)[1:], roi_id="ROI1", n_features=6)
features_no_roi_id = features.drop(columns=["roi_id"])
# make_napari_viewer is a pytest fixture that returns a napari viewer object
# TODO: Verify the actual results of the classification
def test_running_classification_through_widget(make_napari_viewer):
@pytest.mark.parametrize("features", [features, features_no_roi_id])
def test_running_classification_through_widget(features, make_napari_viewer):
"""
Tests if the main widget launches
"""
# make viewer and add an image layer using our fixture
viewer = make_napari_viewer()
label_layer = viewer.add_labels(lbl_img_np)
labels = np.unique(lbl_img_np)[1:]
label_layer.features = make_features(labels, roi_id="ROI1", n_features=6)
label_layer.features = features

# Start init widget
classifier_widget = ClassifierWidget(viewer)
Expand All @@ -66,7 +70,16 @@ def test_running_classification_through_widget(make_napari_viewer):
# Run the classifier
classifier_widget._run_container.run()

# TODO: Assert something
# Assert something that the layer is visible, predictions exist and are not NaN
assert classifier_widget._run_container._prediction_layer.visible
assert "prediction" in label_layer.features.columns
assert pd.notna(label_layer.features["prediction"]).all().all()

# Check that the classifier file was saved
assert Path("lbl_img_np_classifier.clf").exists()

# Delete the classifier file (cleanup to avoid overwriting confirmation)
os.remove("lbl_img_np_classifier.clf")

# TODO: Add a test to check the overwrite confirmations working correctly
# For classifier files, for annotations and for exported predictions
12 changes: 9 additions & 3 deletions src/napari_feature_classifier/classifier_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,8 +298,12 @@ def run(self):
self.add_features_to_classifier()
try:
self._classifier.train()
except ValueError:
napari_info("Not enough annotations made. Please make more annotations.")
except ValueError as e:
napari_info(
"Training failed. A typical reason are not having "
"enough annotations. \nThe error message was: "
f"{e}"
)
else:
self.make_predictions()
self._prediction_layer.visible = True
Expand Down Expand Up @@ -502,7 +506,9 @@ def _update_export_destination(self, label_layer: napari.layers.Labels):
"""
base_path = Path(self._export_destination.value).parent
self._export_destination.value = base_path / f"{label_layer.name}_predictions.csv"
self._export_destination.value = (
base_path / f"{label_layer.name}_predictions.csv"
)

def export_results(self):
"""
Expand Down

0 comments on commit 15aee5c

Please sign in to comment.