-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.py
124 lines (99 loc) · 3.25 KB
/
util.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
"""
utility functions to handle MoBIE projects in napari
"""
import os
import requests
class MoBIEState(object):
"""
Necessary metadata to actively work within a MoBIE project.
"""
def __init__(self):
self.project_root = None
self.datasets = []
self.dataset = None
self.imported_dataset = None
self.ds_name = ''
self.views = []
self.sources = []
self.view = {}
self.allviews = dict()
self.view_groups = []
self.displays = []
self.display = {}
def to_napari_layer_metadata(self):
"""
Generates relevant MoBIE metadata for serialized storage in a napari layer.
Returns
-------
dict
The metadata dictionary under the key "MoBIE".
"""
return {"MoBIE": {
"project_root": self.project_root,
"dataset": self.imported_dataset,
"ds_name": self.ds_name,
"view": self.view,
"display": self.display
}
}
def update_napari_image_layer(self, layer):
if 'imageDisplay' in self.display.keys():
disp = self.display['imageDisplay']
if disp['contrastLimits'] not in ([0, 255], [0, 65535]):
layer.contrast_limits = disp['contrastLimits']
layer.opacity = disp['opacity']
layer.visible = disp['visible']
def is_mobie_project(path):
"""
Checks if a given path contains a MoBIE project. Can be at top level or in the `data` sub-folder.
Parameters
----------
path : os.PathLike The path to check.
Returns
-------
(bool, str)
True if a MoBIE project structure is found and the complete path to the MoBIE project or if not: (False, '').
"""
if os.path.isdir(path):
if 'project.json' in os.listdir(path):
return True, path
elif os.path.isdir(os.path.join(path, 'data')):
if 'project.json' in os.listdir(os.path.join(path, 'data')):
return True, os.path.join(path, 'data')
return False, ''
def s3link(indict):
"""
Parameters
----------
indict : dict The MoBIE definitions dictionary of an S3 source.
Returns
-------
str
The full URL to the source for an S3 reader to open.
"""
url = indict['s3Address']
return url
def check_image_source(src_type, im_metadata, ds_path):
"""
Parameters
----------
src_type : str Source type specifier
im_metadata : dict image source dictionary defining the links to original data
ds_path : str full path of the dataset (local file system).
Returns
-------
(str, None)
Path to the image that napari reader(s) can open or None if not possible.
"""
if src_type not in im_metadata.keys():
return None
if src_type == 'ome.zarr.s3':
if requests.get(s3link(im_metadata[src_type]) + '/.zattrs').ok:
return s3link(im_metadata[src_type])
elif src_type == 'ome.zarr':
zpath = os.path.join(ds_path,
im_metadata[src_type]['relativePath'].lstrip('./').replace('/', os.pathsep))
if os.path.exists(zpath) and os.path.exists(os.path.join(zpath, '.zattrs')):
return zpath
else:
return None