Skip to content

Commit 4f9917f

Browse files
Add chreef analysis plotting scripts WIP
1 parent 1836e50 commit 4f9917f

File tree

2 files changed

+98
-3
lines changed

2 files changed

+98
-3
lines changed

flamingo_tools/segmentation/chreef_utils.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,17 @@ def coord_from_string(center_str):
1212
return tuple([int(c) for c in center_str.split("-")])
1313

1414

15-
def find_annotations(annotation_dir, cochlea) -> dict:
16-
"""Create dictionary for analysis of ChReef annotations.
15+
def find_annotations(annotation_dir: str, cochlea: str) -> dict:
16+
"""Create a dictionary for the analysis of ChReef annotations.
17+
1718
Annotations should have format positive-negative_<cochlea>_crop_<coord>_allNegativeExcluded_thr<thr>.tif
1819
1920
Args:
2021
annotation_dir: Directory containing annotations.
22+
cochlea: The name of the cochlea to analyze.
23+
24+
Returns:
25+
Dictionary with information about the intensity annotations.
2126
"""
2227

2328
def extract_center_string(cochlea, name):
@@ -58,7 +63,7 @@ def get_roi(coord: tuple, roi_halo: tuple, resolution: float = 0.38) -> Tuple[in
5863
resolution: Resolution of array in µm.
5964
6065
Returns:
61-
region of interest
66+
The region of interest.
6267
"""
6368
coords = list(coord)
6469
# reverse dimensions for correct extraction
@@ -124,6 +129,9 @@ def find_inbetween_ids(
124129
arr_negexc: Array with all negatives excluded.
125130
arr_allweak: Array with all weak positives.
126131
roi_sgn: Region of interest of segmentation.
132+
133+
Returns:
134+
A list of the ids that are in between the respective thresholds.
127135
"""
128136
# negative annotation == 1, positive annotation == 2
129137
negexc_negatives = find_overlapping_masks(arr_negexc, roi_seg, label_id_base=1)

scripts/plots/chreef_analysis.py

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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

Comments
 (0)