|
| 1 | +import os |
| 2 | +from glob import glob |
| 3 | + |
| 4 | +import h5py |
| 5 | +import numpy as np |
| 6 | +from tqdm import tqdm |
| 7 | +from skimage.transform import resize |
| 8 | + |
| 9 | +ROOT = "/mnt/ceph-hdd/cold_store/projects/nim00007/AZ_data/training_data" |
| 10 | +INTER_ROOT = "/mnt/ceph-hdd/cold_store/projects/nim00007/AZ_predictions" |
| 11 | +OUTPUT_ROOT = "/mnt/ceph-hdd/cold_store/projects/nim00007/new_AZ_train_data" |
| 12 | + |
| 13 | + |
| 14 | +def _check_data(files, label_folder, check_thinned): |
| 15 | + for ff in files: |
| 16 | + with h5py.File(ff, "r") as f: |
| 17 | + shape = f["raw"].shape |
| 18 | + az = f["labels/az"][:] |
| 19 | + n_az = az.max() |
| 20 | + |
| 21 | + if check_thinned: |
| 22 | + label_file = os.path.join(label_folder, os.path.basename(ff)) |
| 23 | + with h5py.File(label_file, "r") as f: |
| 24 | + az_thin = f["labels/az_thin2"][:] |
| 25 | + n_az_thin = az_thin.max() |
| 26 | + else: |
| 27 | + n_az_thin = None |
| 28 | + |
| 29 | + print(os.path.basename(ff), ":", shape, ":", n_az, ":", n_az_thin) |
| 30 | + |
| 31 | + |
| 32 | +def assort_tem(): |
| 33 | + old_name = "01data_withoutInvertedFiles_minusSVseg_corrected" |
| 34 | + new_name = "tem" |
| 35 | + |
| 36 | + raw_folder = os.path.join(ROOT, old_name) |
| 37 | + label_folder = os.path.join(INTER_ROOT, old_name) |
| 38 | + output_folder = os.path.join(OUTPUT_ROOT, new_name) |
| 39 | + os.makedirs(output_folder, exist_ok=True) |
| 40 | + |
| 41 | + files = glob(os.path.join(raw_folder, "*.h5")) |
| 42 | + for ff in tqdm(files): |
| 43 | + with h5py.File(ff, "r") as f: |
| 44 | + raw = f["raw"][:] |
| 45 | + az = f["labels/az"][:] |
| 46 | + |
| 47 | + label_path = os.path.join(label_folder, os.path.basename(ff)) |
| 48 | + with h5py.File(label_path, "r") as f: |
| 49 | + az_thin = f["labels/az_thin2"][:] |
| 50 | + |
| 51 | + z_range1 = np.where(az != 0)[0] |
| 52 | + z_range2 = np.where(az != 0)[0] |
| 53 | + z_range = slice( |
| 54 | + np.min(np.concatenate([z_range1, z_range2])), |
| 55 | + np.max(np.concatenate([z_range1, z_range2])) + 1, |
| 56 | + ) |
| 57 | + raw, az, az_thin = raw[z_range], az[z_range], az_thin[z_range] |
| 58 | + |
| 59 | + out_path = os.path.join(output_folder, os.path.basename(ff)) |
| 60 | + with h5py.File(out_path, "a") as f: |
| 61 | + f.create_dataset("raw", data=raw, compression="lzf") |
| 62 | + f.create_dataset("labels/az_thin", data=az_thin, compression="lzf") |
| 63 | + f.create_dataset("labels/az", data=az, compression="lzf") |
| 64 | + |
| 65 | + |
| 66 | +def assort_chemical_fixation(): |
| 67 | + old_name = "12_chemical_fix_cryopreparation_minusSVseg_corrected" |
| 68 | + new_name = "chemical_fixation" |
| 69 | + |
| 70 | + raw_folder = os.path.join(ROOT, old_name) |
| 71 | + label_folder = os.path.join(INTER_ROOT, old_name) |
| 72 | + output_folder = os.path.join(OUTPUT_ROOT, new_name) |
| 73 | + os.makedirs(output_folder, exist_ok=True) |
| 74 | + |
| 75 | + label_key = "labels/az_thin2" |
| 76 | + |
| 77 | + files = glob(os.path.join(raw_folder, "*.h5")) |
| 78 | + for ff in tqdm(files): |
| 79 | + with h5py.File(ff, "r") as f: |
| 80 | + raw = f["raw"][:] |
| 81 | + az = f["labels/az"][:] |
| 82 | + |
| 83 | + label_path = os.path.join(label_folder, os.path.basename(ff)) |
| 84 | + with h5py.File(label_path, "r") as f: |
| 85 | + az_thin = f[label_key][:] |
| 86 | + |
| 87 | + z_range1 = np.where(az != 0)[0] |
| 88 | + z_range2 = np.where(az != 0)[0] |
| 89 | + z_range = slice( |
| 90 | + np.min(np.concatenate([z_range1, z_range2])), |
| 91 | + np.max(np.concatenate([z_range1, z_range2])) + 1, |
| 92 | + ) |
| 93 | + raw, az, az_thin = raw[z_range], az[z_range], az_thin[z_range] |
| 94 | + |
| 95 | + out_path = os.path.join(output_folder, os.path.basename(ff)) |
| 96 | + with h5py.File(out_path, "a") as f: |
| 97 | + f.create_dataset("raw", data=raw, compression="lzf") |
| 98 | + f.create_dataset("labels/az_thin", data=az_thin, compression="lzf") |
| 99 | + f.create_dataset("labels/az", data=az, compression="lzf") |
| 100 | + |
| 101 | + |
| 102 | +def assort_stem(): |
| 103 | + old_names = [ |
| 104 | + "04_hoi_stem_examples_fidi_and_sarah_corrected", |
| 105 | + "04_hoi_stem_examples_minusSVseg_cropped_corrected", |
| 106 | + "06_hoi_wt_stem750_fm_minusSVseg_cropped_corrected", |
| 107 | + ] |
| 108 | + new_names = ["stem", "stem_cropped", "stem_cropped"] |
| 109 | + for old_name, new_name in zip(old_names, new_names): |
| 110 | + print(old_name) |
| 111 | + raw_folder = os.path.join(ROOT, f"{old_name}_rescaled_tomograms") |
| 112 | + label_folder = os.path.join(INTER_ROOT, old_name) |
| 113 | + files = glob(os.path.join(raw_folder, "*.h5")) |
| 114 | + |
| 115 | + # _check_data(files, label_folder, check_thinned=True) |
| 116 | + # continue |
| 117 | + |
| 118 | + output_folder = os.path.join(OUTPUT_ROOT, new_name) |
| 119 | + os.makedirs(output_folder, exist_ok=True) |
| 120 | + for ff in tqdm(files): |
| 121 | + with h5py.File(ff, "r") as f: |
| 122 | + raw = f["raw"][:] |
| 123 | + az = f["labels/az"][:] |
| 124 | + |
| 125 | + label_path = os.path.join(label_folder, os.path.basename(ff)) |
| 126 | + with h5py.File(label_path, "r") as f: |
| 127 | + az_thin = f["labels/az_thin2"][:] |
| 128 | + az_thin = resize(az_thin, az.shape, order=0, anti_aliasing=False, preserve_range=True).astype(az_thin.dtype) |
| 129 | + assert az_thin.shape == az.shape |
| 130 | + |
| 131 | + out_path = os.path.join(output_folder, os.path.basename(ff)) |
| 132 | + with h5py.File(out_path, "a") as f: |
| 133 | + f.create_dataset("raw", data=raw, compression="lzf") |
| 134 | + f.create_dataset("labels/az_thin", data=az_thin, compression="lzf") |
| 135 | + f.create_dataset("labels/az", data=az, compression="lzf") |
| 136 | + |
| 137 | + |
| 138 | +def assort_wichmann(): |
| 139 | + old_name = "wichmann_withAZ_rescaled_tomograms" |
| 140 | + new_name = "endbulb_of_held" |
| 141 | + |
| 142 | + raw_folder = os.path.join(ROOT, old_name) |
| 143 | + output_folder = os.path.join(OUTPUT_ROOT, new_name) |
| 144 | + os.makedirs(output_folder, exist_ok=True) |
| 145 | + |
| 146 | + files = glob(os.path.join(raw_folder, "*.h5")) |
| 147 | + |
| 148 | + output_folder = os.path.join(OUTPUT_ROOT, new_name) |
| 149 | + os.makedirs(output_folder, exist_ok=True) |
| 150 | + for ff in tqdm(files): |
| 151 | + with h5py.File(ff, "r") as f: |
| 152 | + raw = f["raw"][:] |
| 153 | + az = f["labels/az"][:] |
| 154 | + |
| 155 | + output_file = os.path.join(output_folder, os.path.basename(ff)) |
| 156 | + with h5py.File(output_file, "a") as f: |
| 157 | + f.create_dataset("raw", data=raw, compression="lzf") |
| 158 | + f.create_dataset("labels/az", data=az, compression="lzf") |
| 159 | + f.create_dataset("labels/az_thin", data=az, compression="lzf") |
| 160 | + |
| 161 | + |
| 162 | +def crop_wichmann(): |
| 163 | + input_name = "endbulb_of_held" |
| 164 | + output_name = "endbulb_of_held_cropped" |
| 165 | + |
| 166 | + input_folder = os.path.join(OUTPUT_ROOT, input_name) |
| 167 | + output_folder = os.path.join(OUTPUT_ROOT, output_name) |
| 168 | + os.makedirs(output_folder, exist_ok=True) |
| 169 | + files = glob(os.path.join(input_folder, "*.h5")) |
| 170 | + |
| 171 | + min_shape = (32, 512, 512) |
| 172 | + |
| 173 | + for ff in tqdm(files): |
| 174 | + with h5py.File(ff, "r") as f: |
| 175 | + az = f["labels/az"][:] |
| 176 | + bb = np.where(az != 0) |
| 177 | + bb = tuple(slice(int(b.min()), int(b.max()) + 1) for b in bb) |
| 178 | + pad_width = [max(sh - (b.stop - b.start), 0) // 2 for b, sh in zip(bb, min_shape)] |
| 179 | + bb = tuple( |
| 180 | + slice(max(b.start - pw, 0), min(b.stop + pw, sh)) for b, pw, sh in zip(bb, pad_width, az.shape) |
| 181 | + ) |
| 182 | + az = az[bb] |
| 183 | + raw = f["raw"][bb] |
| 184 | + |
| 185 | + # import napari |
| 186 | + # v = napari.Viewer() |
| 187 | + # v.add_image(raw) |
| 188 | + # v.add_labels(az) |
| 189 | + # v.add_labels(az_thin) |
| 190 | + # napari.run() |
| 191 | + |
| 192 | + output_path = os.path.join(output_folder, os.path.basename(ff).replace(".h5", "_cropped.h5")) |
| 193 | + with h5py.File(output_path, "a") as f: |
| 194 | + f.create_dataset("raw", data=raw, compression="lzf") |
| 195 | + f.create_dataset("labels/az", data=az, compression="lzf") |
| 196 | + f.create_dataset("labels/az_thin", data=az, compression="lzf") |
| 197 | + |
| 198 | + |
| 199 | +def main(): |
| 200 | + # assort_tem() |
| 201 | + # assort_chemical_fixation() |
| 202 | + |
| 203 | + # assort_stem() |
| 204 | + |
| 205 | + # assort_wichmann() |
| 206 | + crop_wichmann() |
| 207 | + |
| 208 | + |
| 209 | +if __name__ == "__main__": |
| 210 | + main() |
0 commit comments