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..b131e38 --- /dev/null +++ b/napari_skimage_regionprops/_load_csv.py @@ -0,0 +1,28 @@ +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 + + from ._table import add_table + add_table(labels_layer, viewer)