Skip to content

Commit a17e83d

Browse files
anna-grimanna-grim
andauthored
Feat zarr reader (#294)
* feat: zarr reader * doc: improved graph_util documentation --------- Co-authored-by: anna-grim <anna.grim@alleninstitute.org>
1 parent 6b43609 commit a17e83d

File tree

2 files changed

+52
-41
lines changed

2 files changed

+52
-41
lines changed

src/deep_neurographs/utils/graph_util.py

Lines changed: 50 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,24 @@
1212
1313
Graph Construction Algorithm:
1414
1. Load Neuron Fragments
15-
to do...
15+
Reads SWC files and stores the contents as a dictionary with the
16+
keys: "id", "xyz", "radius", "pid", and "swc_id". Each SWC file is
17+
assumed to contain unformly spaced points, each are separated by 1
18+
voxel.
1619
1720
2. Extract Irreducibles
18-
to do...
21+
Finds the components of the irreducible subgraph from each SWC
22+
file. The irreducible components of a graph are the following:
23+
(1) Leafs: Nodes of degree 1
24+
(2) Branchings: Nodes of degree 3+
25+
(3) Paths between irreducible nodes
1926
2027
3. Build FragmentsGraph
2128
to do...
2229
30+
Note: We use the term "branch" to refer to a path in a graph from a branching
31+
node to a leaf.
32+
2333
"""
2434

2535
from concurrent.futures import ProcessPoolExecutor, as_completed
@@ -35,7 +45,8 @@
3545

3646
class GraphLoader:
3747
"""
38-
Class that is used to build an instance of FragmentsGraph.
48+
Class that loads SWC files and constructs a FragmentsGraph instance from
49+
the data.
3950
4051
"""
4152

@@ -49,7 +60,7 @@ def __init__(
4960
verbose=False,
5061
):
5162
"""
52-
Builds a FragmentsGraph by reading swc files stored either on the
63+
Builds a FragmentsGraph by reading swc files stored on either the
5364
cloud or local machine, then extracting the irreducible components.
5465
5566
Parameters
@@ -58,49 +69,52 @@ def __init__(
5869
Image to physical coordinates scaling factors to account for the
5970
anisotropy of the microscope. The default is [1.0, 1.0, 1.0].
6071
min_size : float, optional
61-
Minimum path length of swc files which are stored as connected
62-
components in the FragmentsGraph. The default is 30.0 (microns).
72+
Minimum path length of swc files that are loaded into the
73+
FragmentsGraph. The default is 30.0 (microns).
6374
node_spacing : int, optional
64-
Spacing (in microns) between nodes. The default is 1.
75+
Sampling rate for nodes in FragmentsGraph. Every "node_spacing"
76+
node is retained.
6577
prune_depth : int, optional
66-
Branches less than "prune_depth" microns are pruned if "prune" is
67-
True. The default is 20.0 (microns).
78+
Branches with length less than "prune_depth" microns are pruned.
79+
The default is 20.0 microns.
6880
smooth_bool : bool, optional
69-
Indication of whether to smooth branches from swc files. The
70-
default is True.
81+
Indication of whether to smooth xyz coordinates from SWC files.
82+
The default is True.
7183
verbose : bool, optional
72-
Indication of whether to print out a progress bar while building
73-
graph. The default is True.
84+
Indication of whether to display a progress bar while building
85+
FragmentsGraph. The default is True.
7486
7587
Returns
7688
-------
7789
None
7890
7991
"""
80-
self.anisotropy = anisotropy
92+
# Class attributes
8193
self.min_size = min_size
8294
self.node_spacing = node_spacing
8395
self.prune_depth = prune_depth
8496
self.smooth_bool = smooth_bool
8597
self.verbose = verbose
8698

99+
# SWC Reader
87100
self.reader = swc_util.Reader(anisotropy, min_size)
88101

89102
def run(self, fragments_pointer):
90103
"""
91104
Builds a FragmentsGraph by reading swc files stored either on the
92-
cloud or local machine, then extracting the irreducible components.
105+
cloud or local machine, then extracting the irreducible components
106+
from each SWC file.
93107
94108
Parameters
95109
----------
96110
fragments_pointer : dict, list, str
97-
Pointer to swc files used to build an instance of FragmentsGraph,
111+
Pointer to SWC files used to build an instance of FragmentsGraph,
98112
see "swc_util.Reader" for further documentation.
99113
100114
Returns
101115
-------
102116
FragmentsGraph
103-
Graph generated from swc files.
117+
Graph generated from SWC files.
104118
105119
"""
106120
from deep_neurographs.fragments_graph import FragmentsGraph
@@ -120,19 +134,19 @@ def run(self, fragments_pointer):
120134
def get_irreducibles(self, swc_dicts):
121135
"""
122136
Processes a list of swc dictionaries in parallel and extracts the
123-
components of the irreducible subgraph from each swc file.
137+
components of the irreducible subgraph from each.
124138
125139
Parameters
126140
----------
127141
swc_dicts : List[dict]
128142
List of dictionaries such that each contains the contents of an
129-
swc file.
143+
SWC file.
130144
131145
Returns
132146
-------
133147
List[dict]
134148
List of dictionaries such that each contains the components of the
135-
irreducible subgraph extracted from an swc file.
149+
irreducible subgraph extracted from each SWC dictionary.
136150
137151
"""
138152
# Initializations
@@ -163,16 +177,13 @@ def get_irreducibles(self, swc_dicts):
163177

164178
def extract_irreducibles(self, swc_dict):
165179
"""
166-
Gets the components of the irreducible subgraph from a given swc file.
167-
Note that the irreducible components consist of the following:
168-
(1) Leafs: Nodes of degree 1
169-
(2) Branchings: Nodes of degree 3+
170-
(3) Edges that contain the path between irreducible nodes
180+
Gets the components of the irreducible subgraph from a given SWC
181+
dictionary.
171182
172183
Parameters
173184
----------
174185
swc_dict : dict
175-
Contents of an swc file.
186+
Contents of an SWC file.
176187
177188
Returns
178189
-------
@@ -221,8 +232,7 @@ def extract_irreducibles(self, swc_dict):
221232

222233
def prune_branches(self, graph):
223234
"""
224-
Prunes short branches from the graph, defined as paths between a leaf
225-
and a branching node with a length less than self.prune_depth microns.
235+
Prunes branches with length less than "self.prune_depth" microns.
226236
227237
Parameters
228238
----------
@@ -262,7 +272,7 @@ def prune_branches(self, graph):
262272
break
263273

264274

265-
# --- Irreducible Extraction Utils ---
275+
# --- Irreducibles Extraction ---
266276
def get_irreducible_nodes(graph):
267277
"""
268278
Gets irreducible nodes (i.e. leafs and branchings) of a graph.
@@ -345,7 +355,7 @@ def upd_edge_attrs(graph, attrs, i, j):
345355
Parameters
346356
----------
347357
swc_dict : dict
348-
Contents of an swc file.
358+
Contents of an SWC file.
349359
attrs : dict
350360
Edge attribute dictionary to be updated.
351361
i : int
@@ -395,9 +405,8 @@ def to_numpy(attrs):
395405
# --- Miscellaneous ---
396406
def smooth_branch(graph, attrs, i, j):
397407
"""
398-
Smoothes a branch then updates "swc_dict" and "edges" with the new xyz
399-
coordinates of the branch end points. Note that this branch is an edge
400-
in the irreducible graph being built.
408+
Smoothes branch then updates "graph" and "attrs" with the new xyz
409+
coordinates.
401410
402411
Parameters
403412
----------
@@ -458,15 +467,15 @@ def compute_dist(graph, i, j):
458467
Returns
459468
-------
460469
float
461-
Euclidean distance between i and j.
470+
Euclidean distance between nodes i and j.
462471
463472
"""
464473
return geometry.dist(graph.nodes[i]["xyz"], graph.nodes[j]["xyz"])
465474

466475

467476
def cycle_exists(graph):
468477
"""
469-
Checks whether a cycle exists in "graph".
478+
Checks the given graph has a cycle.
470479
471480
Paramaters
472481
----------
@@ -476,7 +485,7 @@ def cycle_exists(graph):
476485
Returns
477486
-------
478487
bool
479-
Indication of whether there exists a cycle.
488+
Indication of whether graph has a cycle.
480489
481490
"""
482491
try:
@@ -497,8 +506,8 @@ def get_leafs(graph):
497506
498507
Returns
499508
-------
500-
list
501-
Leaf nodes "graph".
509+
List[int]
510+
Leaf nodes of "graph".
502511
503512
"""
504513
return [i for i in graph.nodes if graph.degree[i] == 1]
@@ -536,7 +545,7 @@ def get_component(graph, root):
536545
537546
Returns
538547
-------
539-
set[int]
548+
Set[int]
540549
Set of nodes in the connected component corresponding to "root".
541550
542551
"""
@@ -560,7 +569,7 @@ def count_components(graph):
560569
Graph to be searched.
561570
562571
Returns
563-
-------'
572+
-------
564573
int
565574
Number of connected components.
566575
@@ -581,7 +590,7 @@ def largest_components(graph, k):
581590
582591
Returns
583592
-------
584-
list
593+
List[int]
585594
List where each entry is a random node from one of the k largest
586595
connected components.
587596

src/deep_neurographs/utils/img_util.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
from deep_neurographs.utils import util
2121

2222

23+
### PERMUTE COORDINATE ORDER IS TURNED OFF
24+
2325
class ImageReader(ABC):
2426
"""
2527
Abstract class to create image readers classes.

0 commit comments

Comments
 (0)