|
| 1 | +import os |
| 2 | + |
| 3 | +import matplotlib.pyplot as plt |
| 4 | +import numpy as np |
| 5 | +import pandas as pd |
| 6 | + |
| 7 | +from flamingo_tools.s3_utils import get_s3_path |
| 8 | +from flamingo_tools.measurements import compute_object_measures |
| 9 | + |
| 10 | + |
| 11 | +def compute_sgn_volume(cochlea, output_path, voxel_spacing, seg_name="SGN_v2"): |
| 12 | + img_path = f"{cochlea}/images/ome-zarr/PV.ome.zarr" |
| 13 | + seg_path = f"{cochlea}/images/ome-zarr/{seg_name}.ome.zarr" |
| 14 | + |
| 15 | + img_path, _ = get_s3_path(img_path) |
| 16 | + seg_path, _ = get_s3_path(seg_path) |
| 17 | + |
| 18 | + segmentation_table_path = f"{cochlea}/tables/{seg_name}/default.tsv" |
| 19 | + feature_set = "morphology" |
| 20 | + compute_object_measures( |
| 21 | + image_path=img_path, |
| 22 | + segmentation_path=seg_path, |
| 23 | + segmentation_table_path=segmentation_table_path, |
| 24 | + output_table_path=output_path, |
| 25 | + resolution=voxel_spacing, |
| 26 | + feature_set=feature_set, |
| 27 | + component_list=[1], |
| 28 | + s3_flag=True, |
| 29 | + image_key="s0", |
| 30 | + segmentation_key="s0", |
| 31 | + ) |
| 32 | + |
| 33 | + |
| 34 | +def compute_volumes_flamingo(): |
| 35 | + output_folder = "./data/volumes_flamingo" |
| 36 | + os.makedirs(output_folder, exist_ok=True) |
| 37 | + cochleae = ["M_LR_000226_L", "M_LR_000226_R", "M_LR_000227_L", "M_LR_000227_R"] |
| 38 | + voxel_spacing = (0.38, 0.38, 0.38) |
| 39 | + for cochlea in cochleae: |
| 40 | + output_path = os.path.join(output_folder, f"{cochlea}.tsv") |
| 41 | + if os.path.exists(output_path): |
| 42 | + continue |
| 43 | + compute_sgn_volume(cochlea, output_path, voxel_spacing) |
| 44 | + |
| 45 | + |
| 46 | +def compute_volumes_lavision(): |
| 47 | + output_folder = "./data/volumes_lavision" |
| 48 | + os.makedirs(output_folder, exist_ok=True) |
| 49 | + cochleae = ["LaVision-M02", "LaVision-M03"] |
| 50 | + # Note: we used a wrong voxel spacing in MoBIE (1.9 micron in-plane instead of 0.76) |
| 51 | + # We solved this here in a hacky fashion by hard-coding the resolution for the volume |
| 52 | + # calculation temporariliy in the measurement function. |
| 53 | + voxel_spacing = (3.0, 1.887779, 1.887779) |
| 54 | + # voxel_spacing = (3.0, 0.76, 0.76) |
| 55 | + for cochlea in cochleae: |
| 56 | + output_path = os.path.join(output_folder, f"{cochlea}.tsv") |
| 57 | + if os.path.exists(output_path): |
| 58 | + continue |
| 59 | + compute_sgn_volume(cochlea, output_path, voxel_spacing, seg_name="SGN_LOWRES-v5c") |
| 60 | + |
| 61 | + |
| 62 | +def compare_volumes(): |
| 63 | + cochleae_flamingo = ["M_LR_000226_L", "M_LR_000226_R", "M_LR_000227_L", "M_LR_000227_R"] |
| 64 | + cochleae_lavision = ["LaVision-M02", "LaVision-M03"] |
| 65 | + |
| 66 | + folder_flamingo = "./data/volumes_flamingo" |
| 67 | + folder_lavision = "./data/volumes_lavision" |
| 68 | + |
| 69 | + data_flamingo = [] |
| 70 | + for cochlea in cochleae_flamingo: |
| 71 | + x = pd.read_csv(os.path.join(folder_flamingo, f"{cochlea}.tsv"), sep="\t") |
| 72 | + volumes = x["volume"].values |
| 73 | + volumes = volumes[~np.isnan(volumes)] |
| 74 | + data_flamingo.append(volumes) |
| 75 | + |
| 76 | + data_lavision = [] |
| 77 | + for cochlea in cochleae_lavision: |
| 78 | + x = pd.read_csv(os.path.join(folder_lavision, f"{cochlea}.tsv"), sep="\t") |
| 79 | + volumes = x["volume"].values |
| 80 | + volumes = volumes[~np.isnan(volumes)] |
| 81 | + data_lavision.append(volumes) |
| 82 | + |
| 83 | + fig, axes = plt.subplots(2, sharey=True) |
| 84 | + |
| 85 | + ax = axes[0] |
| 86 | + ax.boxplot(data_flamingo, tick_labels=cochleae_flamingo) |
| 87 | + ax.set_ylabel("SGN Volume [µm^3]") |
| 88 | + |
| 89 | + ax = axes[1] |
| 90 | + ax.boxplot(data_lavision, tick_labels=cochleae_lavision) |
| 91 | + ax.set_ylabel("SGN Volume [µm^3]") |
| 92 | + |
| 93 | + plt.show() |
| 94 | + |
| 95 | + |
| 96 | +def main(): |
| 97 | + compute_volumes_flamingo() |
| 98 | + compute_volumes_lavision() |
| 99 | + |
| 100 | + compare_volumes() |
| 101 | + |
| 102 | + |
| 103 | +if __name__ == "__main__": |
| 104 | + main() |
0 commit comments