|
| 1 | +import json |
| 2 | +import os |
| 3 | +import pickle |
| 4 | + |
| 5 | +# import matplotlib.pyplot as plt |
| 6 | +# import numpy as np |
| 7 | +import pandas as pd |
| 8 | +# import tifffile |
| 9 | +# import zarr |
| 10 | +# from matplotlib import cm, colors |
| 11 | + |
| 12 | +from flamingo_tools.s3_utils import BUCKET_NAME, create_s3_target |
| 13 | + |
| 14 | +INTENSITY_ROOT = "/mnt/vast-nhr/projects/nim00007/data/moser/cochlea-lightsheet/mobie_project/cochlea-lightsheet/tables/measurements" # noqa |
| 15 | +# The cochlea for the CHReef analysis. |
| 16 | +COCHLEAE = [ |
| 17 | + "M_LR_000143_L", |
| 18 | + "M_LR_000144_L", |
| 19 | + "M_LR_000145_L", |
| 20 | + "M_LR_000153_L", |
| 21 | + "M_LR_000155_L", |
| 22 | + "M_LR_000189_L", |
| 23 | + "M_LR_000143_R", |
| 24 | + "M_LR_000144_R", |
| 25 | + "M_LR_000145_R", |
| 26 | + "M_LR_000153_R", |
| 27 | + "M_LR_000155_R", |
| 28 | + "M_LR_000189_R", |
| 29 | +] |
| 30 | + |
| 31 | + |
| 32 | +def download_data(): |
| 33 | + s3 = create_s3_target() |
| 34 | + source_name = "SGN_v2" |
| 35 | + |
| 36 | + cache_path = "./chreef_data.pkl" |
| 37 | + if os.path.exists(cache_path): |
| 38 | + with open(cache_path, "rb") as f: |
| 39 | + return pickle.load(f) |
| 40 | + |
| 41 | + chreef_data = {} |
| 42 | + for cochlea in COCHLEAE: |
| 43 | + print("Processsing cochlea:", cochlea) |
| 44 | + content = s3.open(f"{BUCKET_NAME}/{cochlea}/dataset.json", mode="r", encoding="utf-8") |
| 45 | + info = json.loads(content.read()) |
| 46 | + sources = info["sources"] |
| 47 | + |
| 48 | + # Load the seg table and filter the compartments. |
| 49 | + source = sources[source_name]["segmentation"] |
| 50 | + rel_path = source["tableData"]["tsv"]["relativePath"] |
| 51 | + table_content = s3.open(os.path.join(BUCKET_NAME, cochlea, rel_path, "default.tsv"), mode="rb") |
| 52 | + table = pd.read_csv(table_content, sep="\t") |
| 53 | + |
| 54 | + # May need to be adjusted for some cochleae. |
| 55 | + table = table[table.component_labels == 1] |
| 56 | + # The relevant values for analysis. |
| 57 | + try: |
| 58 | + values = table[["label_id", "length[µm]", "frequency[kHz]", "marker_labels"]] |
| 59 | + except KeyError: |
| 60 | + print("Could not find the values for", cochlea, "it will be skippped.") |
| 61 | + continue |
| 62 | + |
| 63 | + fname = f"{cochlea.replace('_', '-')}_GFP_SGN-v2_object-measures.tsv" |
| 64 | + intensity_file = os.path.join(INTENSITY_ROOT, fname) |
| 65 | + assert os.path.exists(intensity_file), intensity_file |
| 66 | + intensity_table = pd.read_csv(intensity_file, sep="\t") |
| 67 | + values = values.merge(intensity_table, on="label_id") |
| 68 | + |
| 69 | + chreef_data[cochlea] = values |
| 70 | + |
| 71 | + with open(cache_path, "wb") as f: |
| 72 | + chreef_data = pickle.dump(chreef_data, f) |
| 73 | + return chreef_data |
| 74 | + |
| 75 | + |
| 76 | +def analyze_transduction(chreef_data): |
| 77 | + breakpoint() |
| 78 | + pass |
| 79 | + |
| 80 | + |
| 81 | +def main(): |
| 82 | + chreef_data = download_data() |
| 83 | + analyze_transduction(chreef_data) |
| 84 | + |
| 85 | + |
| 86 | +if __name__ == "__main__": |
| 87 | + main() |
0 commit comments