|
| 1 | +import os |
| 2 | +import tempfile |
| 3 | +from glob import glob |
| 4 | +from subprocess import run |
| 5 | +from shutil import copyfile |
| 6 | + |
| 7 | +import h5py |
| 8 | +import pandas as pd |
| 9 | + |
| 10 | +from synaptic_reconstruction.imod.to_imod import write_segmentation_to_imod |
| 11 | +from scipy.ndimage import binary_dilation, binary_closing |
| 12 | + |
| 13 | + |
| 14 | +def check_imod(tomo_path, mod_path): |
| 15 | + run(["imod", tomo_path, mod_path]) |
| 16 | + |
| 17 | + |
| 18 | +def export_all_to_imod(check_input=True, check_export=True): |
| 19 | + files = sorted(glob("./az_segmentation/**/*.h5", recursive=True)) |
| 20 | + mrc_root = "./mrc_files" |
| 21 | + output_folder = "./az_export/initial_model" |
| 22 | + |
| 23 | + for ff in files: |
| 24 | + ds, fname = os.path.split(ff) |
| 25 | + ds = os.path.basename(ds) |
| 26 | + out_folder = os.path.join(output_folder, ds) |
| 27 | + out_path = os.path.join(out_folder, fname.replace(".h5", ".mod")) |
| 28 | + if os.path.exists(out_path): |
| 29 | + continue |
| 30 | + |
| 31 | + os.makedirs(out_folder, exist_ok=True) |
| 32 | + mrc_path = os.path.join(mrc_root, ds, fname.replace(".h5", ".rec")) |
| 33 | + assert os.path.exists(mrc_path), mrc_path |
| 34 | + |
| 35 | + with h5py.File(ff, "r") as f: |
| 36 | + seg = f["thin_az"][:] |
| 37 | + |
| 38 | + seg = binary_dilation(seg, iterations=2) |
| 39 | + seg = binary_closing(seg, iterations=2) |
| 40 | + |
| 41 | + write_segmentation_to_imod(mrc_path, seg, out_path) |
| 42 | + |
| 43 | + if check_input: |
| 44 | + import napari |
| 45 | + from elf.io import open_file |
| 46 | + with open_file(mrc_path, "r") as f: |
| 47 | + raw = f["data"][:] |
| 48 | + v = napari.Viewer() |
| 49 | + v.add_image(raw) |
| 50 | + v.add_labels(seg) |
| 51 | + napari.run() |
| 52 | + |
| 53 | + if check_export: |
| 54 | + check_imod(mrc_path, out_path) |
| 55 | + |
| 56 | + |
| 57 | +# https://bio3d.colorado.edu/imod/doc/man/reducecont.html |
| 58 | +def reduce_all_contours(): |
| 59 | + pass |
| 60 | + |
| 61 | + |
| 62 | +# https://bio3d.colorado.edu/imod/doc/man/smoothsurf.html#TOP |
| 63 | +def smooth_all_surfaces(check_output=True): |
| 64 | + input_files = sorted(glob("./az_export/initial_model/**/*.mod", recursive=True)) |
| 65 | + |
| 66 | + mrc_root = "./mrc_files" |
| 67 | + output_folder = "./az_export/smoothed_model" |
| 68 | + for ff in input_files: |
| 69 | + ds, fname = os.path.split(ff) |
| 70 | + ds = os.path.basename(ds) |
| 71 | + out_folder = os.path.join(output_folder, ds) |
| 72 | + out_file = os.path.join(out_folder, fname) |
| 73 | + if os.path.exists(out_file): |
| 74 | + continue |
| 75 | + |
| 76 | + os.makedirs(out_folder, exist_ok=True) |
| 77 | + run(["smoothsurf", ff, out_file]) |
| 78 | + if check_output: |
| 79 | + mrc_path = os.path.join(mrc_root, ds, fname.replace(".mod", ".rec")) |
| 80 | + assert os.path.exists(mrc_path), mrc_path |
| 81 | + check_imod(mrc_path, out_file) |
| 82 | + |
| 83 | + |
| 84 | +def measure_surfaces(): |
| 85 | + input_files = sorted(glob("./az_export/smoothed_model/**/*.mod", recursive=True)) |
| 86 | + |
| 87 | + result = { |
| 88 | + "Dataset": [], |
| 89 | + "Tomogram": [], |
| 90 | + "AZ Surface": [], |
| 91 | + } |
| 92 | + for ff in input_files: |
| 93 | + ds, fname = os.path.split(ff) |
| 94 | + ds = os.path.basename(ds) |
| 95 | + fname = os.path.splitext(fname)[0] |
| 96 | + |
| 97 | + with tempfile.NamedTemporaryFile() as f_mesh, tempfile.NamedTemporaryFile() as f_mod: |
| 98 | + tmp_path_mesh = f_mesh.name |
| 99 | + tmp_path_mod = f_mod.name |
| 100 | + copyfile(ff, tmp_path_mesh) |
| 101 | + run(["imodmesh", tmp_path_mesh]) |
| 102 | + run(["imodinfo", "-f", tmp_path_mod, tmp_path_mesh]) |
| 103 | + area = None |
| 104 | + with open(tmp_path_mod, "r") as f: |
| 105 | + for line in f.readlines(): |
| 106 | + line = line.strip() |
| 107 | + if line.startswith("Total mesh surface area"): |
| 108 | + area = float(line.split(" ")[-1]) |
| 109 | + assert area is not None |
| 110 | + area /= 2 |
| 111 | + |
| 112 | + result["Dataset"].append(ds) |
| 113 | + result["Tomogram"].append(fname) |
| 114 | + result["AZ Surface"].append(area) |
| 115 | + |
| 116 | + result = pd.DataFrame(result) |
| 117 | + result.to_excel("./az_measurements_all.xlsx", index=False) |
| 118 | + |
| 119 | + |
| 120 | +def filter_surfaces(): |
| 121 | + all_results = pd.read_excel("./az_measurements_all.xlsx") |
| 122 | + man_tomos = pd.read_csv("./man_tomos.tsv") |
| 123 | + |
| 124 | + man_results = all_results.merge(man_tomos[["Dataset", "Tomogram"]], on=["Dataset", "Tomogram"], how="inner") |
| 125 | + man_results.to_excel("./az_measuerements_manual.xlsx", index=False) |
| 126 | + |
| 127 | + |
| 128 | +def main(): |
| 129 | + export_all_to_imod(False, False) |
| 130 | + smooth_all_surfaces(False) |
| 131 | + # measure_surfaces() |
| 132 | + filter_surfaces() |
| 133 | + |
| 134 | + |
| 135 | +if __name__ == "__main__": |
| 136 | + main() |
0 commit comments