55@email: anna.grim@alleninstitute.org
66
77
8- Routines for loading fragments and building a fragments_graph.
8+ Overview
9+ --------
10+ Code that reads and preprocesses neuron fragments stored as swc files, then
11+ constructs a custom graph object called a "FragmentsGraph".
912
13+ Graph Construction Algorithm:
14+ 1. Read Neuron Fragments
15+ to do...
16+
17+ 2. Preprocess Fragments and Extract Irreducibles
18+ to do...
19+
20+ 3. Build FragmentsGraph
21+ to do...
1022
1123Terminology
1224------------
3143from tqdm import tqdm
3244
3345from deep_neurographs import geometry
34- from deep_neurographs .utils import img_util , swc_util , util
35-
36- MIN_SIZE = 30
37- NODE_SPACING = 1
38- SMOOTH_BOOL = True
39- PRUNE_DEPTH = 20
46+ from deep_neurographs .utils import swc_util , util
4047
4148
4249class GraphLoader :
@@ -48,36 +55,35 @@ class GraphLoader:
4855 def __init__ (
4956 self ,
5057 anisotropy = [1.0 , 1.0 , 1.0 ],
51- min_size = MIN_SIZE ,
52- node_spacing = NODE_SPACING ,
58+ min_size = 30 ,
59+ node_spacing = 1 ,
5360 progress_bar = False ,
54- prune_depth = PRUNE_DEPTH ,
55- smooth_bool = SMOOTH_BOOL ,
61+ prune_depth = 20 ,
62+ smooth_bool = True ,
5663 ):
5764 """
5865 Builds a FragmentsGraph by reading swc files stored either on the
5966 cloud or local machine, then extracting the irreducible components.
6067
6168 Parameters
6269 ----------
63- anisotropy : list [float], optional
64- Scaling factors applied to xyz coordinates to account for the
65- anisotropy of microscope. The default is [1.0, 1.0, 1.0].
70+ anisotropy : List [float], optional
71+ Image to physical coordinates scaling factors to account for the
72+ anisotropy of the microscope. The default is [1.0, 1.0, 1.0].
6673 min_size : float, optional
6774 Minimum path length of swc files which are stored as connected
68- components in the FragmentsGraph. The default is 30ums .
75+ components in the FragmentsGraph. The default is 30.0 (microns) .
6976 node_spacing : int, optional
70- Spacing (in microns) between nodes. The default is the global
71- variable "NODE_SPACING".
77+ Spacing (in microns) between nodes. The default is 1.
7278 progress_bar : bool, optional
7379 Indication of whether to print out a progress bar while building
7480 graph. The default is True.
7581 prune_depth : int, optional
7682 Branches less than "prune_depth" microns are pruned if "prune" is
77- True. The default is the global variable "PRUNE_DEPTH" .
83+ True. The default is 20.0 (microns) .
7884 smooth_bool : bool, optional
7985 Indication of whether to smooth branches from swc files. The
80- default is the global variable "SMOOTH" .
86+ default is True .
8187
8288 Returns
8389 -------
@@ -90,12 +96,9 @@ def __init__(
9096 self .progress_bar = progress_bar
9197 self .prune_depth = prune_depth
9298 self .smooth_bool = smooth_bool
93-
9499 self .reader = swc_util .Reader (anisotropy , min_size )
95100
96- def run (
97- self , fragments_pointer , img_patch_origin = None , img_patch_shape = None
98- ):
101+ def run (self , fragments_pointer ):
99102 """
100103 Builds a FragmentsGraph by reading swc files stored either on the
101104 cloud or local machine, then extracting the irreducible components.
@@ -105,12 +108,6 @@ def run(
105108 fragments_pointer : dict, list, str
106109 Pointer to swc files used to build an instance of FragmentsGraph,
107110 see "swc_util.Reader" for further documentation.
108- img_patch_origin : list[int], optional
109- An xyz coordinate which is the upper, left, front corner of the
110- image patch that contains the swc files. The default is None.
111- img_patch_shape : list[int], optional
112- Shape of the image patch which contains the swc files. The default
113- is None.
114111
115112 Returns
116113 -------
@@ -120,12 +117,13 @@ def run(
120117 """
121118 from deep_neurographs .fragments_graph import FragmentsGraph
122119
123- # Load fragments and extract irreducibles
124- self .img_bbox = img_util .init_bbox (img_patch_origin , img_patch_shape )
120+ # Step 1: Read Neuron Fragments
125121 swc_dicts = self .reader .load (fragments_pointer )
122+
123+ # Step: Preprocess Fragments and Extract Irreducibles
126124 irreducibles = self .schedule_processes (swc_dicts )
127125
128- # Build FragmentsGraph
126+ # Step 3: Build FragmentsGraph
129127 fragments_graph = FragmentsGraph (node_spacing = self .node_spacing )
130128 while len (irreducibles ):
131129 irreducible_set = irreducibles .pop ()
@@ -186,15 +184,14 @@ def get_irreducibles(self, swc_dict):
186184
187185 Returns
188186 -------
189- list
187+ List[dict]
190188 List of dictionaries such that each is the set of irreducibles in
191189 a connected component of the graph corresponding to "swc_dict".
192190
193191 """
194192 # Build dense graph
195193 swc_dict ["idx" ] = dict (zip (swc_dict ["id" ], range (len (swc_dict ["id" ]))))
196194 graph , _ = swc_util .to_graph (swc_dict , set_attrs = True )
197- self .clip_branches (graph , swc_dict ["swc_id" ])
198195 self .prune_branches (graph )
199196
200197 # Extract irreducibles
@@ -210,28 +207,6 @@ def get_irreducibles(self, swc_dict):
210207 irreducibles .append (result )
211208 return irreducibles
212209
213- def clip_branches (self , graph , swc_id ):
214- """
215- Deletes all nodes from "graph" that are not contained in "img_bbox".
216-
217- Parameters
218- ----------
219- graph : networkx.Graph
220- Graph to be searched
221-
222- Returns
223- -------
224- None
225-
226- """
227- if self .img_bbox :
228- delete_nodes = set ()
229- for i in graph .nodes :
230- xyz = img_util .to_voxels (graph .nodes [i ]["xyz" ], self .to_anisotropy )
231- if not util .is_contained (self .img_bbox , xyz ):
232- delete_nodes .add (i )
233- graph .remove_nodes_from (delete_nodes )
234-
235210 def prune_branches (self , graph ):
236211 """
237212 Prunes all short branches from "graph". A short branch is a path
@@ -316,7 +291,6 @@ def get_component_irreducibles(self, graph, swc_dict):
316291 # Visit j
317292 attrs = upd_edge_attrs (swc_dict , attrs , j )
318293 if j in leafs or j in branchings :
319- # Check whether to smooth
320294 attrs ["length" ] = branch_length
321295 attrs = to_numpy (attrs )
322296 if self .smooth_bool :
0 commit comments