Skip to content

Commit

Permalink
Enhance point addition functionality to support parent-child relation…
Browse files Browse the repository at this point in the history
…ships and update shape layer visualization
  • Loading branch information
ClementCaporal committed Dec 19, 2024
1 parent b7fdabe commit 2e16985
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
23 changes: 22 additions & 1 deletion src/napari_swc_editor/bindings.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from .swc_io import (
add_edge,
add_points,
create_line_data_from_swc_data,
get_treenode_id_from_index,
move_points,
parse_data_from_swc_file,
Expand Down Expand Up @@ -57,6 +58,7 @@ def add_napari_layers_from_swc_content(
"metadata": {
"raw_swc": file_content,
"shape_layer": shape_layer,
"Ctrl_activated": False,
},
}

Expand Down Expand Up @@ -107,14 +109,33 @@ def event_add_points(event):
new_pos = event.source.data[list(event.data_indices)]
new_radius = event.source.size[list(event.data_indices)]
new_structure = event.source.symbol[list(event.data_indices)]
new_parents = -1

# if shift is activated, the add the new edges from previous selected point
if (
event.source.metadata["Ctrl_activated"]
and len(event.source.selected_data) > 0
):

previous_selected = list(event.source.selected_data)[-1]
new_parents = get_treenode_id_from_index([previous_selected], df)[
0
]

new_swc, df = add_points(
raw_swc, new_pos, new_radius, new_structure, df
raw_swc, new_pos, new_radius, new_structure, new_parents, df
)

event.source.metadata["raw_swc"] = new_swc
event.source.metadata["swc_data"] = df

if new_parents != -1:
new_lines, new_r = create_line_data_from_swc_data(df)
event.source.metadata["shape_layer"].data = []
event.source.metadata["shape_layer"].add_lines(
new_lines, edge_width=new_r
)


def event_move_points(event):

Expand Down
16 changes: 12 additions & 4 deletions src/napari_swc_editor/swc_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,12 @@ def write_swc_content(df, swc_content=None):


def add_points(
swc_content, new_positions, new_radius, new_structure=0, swc_df=None
swc_content,
new_positions,
new_radius,
structure_id=0,
parent_treenode_id=-1,
swc_df=None,
):
"""Add a point to the swc content
Expand All @@ -257,9 +262,12 @@ def add_points(
New positions to be added in napari order (z, y, x)
new_radius : np.ndarray
Radius of the new positions
new_structure : np.ndarray
structure_id : np.ndarray
Structure of the new positions, see SWC_SYMBOL
Default is 0 (undefined)
parent_treenode_id : np.ndarray
Parent of the new positions
Default is -1 (no parent)
swc_df : pd.DataFrame
Dataframe extracted from the swc file. Should have the following columns:
- x: x coordinate of the node
Expand All @@ -283,8 +291,8 @@ def add_points(
# change napari position order to swc order
new_points = pd.DataFrame(new_positions, columns=["z", "y", "x"])
new_points["r"] = new_radius
new_points["structure_id"] = new_structure
new_points["parent_treenode_id"] = -1
new_points["structure_id"] = structure_id
new_points["parent_treenode_id"] = parent_treenode_id

# order columns to respect swc format
new_points = new_points[
Expand Down

0 comments on commit 2e16985

Please sign in to comment.