@@ -57,8 +57,7 @@ def extend_block(block_id):
5757 outer_block = block .outerBlock
5858 inner_block = block .innerBlock
5959
60- # TODO get the indices and coordinates of the markers in the INNER block
61- # markers_in_block_ids = [int(i) for i in np.unique(inner_block)[1:]]
60+ # get the indices and coordinates of the markers in the INNER block
6261 mask = (
6362 (inner_block .begin [0 ] <= markers [:, 0 ]) & (markers [:, 0 ] <= inner_block .end [0 ]) &
6463 (inner_block .begin [1 ] <= markers [:, 1 ]) & (markers [:, 1 ] <= inner_block .end [1 ]) &
@@ -67,33 +66,35 @@ def extend_block(block_id):
6766 markers_in_block_ids = np .where (mask )[0 ]
6867 markers_in_block_coords = markers [markers_in_block_ids ]
6968
70- # TODO offset the marker coordinates with respect to the OUTER block
71- markers_in_block_coords = [coord - outer_block .begin for coord in markers_in_block_coords ]
72- markers_in_block_coords = [[round (c ) for c in coord ] for coord in markers_in_block_coords ]
73- markers_in_block_coords = np .array (markers_in_block_coords , dtype = int )
74- z , y , x = markers_in_block_coords .T
69+ # proceed if detections fall within inner block
70+ if len (markers_in_block_coords ) > 0 :
71+ markers_in_block_coords = [coord - outer_block .begin for coord in markers_in_block_coords ]
72+ markers_in_block_coords = [[round (c ) for c in coord ] for coord in markers_in_block_coords ]
7573
76- # Shift index by one so that zero is reserved for background id
77- markers_in_block_ids += 1
74+ markers_in_block_coords = np . array ( markers_in_block_coords , dtype = int )
75+ z , y , x = markers_in_block_coords . T
7876
79- # Create the seed volume.
80- outer_block_shape = tuple (end - begin for begin , end in zip (outer_block .begin , outer_block .end ))
81- seeds = np .zeros (outer_block_shape , dtype = "uint32" )
82- seeds [z , y , x ] = markers_in_block_ids
77+ # Shift index by one so that zero is reserved for background id
78+ markers_in_block_ids += 1
8379
84- # Compute the distance map.
85- distance = distance_transform_edt (seeds == 0 , sampling = sampling )
80+ # Create the seed volume.
81+ outer_block_shape = tuple (end - begin for begin , end in zip (outer_block .begin , outer_block .end ))
82+ seeds = np .zeros (outer_block_shape , dtype = "uint32" )
83+ seeds [z , y , x ] = markers_in_block_ids
8684
87- # And extend the seeds
88- mask = distance < extension_distance
89- segmentation = watershed (distance .max () - distance , markers = seeds , mask = mask )
85+ # Compute the distance map.
86+ distance = distance_transform_edt (seeds == 0 , sampling = sampling )
9087
91- # Write the segmentation. Note: we need to lock here because we write outside of our inner block
92- bb = tuple (slice (begin , end ) for begin , end in zip (outer_block .begin , outer_block .end ))
93- with lock :
94- this_output = output [bb ]
95- this_output [mask ] = segmentation [mask ]
96- output [bb ] = this_output
88+ # And extend the seeds
89+ mask = distance < extension_distance
90+ segmentation = watershed (distance .max () - distance , markers = seeds , mask = mask )
91+
92+ # Write the segmentation. Note: we need to lock here because we write outside of our inner block
93+ bb = tuple (slice (begin , end ) for begin , end in zip (outer_block .begin , outer_block .end ))
94+ with lock :
95+ this_output = output [bb ]
96+ this_output [mask ] = segmentation [mask ]
97+ output [bb ] = this_output
9798
9899 n_blocks = blocking .numberOfBlocks
99100 with futures .ThreadPoolExecutor (n_threads ) as tp :
@@ -146,10 +147,18 @@ def sgn_detection(
146147 detection_path = os .path .join (output_folder , "SGN_detection.tsv" )
147148 if not os .path .exists (detection_path ):
148149 input_ = zarr .open (output_path , "r" )[prediction_key ]
149- detections = find_local_maxima (
150+ detections_maxima = find_local_maxima (
150151 input_ , block_shape = block_shape , min_distance = 4 , threshold_abs = 0.5 , verbose = True , n_threads = 16 ,
151152 )
152153
154+ # Save the result in mobie compatible format.
155+ detections = np .concatenate (
156+ [np .arange (1 , len (detections_maxima ) + 1 )[:, None ], detections_maxima [:, ::- 1 ]], axis = 1
157+ )
158+ detections = pd .DataFrame (detections , columns = ["spot_id" , "x" , "y" , "z" ])
159+ detections .to_csv (detection_path , index = False , sep = "\t " )
160+
161+ # extend detection
153162 shape = input_ .shape
154163 chunks = (128 , 128 , 128 )
155164 segmentation_path = os .path .join (output_folder , "segmentation.zarr" )
@@ -161,17 +170,11 @@ def sgn_detection(
161170 )
162171
163172 distance_based_marker_extension (
164- markers = detections ,
173+ markers = detections_maxima ,
165174 output = output_dataset ,
166175 extension_distance = extension_distance ,
167176 sampling = sampling ,
168177 block_shape = (128 , 128 , 128 ),
169178 n_threads = n_threads ,
170179 )
171180
172- # Save the result in mobie compatible format.
173- detections = np .concatenate (
174- [np .arange (1 , len (detections ) + 1 )[:, None ], detections [:, ::- 1 ]], axis = 1
175- )
176- detections = pd .DataFrame (detections , columns = ["spot_id" , "x" , "y" , "z" ])
177- detections .to_csv (detection_path , index = False , sep = "\t " )
0 commit comments