-
Notifications
You must be signed in to change notification settings - Fork 4
/
masks.py
245 lines (186 loc) · 6.73 KB
/
masks.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
import numpy as np
import tiled
from functools import reduce
from numpy import dtype
from pathlib import Path
from tiled.queries import Key
DETECTOR_SHAPES = {'eiger4m_single_image': (2167, 2070),
'eiger1m_single_image': (1065, 1030),
'eiger500K_single_image': (514, 1030)}
class MaskNotFound(Exception):
pass
class MaskNotUnique(Exception):
pass
def load_array_from_file(filename):
"""
This function reads in pixel mask files and
outputs a 1d or 2d numpy array object
"""
return np.load(filename)
def list_to_mask(pixel_list, shape=(2167, 2070)):
"""
This function accepts a 1d array and returns a
2d mask object
"""
pixel_mask = np.ones(shape=shape)
pixel_mask.ravel()[pixel_list] = 0
return pixel_mask.astype(bool)
def combine_masks(masks):
"""
Get a composite mask from tiled.
This does not support version specification.
This combines all masks in the list masks and returns a single mask.
Parameters
----------
masks: list
list of masks
Returns
-------
mask: DaskArray
"""
return reduce(lambda x, y: x & y, masks)
class MaskClient:
"""
MaskClient is a client for a tiled mask database.
- Each registered Mask has a unique identifier (uid).
- Arbitrary metadata can be registered along with the Mask.
- We can search for Masks by detector, name, or other metadata.
- A Mask is a NumPy array of Bools, that matches the detector shape,
where False represents the values to be masked.
- detector_name + mask_name + version is unique.
- When you get a Mask from the registry it's type is a DaskArray,
this is to support laziness and parallelization. To get the NumPy
Array call compute() on it.
"""
def __init__(self, tiled_client):
self._tiled_client = tiled_client
@classmethod
def from_profile(cls, *args, **kwargs):
return cls(tiled.client.from_profile(*args, **kwargs))
def register_mask(self, detector, name, mask, version=0, optional_metadata={}):
"""
Save a mask into tiled.
Parameters
----------
name: string
detector: string
The name of the detector that the mask is for.
mask: numpy.ndarray
version: integer, optional
Verion information
Returns
-------
node: tiled.Node
"""
if not isinstance(mask, np.ndarray):
raise ValueError("The mask must be a numpy.ndarray.")
if mask.shape != DETECTOR_SHAPES[detector]:
raise ValueError("Mask shape {mask.shape} does not match the detector"
" shape {DETECTOR_SHAPES[detector].")
if mask.dtype != dtype('bool'):
raise ValueError("Mask dtype {mask.dtype} must be dtype('bool').")
metadata = {"spec": "mask",
"name": name,
'detector': detector,
'version': version}
metadata.update(optional_metadata)
# Make sure the mask doesn't already exist.
masks = self._tiled_client.search(Key("spec") == "mask") \
.search(Key("name") == name) \
.search(Key("detector") == detector) \
.search(Key("version") == version)
if len(masks):
raise RuntimeError("A mask with this name and detector already exists.")
result = self._tiled_client.write_array(
mask,
metadata=metadata,
)
return result
def get_mask(self, detector, name, version=None):
"""
Get a mask from tiled.
Parameters
----------
detector: string
name: string
version: integer, None
A None value here will return the highest version.
Returns
-------
mask_uid: str
mask: DaskArray
"""
results = self._tiled_client.search(Key("spec") == "mask") \
.search(Key("name") == name) \
.search(Key('detector') == detector)
if version is None:
if len(results) == 0:
raise MaskNotFound(f"detector={detector}, name={name}, version={version}")
uid, mask = max(results.items(),
key=lambda item: item[1].metadata['version'])
else:
results = results.search(Key('version') == version)
if len(results) == 0:
raise MaskNotFound(f"detector={detector}, name={name}, version={version}")
if len(results) > 1:
raise MaskNotUnique(f"{len(results)} masks found that matched detector={detector}, name={name}, version={version}")
uid, mask = list(results.items())[0]
return uid, mask.read()
def get_mask_by_uid(self, uid):
"""
Get a mask from tiled.
nod
Parameters
----------
uid: string
Returns
-------
mask: DaskArray
"""
return self._tiled_client[uid].read()
def get_mask_uid(self, detector, name, version):
"""
Get a mask_uid from tiled.
Parameters
----------
detector: string
name: string
version: int
Returns
-------
uid: string
"""
results = self._tiled_client.search(Key("spec") == "mask") \
.search(Key("name") == name) \
.search(Key('detector') == detector) \
.search(Key('version') == version)
assert len(results) == 1
return list(results)[0]
def delete_mask(self, detector, name, version):
"""
Delete a mask from tiled.
Parameters
----------
detector: string
name: string
version: int
"""
results = self._tiled_client.search(Key("spec") == "mask") \
.search(Key("name") == name) \
.search(Key('detector') == detector) \
.search(Key('version') == version)
uids = list(results)
for uid in uids:
del self._tiled_client[uid]
def list_masks(self):
"""
Get a list of the available masks.
Returns
-------
mask_details: list
"""
results = self._tiled_client.search(Key("spec") == "mask")
return [(node.metadata.get('detector', 'any'),
node.metadata["name"],
node.metadata.get("version"))
for node in results.values()]