diff --git a/napari_clusters_plotter/_tests/test_plotter.py b/napari_clusters_plotter/_tests/test_plotter.py index 129b783d..c3e714b3 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 + + +def test_plotting(make_napari_viewer): + viewer = make_napari_viewer() + widget_list = ncp.napari_experimental_provide_dock_widget() - # image = label * 1.5 + 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,59 @@ 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") + + 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 + assert int(viewer.layers["cluster_ids_in_space"].data.max()) == 3