-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImportScan.py
More file actions
423 lines (332 loc) · 20.6 KB
/
ImportScan.py
File metadata and controls
423 lines (332 loc) · 20.6 KB
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
# Created 2016, Zack Gainsforth
import numpy as np
import h5py
import os
import sys
import skimage.external.tifffile as tifffile
import json
import BasicProcessing
import PhysicsBasics as pb
from PyQt5 import QtGui, QtCore
def WriteExampleMonoConfigFile():
"""Importing data into MultiLaue requires knowing all the beamline parameters and such. These can be written into
a text file (json format). This function just writes an example config file -- you probably want to make your
future files by hand with the data custom to the scan."""
MultiLaueVersion = {'Version': '1.0'}
ScanInfo = {'ScanName': 'GRA95229_15kev_4', 'ScanDate': '2016-04-28', 'xPixels': 14, 'yPixels': 12, 'zPixels': 1,
'ScanType': 'Mono', 'Energy': 15000, 'Synchrotron': 'ALS', 'BeamLine': '12.3.2', 'Detector': 'Pilatus'}
RawScanInfo = {'RawDataPath': '/Users/Zack/Desktop/20160428 - 12.3.2/003 GRA95229,30/007 GRA95229_15kev_4',
'FileNameFormat': 'GRA95229_15kev_4_%05d.tif', 'StartIndex': 1, 'EndIndex': 168}
Calibration = {'DetName': 'ALS12.3.2Pilatus', 'Energy': -1, 'xmm': 179.0, 'TwoThetaCenter': 75.0, 'xPixels': 1043,
'xCenter': 538.153, 'yPixels': 981, 'DetectorDistance': 162.498, 'yCenter': 230.63, 'ymm': 168.387,
'yaw': 0, 'pitch': 0, 'roll': 0}
Config = {'MultiLaueVersion': MultiLaueVersion, 'ScanInfo': ScanInfo, 'RawScanInfo': RawScanInfo, 'Calibration': Calibration}
ConfigStr = json.dumps(Config, indent=4)
with open('MonoScan.json', 'w') as f:
f.write(ConfigStr)
def WriteExampleLaueConfigFile():
"""Importing data into MultiLaue requires knowing all the beamline parameters and such. These can be written into
a text file (json format). This function just writes an example config file -- you probably want to make your
future files by hand with the data custom to the scan."""
MultiLaueVersion = {'Version': '1.0'}
ScanInfo = {'ScanName': 'GRA95229_Laue_6', 'ScanDate': '2016-04-28', 'xPixels': 26, 'yPixels': 24, 'zPixels': 1,
'ScanType': 'Laue', 'Energy': -1, 'Synchrotron': 'ALS', 'BeamLine': '12.3.2', 'Detector': 'Pilatus'}
RawScanInfo = {'RawDataPath': '/Users/Zack/Desktop/20160428 - 12.3.2/003 GRA95229,30/010 GRA95229_Laue_6',
'FileNameFormat': 'GRA95229_mLaue_6_%05d.tif', 'StartIndex': 1, 'EndIndex': 624}
Calibration = {'DetName': 'ALS12.3.2Pilatus', 'Energy': -1, 'xmm': 179.0, 'TwoThetaCenter': 75.0, 'xPixels': 1043,
'xCenter': 538.153, 'yPixels': 981, 'DetectorDistance': 162.498, 'yCenter': 230.63, 'ymm': 168.387,
'yaw': 0, 'pitch': 0, 'roll': 0}
Config = {'MultiLaueVersion': MultiLaueVersion, 'ScanInfo': ScanInfo, 'RawScanInfo': RawScanInfo, 'Calibration': Calibration}
ConfigStr = json.dumps(Config, indent=4)
with open('LaueScan.json', 'w') as f:
f.write(ConfigStr)
def WriteExampleMultiLaueConfigFile():
"""Importing data into MultiLaue requires knowing all the beamline parameters and such. These can be written into
a text file (json format). This function just writes an example config file -- you probably want to make your
future files by hand with the data custom to the scan."""
MultiLaueVersion = {'Version': '1.0'}
ScanInfo = {'ScanName': 'GRA95229_mLaue_7', 'ScanDate': '2016-04-28', 'xPixels': 8, 'yPixels': 7, 'zPixels': 4,
'ScanType': 'MultiLaue', 'Energy': -1, 'Synchrotron': 'ALS', 'BeamLine': '12.3.2', 'Detector': 'Pilatus'}
RawScanInfo = {'RawDataPath': '/Users/Zack/Desktop/20160428 - 12.3.2/003 GRA95229,30/011 GRA95229_mLaue_7',
'LineDirectoryFormat': 'GRA95229_mLaue7_%02d', 'FileNameFormat': 'GRA95229_mLaue7_%02d_%05d.tif'}
Calibration = {'DetName': 'ALS12.3.2Pilatus', 'Energy': -1, 'xmm': 179.0, 'TwoThetaCenter': 75.0, 'xPixels': 1043,
'xCenter': 538.153, 'yPixels': 981, 'DetectorDistance': 162.498, 'yCenter': 230.63, 'ymm': 168.387,
'yaw': 0, 'pitch': 0, 'roll': 0}
# Make the WtPcts array for the filters. They are all the same in this case.
WtPct = [0.0]*pb.MAXELEMENT
WtPct[pb.Si - 1] = 46.74
WtPct[pb.O - 1] = 53.26
FilterInfo = {'NumberOfPositions': 4,
'FilterThicknesses': [0, 112, 222, 334], # Microns
'FilterDensities': [0, 2.2, 2.2, 2.2], # g/cc
'FilterWtPcts': [WtPct, WtPct, WtPct, WtPct]
}
Config = {'MultiLaueVersion': MultiLaueVersion, 'ScanInfo': ScanInfo, 'RawScanInfo': RawScanInfo, 'Calibration': Calibration, 'FilterInfo': FilterInfo}
ConfigStr = json.dumps(Config, indent=4)
with open('MultiLaueScan.json', 'w') as f:
f.write(ConfigStr)
def ReadConfigFile(FileName):
"""Read config file with scan info etc."""
with open(FileName, 'r') as f:
ConfigStr = f.read()
Config = json.loads(ConfigStr)
# Do basic sanity checks.
assert Config['MultiLaueVersion']['Version'] == '1.0', 'MultiLaue only supports version 1.0 currently.'
assert Config['ScanInfo']['Synchrotron'] == 'ALS', 'MultiLaue only supports ALS beamline 12.3.2 with the Pilatus detector currently'
assert Config['ScanInfo']['BeamLine'] == '12.3.2', 'MultiLaue only supports ALS beamline 12.3.2 with the Pilatus detector currently'
assert Config['ScanInfo']['Detector'] == 'Pilatus', 'MultiLaue only supports ALS beamline 12.3.2 with the Pilatus detector currently'
return Config
def ImportScan(ConfigFile, WorkingDirectory=None, StatusFunc=None):
# Read in the info about the scan so we can import the data into HDF5.
Config = ReadConfigFile(os.path.join(WorkingDirectory, ConfigFile))
Config['WorkingDirectory'] = WorkingDirectory
Config['StatusFunc'] = StatusFunc
# Check what kind of scan this is.
if Config['ScanInfo']['ScanType'] == 'Mono':
return ImportMonoScan(Config)
elif Config['ScanInfo']['ScanType'] == 'Laue':
return ImportLaueScan(Config)
elif Config['ScanInfo']['ScanType'] == 'MultiLaue':
return ImportMultiLaueScan(Config)
else:
print(Config['ScanInfo']['ScanType'] + ' is an unsupported scan type.')
class ImportScanThread(QtCore.QThread):
StatusSignal = QtCore.pyqtSignal(object)
def __init__(self, ConfigFile, WorkingDirectory=None):
QtCore.QThread.__init__(self)
self.ConfigFile = ConfigFile
self.WorkingDirectory = WorkingDirectory
def run(self):
ImportScan(self.ConfigFile, self.WorkingDirectory, self.StatusFunc)
self.StatusFunc('Import Complete')
def StatusFunc(self, StatusStr):
if self.StatusSignal is not None:
self.StatusSignal.emit(StatusStr)
#self.emit(QtCore.SIGNAL(self.StatusSignal), StatusStr)
else:
print(StatusStr)
def ImportMonoScan(Config):
# First we just need the info about the raw data.
RawScanInfo = Config['RawScanInfo']
# Make the HDF file
h5FileName = os.path.join(Config['WorkingDirectory'], Config['ScanInfo']['ScanName'] + '.hdf5')
h5 = h5py.File(h5FileName, 'w')
# Version number goes on the root node.
h5.attrs['MultiLaueVersion'] = '1.0'
# All the data goes in the group "scan".
Scan = h5.create_group('Scan')
# The ScanInfo section of the config file is the attributes for the scan.
for k,v in list(Config['ScanInfo'].items()):
Scan.attrs[k] = v
# Add a calibration group.
Calibration = Scan.create_group('Calibration')
# The Calibration section of the config file is the attributes for the calibration.
for k,v in list(Config['Calibration'].items()):
Calibration.attrs[k] = v
# Make a dummy image to check if the image tranformation on import is going to change dimensions.
DummyImage = np.zeros((Config['Calibration']['xPixels'], Config['Calibration']['yPixels']))
DummyImage = TransformImageForDetector(DummyImage, Config['ScanInfo'])
# Now make the data cube. For Laue this is 4D: x,y stage, and x,y image.
CubeShape = (Config['ScanInfo']['xPixels'], Config['ScanInfo']['yPixels'], DummyImage.shape[0], DummyImage.shape[1])
# Note compression greatly speeds up the later processing since these stacks are mostly IO bound. (Factor of 4 compression is a 4X performance gain!)
Cube = Scan.create_dataset('DataCube', shape=CubeShape, dtype='float32', chunks=(1,1,200,200), compression='gzip')
# We also have a grid for the metadata stored in each tiff file.
dt = h5py.special_dtype(vlen=bytes)
MetadataCube = Scan.create_dataset('MetadataCube', shape=CubeShape[:2], dtype=dt)
# Now loop through all the images and stuff them in the data cube.
FilePath = Config['RawScanInfo']['RawDataPath']
FileNameFormat = Config['RawScanInfo']['FileNameFormat']
# For each pixel in the stage scan (x,y stage) we will insert a 2D image.
for y in range(Cube.shape[1]):
for x in range(Cube.shape[0]):
# The images are recorded by the beamline as a linear sequence. Get our index into that sequence.
i = y*Cube.shape[0] + x + Config['RawScanInfo']['StartIndex']
# If the scan was aborted early, then we won't have enough images to fill the cube.
if i > Config['RawScanInfo']['EndIndex']:
print('Number of images does not completely fill the data cube. Substituting zeros for remainder of cube.')
break
# Read this image in and populate the cube.
try:
with tifffile.TiffFile(os.path.join(FilePath, FileNameFormat % i)) as T:
StatusStr = "Loading image: x=%d, y=%d, Pixel # %d of %d" % (x, y, y * Cube.shape[0] + x + 1, Cube.shape[0] * Cube.shape[1])
if Config['StatusFunc'] is not None:
Config['StatusFunc'](StatusStr)
else:
print(StatusStr)
Cube[x, y, :, :] = TransformImageForDetector(T[0].asarray(), Config['ScanInfo'])
MetadataCube[x,y] = T[0].tags['image_description'].value
except IOError:
print(FileNameFormat % i + ' could not be found. Substituting zeros.')
# Now make the sum image (and log of the sum image) from the Cube.
Sum, SumLog = BasicProcessing.MakeSumImage(Scan, Config['StatusFunc'])
Scan.create_dataset('SumImage', data=Sum, dtype='float32')
Scan.create_dataset('SumLogImage', data=SumLog, dtype='float32')
# And a stdev image.
StDev, StDevLog = BasicProcessing.MakeStDevImage(Scan, Config['StatusFunc'])
Scan.create_dataset('StDevImage', data=StDev, dtype='float32')
Scan.create_dataset('StDevLogImage', data=StDevLog, dtype='float32')
h5.close()
def ImportLaueScan(Config):
# First we just need the info about the raw data.
RawScanInfo = Config['RawScanInfo']
# Make the HDF file
h5FileName = os.path.join(Config['WorkingDirectory'], Config['ScanInfo']['ScanName'] + '.hdf5')
h5 = h5py.File(h5FileName, 'w')
# Version number goes on the root node.
h5.attrs['MultiLaueVersion'] = '1.0'
# All the data goes in the group "scan".
Scan = h5.create_group('Scan')
# The ScanInfo section of the config file is the attributes for the scan.
for k,v in list(Config['ScanInfo'].items()):
Scan.attrs[k] = v
# Add a calibration group.
Calibration = Scan.create_group('Calibration')
# The Calibration section of the config file is the attributes for the calibration.
for k,v in list(Config['Calibration'].items()):
Calibration.attrs[k] = v
# Make a dummy image to check if the image tranformation on import is going to change dimensions.
DummyImage = np.zeros((Config['Calibration']['xPixels'], Config['Calibration']['yPixels']))
DummyImage = TransformImageForDetector(DummyImage, Config['ScanInfo'])
# Now make the data cube. For Laue this is 4D: x,y stage, and x,y image.
CubeShape = (Config['ScanInfo']['xPixels'], Config['ScanInfo']['yPixels'], DummyImage.shape[0], DummyImage.shape[1])
# Note compression greatly speeds up the later processing since these stacks are mostly IO bound. (Factor of 4 compression is a 4X performance gain!)
Cube = Scan.create_dataset('DataCube', shape=CubeShape, dtype='float32', chunks=(1,1,200,200), compression='gzip')
# We also have a grid for the metadata stored in each tiff file.
dt = h5py.special_dtype(vlen=bytes)
MetadataCube = Scan.create_dataset('MetadataCube', shape=CubeShape[:2], dtype=dt)
# Now loop through all the images and stuff them in the data cube.
FilePath = Config['RawScanInfo']['RawDataPath']
FileNameFormat = Config['RawScanInfo']['FileNameFormat']
# For each pixel in the stage scan (x,y stage) we will insert a 2D image.
for y in range(Cube.shape[1]):
for x in range(Cube.shape[0]):
# The images are recorded by the beamline as a linear sequence. Get our index into that sequence.
i = y*Cube.shape[0] + x + Config['RawScanInfo']['StartIndex']
# If the scan was aborted early, then we won't have enough images to fill the cube.
if i > Config['RawScanInfo']['EndIndex']:
print('Number of images does not completely fill the data cube. Substituting zeros for remainder of cube.')
break
# Read this image in and populate the cube.
try:
with tifffile.TiffFile(os.path.join(FilePath, FileNameFormat % i)) as T:
#print T[0].tags['image_description'].value
StatusStr = "Loading image: x=%d, y=%d, Pixel # %d of %d" % (x, y, y * Cube.shape[0] + x + 1, Cube.shape[0] * Cube.shape[1])
if Config['StatusFunc'] is not None:
Config['StatusFunc'](StatusStr)
else:
print(StatusStr)
Cube[x, y, :, :] = TransformImageForDetector(T[0].asarray(), Config['ScanInfo'])
MetadataCube[x,y] = T[0].tags['image_description'].value
except IOError:
print(FileNameFormat % i + ' could not be found. Substituting zeros.')
# Now make the sum image (and log of the sum image) from the Cube.
Sum, SumLog = BasicProcessing.MakeSumImage(Scan, Config['StatusFunc'])
Scan.create_dataset('SumImage', data=Sum, dtype='float32')
Scan.create_dataset('SumLogImage', data=SumLog, dtype='float32')
# And a stdev image.
StDev, StDevLog = BasicProcessing.MakeStDevImage(Scan, Config['StatusFunc'])
Scan.create_dataset('StDevImage', data=StDev, dtype='float32')
Scan.create_dataset('StDevLogImage', data=StDevLog, dtype='float32')
h5.close()
def ImportMultiLaueScan(Config):
# Make the HDF file
h5FileName = os.path.join(Config['WorkingDirectory'], Config['ScanInfo']['ScanName'] + '.hdf5')
h5 = h5py.File(h5FileName, 'w')
# Version number goes on the root node.
h5.attrs['MultiLaueVersion'] = '1.0'
# All the data goes in the group "scan".
Scan = h5.create_group('Scan')
# The ScanInfo section of the config file is the attributes for the scan.
for k,v in list(Config['ScanInfo'].items()):
Scan.attrs[k] = v
# Add a calibration group.
Calibration = Scan.create_group('Calibration')
# The Calibration section of the config file is the attributes for the calibration.
for k,v in list(Config['Calibration'].items()):
Calibration.attrs[k] = v
# Add the filter group (MultiLaue only).
Filter = Scan.create_group('Filter')
for k,v in list(Config['FilterInfo'].items()):
if k == 'FilterWtPcts':
WtPcts = np.array(v)
FilterWtPcts = Filter.create_dataset('FilterWtPcts', data=WtPcts)
else:
Filter.attrs[k] = v
# Make a dummy image to check if the image tranformation on import is going to change dimensions.
DummyImage = np.zeros((Config['Calibration']['xPixels'], Config['Calibration']['yPixels']))
DummyImage = TransformImageForDetector(DummyImage, Config['ScanInfo'])
# Now make the data cube. For MultiLaue this is 5D: x,y stage, x,y image by # filters.
CubeShape = (Config['ScanInfo']['xPixels'], Config['ScanInfo']['yPixels'], DummyImage.shape[0], DummyImage.shape[1],
Config['FilterInfo']['NumberOfPositions'])
# Note compression greatly speeds up the later processing since these stacks are mostly IO bound. (Factor of 4 compression is a 4X performance gain!)
Cube = Scan.create_dataset('DataCube', shape=CubeShape, dtype='float32', chunks=(1,1,200,200,Config['FilterInfo']['NumberOfPositions']), compression='gzip')#, compression_opts=7) # Compression 7 will improve by 10% but take waaay longer for the first import.
# We also have a grid for the metadata stored in each tiff file.
# The metadata has stage pixels, and filter pixels, so 3D.
dt = h5py.special_dtype(vlen=bytes)
MetadataCube = Scan.create_dataset('MetadataCube', shape=np.array(CubeShape)[[0,1,-1]], dtype=dt)
# Now loop through all the images and stuff them in the data cube.
FilePath = Config['RawScanInfo']['RawDataPath']
LineDirectoryFormat = Config['RawScanInfo']['LineDirectoryFormat']
FileNameFormat = Config['RawScanInfo']['FileNameFormat']
FilterPositions = Config['FilterInfo']['NumberOfPositions']
# Compute how many images total.
NumImages = np.array(Cube.shape)[[0,1,-1]].prod()
n = 1 # Starting with the first image.
# For each pixel in the stage scan (x,y stage) we will insert a 2D image.
for y in range(Cube.shape[1]):
for x in range(Cube.shape[0]):
for f in range(Cube.shape[-1]):
# Read this image in and populate the cube.
# Each line of the scan is stored in a subdir with the name LineDirectoryFormat.
# Compute the index of this image. It is the x coordinate x the filter (x1,f1, x1,f2, x2,f1, x2,f2, x3,f1 ...)
i = x*FilterPositions + f
if Cube.shape[1] == Cube.shape[0] == 1:
# Single pixel scans have a different directory structure since we don't need each line in a separate directory.
FullPath = os.path.join(FilePath, LineDirectoryFormat, FileNameFormat%(i+1))
else:
FullPath = os.path.join(FilePath, LineDirectoryFormat%(y+1), FileNameFormat % (y+1, i+1))
try:
with tifffile.TiffFile(FullPath) as T:
StatusStr = "Loading image: x=%d, y=%d, f=%d, Frame # %d of %d" % (x, y, f, n, NumImages)
if Config['StatusFunc'] is not None:
Config['StatusFunc'](StatusStr)
else:
print(StatusStr)
Cube[x, y, :, :, f] = TransformImageForDetector(T[0].asarray(), Config['ScanInfo'])
MetadataCube[x, y, f] = T[0].tags['image_description'].value
except IOError:
print(FileNameFormat % (y+1,i+1) + ' could not be found. Substituting zeros.')
# Increment the frame count.
n += 1
# Now make the sum image (and log of the sum image) from the Cube.
Sum, SumLog = BasicProcessing.MakeSumImage(Scan, Config['StatusFunc'])
Scan.create_dataset('SumImage', data=Sum, dtype='float32')
Scan.create_dataset('SumLogImage', data=SumLog, dtype='float32')
# And a stdev image.
StDev, StDevLog = BasicProcessing.MakeStDevImage(Scan, Config['StatusFunc'])
Scan.create_dataset('StDevImage', data=StDev, dtype='float32')
Scan.create_dataset('StDevLogImage', data=StDevLog, dtype='float32')
h5.close()
def TransformImageForDetector(ImageData, ScanInfo):
if (ScanInfo['Synchrotron'] == 'ALS') and (ScanInfo['BeamLine'] == '12.3.2') and (ScanInfo['Detector'] == 'Pilatus'):
# Get the numpy array from the HDF5 object, rotate it for the correct orientation on the screen, get rids of nans,
# and change any negative infinities to zero (happens in the log images).
NewImageData = np.rot90(ImageData[:], 3)
NewImageData[np.isinf(NewImageData)] = 0
NewImageData = np.nan_to_num(NewImageData)
return NewImageData
else:
assert False, 'Unrecognized Synchrotron, Beamline, Detector combination.'
if __name__ == '__main__':
# # Just an example to get started.
WriteExampleMonoConfigFile()
# # Read the scan into an HDF5 file.
# ImportScan('MonoScan.json')
# # Just an example to get started.
WriteExampleLaueConfigFile()
# # Read the scan into an HDF5 file.
# ImportScan('LaueScan.json')
# Just an example to get started.
WriteExampleMultiLaueConfigFile()
# Read the scan into an HDF5 file.
#ImportScan('MultiLaueScan.json')