Skip to content

Commit

Permalink
added function for loading CSV files into label layer properties
Browse files Browse the repository at this point in the history
  • Loading branch information
haesleinhuepf committed Apr 11, 2022
1 parent cfb2c11 commit 80ed208
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
7 changes: 4 additions & 3 deletions napari_skimage_regionprops/__init__.py
Original file line number Diff line number Diff line change
@@ -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]

28 changes: 28 additions & 0 deletions napari_skimage_regionprops/_load_csv.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 80ed208

Please sign in to comment.