Skip to content

Commit

Permalink
Merge pull request haesleinhuepf#21 from haesleinhuepf/load_csv
Browse files Browse the repository at this point in the history
added function for loading CSV files into label layer properties
  • Loading branch information
haesleinhuepf authored Apr 18, 2022
2 parents cfb2c11 + 9b1ba5d commit a8feed1
Show file tree
Hide file tree
Showing 3 changed files with 41 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]

29 changes: 29 additions & 0 deletions napari_skimage_regionprops/_load_csv.py
Original file line number Diff line number Diff line change
@@ -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)
8 changes: 8 additions & 0 deletions napari_skimage_regionprops/_tests/test_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit a8feed1

Please sign in to comment.