@@ -23,19 +23,27 @@ def export_synapse_detections(
2323 radius : float ,
2424 id_offset : int ,
2525 filter_ihc_components : List [int ],
26+ position : str ,
27+ halo : List [int ],
28+ as_float : bool = False ,
29+ use_syn_ids : bool = False ,
2630):
27- """Export synapse detections fro lower resolutions .
31+ """Export synapse detections from S3. .
2832
2933 Args:
3034 cochlea: Cochlea name on S3 bucket.
3135 scales: Scale for export of lower resolution.
32- output_folder:
33- synapse_name:
36+ output_folder: The output folder for saving the exported data.
37+ synapse_name: The name of the synapse detection source.
3438 reference_ihcs: Name of IHC segmentation.
3539 max_dist: Maximal distance of synapse to IHC segmentation.
36- radius:
40+ radius: The radius for writing the synapse points to the output volume.
3741 id_offset: Offset of label id of synapse output to have different colours for visualization.
3842 filter_ihc_components: Component label(s) for filtering IHC segmentation.
43+ position: Optional position for extracting a crop from the data. Requires to also pass halo.
44+ halo: Halo for extracting a crop from the data.
45+ as_float: Whether to save the exported data as floating point values.
46+ use_syn_ids: Whether to write the synapse IDs or the matched IHC IDs to the output volume.
3947 """
4048 s3 = create_s3_target ()
4149
@@ -79,17 +87,37 @@ def export_synapse_detections(
7987 coordinates = np .round (coordinates , 0 ).astype ("int" )
8088
8189 ihc_ids = syn_table ["matched_ihc" ].values
90+ syn_ids = syn_table ["spot_id" ].values
91+
92+ if position is not None :
93+ assert halo is not None
94+ center = json .loads (position )
95+ assert len (halo ) == len (center )
96+ center = [int (ce / (resolution * (2 ** scale ))) for ce in center [::- 1 ]]
97+ start = np .array ([max (0 , ce - ha ) for ce , ha in zip (center , halo )])[None ]
98+ stop = np .array ([min (sh , ce + ha ) for ce , ha , sh in zip (center , halo , shape )])[None ]
99+
100+ mask = ((coordinates >= start ) & (coordinates < stop )).all (axis = 1 )
101+ coordinates = coordinates [mask ]
102+ coordinates -= start
103+
104+ ihc_ids = ihc_ids [mask ]
105+ syn_ids = syn_ids [mask ]
106+
107+ shape = tuple (int (sto - sta ) for sta , sto in zip (start .squeeze (), stop .squeeze ()))
82108
83109 # Create the output.
84110 output = np .zeros (shape , dtype = "uint16" )
85111 mask = ball (radius ).astype (bool )
86112
87- for coord , matched_ihc in tqdm (
88- zip (coordinates , ihc_ids ), total = len (coordinates ), desc = "Writing synapses to volume"
113+ ids = syn_ids if use_syn_ids else ihc_ids
114+
115+ for coord , syn_id in tqdm (
116+ zip (coordinates , ids ), total = len (coordinates ), desc = "Writing synapses to volume"
89117 ):
90118 bb = tuple (slice (c - radius , c + radius + 1 ) for c in coord )
91119 try :
92- output [bb ][mask ] = matched_ihc + id_offset
120+ output [bb ][mask ] = syn_id + id_offset
93121 except IndexError :
94122 print ("Index error for" , coord )
95123 continue
@@ -101,6 +129,10 @@ def export_synapse_detections(
101129 out_path = os .path .join (out_folder , f"{ synapse_name } _offset{ id_offset } .tif" )
102130 else :
103131 out_path = os .path .join (out_folder , f"{ synapse_name } .tif" )
132+
133+ if as_float :
134+ output = output .astype ("float32" )
135+
104136 print ("Writing synapses to" , out_path )
105137 tifffile .imwrite (out_path , output , bigtiff = True , compression = "zlib" )
106138
@@ -116,13 +148,19 @@ def main():
116148 parser .add_argument ("--radius" , type = int , default = 3 )
117149 parser .add_argument ("--id_offset" , type = int , default = 0 )
118150 parser .add_argument ("--filter_ihc_components" , nargs = "+" , type = int , default = [1 ])
151+ parser .add_argument ("--position" , default = None )
152+ parser .add_argument ("--halo" , default = None , nargs = "+" , type = int )
153+ parser .add_argument ("--as_float" , action = "store_true" )
154+ parser .add_argument ("--use_syn_ids" , action = "store_true" )
119155 args = parser .parse_args ()
120156
121157 export_synapse_detections (
122158 args .cochlea , args .scale , args .output_folder ,
123159 args .synapse_name , args .reference_ihcs ,
124160 args .max_dist , args .radius ,
125161 args .id_offset , args .filter_ihc_components ,
162+ position = args .position , halo = args .halo ,
163+ as_float = args .as_float , use_syn_ids = args .use_syn_ids ,
126164 )
127165
128166
0 commit comments