|
| 1 | +import os |
| 2 | +from glob import glob |
| 3 | + |
| 4 | +import imageio.v3 as imageio |
| 5 | +import h5py |
| 6 | +import mrcfile |
| 7 | +import napari |
| 8 | +import numpy as np |
| 9 | + |
| 10 | +# TODO refactor everything once things are merged |
| 11 | +ROOT = "/home/ag-wichmann/data/otoferlin/tomograms" |
| 12 | +if not os.path.exists(ROOT): |
| 13 | + ROOT = "./data/tomograms" |
| 14 | + |
| 15 | +SEG_ROOT = "./segmentation/v2" |
| 16 | + |
| 17 | + |
| 18 | +def correct_structure_segmentation(mrc_path): |
| 19 | + rel_path = os.path.relpath(mrc_path, ROOT) |
| 20 | + rel_folder, fname = os.path.split(rel_path) |
| 21 | + fname = os.path.splitext(fname)[0] |
| 22 | + seg_path = os.path.join(SEG_ROOT, rel_folder, f"{fname}.h5") |
| 23 | + |
| 24 | + with mrcfile.open(mrc_path, permissive=True) as mrc: |
| 25 | + data = np.asarray(mrc.data[:]) |
| 26 | + data = np.flip(data, axis=1) |
| 27 | + |
| 28 | + correction_folder = os.path.join(SEG_ROOT, rel_folder, "correction") |
| 29 | + |
| 30 | + names = ("ribbon", "PD", "membrane", "veiscles_postprocessed") |
| 31 | + segmentations = {} |
| 32 | + with h5py.File(seg_path, "r") as f: |
| 33 | + for name in names: |
| 34 | + correction_path = os.path.join(correction_folder, f"{name}.tif") |
| 35 | + if os.path.exists(correction_path): |
| 36 | + print("Loading segmentation for", name, "from", correction_path) |
| 37 | + segmentations[name] = imageio.imread(correction_path) |
| 38 | + else: |
| 39 | + segmentations[name] = f[f"segmentation/{name}"][:] |
| 40 | + color_maps = { |
| 41 | + "ribbon": {1: "red", None: "gray"}, |
| 42 | + "PD": {1: "purple", None: "gray"}, |
| 43 | + "membrane": {1: "magenta", None: "gray"}, |
| 44 | + } |
| 45 | + |
| 46 | + v = napari.Viewer() |
| 47 | + v.add_image(data) |
| 48 | + for name, seg in segmentations.items(): |
| 49 | + v.add_labels(seg, name=name, colormap=color_maps.get(name, None)) |
| 50 | + v.title = fname |
| 51 | + napari.run() |
| 52 | + |
| 53 | + |
| 54 | +def main(): |
| 55 | + tomograms = glob(os.path.join(ROOT, "**", "*.mrc"), recursive=True) |
| 56 | + tomograms += glob(os.path.join(ROOT, "**", "*.rec"), recursive=True) |
| 57 | + tomograms = sorted(tomograms) |
| 58 | + |
| 59 | + for tomo in tomograms: |
| 60 | + correct_structure_segmentation(tomo) |
| 61 | + |
| 62 | + |
| 63 | +if __name__ == "__main__": |
| 64 | + main() |
0 commit comments