|
| 1 | +import os |
| 2 | +from shutil import copyfile |
| 3 | +from subprocess import run |
| 4 | + |
| 5 | +import imageio.v3 as imageio |
| 6 | +import mrcfile |
| 7 | +import napari |
| 8 | +import numpy as np |
| 9 | +import pandas as pd |
| 10 | +from elf.io import open_file |
| 11 | +from skimage.transform import resize |
| 12 | +from synaptic_reconstruction.imod.to_imod import write_segmentation_to_imod, write_segmentation_to_imod_as_points |
| 13 | + |
| 14 | +out_folder = "./auto_seg_export" |
| 15 | +os.makedirs(out_folder, exist_ok=True) |
| 16 | + |
| 17 | + |
| 18 | +def _resize(seg, tomo_path): |
| 19 | + with open_file(tomo_path, "r") as f: |
| 20 | + shape = f["data"].shape |
| 21 | + |
| 22 | + if shape != seg.shape: |
| 23 | + seg = resize(seg, shape, order=0, anti_aliasing=False, preserve_range=True).astype(seg.dtype) |
| 24 | + assert seg.shape == shape |
| 25 | + return seg |
| 26 | + |
| 27 | + |
| 28 | +def check_imod(tomo_path, mod_path): |
| 29 | + run(["imod", tomo_path, mod_path]) |
| 30 | + |
| 31 | + |
| 32 | +def export_pool(pool_name, pool_seg, tomo_path): |
| 33 | + seg_path = f"./auto_seg_export/{pool_name}.tif" |
| 34 | + pool_seg = _resize(pool_seg, tomo_path) |
| 35 | + imageio.imwrite(seg_path, pool_seg, compression="zlib") |
| 36 | + |
| 37 | + output_path = f"./auto_seg_export/{pool_name}.mod" |
| 38 | + write_segmentation_to_imod_as_points(tomo_path, seg_path, output_path, min_radius=5) |
| 39 | + |
| 40 | + check_imod(tomo_path, output_path) |
| 41 | + |
| 42 | + |
| 43 | +def export_vesicles(folder, tomo_path): |
| 44 | + vesicle_pool_path = os.path.join(folder, "Korrektur", "vesicle_pools.tif") |
| 45 | + # pool_correction_path = os.path.join(folder, "Korrektur", "pool_correction.tif") |
| 46 | + # pool_correction = imageio.imread(pool_correction_path) |
| 47 | + |
| 48 | + assignment_path = os.path.join(folder, "Korrektur", "measurements.xlsx") |
| 49 | + assignments = pd.read_excel(assignment_path) |
| 50 | + |
| 51 | + vesicles = imageio.imread(vesicle_pool_path) |
| 52 | + |
| 53 | + pools = {} |
| 54 | + for pool_name in pd.unique(assignments.pool): |
| 55 | + pool_ids = assignments[assignments.pool == pool_name].id.values |
| 56 | + pool_seg = vesicles.copy() |
| 57 | + pool_seg[~np.isin(vesicles, pool_ids)] = 0 |
| 58 | + pools[pool_name] = pool_seg |
| 59 | + |
| 60 | + view = False |
| 61 | + if view: |
| 62 | + v = napari.Viewer() |
| 63 | + v.add_labels(vesicles, visible=False) |
| 64 | + for pool_name, pool_seg in pools.items(): |
| 65 | + v.add_labels(pool_seg, name=pool_name) |
| 66 | + napari.run() |
| 67 | + else: |
| 68 | + for pool_name, pool_seg in pools.items(): |
| 69 | + export_pool(pool_name, pool_seg, tomo_path) |
| 70 | + |
| 71 | + |
| 72 | +def export_structure(folder, tomo, name, view=False): |
| 73 | + path = os.path.join(folder, "Korrektur", f"{name}.tif") |
| 74 | + seg = imageio.imread(path) |
| 75 | + seg = _resize(seg, tomo) |
| 76 | + |
| 77 | + if view: |
| 78 | + with open_file(tomo, "r") as f: |
| 79 | + raw = f["data"][:] |
| 80 | + |
| 81 | + v = napari.Viewer() |
| 82 | + v.add_image(raw) |
| 83 | + v.add_labels(seg) |
| 84 | + napari.run() |
| 85 | + |
| 86 | + return |
| 87 | + |
| 88 | + seg_path = f"./auto_seg_export/{name}.tif" |
| 89 | + imageio.imwrite(seg_path, seg, compression="zlib") |
| 90 | + output_path = f"./auto_seg_export/{name}.mod" |
| 91 | + write_segmentation_to_imod(tomo, seg_path, output_path) |
| 92 | + check_imod(tomo, output_path) |
| 93 | + |
| 94 | + |
| 95 | +def remove_scale(tomo): |
| 96 | + new_path = "./auto_seg_export/Emb71M1aGridA1sec1mod7.rec.rec" |
| 97 | + if os.path.exists(new_path): |
| 98 | + return new_path |
| 99 | + |
| 100 | + copyfile(tomo, new_path) |
| 101 | + |
| 102 | + with mrcfile.open(new_path, "r+") as f: |
| 103 | + # Set the origin to (0, 0, 0) |
| 104 | + f.header.nxstart = 0 |
| 105 | + f.header.nystart = 0 |
| 106 | + f.header.nzstart = 0 |
| 107 | + f.header.origin = (0.0, 0.0, 0.0) |
| 108 | + |
| 109 | + # Save changes |
| 110 | + f.flush() |
| 111 | + |
| 112 | + return new_path |
| 113 | + |
| 114 | + |
| 115 | +def main(): |
| 116 | + folder = "/home/pape/Work/data/moser/em-synapses/Electron-Microscopy-Susi/Analyse/WT strong stim/Mouse 1/modiolar/1" |
| 117 | + tomo = os.path.join(folder, "Emb71M1aGridA1sec1mod7.rec.rec") |
| 118 | + |
| 119 | + tomo = remove_scale(tomo) |
| 120 | + |
| 121 | + # export_vesicles(folder, tomo) |
| 122 | + # export_structure(folder, tomo, "ribbon", view=False) |
| 123 | + # export_structure(folder, tomo, "membrane", view=False) |
| 124 | + export_structure(folder, tomo, "PD", view=False) |
| 125 | + |
| 126 | + |
| 127 | +if __name__ == "__main__": |
| 128 | + main() |
0 commit comments