-
Notifications
You must be signed in to change notification settings - Fork 5
/
__init__.py
331 lines (258 loc) · 12.3 KB
/
__init__.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
from collections import defaultdict
from pathlib import Path
import numpy
from tifffile import TiffWriter
import event_model
import suitcase.utils
from ._version import get_versions
__version__ = get_versions()['version']
del get_versions
def export(gen, directory, file_prefix='{start[uid]}-', astype='uint16',
bigtiff=False, byteorder=None, imagej=False, **kwargs):
"""
Export a stream of documents to TIFF stack(s).
This creates a file named:
``<directory>/<file_prefix>{stream_name}-{field}.tiff``
for every Event stream and field that contains 2D 'image like' data.
.. warning::
This process explicitly ignores all data that is not 2D and does not
include any metadata in the output file.
.. note::
This can alternatively be used to write data to generic buffers rather
than creating files on disk. See the documentation for the
``directory`` parameter below.
Parameters
----------
gen : generator
expected to yield ``(name, document)`` pairs
directory : string, Path or Manager.
For basic uses, this should be the path to the output directory given
as a string or Path object. Use an empty string ``''`` to place files
in the current working directory.
In advanced applications, this may direct the serialized output to a
memory buffer, network socket, or other writable buffer. It should be
an instance of ``suitcase.utils.MemoryBufferManager`` and
``suitcase.utils.MultiFileManager`` or any object implementing that
interface. See the suitcase documentation
(http://nsls-ii.github.io/suitcase) for details.
file_prefix : str, optional
The first part of the filename of the generated output files. This
string may include templates as in
``{start[proposal_id]}-{start[sample_name]}-``,
which are populated from the RunStart document. The default value is
``{start[uid]}-`` which is guaranteed to be present and unique. A more
descriptive value depends on the application and is therefore left to
the user.
Two additional template parameters ``{stream_name}`` and ``{field}``
are supported. These will be replaced with stream name and detector
name, respectively.
astype : numpy dtype
The image array is converted to this type before being passed to
tifffile. The default is 16-bit integer (``'uint16'``) since many image
viewers cannot open higher bit depths. This parameter may be given as a
numpy dtype object (``numpy.uint32``) or the equivalent string
(``'uint32'``).
bigtiff : boolean, optional
Passed into ``tifffile.TiffWriter``. Default False.
byteorder : string or None, optional
Passed into ``tifffile.TiffWriter``. Default None.
imagej: boolean, optional
Passed into ``tifffile.TiffWriter``. Default False.
**kwargs : kwargs
kwargs to be passed to ``tifffile.TiffWriter.write``.
Returns
-------
artifacts : dict
Maps 'labels' to lists of artifacts (e.g. filepaths)
Examples
--------
Generate files with unique-identifier names in the current directory.
>>> export(gen, '')
Generate files with more readable metadata in the file names.
>>> export(gen, '', '{start[plan_name]}-{start[motors]}-')
Include the experiment's start time formatted as YY-MM-DD_HH-MM.
>>> export(gen, '', '{start[time]:%Y-%m-%d_%H:%M}-')
Place the files in a different directory, such as on a mounted USB stick.
>>> export(gen, '/path/to/my_usb_stick')
"""
with Serializer(directory, file_prefix,
astype=astype,
bigtiff=bigtiff,
byteorder=byteorder,
imagej=imagej,
**kwargs) as serializer:
for item in gen:
serializer(*item)
return serializer.artifacts
class Serializer(event_model.DocumentRouter):
"""
Serialize a stream of documents to TIFF stack(s).
This creates a file named:
``<directory>/<file_prefix>{stream_name}-{field}.tiff``
for every Event stream and field that contains 2D 'image like' data.
.. warning::
This process explicitly ignores all data that is not 2D and does not
include any metadata in the output file.
.. note::
This can alternatively be used to write data to generic buffers rather
than creating files on disk. See the documentation for the
``directory`` parameter below.
Parameters
----------
directory : string, Path or Manager.
For basic uses, this should be the path to the output directory given
as a string or Path object. Use an empty string ``''`` to place files
in the current working directory.
In advanced applications, this may direct the serialized output to a
memory buffer, network socket, or other writable buffer. It should be
an instance of ``suitcase.utils.MemoryBufferManager`` and
``suitcase.utils.MultiFileManager`` or any object implementing that
interface. See the suitcase documentation
(http://nsls-ii.github.io/suitcase) for details.
file_prefix : str, optional
The first part of the filename of the generated output files. This
string may include templates as in
``{start[proposal_id]}-{start[sample_name]}-``,
which are populated from the RunStart document. The default value is
``{start[uid]}-`` which is guaranteed to be present and unique. A more
descriptive value depends on the application and is therefore left to
the user.
Two additional template parameters ``{stream_name}`` and ``{field}``
are supported. These will be replaced with stream name and detector
name, respectively.
astype : numpy dtype
The image array is converted to this type before being passed to
tifffile. The default is 16-bit integer (``'uint16'``) since many image
viewers cannot open higher bit depths. This parameter may be given as a
numpy dtype object (``numpy.uint32``) or the equivalent string
(``'uint32'``).
bigtiff : boolean, optional
Passed into ``tifffile.TiffWriter``. Default False.
byteorder : string or None, optional
Passed into ``tifffile.TiffWriter``. Default None.
imagej: boolean, optional
Passed into ``tifffile.TiffWriter``. Default False.
**kwargs : kwargs
kwargs to be passed to ``tifffile.TiffWriter.write``.
"""
def __init__(self, directory, file_prefix='{start[uid]}-', astype='uint16',
bigtiff=False, byteorder=None, imagej=False, **kwargs):
if isinstance(directory, (str, Path)):
self._manager = suitcase.utils.MultiFileManager(directory)
else:
self._manager = directory
# Map stream name to dict that maps field names to TiffWriter objects.
self._tiff_writers = defaultdict(dict)
self._file_prefix = file_prefix
self._astype = astype # convert numpy array dtype before tifffile
self._init_kwargs = {'bigtiff': bigtiff, 'byteorder': byteorder,
'imagej': imagej} # passed to TiffWriter()
self._kwargs = kwargs # passed to TiffWriter.write()
self._start = None # holds the start document information
self._descriptors = {} # maps the descriptor uids to descriptor docs.
@property
def artifacts(self):
# The manager's artifacts attribute is itself a property, and we must
# access it a new each time to be sure to get the latest content.
return self._manager.artifacts
def start(self, doc):
'''Extracts `start` document information for formatting file_prefix.
This method checks that only one `start` document is seen and formats
`file_prefix` based on the contents of the `start` document.
Parameters:
-----------
doc : dict
RunStart document
'''
# raise an error if this is the second `start` document seen.
if self._start:
raise RuntimeError(
"The serializer in suitcase.tiff expects documents from one "
"run only. Two `start` documents where sent to it")
else:
self._start = doc # record the start doc for later use
def descriptor(self, doc):
'''Use `descriptor` doc to map stream_names to descriptor uid's.
This method uses the descriptor document information to map the
stream_names to descriptor uid's.
Parameters:
-----------
doc : dict
EventDescriptor document
'''
# record the doc for later use
self._descriptors[doc['uid']] = doc
def event_page(self, doc):
'''Add event page document information to a ".tiff" file.
This method adds event_page document information to a ".tiff" file,
creating it if nesecary.
.. warning::
All non 2D 'image like' data is explicitly ignored.
.. note::
The data in Events might be structured as an Event, an EventPage,
or a "bulk event" (deprecated). The DocumentRouter base class takes
care of first transforming the other representations into an
EventPage and then routing them through here, so no further action
is required in this class. We can assume we will always receive an
EventPage.
Parameters:
-----------
doc : dict
EventPage document
'''
event_model.verify_filled(doc)
descriptor = self._descriptors[doc['descriptor']]
stream_name = descriptor.get('name')
for field in doc['data']:
for img in doc['data'][field]:
# Check that the data is 2D or 3D; if not ignore it.
data_key = descriptor['data_keys'][field]
ndim = len(data_key['shape'] or [])
if data_key['dtype'] == 'array' and 1 < ndim < 4:
# there is data to be written so
# create a file for this stream and field
# if one does not exist yet
if not self._tiff_writers.get(stream_name, {}).get(field):
filename = get_prefixed_filename(
file_prefix=self._file_prefix,
start_doc=self._start,
stream_name=stream_name,
field=field
)
fname = self._manager.reserve_name('stream_data', filename)
Path(fname).parent.mkdir(parents=True, exist_ok=True)
tw = TiffWriter(fname, **self._init_kwargs)
self._tiff_writers[stream_name][field] = tw
# write the data
img_asarray = numpy.asarray(img, dtype=self._astype)
if ndim == 2:
# handle 2D data just like 3D data
# by adding a 3rd dimension
img_asarray = numpy.expand_dims(img_asarray, axis=0)
for i in range(img_asarray.shape[0]):
img_asarray_2d = img_asarray[i, :]
# append the image to the file
tw = self._tiff_writers[stream_name][field]
tw.write(img_asarray_2d, contiguous=True, *self._kwargs)
def stop(self, doc):
self.close()
def close(self):
'''Close all of the files opened by this Serializer.
'''
# Close all the TiffWriter instances, which do some work on cleanup.
for tw_by_stream in self._tiff_writers.values():
for tw in tw_by_stream.values():
tw.close()
# Then let the manager (perhaps redundantly) close the underlying
# files.
self._manager.close()
def __enter__(self):
return self
def __exit__(self, *exception_details):
self.close()
def get_prefixed_filename(file_prefix, start_doc, stream_name, field):
'''Assemble the prefixed filename.'''
templated_file_prefix = file_prefix.format(
start=start_doc, field=field, stream_name=stream_name)
filename = f'{templated_file_prefix}{stream_name}-{field}.tiff'
return filename