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

add example in command line #5

Merged
merged 4 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
167 changes: 167 additions & 0 deletions examples/example_io.ipynb

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions src/napari_swc_editor/_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def reader_function(path):
with open(_path) as f:
file_content = f.read()

nodes, radius, lines, structure = parse_data_from_swc_file(
points, radius, lines, structure = parse_data_from_swc_file(
file_content
)

Expand All @@ -87,7 +87,7 @@ def reader_function(path):
},
}

point_layer = viewer.add_points(nodes, **add_kwargs_points)
point_layer = viewer.add_points(points, **add_kwargs_points)

bind_layers_with_events(point_layer, shape_layer)

Expand Down
3 changes: 2 additions & 1 deletion src/napari_swc_editor/_tests/test_reader.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import numpy as np
from napari_swc_reader import napari_get_reader

from napari_swc_editor import napari_get_reader


# tmp_path is a pytest fixture
Expand Down
16 changes: 8 additions & 8 deletions src/napari_swc_editor/bindings.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
remove_points,
sort_edge_indices,
symbol_to_structure_id,
update_node_properties,
update_point_properties,
)


Expand All @@ -33,8 +33,8 @@ def bind_layers_with_events(point_layer, shape_layer):
point_layer.events.data.connect(event_add_points)
point_layer.events.data.connect(event_move_points)
point_layer.events.data.connect(event_remove_points)
point_layer.events.size.connect(event_update_node_properties)
point_layer.events.symbol.connect(event_update_node_properties)
point_layer.events.size.connect(event_update_point_properties)
point_layer.events.symbol.connect(event_update_point_properties)

point_layer.bind_key("l")(event_add_edge)
point_layer.bind_key("Shift-l")(event_add_edge_wo_sort)
Expand Down Expand Up @@ -94,9 +94,9 @@ def event_remove_points(event):
event.source.metadata["swc_data"] = df


def event_update_node_properties(event):
def event_update_point_properties(event):
"""Update the properties (`size` -> `radius` and `symbol` -> `structure_id`)
of a point to the swc content node
of a point to the swc content point

Parameters
----------
Expand All @@ -119,7 +119,7 @@ def event_update_node_properties(event):
"structure_id": structure_id,
}

new_swc, new_lines, new_r, df = update_node_properties(
new_swc, new_lines, new_r, df = update_point_properties(
raw_swc,
indices,
properties,
Expand Down Expand Up @@ -155,7 +155,7 @@ def event_add_edge(layer, sort=True):
layer : napari.layers.Points
Points layer
sort : bool, optional
If True, the indices will be sorted so soma are linked to other nodes,
If True, the indices will be sorted so soma are linked to other points,
by default True
"""

Expand Down Expand Up @@ -184,7 +184,7 @@ def event_remove_edge(layer, sort=True):
layer : napari.layers.Points
Points layer
sort : bool, optional
If True, the indices will be sorted so soma are linked to other nodes,
If True, the indices will be sorted so soma are linked to other points,
by default True
"""

Expand Down
62 changes: 31 additions & 31 deletions src/napari_swc_editor/swc_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,22 +113,22 @@ def parse_data_from_swc_file(file_content):

Returns
-------
nodes : np.ndarray
All positions of the nodes
points : np.ndarray
All positions of the points
radius : np.ndarray
Radius of the nodes
Radius of the points
lines : np.ndarray
All lines connecting the nodes
All lines connecting the points
structure : np.ndarray
Structure of the nodes
Structure of the points
"""

df = parse_swc_content(file_content)

nodes, radius, structure = create_point_data_from_swc_data(df)
points, radius, structure = create_point_data_from_swc_data(df)
lines, _ = create_line_data_from_swc_data(df)

return nodes, radius, lines, structure
return points, radius, lines, structure


def create_point_data_from_swc_data(df):
Expand All @@ -146,22 +146,22 @@ def create_point_data_from_swc_data(df):

