|
1 |
| -# TODO implement analysis pipeline for our tomo sample data: |
2 |
| -# - segment vesicles, AZ, and compartment |
3 |
| -# - use compartment segmentation to find the presynaptic terminal |
4 |
| -# - postprocess the AZ segmentation |
5 |
| -# - measure distances between vesicles and AZ |
6 |
| -# - vesicle pool assignment into docked and non-attached terminals |
7 |
| -# - export table with distance and morphology measurements for the two pools |
| 1 | +import napari |
| 2 | +import pandas as pd |
| 3 | + |
| 4 | +from synapse_net.file_utils import read_mrc |
| 5 | +from synapse_net.sample_data import get_sample_data |
| 6 | +from synapse_net.tools.util import run_segmentation, get_model, compute_scale_from_voxel_size |
| 7 | + |
| 8 | + |
| 9 | +def segment_structures(tomogram, voxel_size): |
| 10 | + # Segment the synaptic vesicles. The data will automatically be resized |
| 11 | + # to match the average voxel size of the training data. |
| 12 | + model_name = "vesicles_3d" # This is the name for the vesicle model for EM tomography. |
| 13 | + model = get_model(model_name) # Load the corresponding model. |
| 14 | + # Compute the scale to match the tomogram voxel size to the training data. |
| 15 | + scale = compute_scale_from_voxel_size(voxel_size, model_name) |
| 16 | + vesicles = run_segmentation(tomogram, model, model_name, scale=scale) |
| 17 | + |
| 18 | + # Segment the active zone. |
| 19 | + model_name = "active_zone" |
| 20 | + model = get_model(model_name) |
| 21 | + scale = compute_scale_from_voxel_size(voxel_size, model_name) |
| 22 | + active_zone = run_segmentation(tomogram, model, model_name, scale=scale) |
| 23 | + |
| 24 | + # Segment the synaptic compartments. |
| 25 | + model_name = "compartments" |
| 26 | + model = get_model(model_name) |
| 27 | + scale = compute_scale_from_voxel_size(voxel_size, model_name) |
| 28 | + compartments = run_segmentation(tomogram, model, model_name, scale=scale) |
| 29 | + |
| 30 | + return {"vesicles": vesicles, "active_zone": active_zone, "compartments": compartments} |
| 31 | + |
| 32 | + |
| 33 | +def postprocess_segmentation(segmentations): |
| 34 | + pass |
| 35 | + |
| 36 | + |
| 37 | +def measure_distances(segmentations): |
| 38 | + pass |
| 39 | + |
| 40 | + |
| 41 | +def assign_vesicle_pools(distances): |
| 42 | + pass |
| 43 | + |
| 44 | + |
| 45 | +def visualize_results(tomogram, segmentations, vesicle_pools): |
| 46 | + # TODO vesicle pool visualization |
| 47 | + viewer = napari.Viewer() |
| 48 | + viewer.add_image(tomogram) |
| 49 | + for name, segmentation in segmentations.items(): |
| 50 | + viewer.add_labels(segmentation, name=name) |
| 51 | + napari.run() |
| 52 | + |
| 53 | + |
| 54 | +def save_analysis(segmentations, distances, vesicle_pools, save_path): |
| 55 | + pass |
| 56 | + |
| 57 | + |
| 58 | +def main(): |
| 59 | + """This script implements an example analysis pipeline with SynapseNet and applies it to a tomogram. |
| 60 | + Here, we analyze docked and non-attached vesicles in a sample tomogram.""" |
| 61 | + |
| 62 | + # Load the tomogram for our sample data. |
| 63 | + mrc_path = get_sample_data("tem_tomo") |
| 64 | + tomogram, voxel_size = read_mrc(mrc_path) |
| 65 | + |
| 66 | + # Segment synaptic vesicles, the active zone, and the synaptic compartment. |
| 67 | + segmentations = segment_structures(tomogram, voxel_size) |
| 68 | + |
| 69 | + # Post-process the segmentations, to find the presynaptic terminal, |
| 70 | + # filter out vesicles not in the terminal, and to 'snape' the AZ to the presynaptic boundary. |
| 71 | + segmentations = postprocess_segmentation(segmentations) |
| 72 | + |
| 73 | + # Measure the distances between the AZ and vesicles. |
| 74 | + distances = measure_distances(segmentations) |
| 75 | + |
| 76 | + # Assign the vesicle pools, 'docked' and 'non-attached' vesicles, based on the distances. |
| 77 | + vesicle_pools = assign_vesicle_pools(distances) |
| 78 | + |
| 79 | + # Visualize the results. |
| 80 | + visualize_results(tomogram, segmentations, vesicle_pools) |
| 81 | + |
| 82 | + # Compute the vesicle radii and combine and save all measurements. |
| 83 | + save_path = "analysis_results.xlsx" |
| 84 | + save_analysis(segmentations, distances, vesicle_pools, save_path) |
| 85 | + |
| 86 | + |
| 87 | +if __name__ == "__main__": |
| 88 | + main() |
0 commit comments