1
1
from typing import Dict , Union , List , Tuple
2
2
3
+ from collections import defaultdict
3
4
import copy
4
5
5
6
import numpy as np
@@ -168,6 +169,9 @@ def cross_sectional_area_helper(skel, binimg, roi):
168
169
areas = np .zeros ([all_verts .shape [0 ]], dtype = np .float32 )
169
170
contacts = np .zeros ([all_verts .shape [0 ]], dtype = np .uint8 )
170
171
172
+ branch_pts = set (skel .branches ())
173
+ branch_pt_vals = defaultdict (list )
174
+
171
175
paths = skel .paths ()
172
176
173
177
normal = np .array ([1 ,0 ,0 ], dtype = np .float32 )
@@ -190,19 +194,25 @@ def cross_sectional_area_helper(skel, binimg, roi):
190
194
normal = normals [i ,:]
191
195
normal /= np .linalg .norm (normal )
192
196
193
- for i , vert in enumerate (path ):
197
+ for i , vert in tqdm ( enumerate (path ), total = path . shape [ 0 ] ):
194
198
if np .any (vert < 0 ) or np .any (vert > shape ):
195
199
continue
196
200
197
201
idx = mapping [tuple (vert )]
198
202
normal = normals [i ]
199
203
200
- if areas [idx ] == 0 or (repair_contacts and contacts [idx ] > 0 ):
201
- areas [idx ], contacts [ idx ] = xs3d .cross_sectional_area (
204
+ if areas [idx ] == 0 or idx in branch_pts or (repair_contacts and contacts [idx ] > 0 ):
205
+ areas [idx ], contact = xs3d .cross_sectional_area (
202
206
binimg , vert ,
203
207
normal , anisotropy ,
204
208
return_contact = True ,
205
209
)
210
+ if repair_contacts :
211
+ contacts [idx ] = contact
212
+ else :
213
+ contacts [idx ] |= contact # accumulate for branch points
214
+ if idx in branch_pts :
215
+ branch_pt_vals [idx ].append (areas [idx ])
206
216
if visualize_section_planes :
207
217
img = xs3d .cross_section (
208
218
binimg , vert ,
@@ -214,6 +224,9 @@ def cross_sectional_area_helper(skel, binimg, roi):
214
224
import microviewer
215
225
microviewer .view (cross_sections , seg = True )
216
226
227
+ for idx , vals in branch_pt_vals .items ():
228
+ areas [idx ] = sum (vals ) / len (vals )
229
+
217
230
add_property (skel , prop )
218
231
219
232
skel .cross_sectional_area = areas
0 commit comments