|
| 1 | +import os |
| 2 | +import sys |
| 3 | + |
| 4 | +from glob import glob |
| 5 | + |
| 6 | +import mrcfile |
| 7 | +import pandas as pd |
| 8 | +from tqdm import tqdm |
| 9 | + |
| 10 | +from synaptic_reconstruction.imod.export import load_points_from_imodinfo |
| 11 | +from synaptic_reconstruction.file_utils import get_data_path |
| 12 | + |
| 13 | +sys.path.append("../processing") |
| 14 | + |
| 15 | + |
| 16 | +def aggregate_radii(data_root, table, save_path, get_tab): |
| 17 | + if os.path.exists(save_path): |
| 18 | + return |
| 19 | + |
| 20 | + radius_table = [] |
| 21 | + for _, row in tqdm(table.iterrows(), total=len(table), desc="Collect tomo information"): |
| 22 | + folder = row["Local Path"] |
| 23 | + if folder == "": |
| 24 | + continue |
| 25 | + |
| 26 | + tomo_name = os.path.relpath(folder, os.path.join(data_root, "Electron-Microscopy-Susi/Analyse")) |
| 27 | + tab_path = get_tab(folder) |
| 28 | + if tab_path is None: |
| 29 | + continue |
| 30 | + |
| 31 | + tab = pd.read_excel(tab_path) |
| 32 | + this_tab = tab[["pool", "radius [nm]"]] |
| 33 | + this_tab.insert(0, "tomogram", [tomo_name] * len(this_tab)) |
| 34 | + radius_table.append(this_tab) |
| 35 | + |
| 36 | + radius_table = pd.concat(radius_table) |
| 37 | + print("Saving table for", len(radius_table), "vesicles to", save_path) |
| 38 | + radius_table.to_excel(save_path, index=False) |
| 39 | + |
| 40 | + |
| 41 | +def aggregate_radii_imod(data_root, table, save_path): |
| 42 | + if os.path.exists(save_path): |
| 43 | + return |
| 44 | + |
| 45 | + radius_table = [] |
| 46 | + for _, row in tqdm(table.iterrows(), total=len(table), desc="Collect tomo information"): |
| 47 | + folder = row["Local Path"] |
| 48 | + if folder == "": |
| 49 | + continue |
| 50 | + |
| 51 | + tomo_name = os.path.relpath(folder, os.path.join(data_root, "Electron-Microscopy-Susi/Analyse")) |
| 52 | + annotation_folder = os.path.join(folder, "manuell") |
| 53 | + if not os.path.exists(annotation_folder): |
| 54 | + annotation_folder = os.path.join(folder, "Manuell") |
| 55 | + if not os.path.exists(annotation_folder): |
| 56 | + continue |
| 57 | + |
| 58 | + annotations = glob(os.path.join(annotation_folder, "*.mod")) |
| 59 | + annotation_file = [ann for ann in annotations if ("vesikel" in ann.lower()) or ("vesicle" in ann.lower())] |
| 60 | + if len(annotation_file) != 1: |
| 61 | + continue |
| 62 | + annotation_file = annotation_file[0] |
| 63 | + |
| 64 | + tomo_file = get_data_path(folder) |
| 65 | + with mrcfile.open(tomo_file) as f: |
| 66 | + shape = f.data.shape |
| 67 | + resolution = list(f.voxel_size.item()) |
| 68 | + resolution = [res / 10 for res in resolution][0] |
| 69 | + |
| 70 | + try: |
| 71 | + _, radii, labels, label_names = load_points_from_imodinfo(annotation_file, shape, resolution=resolution) |
| 72 | + except AssertionError: |
| 73 | + continue |
| 74 | + |
| 75 | + this_tab = pd.DataFrame({ |
| 76 | + "tomogram": [tomo_name] * len(radii), |
| 77 | + "pool": [label_names[label_id] for label_id in labels], |
| 78 | + "radius [nm]": radii, |
| 79 | + }) |
| 80 | + radius_table.append(this_tab) |
| 81 | + |
| 82 | + radius_table = pd.concat(radius_table) |
| 83 | + print("Saving table for", len(radius_table), "vesicles to", save_path) |
| 84 | + radius_table.to_excel(save_path, index=False) |
| 85 | + |
| 86 | + |
| 87 | +def get_tab_automatic(folder): |
| 88 | + tab_name = "measurements_uncorrected_assignments.xlsx" |
| 89 | + res_path = os.path.join(folder, "korrektur", tab_name) |
| 90 | + if not os.path.exists(res_path): |
| 91 | + res_path = os.path.join(folder, "Korrektur", tab_name) |
| 92 | + if not os.path.exists(res_path): |
| 93 | + res_path = None |
| 94 | + return res_path |
| 95 | + |
| 96 | + |
| 97 | +def get_tab_semi_automatic(folder): |
| 98 | + tab_name = "measurements.xlsx" |
| 99 | + res_path = os.path.join(folder, "korrektur", tab_name) |
| 100 | + if not os.path.exists(res_path): |
| 101 | + res_path = os.path.join(folder, "Korrektur", tab_name) |
| 102 | + if not os.path.exists(res_path): |
| 103 | + res_path = None |
| 104 | + return res_path |
| 105 | + |
| 106 | + |
| 107 | +def get_tab_manual(folder): |
| 108 | + tab_name = "measurements.xlsx" |
| 109 | + res_path = os.path.join(folder, "manuell", tab_name) |
| 110 | + if not os.path.exists(res_path): |
| 111 | + res_path = os.path.join(folder, "Manuell", tab_name) |
| 112 | + if not os.path.exists(res_path): |
| 113 | + res_path = None |
| 114 | + return res_path |
| 115 | + |
| 116 | + |
| 117 | +def main(): |
| 118 | + from parse_table import parse_table, get_data_root |
| 119 | + |
| 120 | + data_root = get_data_root() |
| 121 | + table_path = os.path.join(data_root, "Electron-Microscopy-Susi", "Übersicht.xlsx") |
| 122 | + table = parse_table(table_path, data_root) |
| 123 | + |
| 124 | + # TODO get the radii from imod |
| 125 | + aggregate_radii(data_root, table, save_path="./results/vesicle_radii_automatic.xlsx", get_tab=get_tab_automatic) |
| 126 | + aggregate_radii(data_root, table, save_path="./results/vesicle_radii_semi_automatic.xlsx", get_tab=get_tab_semi_automatic) # noqa |
| 127 | + aggregate_radii(data_root, table, save_path="./results/vesicle_radii_manual.xlsx", get_tab=get_tab_manual) |
| 128 | + aggregate_radii_imod(data_root, table, save_path="./results/vesicle_radii_imod.xlsx") |
| 129 | + |
| 130 | + |
| 131 | +if __name__ == "__main__": |
| 132 | + main() |
0 commit comments