Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing Issue #84 #85

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
2 changes: 1 addition & 1 deletion .github/workflows/test_and_deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
runs-on: ${{ matrix.platform }}
strategy:
matrix:
platform: [ubuntu-latest, windows-latest, macos-latest]
platform: [ubuntu-20.04, windows-latest] #, macos-latest]
python-version: ['3.8', '3.9', '3.10']

steps:
Expand Down
23 changes: 20 additions & 3 deletions napari_skimage_regionprops/_load_csv.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import numpy as np
from napari_tools_menu import register_function
from napari.utils.notifications import show_warning

try:
import napari
Expand All @@ -9,7 +10,7 @@


@register_function(menu="Measurement > Load from CSV (nsr)")
def load_csv(csv_filename:"magicgui.types.PathLike", labels_layer: "napari.layers.Labels", show_table: bool = True, viewer: "napari.Viewer" = None):
def load_csv(csv_filename:"magicgui.types.PathLike", labels_layer: "napari.layers.Layer", show_table: bool = True, 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
Expand All @@ -24,9 +25,25 @@ def load_csv(csv_filename:"magicgui.types.PathLike", labels_layer: "napari.layer
{"label": np.array(range(1, (len(edited_reg_props) + 1)))}
)
edited_reg_props = pd.concat([label_column, edited_reg_props], axis=1)

# Add 'properties' and 'features' attributes if layer is Surface
if isinstance(labels_layer, napari.layers.Surface):
# Check if features and properties are already defined
if not hasattr(labels_layer, "features"):
labels_layer.features = {}
if not hasattr(labels_layer, "properties"):
labels_layer.properties = {}
elif isinstance(labels_layer, napari.layers.Points):
# Table shape must match number of points
if edited_reg_props.shape[0] < labels_layer.data.shape[0]:
# fill in missing rows with NaNs
show_warning("Number of rows in CSV file is less than number of points in the layer. Filling in missing rows with NaNs.")
edited_reg_props = pd.concat([edited_reg_props, pd.DataFrame(np.nan, index=range(labels_layer.data.shape[0] - edited_reg_props.shape[0]), columns=edited_reg_props.columns)]).reset_index(drop=True)
elif edited_reg_props.shape[0] > labels_layer.data.shape[0]:
# truncate if necessary
show_warning("Number of rows in CSV file is greater than number of points in the layer. Truncating to match number of points.")
edited_reg_props = edited_reg_props.iloc[:labels_layer.data.shape[0],:]
if hasattr(labels_layer, "properties"):
labels_layer.properties = edited_reg_props
labels_layer.properties = edited_reg_props.to_dict(orient="list")
if hasattr(labels_layer, "features"):
labels_layer.features = edited_reg_props

Expand Down
9 changes: 9 additions & 0 deletions napari_skimage_regionprops/_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,17 @@ def __init__(self, layer: "napari.layers.Layer", viewer: "napari.Viewer" = None)

self._view = QTableWidget()
self._view.setEditTriggers(QTableWidget.EditTrigger.NoEditTriggers)
# Add 'properties' and 'features' attributes if layer is Surface
if isinstance(layer, napari.layers.Surface):
# Check if features and properties are already defined
if not hasattr(layer, "features"):
layer.features = {}
if not hasattr(layer, "properties"):
Cryaaa marked this conversation as resolved.
Show resolved Hide resolved
layer.properties = {}
if hasattr(layer, "properties"):
self.set_content(layer.properties)
elif hasattr(layer, "features"):
self.set_content(layer.features.to_dict(orient="list"))
else:
self.set_content({})

Expand Down
47 changes: 47 additions & 0 deletions napari_skimage_regionprops/_tests/test_function.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,40 @@
# from napari_skimage_regionprops import threshold, image_arithmetic
def generate_sphere_mesh(radius, segments):
"""
Generate vertices and faces for a sphere mesh.

Parameters:
radius (float): Radius of the sphere.
segments (int): Number of segments used to generate the sphere, higher means finer mesh.

Returns:
tuple: vertices (N, 3 array), faces (M, 3 array)
"""
import numpy as np
theta = np.linspace(0, np.pi, segments)
phi = np.linspace(0, 2 * np.pi, segments)
theta, phi = np.meshgrid(theta, phi)

x = radius * np.sin(theta) * np.cos(phi)
y = radius * np.sin(theta) * np.sin(phi)
z = radius * np.cos(theta)

vertices = np.column_stack((x.ravel(), y.ravel(), z.ravel()))

# Generate faces (indices of vertices that make up each triangle)
faces = []
for i in range(len(theta) - 1):
for j in range(len(phi) - 1):
v0 = i * len(phi) + j
v1 = v0 + 1
v2 = v0 + len(phi)
v3 = v2 + 1
faces.append([v0, v1, v2])
faces.append([v1, v3, v2])

faces = np.array(faces)

return vertices, faces

# add your tests here...
def test_regionprops(make_napari_viewer):
Expand Down Expand Up @@ -90,6 +126,17 @@ def test_regionprops(make_napari_viewer):
load_csv("test.csv", labels_layer)
load_csv("test.csv", labels_layer, viewer)

points_layer = viewer.add_points(np.array([[2, 1], [2, 5], [5, 3], [6, 6]]))
vertices, faces = generate_sphere_mesh(1, 50)
surface_layer = viewer.add_surface((vertices, faces))

# test loading csv to points
load_csv("test.csv", points_layer)
load_csv("test.csv", points_layer, viewer)

# test loading csv to surface
load_csv("test.csv", surface_layer)
load_csv("test.csv", surface_layer, viewer)

# empty table
table_widget.set_content(None)
Expand Down
Loading