diff --git a/napari_skimage_regionprops/__init__.py b/napari_skimage_regionprops/__init__.py index 5173c48..f049527 100644 --- a/napari_skimage_regionprops/__init__.py +++ b/napari_skimage_regionprops/__init__.py @@ -1,15 +1,16 @@ from ._table import add_table, get_table, TableWidget from ._regionprops import regionprops, regionprops_table, regionprops_table_all_frames from ._parametric_images import visualize_measurement_on_labels +from napari_plugin_engine import napari_hook_implementation +from ._load_csv import load_csv + try: from ._version import version as __version__ except ImportError: __version__ = "0.4.1" -from napari_plugin_engine import napari_hook_implementation - @napari_hook_implementation def napari_experimental_provide_function(): - return [regionprops_table, visualize_measurement_on_labels] + return [regionprops_table, visualize_measurement_on_labels, load_csv] diff --git a/napari_skimage_regionprops/_load_csv.py b/napari_skimage_regionprops/_load_csv.py new file mode 100644 index 0000000..3247b79 --- /dev/null +++ b/napari_skimage_regionprops/_load_csv.py @@ -0,0 +1,29 @@ +import napari +import numpy as np +from napari_tools_menu import register_function + +@register_function(menu="Measurement > Load from CSV (nsr)") +def load_csv(csv_filename:"magicgui.types.PathLike", labels_layer:napari.layers.Labels, viewer:napari.Viewer=None): + """Save contents of a CSV file into a given layer's properties""" + import pandas as pd + # load region properties from csv file + reg_props = pd.read_csv(csv_filename) + try: + edited_reg_props = reg_props.drop(["Unnamed: 0"], axis=1) + except KeyError: + edited_reg_props = reg_props + + if "label" not in edited_reg_props.keys().tolist(): + label_column = pd.DataFrame( + {"label": np.array(range(1, (len(edited_reg_props) + 1)))} + ) + edited_reg_props = pd.concat([label_column, edited_reg_props], axis=1) + + if hasattr(labels_layer, "properties"): + labels_layer.properties = edited_reg_props + if hasattr(labels_layer, "features"): + labels_layer.features = edited_reg_props + + if viewer is not None: + from ._table import add_table + add_table(labels_layer, viewer) diff --git a/napari_skimage_regionprops/_tests/test_function.py b/napari_skimage_regionprops/_tests/test_function.py index 03ddb9b..985fbfa 100644 --- a/napari_skimage_regionprops/_tests/test_function.py +++ b/napari_skimage_regionprops/_tests/test_function.py @@ -81,6 +81,14 @@ def test_regionprops(make_napari_viewer): table_widget.append_content(fake_area) assert 'Double area' in table_widget.get_content().keys() + # save table to disk + import pandas as pd + pd.DataFrame(labels_layer.properties).to_csv("test.csv") + from napari_skimage_regionprops import load_csv + load_csv("test.csv", labels_layer) + load_csv("test.csv", labels_layer, viewer) + + # empty table table_widget.set_content(None) table_widget.update_content()