forked from haesleinhuepf/napari-skimage-regionprops
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_load_csv.py
35 lines (29 loc) · 1.18 KB
/
_load_csv.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import numpy as np
from napari_tools_menu import register_function
try:
import napari
except Exception as e:
import warnings
warnings.warn(str(e))
@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)