22
33import h5py
44import numpy as np
5- import pandas as pd
65
76from skimage .measure import label
87from skimage .segmentation import relabel_sequential
98
10- from synapse_net .distance_measurements import measure_segmentation_to_object_distances , load_distances
9+ from synapse_net .distance_measurements import measure_segmentation_to_object_distances
1110from synapse_net .file_utils import read_mrc
1211from synapse_net .inference .vesicles import segment_vesicles
1312from synapse_net .tools .util import get_model , compute_scale_from_voxel_size , _segment_ribbon_AZ
1413from tqdm import tqdm
1514
16- from common import STRUCTURE_NAMES , get_all_tomograms , get_seg_path , get_adapted_model
15+ from common import get_all_tomograms , get_seg_path , get_adapted_model
1716
1817# These are tomograms for which the sophisticated membrane processing fails.
1918# In this case, we just select the largest boundary piece.
@@ -159,74 +158,6 @@ def postprocess_vesicles(mrc_path, output_path, process_center_crop):
159158 f .create_dataset (key , data = vesicles , compression = "gzip" )
160159
161160
162- def measure_distances (mrc_path , seg_path , output_folder ):
163- result_folder = os .path .join (output_folder , "distances" )
164- if os .path .exists (result_folder ):
165- return
166-
167- # Get the voxel size.
168- _ , voxel_size = read_mrc (mrc_path )
169- resolution = tuple (voxel_size [ax ] for ax in "zyx" )
170-
171- # Load the segmentations.
172- with h5py .File (seg_path , "r" ) as f :
173- g = f ["segmentation" ]
174- vesicles = g ["vesicles" ][:]
175- structures = {name : g [name ][:] for name in STRUCTURE_NAMES }
176-
177- # Measure all the object distances.
178- os .makedirs (result_folder , exist_ok = True )
179- for name , seg in structures .items ():
180- if seg .sum () == 0 :
181- print (name , "was not found, skipping the distance computation." )
182- continue
183- print ("Compute vesicle distances to" , name )
184- save_path = os .path .join (result_folder , f"{ name } .npz" )
185- measure_segmentation_to_object_distances (vesicles , seg , save_path = save_path , resolution = resolution )
186-
187-
188- def assign_vesicle_pools (output_folder ):
189- assignment_path = os .path .join (output_folder , "vesicle_pools.csv" )
190- if os .path .exists (assignment_path ):
191- return
192-
193- distance_folder = os .path .join (output_folder , "distances" )
194- distance_paths = {name : os .path .join (distance_folder , f"{ name } .npz" ) for name in STRUCTURE_NAMES }
195- if not all (os .path .exists (path ) for path in distance_paths .values ()):
196- print ("Skip vesicle pool assignment, because some distances are missing." )
197- print ("This is probably due to the fact that the corresponding structures were not found." )
198- return
199- distances = {name : load_distances (path ) for name , path in distance_paths .items ()}
200-
201- # The distance criteria.
202- rav_ribbon_distance = 80 # nm
203- mpv_pd_distance = 100 # nm
204- mpv_mem_distance = 50 # nm
205- docked_pd_distance = 100 # nm
206- docked_mem_distance = 2 # nm
207-
208- rav_distances , seg_ids = distances ["ribbon" ][0 ], np .array (distances ["ribbon" ][- 1 ])
209- rav_ids = seg_ids [rav_distances < rav_ribbon_distance ]
210-
211- pd_distances , mem_distances = distances ["PD" ][0 ], distances ["membrane" ][0 ]
212- assert len (pd_distances ) == len (mem_distances ) == len (rav_distances )
213-
214- mpv_ids = seg_ids [np .logical_and (pd_distances < mpv_pd_distance , mem_distances < mpv_mem_distance )]
215- docked_ids = seg_ids [np .logical_and (pd_distances < docked_pd_distance , mem_distances < docked_mem_distance )]
216-
217- # Create a dictionary to map vesicle ids to their corresponding pool.
218- # (RA-V get's over-written by MP-V, which is correct).
219- pool_assignments = {vid : "RA-V" for vid in rav_ids }
220- pool_assignments .update ({vid : "MP-V" for vid in mpv_ids })
221- pool_assignments .update ({vid : "Docked-V" for vid in docked_ids })
222-
223- pool_assignments = pd .DataFrame ({
224- "vesicle_id" : list (pool_assignments .keys ()),
225- "pool" : list (pool_assignments .values ()),
226- })
227- pool_assignments .to_csv (assignment_path , index = False )
228-
229-
230161def process_tomogram (mrc_path ):
231162 output_path = get_seg_path (mrc_path )
232163 output_folder = os .path .split (output_path )[0 ]
@@ -238,13 +169,6 @@ def process_tomogram(mrc_path):
238169 process_ribbon_structures (mrc_path , output_path , process_center_crop )
239170 postprocess_vesicles (mrc_path , output_path , process_center_crop )
240171
241- # We don't need to do the analysis of the auto semgentation, it only
242- # makes sense to do this after segmentation. I am leaving this here for
243- # now, to move it to the files for analysis later.
244-
245- # measure_distances(mrc_path, output_path, output_folder)
246- # assign_vesicle_pools(output_folder)
247-
248172
249173def main ():
250174 tomograms = get_all_tomograms ()
0 commit comments