Returns
-------
nodes : np.ndarray
All positions of the nodes
points : np.ndarray
All positions of the points
radius : np.ndarray
Radius of the nodes
Radius of the points
structure : np.ndarray
Structure of the nodes
Structure of the points
"""

radius = df["r"].values

structure = df["structure_id"].values

# for each node create a point
nodes = df[["x", "y", "z"]].values
points = df[["x", "y", "z"]].values

return nodes, radius, structure
return points, radius, structure


def create_line_data_from_swc_data(df):
Expand All @@ -180,25 +180,25 @@ def create_line_data_from_swc_data(df):
Returns
-------
lines : np.ndarray
All lines connecting the nodes
All lines connecting the points
radius : np.ndarray
Radius of the lines
"""

# for each node create a point
nodes = df[["x", "y", "z"]].values
# for each nodes create a point
points = df[["x", "y", "z"]].values

# for each edge create a line
edges = df["parent_treenode_id"].values

# remove all soma nodes
nodes = nodes[edges != -1]
points = points[edges != -1]
edges = edges[edges != -1]

# for each id in edges, get the corresponding node according to its index
prev_node = df.loc[edges, ["x", "y", "z"]].values
prev_point = df.loc[edges, ["x", "y", "z"]].values

lines = np.array([nodes, prev_node])
lines = np.array([points, prev_point])
lines = np.moveaxis(lines, 0, 1)

radius = df.loc[edges, "r"].values
Expand Down Expand Up @@ -280,21 +280,21 @@ def add_points(
if swc_df is None:
swc_df = parse_swc_content(swc_content)

new_nodes = pd.DataFrame(new_positions, columns=["x", "y", "z"])
new_nodes["r"] = new_radius
new_nodes["structure_id"] = new_structure
new_nodes["parent_treenode_id"] = -1
new_points = pd.DataFrame(new_positions, columns=["x", "y", "z"])
new_points["r"] = new_radius
new_points["structure_id"] = new_structure
new_points["parent_treenode_id"] = -1

if swc_df.size > 0:
previous_max = swc_df.index.max()
max_index = np.array(
previous_max + np.arange(1, len(new_nodes) + 1)
previous_max + np.arange(1, len(new_points) + 1)
).astype(int)
new_nodes.index = max_index
new_points.index = max_index

new_df = pd.concat([swc_df, new_nodes])
new_df = pd.concat([swc_df, new_points])
else:
new_df = new_nodes
new_df = new_points

new_df.index.name = "treenode_id"
new_swc_content = write_swc_content(new_df, swc_content)
Expand Down Expand Up @@ -325,7 +325,7 @@ def move_points(swc_content, index, new_positions, swc_df=None):
new_swc_content : swc_content
New content of the swc file
moved_lines : np.ndarray
New lines connecting the nodes
New lines connecting the points
swc_df : pd.DataFrame
Dataframe extracted from the new swc file
"""
Expand Down Expand Up @@ -363,7 +363,7 @@ def remove_points(swc_content, indices, swc_df=None):
new_swc_content : swc_content
New content of the sw
moved_lines : np.ndarray
New lines connecting the nodes
New lines connecting the points
new_r : np.ndarray
New radius of the lines
swc_df : pd.DataFrame
Expand Down Expand Up @@ -472,7 +472,7 @@ def remove_edge(swc_content, indices, swc_df=None):
new_swc_content : str
New content of the swc file
new_lines : np.ndarray
New lines connecting the nodes
New lines connecting the points
new_r : np.ndarray
New radius of the lines
swc_df : pd.DataFrame
Expand Down Expand Up @@ -565,7 +565,7 @@ def sort_edge_indices(swc_content, indices, swc_df=None):
return sorted_indices


def update_node_properties(swc_content, indices, new_properties, swc_df=None):
def update_point_properties(swc_content, indices, new_properties, swc_df=None):
"""Update the properties of the nodes

Parameters
Expand Down
Loading