|
| 1 | +import os |
| 2 | +import shutil |
| 3 | +from glob import glob |
| 4 | + |
| 5 | +import numpy as np |
| 6 | +import pandas as pd |
| 7 | +import tifffile |
| 8 | +import zarr |
| 9 | + |
| 10 | +from flamingo_tools.s3_utils import get_s3_path, BUCKET_NAME, SERVICE_ENDPOINT |
| 11 | + |
| 12 | + |
| 13 | +def get_scale_factor(): |
| 14 | + original_path = "/mnt/ceph-hdd/cold/nim00007/cochlea-lightsheet/M_LR_000169_R/MLR000169R_PV.tif" |
| 15 | + original_shape = tifffile.memmap(original_path).shape |
| 16 | + |
| 17 | + cochlea = "M_LR_000169_R" |
| 18 | + internal_path = os.path.join(cochlea, "images", "ome-zarr", "SGN_v2.ome.zarr") |
| 19 | + s3_store, fs = get_s3_path(internal_path, bucket_name=BUCKET_NAME, service_endpoint=SERVICE_ENDPOINT) |
| 20 | + |
| 21 | + input_key = "s0" |
| 22 | + with zarr.open(s3_store, mode="r") as f: |
| 23 | + new_shape = f[input_key].shape |
| 24 | + |
| 25 | + scale_factor = tuple( |
| 26 | + float(nsh) / float(osh) for nsh, osh in zip(new_shape, original_shape) |
| 27 | + ) |
| 28 | + return scale_factor |
| 29 | + |
| 30 | + |
| 31 | +def rescale_annotations(input_path, scale_factor, bkp_folder): |
| 32 | + annotations = pd.read_csv(input_path) |
| 33 | + |
| 34 | + annotations_rescaled = annotations.copy() |
| 35 | + annotations_rescaled["axis-1"] = annotations["axis-1"] * scale_factor[1] |
| 36 | + annotations_rescaled["axis-2"] = annotations["axis-2"] * scale_factor[2] |
| 37 | + |
| 38 | + fname = os.path.basename(input_path) |
| 39 | + name_components = fname.split("_") |
| 40 | + z = int(name_components[2][1:]) |
| 41 | + new_z = int(np.round(z * scale_factor[0])) |
| 42 | + |
| 43 | + name_components[2] = f"z{new_z}" |
| 44 | + name_components = name_components[:-1] + ["rescaled"] + name_components[-1:] |
| 45 | + new_fname = "_".join(name_components) |
| 46 | + |
| 47 | + input_folder = os.path.split(input_path)[0] |
| 48 | + out_path = os.path.join(input_folder, new_fname) |
| 49 | + bkp_path = os.path.join(bkp_folder, fname) |
| 50 | + |
| 51 | + # print(input_path) |
| 52 | + # print(out_path) |
| 53 | + # print(bkp_path) |
| 54 | + # print() |
| 55 | + # return |
| 56 | + |
| 57 | + shutil.move(input_path, bkp_path) |
| 58 | + annotations_rescaled.to_csv(out_path, index=False) |
| 59 | + |
| 60 | + |
| 61 | +def main(): |
| 62 | + # scale_factor = get_scale_factor() |
| 63 | + # print(scale_factor) |
| 64 | + scale_factor = (2.6314,) * 3 |
| 65 | + |
| 66 | + root = "/mnt/vast-nhr/projects/nim00007/data/moser/cochlea-lightsheet/AnnotatedImageCrops/F1ValidationSGNs" |
| 67 | + annotation_folders = ["AnnotationsEK", "AnnotationsAMD", "AnnotationsLR"] |
| 68 | + for folder in annotation_folders: |
| 69 | + bkp_folder = os.path.join(root, folder, "rescaled_bkp") |
| 70 | + os.makedirs(bkp_folder, exist_ok=True) |
| 71 | + |
| 72 | + files = glob(os.path.join(root, folder, "*.csv")) |
| 73 | + for annotation_file in files: |
| 74 | + fname = os.path.basename(annotation_file) |
| 75 | + if not fname.startswith(("MLR169R_PV_z722", "MLR169R_PV_z979")): |
| 76 | + continue |
| 77 | + print("Rescaling", annotation_file) |
| 78 | + rescale_annotations(annotation_file, scale_factor, bkp_folder) |
| 79 | + |
| 80 | + |
| 81 | +# Rescale the point annotations for the cochlea MLR169R, which was |
| 82 | +# annotated at the original scale, but then rescaled for segmentation. |
| 83 | +if __name__ == "__main__": |
| 84 | + main() |
0 commit comments