From 1849096723d4fae457934e6f53cf32551f66ee5c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Laura=20=C5=BDigutyt=C4=97?= Date: Fri, 24 Mar 2023 13:44:19 +0100 Subject: [PATCH 1/3] add a test for histogram plotting --- .../_tests/test_plotter.py | 51 ++++++++++++++----- 1 file changed, 39 insertions(+), 12 deletions(-) diff --git a/napari_clusters_plotter/_tests/test_plotter.py b/napari_clusters_plotter/_tests/test_plotter.py index 129b783d..f5d438bb 100644 --- a/napari_clusters_plotter/_tests/test_plotter.py +++ b/napari_clusters_plotter/_tests/test_plotter.py @@ -1,4 +1,5 @@ import numpy as np +import pandas as pd from skimage import measure import napari_clusters_plotter as ncp @@ -20,10 +21,7 @@ from napari_clusters_plotter._utilities import get_layer_tabular_data, get_nice_colormap -def test_plotting(make_napari_viewer): - viewer = make_napari_viewer() - widget_list = ncp.napari_experimental_provide_dock_widget() - +def get_labels_array(): label = np.array( [ [0, 0, 0, 0, 0, 0, 0], @@ -35,30 +33,29 @@ def test_plotting(make_napari_viewer): [0, 7, 7, 0, 0, 0, 0], ] ) + return label - # image = label * 1.5 + +def test_plotting(make_napari_viewer): + viewer = make_napari_viewer() + widget_list = ncp.napari_experimental_provide_dock_widget() + + label = get_labels_array() props = measure.regionprops_table( label, properties=(["label", "area", "perimeter"]) ) label_layer = viewer.add_labels(label, properties=props) - # image_layer = viewer.add_image(image) for widget in widget_list: _widget = widget(viewer) - # Doesn't work for now to use in tests because MeasureWidget uses cle function - # if isinstance(_widget, ncp._measure.MeasureWidget): - # _widget.run( - # image_layer, label_layer, "Measure now intensity shape", None, None - # ) if isinstance(_widget, ncp._plotter.PlotterWidget): plot_widget = _widget viewer.window.add_dock_widget(plot_widget) assert len(viewer.window._dock_widgets) == 1 - # assert len(viewer.window._dock_widgets) == 2 result = get_layer_tabular_data(label_layer) @@ -145,3 +142,33 @@ def test_plotter_utilities(): uc_plot_params = unclustered_plot_parameters(frame_ids, current_frame, n_datapoints) assert uc_plot_params == (result_au, result_su, result_cu) + + +def test_plotting_histogram(make_napari_viewer): + from napari_clusters_plotter._plotter import PlotterWidget + + viewer = make_napari_viewer() + + label = get_labels_array() + measurements = measure.regionprops_table( + label, properties=(["label", "area", "perimeter"]) + ) + label_layer = viewer.add_labels(label, properties=measurements) + label_layer.features = measurements + + viewer.window.add_dock_widget(PlotterWidget(viewer), area="right") + plotter_widget = PlotterWidget(viewer) + + plotter_widget.plotting_type.setCurrentText("HISTOGRAM_2D") + + viewer.window.add_dock_widget(PlotterWidget(viewer), area="right") + plotter_widget = PlotterWidget(viewer) + + plotter_widget.run( + features=pd.DataFrame(measurements), + plot_x_axis_name="area", + plot_y_axis_name="perimeter", + force_redraw=True, + ) + + assert plotter_widget.graphics_widget.axes.has_data() From 40d1063656617bf482dc56821bda1dedff150f31 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Laura=20=C5=BDigutyt=C4=97?= Date: Fri, 24 Mar 2023 14:32:29 +0100 Subject: [PATCH 2/3] add test for cluster image generation for clustering the histogram --- .../_tests/test_plotter.py | 27 ++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/napari_clusters_plotter/_tests/test_plotter.py b/napari_clusters_plotter/_tests/test_plotter.py index f5d438bb..4baac500 100644 --- a/napari_clusters_plotter/_tests/test_plotter.py +++ b/napari_clusters_plotter/_tests/test_plotter.py @@ -158,17 +158,42 @@ def test_plotting_histogram(make_napari_viewer): viewer.window.add_dock_widget(PlotterWidget(viewer), area="right") plotter_widget = PlotterWidget(viewer) - plotter_widget.plotting_type.setCurrentText("HISTOGRAM_2D") + plotter_widget.run( + features=pd.DataFrame(measurements), + plot_x_axis_name="area", + plot_y_axis_name="perimeter", + force_redraw=True, + ) + + assert plotter_widget.graphics_widget.axes.has_data() + + +def test_cluster_image_generation_for_histogram(make_napari_viewer): + from napari_clusters_plotter._plotter import PlotterWidget + + viewer = make_napari_viewer() + + label = get_labels_array() + measurements = measure.regionprops_table( + label, properties=(["label", "area", "perimeter"]) + ) + measurements["MANUAL_CLUSTER_ID"] = np.array([1, 0, 2, -1, 0, 1, 2]) + viewer.add_labels(label, properties=measurements) + viewer.window.add_dock_widget(PlotterWidget(viewer), area="right") plotter_widget = PlotterWidget(viewer) + plotter_widget.plotting_type.setCurrentText("HISTOGRAM_2D") + plotter_widget.log_scale.value = True plotter_widget.run( features=pd.DataFrame(measurements), plot_x_axis_name="area", plot_y_axis_name="perimeter", + plot_cluster_name="MANUAL_CLUSTER_ID", force_redraw=True, ) assert plotter_widget.graphics_widget.axes.has_data() + assert "cluster_ids_in_space" in viewer.layers From e3f0aca730fc7e45bbba9e1d7280f6fc91026bb5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Laura=20=C5=BDigutyt=C4=97?= Date: Wed, 29 Mar 2023 18:33:38 +0200 Subject: [PATCH 3/3] add assertion statement to check resulting nr of clusters in generated img --- napari_clusters_plotter/_tests/test_plotter.py | 1 + 1 file changed, 1 insertion(+) diff --git a/napari_clusters_plotter/_tests/test_plotter.py b/napari_clusters_plotter/_tests/test_plotter.py index 4baac500..c3e714b3 100644 --- a/napari_clusters_plotter/_tests/test_plotter.py +++ b/napari_clusters_plotter/_tests/test_plotter.py @@ -197,3 +197,4 @@ def test_cluster_image_generation_for_histogram(make_napari_viewer): assert plotter_widget.graphics_widget.axes.has_data() assert "cluster_ids_in_space" in viewer.layers + assert int(viewer.layers["cluster_ids_in_space"].data.max()) == 3