|
1 |
| -from typing import Dict, Union, List |
| 1 | +from typing import Dict, Union, List, Tuple |
| 2 | + |
| 3 | +import copy |
2 | 4 |
|
3 | 5 | import numpy as np
|
4 | 6 | import scipy.ndimage
|
|
8 | 10 | import kimimaro.skeletontricks
|
9 | 11 |
|
10 | 12 | import cc3d
|
| 13 | +import dijkstra3d |
11 | 14 | import fastremap
|
12 | 15 | import fill_voids
|
13 | 16 | import xs3d
|
@@ -50,6 +53,54 @@ def add_property(skel, prop):
|
50 | 53 | if needs_prop:
|
51 | 54 | skel.extra_attributes.append(prop)
|
52 | 55 |
|
| 56 | +def shape_iterator(all_labels, skeletons, fill_holes, in_place, progress, fn): |
| 57 | + iterator = skeletons |
| 58 | + if type(skeletons) == dict: |
| 59 | + iterator = skeletons.values() |
| 60 | + total = len(skeletons) |
| 61 | + elif type(skeletons) == Skeleton: |
| 62 | + iterator = [ skeletons ] |
| 63 | + total = 1 |
| 64 | + else: |
| 65 | + total = len(skeletons) |
| 66 | + |
| 67 | + if all_labels.dtype == bool: |
| 68 | + remapping = { True: 1, False: 0, 1:1, 0:0 } |
| 69 | + else: |
| 70 | + all_labels, remapping = fastremap.renumber(all_labels, in_place=in_place) |
| 71 | + |
| 72 | + all_slices = find_objects(all_labels) |
| 73 | + |
| 74 | + for skel in tqdm(iterator, desc="Labels", disable=(not progress), total=total): |
| 75 | + if all_labels.dtype == bool: |
| 76 | + label = 1 |
| 77 | + else: |
| 78 | + label = skel.id |
| 79 | + |
| 80 | + if label == 0: |
| 81 | + continue |
| 82 | + |
| 83 | + label = remapping[label] |
| 84 | + slices = all_slices[label - 1] |
| 85 | + if slices is None: |
| 86 | + continue |
| 87 | + |
| 88 | + roi = Bbox.from_slices(slices) |
| 89 | + if roi.volume() <= 1: |
| 90 | + continue |
| 91 | + |
| 92 | + roi.grow(1) |
| 93 | + roi.minpt = Vec.clamp(roi.minpt, Vec(0,0,0), roi.maxpt) |
| 94 | + slices = roi.to_slices() |
| 95 | + |
| 96 | + binimg = np.asfortranarray(all_labels[slices] == label) |
| 97 | + if fill_holes: |
| 98 | + binimg = fill_voids.fill(binimg, in_place=True) |
| 99 | + |
| 100 | + fn(skel, binimg, roi) |
| 101 | + |
| 102 | + return iterator |
| 103 | + |
53 | 104 | def cross_sectional_area(
|
54 | 105 | all_labels:np.ndarray,
|
55 | 106 | skeletons:Union[Dict[int,Skeleton],List[Skeleton],Skeleton],
|
@@ -100,54 +151,11 @@ def cross_sectional_area(
|
100 | 151 | "num_components": 1,
|
101 | 152 | }
|
102 | 153 |
|
103 |
| - iterator = skeletons |
104 |
| - if type(skeletons) == dict: |
105 |
| - iterator = skeletons.values() |
106 |
| - total = len(skeletons) |
107 |
| - elif type(skeletons) == Skeleton: |
108 |
| - iterator = [ skeletons ] |
109 |
| - total = 1 |
110 |
| - else: |
111 |
| - total = len(skeletons) |
112 |
| - |
113 |
| - if all_labels.dtype == bool: |
114 |
| - remapping = { True: 1, False: 0, 1:1, 0:0 } |
115 |
| - else: |
116 |
| - all_labels, remapping = fastremap.renumber(all_labels, in_place=in_place) |
117 |
| - |
118 |
| - all_slices = find_objects(all_labels) |
119 |
| - |
120 |
| - for skel in tqdm(iterator, desc="Labels", disable=(not progress), total=total): |
121 |
| - if all_labels.dtype == bool: |
122 |
| - label = 1 |
123 |
| - else: |
124 |
| - label = skel.id |
125 |
| - |
126 |
| - if label == 0: |
127 |
| - continue |
128 |
| - |
129 |
| - label = remapping[label] |
130 |
| - slices = all_slices[label - 1] |
131 |
| - if slices is None: |
132 |
| - continue |
133 |
| - |
134 |
| - roi = Bbox.from_slices(slices) |
135 |
| - if roi.volume() <= 1: |
136 |
| - continue |
137 |
| - |
138 |
| - roi.grow(1) |
139 |
| - roi.minpt = Vec.clamp(roi.minpt, Vec(0,0,0), roi.maxpt) |
140 |
| - slices = roi.to_slices() |
141 |
| - |
142 |
| - binimg = np.asfortranarray(all_labels[slices] == label) |
143 |
| - |
| 154 | + def cross_sectional_area_helper(skel, binimg, roi): |
144 | 155 | cross_sections = None
|
145 | 156 | if visualize_section_planes:
|
146 | 157 | cross_sections = np.zeros(binimg.shape, dtype=np.uint32, order="F")
|
147 | 158 |
|
148 |
| - if fill_holes: |
149 |
| - binimg = fill_voids.fill(binimg, in_place=True) |
150 |
| - |
151 | 159 | all_verts = (skel.vertices / anisotropy).round().astype(int)
|
152 | 160 | all_verts -= roi.minpt
|
153 | 161 |
|
@@ -207,11 +215,90 @@ def cross_sectional_area(
|
207 | 215 | microviewer.view(cross_sections, seg=True)
|
208 | 216 |
|
209 | 217 | add_property(skel, prop)
|
| 218 | + |
210 | 219 | skel.cross_sectional_area = areas
|
211 | 220 | skel.cross_sectional_area_contacts = contacts
|
212 | 221 |
|
| 222 | + shape_iterator( |
| 223 | + all_labels, skeletons, |
| 224 | + fill_holes, in_place, progress, |
| 225 | + cross_sectional_area_helper |
| 226 | + ) |
| 227 | + |
213 | 228 | return skeletons
|
214 | 229 |
|
| 230 | +def oversegment( |
| 231 | + all_labels:np.ndarray, |
| 232 | + skeletons:Union[Dict[int,Skeleton],List[Skeleton],Skeleton], |
| 233 | + anisotropy:np.ndarray = np.array([1,1,1], dtype=np.float32), |
| 234 | + progress:bool = False, |
| 235 | + fill_holes:bool = False, |
| 236 | + in_place:bool = False, |
| 237 | + downsample:int = 0, |
| 238 | +) -> Tuple[np.ndarray, Union[Dict[int,Skeleton],List[Skeleton],Skeleton]]: |
| 239 | + """ |
| 240 | + Use skeletons to create an oversegmentation of a pre-existing set |
| 241 | + of labels. This is useful for proofreading systems that work by merging |
| 242 | + labels. |
| 243 | +
|
| 244 | + For each skeleton, get the feature map from its euclidean distance |
| 245 | + field. The final image is the composite of all these feature maps |
| 246 | + numbered from 1. |
| 247 | +
|
| 248 | + Each skeleton will have a new property skel.segments that associates |
| 249 | + a label to each vertex. |
| 250 | + """ |
| 251 | + prop = { |
| 252 | + "id": "segments", |
| 253 | + "data_type": "uint64", |
| 254 | + "num_components": 1, |
| 255 | + } |
| 256 | + |
| 257 | + skeletons = copy.deepcopy(skeletons) |
| 258 | + |
| 259 | + all_features = np.zeros(all_labels.shape, dtype=np.uint64, order="F") |
| 260 | + next_label = 0 |
| 261 | + |
| 262 | + def oversegment_helper(skel, binimg, roi): |
| 263 | + nonlocal next_label |
| 264 | + nonlocal all_features |
| 265 | + |
| 266 | + segment_skel = skel |
| 267 | + if downsample > 0: |
| 268 | + segment_skel = skel.downsample(downsample) |
| 269 | + |
| 270 | + vertices = (segment_skel.vertices / anisotropy).round().astype(int) |
| 271 | + vertices -= roi.minpt |
| 272 | + |
| 273 | + field, feature_map = dijkstra3d.euclidean_distance_field( |
| 274 | + binimg, vertices, |
| 275 | + anisotropy=anisotropy, |
| 276 | + return_feature_map=True |
| 277 | + ) |
| 278 | + del field |
| 279 | + |
| 280 | + add_property(skel, prop) |
| 281 | + |
| 282 | + vertices = (skel.vertices / anisotropy).round().astype(int) |
| 283 | + vertices -= roi.minpt |
| 284 | + |
| 285 | + feature_map[binimg] += next_label |
| 286 | + skel.segments = feature_map[vertices[:,0], vertices[:,1], vertices[:,2]] |
| 287 | + next_label += vertices.shape[0] |
| 288 | + all_features[roi.to_slices()] += feature_map |
| 289 | + |
| 290 | + # iterator is an iterable list of skeletons, not the shape iterator |
| 291 | + iterator = shape_iterator( |
| 292 | + all_labels, skeletons, fill_holes, in_place, progress, |
| 293 | + oversegment_helper |
| 294 | + ) |
| 295 | + |
| 296 | + all_features, mapping = fastremap.renumber(all_features) |
| 297 | + for skel in iterator: |
| 298 | + skel.segments = fastremap.remap(skel.segments, mapping, in_place=True) |
| 299 | + |
| 300 | + return all_features, skeletons |
| 301 | + |
215 | 302 | # From SO: https://stackoverflow.com/questions/14313510/how-to-calculate-rolling-moving-average-using-python-numpy-scipy
|
216 | 303 | def moving_average(a:np.ndarray, n:int, mode:str = "symmetric") -> np.ndarray:
|
217 | 304 | if n <= 0:
|
|
0 commit comments