-
Notifications
You must be signed in to change notification settings - Fork 0
/
5_add_tomo_view.py
101 lines (79 loc) · 2.99 KB
/
5_add_tomo_view.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import json
import os
from concurrent import futures
import mobie
import numpy as np
import pandas as pd
from elf.io import open_file
def get_clims(tomograms, name):
path = f"clims_{name}.json"
if os.path.exists(path):
with open(path) as f:
return json.load(f)
def _clims(tomo):
tomo_path = f"./data/yeast/images/local/{tomo}.n5"
with open_file(tomo_path, "r") as f:
data = f["setup0/timepoint0/s2"][:]
return data.min(), data.max()
with futures.ThreadPoolExecutor(8) as tp:
min_max = list(tp.map(_clims, tomograms))
cmin = min(int(mima[0]) for mima in min_max)
cmax = max(int(mima[1]) for mima in min_max)
clims = [1.1 * cmin, 0.9 * cmax]
with open(path, "w") as f:
json.dump(clims, f)
return clims
# refactor into mobie-utils function
def get_tomo_view(name, tomograms, table_data):
sources = {ii: [tomo] for ii, tomo in enumerate(tomograms)}
annotation_display = mobie.metadata.view_metadata.get_source_annotation_display(
name, sources, table_data, tables=["default.tsv"]
)
clims = get_clims(tomograms, name)
image_display = mobie.metadata.view_metadata.get_image_display(
name, tomograms, color="white",
blendingMode="sumOccluding",
contrastLimits=clims
)
view = {
"sourceDisplays": [
image_display,
annotation_display
],
"isExclusive": False,
"uiSelectionGroup": "bookmarks"
}
return view
def create_source_annotation_table(tomos, name, ds_folder):
annotation_ids = np.arange(len(tomos))
source_names = np.array(tomos)
table = np.concatenate([
annotation_ids[:, None], source_names[:, None]
], axis=1)
table = pd.DataFrame(table, columns=["annotation_id", "source"])
table_path = os.path.join(ds_folder, "tables", name, "default.tsv")
os.makedirs(os.path.join(ds_folder, "tables", name), exist_ok=True)
table.to_csv(table_path, sep="\t", index=False)
rel_path = f"tables/{name}"
return {
"tsv": {"relativePath": rel_path}
}
def add_tomo_view():
ds_folder = "./data/yeast"
metadata = mobie.metadata.read_dataset_metadata(ds_folder)
sources = metadata["sources"]
tomograms = [src for src in sources if "tomo" in src]
name = "hm_tomograms"
hm_tomograms = [tomo for tomo in tomograms if tomo.endswith("hm")]
table_data = create_source_annotation_table(hm_tomograms, name, ds_folder)
hm_view = get_tomo_view(name, hm_tomograms, table_data)
metadata["views"][name] = hm_view
name = "lm_tomograms"
lm_tomograms = [tomo for tomo in tomograms if tomo.endswith("lm")]
table_data = create_source_annotation_table(lm_tomograms, name, ds_folder)
lm_view = get_tomo_view(name, lm_tomograms, table_data)
metadata["views"][name] = lm_view
mobie.metadata.write_dataset_metadata(ds_folder, metadata)
mobie.validation.validate_project("./data")
if __name__ == '__main__':
add_tomo_view()