Skip to content

Commit 30339b3

Browse files
authored
Merge pull request #57 from computational-cell-analytics/plots-cp
Update of figure plots and analysis This branch updated some initial plots for publishing. Later commits improved the ChReef analysis.
2 parents 1836e50 + 03be562 commit 30339b3

File tree

9 files changed

+1168
-48
lines changed

9 files changed

+1168
-48
lines changed

flamingo_tools/segmentation/chreef_utils.py

Lines changed: 20 additions & 5 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
@@ -123,7 +128,10 @@ def find_inbetween_ids(
123128
Args:
124129
arr_negexc: Array with all negatives excluded.
125130
arr_allweak: Array with all weak positives.
126-
roi_sgn: Region of interest of segmentation.
131+
roi_seg: 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)
@@ -141,8 +149,12 @@ def get_median_intensity(file_negexc, file_allweak, center, data_seg, table):
141149

142150
roi_seg = data_seg[roi]
143151
inbetween_ids = find_inbetween_ids(arr_negexc, arr_allweak, roi_seg)
152+
if len(inbetween_ids) == 0:
153+
return None
154+
144155
subset = table[table["label_id"].isin(inbetween_ids)]
145156
intensities = list(subset["median"])
157+
146158
return np.median(list(intensities))
147159

148160

@@ -154,11 +166,14 @@ def localize_median_intensities(annotation_dir, cochlea, data_seg, table_measure
154166

155167
for center_str in annotation_dic["center_strings"]:
156168
center_coord = coord_from_string(center_str)
157-
print(f"Getting mean intensities for {center_coord}.")
169+
print(f"Getting median intensities for {center_coord}.")
158170
file_pos = annotation_dic[center_str]["file_pos"]
159171
file_neg = annotation_dic[center_str]["file_neg"]
160172
median_intensity = get_median_intensity(file_neg, file_pos, center_coord, data_seg, table_measure)
161173

174+
if median_intensity is None:
175+
print(f"No inbetween IDs found for {center_str}.")
176+
162177
annotation_dic[center_str]["median_intensity"] = median_intensity
163178

164179
return annotation_dic

scripts/figures/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)