-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathFormatTIFFgeneric.py
More file actions
974 lines (773 loc) · 30.8 KB
/
FormatTIFFgeneric.py
File metadata and controls
974 lines (773 loc) · 30.8 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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
"""A format class for generic TIFF images plus implementations for specific
detectors producing electron diffraction data"""
from __future__ import annotations
import os
import re
import warnings
from dxtbx import flumpy
from dxtbx.format.Format import Format
from dxtbx.format.FormatStill import FormatStill
from dxtbx.masking import mask_untrusted_rectangle
from dxtbx.model.beam import Probe
from dxtbx.model.detector import Detector
from scitbx.array_family import flex
try:
import tifffile
except ImportError:
tifffile = None
def check_environment_variable(cls):
"""Utility function to determine whether an expected environment variable
has been set to activate the use of a particular plugin class. If not, but
this function has been called, warn that the format class is not activated.
"""
name = cls.__name__
var = cls.check_environment
if os.getenv(var) is None:
warnings.warn(
f"To use the the Format plugin {name} to read this image,"
f"the environment variable {var} must be set"
)
return False
return True
class FormatTIFFgeneric(Format):
"""General-purpose TIFF image reader using tifffile. This will clash with
the dxtbx FormatTIFF tree for Rigaku/Bruker TIFFs."""
@staticmethod
def understand(image_file):
"""Check to see if this looks like a TIFF format image with a single page"""
if not tifffile:
print(
"FormatTIFFgeneric is installed but the required library tifffile is not available"
)
return False
try:
tif = tifffile.TiffFile(image_file)
except tifffile.TiffFileError:
return False
try:
assert len(tif.pages) == 1
assert len(tif.series) == 1
except (AssertionError, KeyError):
return False
finally:
tif.close()
return True
def get_raw_data(self):
"""Get the pixel intensities"""
raw_data = tifffile.imread(self._image_file)
return flumpy.from_numpy(raw_data.astype(float))
def _scan(self):
"""Dummy scan for this image"""
fname = os.path.split(self._image_file)[-1]
# assume that the final number before the extension is the image number
s = fname.split("_")[-1].split(".")[0]
try:
index = int(re.match(".*?([0-9]+)$", s).group(1))
except AttributeError:
index = 1
exposure_times = 0.0
frame = index - 1
# Dummy scan with a 0.5 deg image
oscillation = (frame * 0.5, 0.5)
epochs = [0]
return self._scan_factory.make_scan(
(index, index), exposure_times, oscillation, epochs, deg=True
)
class FormatTIFFgeneric_Merlin(FormatTIFFgeneric):
"""An experimental image reading class for TIFF images from a Quantum
Detectors Merlin detector. We have limited information about the data format
at present.
The header does not contain useful information about the geometry, therefore
we will construct dummy objects and expect to override on import using
site.phil.
WARNING: this format is not very specific so an environment variable,
QD_MERLIN_TIFF, must be set, otherwise this will pick up *any* TIFF file
containing a single 512x512 pixel image.
"""
check_environment = "QD_MERLIN_TIFF"
@classmethod
def understand(cls, image_file):
with tifffile.TiffFile(image_file) as tif:
page = tif.pages[0]
if page.shape != (512, 512):
return False
return check_environment_variable(cls)
def _goniometer(self):
"""Dummy goniometer, 'vertical' as the images are viewed. Not completely
sure about the handedness yet"""
return self._goniometer_factory.known_axis((0, 1, 0))
def _beam(self):
"""Dummy beam, energy 200 keV"""
wavelength = 0.02508
return self._beam_factory.make_polarized_beam(
sample_to_source=(0.0, 0.0, 1.0),
wavelength=wavelength,
polarization=(0, 1, 0),
polarization_fraction=0.5,
probe=Probe.electron,
)
def _detector(self):
"""Dummy detector"""
pixel_size = 0.055, 0.055
image_size = (512, 512)
dyn_range = 12
trusted_range = (0, 2**dyn_range - 1)
beam_centre = [(p * i) / 2 for p, i in zip(pixel_size, image_size)]
d = self._detector_factory.simple(
"PAD", 2440, beam_centre, "+x", "-y", pixel_size, image_size, trusted_range
)
return d
class FormatTIFFgeneric_Timepix512(FormatTIFFgeneric):
"""An experimental image reading class for TIFF images from a Timepix
detectors with 512x512 pixels where the central cross is excluded and the
image is separated into 4 panels.
WARNING: this format is not very specific so an environment variable,
TIMEPIX512_TIFF, must be set, otherwise this will pick up *any* TIFF file
containing a single 512x512 pixel image.
"""
check_environment = "TIMEPIX512_TIFF"
@classmethod
def understand(cls, image_file):
with tifffile.TiffFile(image_file) as tif:
page = tif.pages[0]
if page.shape != (512, 512):
return False
return check_environment_variable(cls)
def _detector(self):
"""Dummy detector"""
from scitbx import matrix
# 55 mu pixels
pixel_size = 0.055, 0.055
trusted_range = (-1, 65535)
thickness = 0.3 # assume 300 mu thick
# Initialise detector frame - dummy origin to place detector at the header
# distance along the canonical beam direction. Dummy distance
fast = matrix.col((1.0, 0.0, 0.0))
slow = matrix.col((0.0, -1.0, 0.0))
cntr = matrix.col((0.0, 0.0, -100.0))
# shifts to go from the centre to the origin - outer pixels are 0.165 mm
self._array_size = (512, 512)
off_x = (self._array_size[0] / 2 - 2) * pixel_size[0]
off_x += 2 * 0.165
shift_x = -1.0 * fast * off_x
off_y = (self._array_size[1] / 2 - 2) * pixel_size[1]
off_y += 2 * 0.165
shift_y = -1.0 * slow * off_y
orig = cntr + shift_x + shift_y
d = Detector()
root = d.hierarchy()
root.set_local_frame(fast.elems, slow.elems, orig.elems)
self.coords = {}
panel_idx = 0
# set panel extent in pixel numbers and x, y mm shifts. Note that the
# outer pixels are 0.165 mm in size. These are excluded from the panel
# extents.
pnl_data = []
pnl_data.append(
{
"xmin": 1,
"ymin": 1,
"xmax": 255,
"ymax": 255,
"xmin_mm": 1 * 0.165,
"ymin_mm": 1 * 0.165,
}
)
pnl_data.append(
{
"xmin": 257,
"ymin": 1,
"xmax": 511,
"ymax": 255,
"xmin_mm": 3 * 0.165 + (511 - 257) * pixel_size[0],
"ymin_mm": 1 * 0.165,
}
)
pnl_data.append(
{
"xmin": 1,
"ymin": 257,
"xmax": 255,
"ymax": 511,
"xmin_mm": 1 * 0.165,
"ymin_mm": 3 * 0.165 + (511 - 257) * pixel_size[1],
}
)
pnl_data.append(
{
"xmin": 257,
"ymin": 257,
"xmax": 511,
"ymax": 511,
"xmin_mm": 3 * 0.165 + (511 - 257) * pixel_size[0],
"ymin_mm": 3 * 0.165 + (511 - 257) * pixel_size[1],
}
)
# redefine fast, slow for the local frame
fast = matrix.col((1.0, 0.0, 0.0))
slow = matrix.col((0.0, 1.0, 0.0))
for ipanel, pd in enumerate(pnl_data):
xmin = pd["xmin"]
xmax = pd["xmax"]
ymin = pd["ymin"]
ymax = pd["ymax"]
xmin_mm = pd["xmin_mm"]
ymin_mm = pd["ymin_mm"]
origin_panel = fast * xmin_mm + slow * ymin_mm
panel_name = "Panel%d" % panel_idx
panel_idx += 1
p = d.add_panel()
p.set_type("SENSOR_PAD")
p.set_name(panel_name)
p.set_raw_image_offset((xmin, ymin))
p.set_image_size((xmax - xmin, ymax - ymin))
p.set_trusted_range(trusted_range)
p.set_pixel_size((pixel_size[0], pixel_size[1]))
p.set_thickness(thickness)
p.set_material("Si")
# p.set_mu(mu)
# p.set_px_mm_strategy(ParallaxCorrectedPxMmStrategy(mu, t0))
p.set_local_frame(fast.elems, slow.elems, origin_panel.elems)
p.set_raw_image_offset((xmin, ymin))
self.coords[panel_name] = (xmin, ymin, xmax, ymax)
return d
def _goniometer(self):
"""Dummy goniometer, 'vertical' as the images are viewed. Not completely
sure about the handedness yet"""
return self._goniometer_factory.known_axis((0, 1, 0))
def _beam(self):
"""Dummy beam, energy 200 keV"""
wavelength = 0.02508
return self._beam_factory.make_polarized_beam(
sample_to_source=(0.0, 0.0, 1.0),
wavelength=wavelength,
polarization=(0, 1, 0),
polarization_fraction=0.5,
probe=Probe.electron,
)
def get_raw_data(self):
raw_data = tifffile.imread(self._image_file)
raw_data = flumpy.from_numpy(raw_data.astype(float))
raw_data.reshape(flex.grid(self._array_size[1], self._array_size[0]))
self._raw_data = []
d = self.get_detector()
for panel in d:
xmin, ymin, xmax, ymax = self.coords[panel.get_name()]
self._raw_data.append(raw_data[ymin:ymax, xmin:xmax])
return tuple(self._raw_data)
class FormatTIFFgeneric_Timepix516(FormatTIFFgeneric):
"""An experimental image reading class for TIFF images from a Timepix
detectors with 516x516 pixels where the central cross is masked out.
WARNING: this format is not very specific so an environment variable,
TIMEPIX516_TIFF, must be set, otherwise this will pick up *any* TIFF file
containing a single 516x516 pixel image.
"""
check_environment = "TIMEPIX516_TIFF"
@classmethod
def understand(cls, image_file):
"""Check to see if this looks like a TIFF format image with a single page"""
with tifffile.TiffFile(image_file) as tif:
page = tif.pages[0]
if page.shape != (516, 516):
return False
return check_environment_variable(cls)
def get_static_mask(self):
"""Return the static mask that excludes the central cross of pixels."""
mask = flex.bool(flex.grid((516, 516)), True)
mask_untrusted_rectangle(mask, 0, 516, 255, 261)
mask_untrusted_rectangle(mask, 255, 261, 0, 516)
return (mask,)
def _goniometer(self):
"""Dummy goniometer, 'vertical' as the images are viewed. Not completely
sure about the handedness yet"""
return self._goniometer_factory.known_axis((0, 1, 0))
def _beam(self):
"""Dummy beam, energy 200 keV"""
wavelength = 0.02508
return self._beam_factory.make_polarized_beam(
sample_to_source=(0.0, 0.0, 1.0),
wavelength=wavelength,
polarization=(0, 1, 0),
polarization_fraction=0.5,
probe=Probe.electron,
)
def _detector(self):
"""Dummy detector"""
pixel_size = 0.055, 0.055
image_size = (516, 516)
dyn_range = 12
trusted_range = (0, 2**dyn_range - 1)
beam_centre = [(p * i) / 2 for p, i in zip(pixel_size, image_size)]
d = self._detector_factory.simple(
"PAD", 2440, beam_centre, "+x", "-y", pixel_size, image_size, trusted_range
)
return d
class FormatTIFFgeneric_ASI(FormatTIFFgeneric):
"""Format reader for the PETS2 Glycine example, which was recorded on an
ASI hybrid pixel detector.
"""
@staticmethod
def understand(image_file):
"""Check to see if this looks like a TIFF format 516*516 image with
an expected string in the ImageDescription tag"""
with tifffile.TiffFile(image_file) as tif:
page = tif.pages[0]
if page.shape != (516, 516):
return False
ImageDescription = page.tags[270]
if "ImageCameraName: timepix" not in ImageDescription.value:
return False
return True
def _goniometer(self):
"""Dummy goniometer, 'vertical' as the images are viewed. Not completely
sure about the handedness yet"""
return self._goniometer_factory.known_axis((0, 1, 0))
def _beam(self):
"""Dummy beam, energy 200 keV"""
wavelength = 0.02508
return self._beam_factory.make_polarized_beam(
sample_to_source=(0.0, 0.0, 1.0),
wavelength=wavelength,
polarization=(0, 1, 0),
polarization_fraction=0.5,
)
def _detector(self):
"""Dummy detector"""
pixel_size = 0.055, 0.055
image_size = (516, 516)
dyn_range = 20 # XXX ?
trusted_range = (-1, 2**dyn_range - 1)
beam_centre = [(p * i) / 2 for p, i in zip(pixel_size, image_size)]
d = self._detector_factory.simple(
"PAD", 2440, beam_centre, "+x", "-y", pixel_size, image_size, trusted_range
)
return d
class FormatTIFFgeneric_FEI_Tecnai_G2(FormatTIFFgeneric):
"""Format reader for the PETS2 Quartz SiO2 example, which was recorded on
an FEI Tecnai G2 microscope with a CCD detector.
"""
@staticmethod
def understand(image_file):
"""Check to see if this looks like a TIFF format 516*516 image with
an expected string in the ImageDescription tag"""
with tifffile.TiffFile(image_file) as tif:
page = tif.pages[0]
if page.shape != (1024, 1024):
return False
OlympusSIS = page.tags[33560]
if "Veleta" not in OlympusSIS.value["cameraname"]:
return False
return True
def _goniometer(self):
"""Dummy goniometer, 'vertical' as the images are viewed. Not completely
sure about the handedness yet"""
return self._goniometer_factory.known_axis((0, 1, 0))
def _beam(self):
"""Dummy beam, energy 200 keV"""
wavelength = 0.02508
return self._beam_factory.make_polarized_beam(
sample_to_source=(0.0, 0.0, 1.0),
wavelength=wavelength,
polarization=(0, 1, 0),
polarization_fraction=0.5,
)
def _detector(self):
"""Dummy detector"""
# 2x2 binning https://cfim.ku.dk/equipment/electron_microscopy/cm100/Veleta.pdf
pixel_size = 0.026, 0.026
image_size = (1024, 1024)
dyn_range = 14 # XXX ?
trusted_range = (-1, 2**dyn_range - 1)
beam_centre = [(p * i) / 2 for p, i in zip(pixel_size, image_size)]
d = self._detector_factory.simple(
"PAD", 2440, beam_centre, "+x", "-y", pixel_size, image_size, trusted_range
)
return d
class FormatTIFFgeneric_Medipix512(FormatTIFFgeneric):
"""An experimental image reading class for TIFF images from a Medipix
detectors with 512x512 pixels where the central cross is excluded and the
image is separated into 4 panels.
This is very similar to the Timepix512 class, except that the gap between
panels is 4 pixels, not 6.
WARNING: this format is not very specific so an environment variable,
MEDIPIX512_TIFF, must be set, otherwise this will pick up *any* TIFF file
containing a single 512x512 pixel image.
"""
check_environment = "MEDIPIX512_TIFF"
@classmethod
def understand(cls, image_file):
with tifffile.TiffFile(image_file) as tif:
page = tif.pages[0]
if page.shape != (512, 512):
return False
return check_environment_variable(cls)
def _detector(self):
"""Dummy detector"""
from scitbx import matrix
# 55 mu pixels
pixel_size = 0.055, 0.055
trusted_range = (-1, 65535)
thickness = 0.3 # assume 300 mu thick
# Initialise detector frame - dummy origin to place detector at the header
# distance along the canonical beam direction. Dummy distance
fast = matrix.col((1.0, 0.0, 0.0))
slow = matrix.col((0.0, -1.0, 0.0))
cntr = matrix.col((0.0, 0.0, -100.0))
# shifts to go from the centre to the origin - outer pixels are 0.110 mm
self._array_size = (512, 512)
off_x = (self._array_size[0] / 2 - 2) * pixel_size[0]
off_x += 2 * 0.11
shift_x = -1.0 * fast * off_x
off_y = (self._array_size[1] / 2 - 2) * pixel_size[1]
off_y += 2 * 0.11
shift_y = -1.0 * slow * off_y
orig = cntr + shift_x + shift_y
d = Detector()
root = d.hierarchy()
root.set_local_frame(fast.elems, slow.elems, orig.elems)
self.coords = {}
panel_idx = 0
# set panel extent in pixel numbers and x, y mm shifts. Note that the
# outer pixels are 0.110 mm in size. These are excluded from the panel
# extents.
pnl_data = []
pnl_data.append(
{
"xmin": 1,
"ymin": 1,
"xmax": 255,
"ymax": 255,
"xmin_mm": 1 * 0.110,
"ymin_mm": 1 * 0.110,
}
)
pnl_data.append(
{
"xmin": 257,
"ymin": 1,
"xmax": 511,
"ymax": 255,
"xmin_mm": 3 * 0.110 + (511 - 257) * pixel_size[0],
"ymin_mm": 1 * 0.110,
}
)
pnl_data.append(
{
"xmin": 1,
"ymin": 257,
"xmax": 255,
"ymax": 511,
"xmin_mm": 1 * 0.110,
"ymin_mm": 3 * 0.110 + (511 - 257) * pixel_size[1],
}
)
pnl_data.append(
{
"xmin": 257,
"ymin": 257,
"xmax": 511,
"ymax": 511,
"xmin_mm": 3 * 0.110 + (511 - 257) * pixel_size[0],
"ymin_mm": 3 * 0.110 + (511 - 257) * pixel_size[1],
}
)
# redefine fast, slow for the local frame
fast = matrix.col((1.0, 0.0, 0.0))
slow = matrix.col((0.0, 1.0, 0.0))
for ipanel, pd in enumerate(pnl_data):
xmin = pd["xmin"]
xmax = pd["xmax"]
ymin = pd["ymin"]
ymax = pd["ymax"]
xmin_mm = pd["xmin_mm"]
ymin_mm = pd["ymin_mm"]
origin_panel = fast * xmin_mm + slow * ymin_mm
panel_name = "Panel%d" % panel_idx
panel_idx += 1
p = d.add_panel()
p.set_type("SENSOR_PAD")
p.set_name(panel_name)
p.set_raw_image_offset((xmin, ymin))
p.set_image_size((xmax - xmin, ymax - ymin))
p.set_trusted_range(trusted_range)
p.set_pixel_size((pixel_size[0], pixel_size[1]))
p.set_thickness(thickness)
p.set_material("Si")
# p.set_mu(mu)
# p.set_px_mm_strategy(ParallaxCorrectedPxMmStrategy(mu, t0))
p.set_local_frame(fast.elems, slow.elems, origin_panel.elems)
p.set_raw_image_offset((xmin, ymin))
self.coords[panel_name] = (xmin, ymin, xmax, ymax)
return d
def _goniometer(self):
"""Dummy goniometer, 'vertical' as the images are viewed. Not completely
sure about the handedness yet"""
return self._goniometer_factory.known_axis((0, 1, 0))
def _beam(self):
"""Dummy beam, energy 200 keV"""
wavelength = 0.02508
return self._beam_factory.make_polarized_beam(
sample_to_source=(0.0, 0.0, 1.0),
wavelength=wavelength,
polarization=(0, 1, 0),
polarization_fraction=0.5,
probe=Probe.electron,
)
def get_raw_data(self):
raw_data = tifffile.imread(self._image_file)
raw_data = flumpy.from_numpy(raw_data.astype(float))
raw_data.reshape(flex.grid(self._array_size[1], self._array_size[0]))
self._raw_data = []
d = self.get_detector()
for panel in d:
xmin, ymin, xmax, ymax = self.coords[panel.get_name()]
self._raw_data.append(raw_data[ymin:ymax, xmin:xmax])
return tuple(self._raw_data)
class FormatTIFFgeneric_Medipix514(FormatTIFFgeneric):
"""An experimental image reading class for TIFF images from a Medipix
detector which have been converted to 16 bits, have 514*514 pixels and
have geometry and flat field corrections applied.
The header does not contain useful information about the geometry, therefore
we will construct dummy objects and expect to override on import using
site.phil.
WARNING: this format is not very specific so an environment variable,
MEDIPIX514_TIFF, must be set, otherwise this will pick up *any* TIFF file
containing a single 514x514 pixel image.
"""
check_environment = "MEDIPIX514_TIFF"
@classmethod
def understand(cls, image_file):
"""Check to see if this looks like a TIFF format image with a single page"""
with tifffile.TiffFile(image_file) as tif:
page = tif.pages[0]
if page.shape != (514, 514):
return False
return check_environment_variable(cls)
def _goniometer(self):
"""Dummy goniometer, 'vertical' as the images are viewed. Not completely
sure about the handedness yet"""
return self._goniometer_factory.known_axis((0, 1, 0))
def _beam(self):
"""Dummy beam, energy 200 keV"""
wavelength = 0.02508
return self._beam_factory.make_polarized_beam(
sample_to_source=(0.0, 0.0, 1.0),
wavelength=wavelength,
polarization=(0, 1, 0),
polarization_fraction=0.5,
probe=Probe.electron,
)
def _detector(self):
"""Dummy detector"""
pixel_size = 0.055, 0.055
image_size = (514, 514)
dyn_range = 16
trusted_range = (
-1,
2**dyn_range - 2,
) # use max of the dynamic range as a mask value
beam_centre = [(p * i) / 2 for p, i in zip(pixel_size, image_size)]
d = self._detector_factory.simple(
"PAD", 2440, beam_centre, "+x", "-y", pixel_size, image_size, trusted_range
)
return d
class FormatTIFFgeneric_Medipix516(FormatTIFFgeneric):
"""An experimental image reading class for TIFF images from a Medipix
detector which have been converted to 16 bits, have 516*516 pixels and
have geometry and flat field corrections applied.
The header does not contain useful information about the geometry, therefore
we will construct dummy objects and expect to override on import using
site.phil.
WARNING: this format is not very specific so an environment variable,
MEDIPIX516_TIFF, must be set, otherwise this will pick up *any* TIFF file
containing a single 516x516 pixel image.
"""
check_environment = "MEDIPIX516_TIFF"
@classmethod
def understand(cls, image_file):
"""Check to see if this looks like a TIFF format image with a single page"""
with tifffile.TiffFile(image_file) as tif:
page = tif.pages[0]
if page.shape != (516, 516):
return False
return check_environment_variable(cls)
def _goniometer(self):
"""Dummy goniometer, 'vertical' as the images are viewed. Not completely
sure about the handedness yet"""
return self._goniometer_factory.known_axis((0, 1, 0))
def _beam(self):
"""Dummy beam, energy 200 keV"""
wavelength = 0.02508
return self._beam_factory.make_polarized_beam(
sample_to_source=(0.0, 0.0, 1.0),
wavelength=wavelength,
polarization=(0, 1, 0),
polarization_fraction=0.5,
probe=Probe.electron,
)
def _detector(self):
"""Dummy detector"""
pixel_size = 0.055, 0.055
image_size = (516, 516)
dyn_range = 16
trusted_range = (
-1,
2**dyn_range - 2,
) # use max of the dynamic range as a mask value
beam_centre = [(p * i) / 2 for p, i in zip(pixel_size, image_size)]
d = self._detector_factory.simple(
"PAD", 2440, beam_centre, "+x", "-y", pixel_size, image_size, trusted_range
)
return d
class FormatTIFFgeneric_BlochwaveSim(FormatTIFFgeneric):
"""Format class to process headerless TIFF images produced by Tarik Drevon's
Bloch wave simulation. Use environment variable BLOCHWAVE_TIFF to activate,
and assumes 2048^2 pixels.
"""
check_environment = "BLOCHWAVE_TIFF"
@classmethod
def understand(cls, image_file):
with tifffile.TiffFile(image_file) as tif:
page = tif.pages[0]
if page.shape != (2048, 2048):
return False
return check_environment_variable(cls)
def _goniometer(self):
return self._goniometer_factory.known_axis((1, 0, 0))
def _beam(self):
wavelength = 0.02508
return self._beam_factory.make_polarized_beam(
sample_to_source=(0.0, 0.0, 1.0),
wavelength=wavelength,
polarization=(0, 1, 0),
polarization_fraction=0.5,
probe=Probe.electron,
)
def _detector(self):
pixel_size = 0.028, 0.028
image_size = (2048, 2048)
dyn_range = 16
trusted_range = (-1, 2**dyn_range - 1)
beam_centre = [(p * i) / 2 for p, i in zip(pixel_size, image_size)]
d = self._detector_factory.simple(
"PAD", 834, beam_centre, "+x", "-y", pixel_size, image_size, trusted_range
)
return d
class FormatTIFF_UED(FormatTIFFgeneric, FormatStill):
"""An experimental image reading class for TIFF images from a UED
instrument. Most of this is probably incorrect. Use environment variable
UED_TIFF to activate.
"""
check_environment = "UED_TIFF"
def __init__(self, image_file, **kwargs):
FormatTIFFgeneric.__init__(self, image_file, **kwargs)
FormatStill.__init__(self, image_file, **kwargs)
return
@classmethod
def understand(cls, image_file):
"""Check to see if this looks like a TIFF format image with a single page"""
with tifffile.TiffFile(image_file) as tif:
page = tif.pages[0]
if page.shape != (1300, 1340):
return False
return check_environment_variable(cls)
def _beam(self):
"""Dummy beam, energy 200 keV"""
wavelength = 0.02508
return self._beam_factory.make_polarized_beam(
sample_to_source=(0.0, 0.0, 1.0),
wavelength=wavelength,
polarization=(0, 1, 0),
polarization_fraction=0.5,
probe=Probe.electron,
)
def _detector(self):
"""Dummy detector"""
pixel_size = 0.060, 0.060
image_size = (1300, 1340)
dyn_range = 20 # No idea what is correct
trusted_range = (-1, 2**dyn_range - 1)
beam_centre = [(p * i) / 2 for p, i in zip(pixel_size, image_size)]
d = self._detector_factory.simple(
"PAD", 2440, beam_centre, "+x", "-y", pixel_size, image_size, trusted_range
)
return d
class FormatTIFF_UED_BNL(FormatTIFFgeneric, FormatStill):
"""An experimental image reading class for TIFF images from a UED
instrument at BNL: https://www.bnl.gov/atf/capabilities/ued.php.
Set environment variable UED_BNL_TIFF to use.
"""
check_environment = "UED_BNL_TIFF"
def __init__(self, image_file, **kwargs):
FormatTIFFgeneric.__init__(self, image_file, **kwargs)
FormatStill.__init__(self, image_file, **kwargs)
return
@classmethod
def understand(cls, image_file):
"""Check to see if this looks like a TIFF format image with a single page"""
with tifffile.TiffFile(image_file) as tif:
page = tif.pages[0]
if page.shape != (512, 512):
return False
return check_environment_variable(cls)
def _beam(self):
"""Dummy beam, energy 200 keV"""
wavelength = 0.03569
return self._beam_factory.make_polarized_beam(
sample_to_source=(0.0, 0.0, 1.0),
wavelength=wavelength,
polarization=(0, 1, 0),
polarization_fraction=0.5,
probe=Probe.electron,
)
def _detector(self):
"""Dummy detector"""
pixel_size = 0.016, 0.016
image_size = (512, 512)
dyn_range = 20 # No idea what is correct
trusted_range = (-1, 2**dyn_range - 1)
beam_centre = [(p * i) / 2 for p, i in zip(pixel_size, image_size)]
d = self._detector_factory.simple(
"CCD", 3480, beam_centre, "+x", "-y", pixel_size, image_size, trusted_range
)
return d
class FormatTIFFgeneric_GatanK3(FormatTIFFgeneric):
"""An experimental image reading class for TIFF images from a Gatan
K3 detector.
WARNING: this format is not very specific so an environment variable,
GATANK3_TIFF, must be set, otherwise this will pick up *any* TIFF file
containing a single 512x512 pixel image.
"""
check_environment = "GATANK3_TIFF"
@classmethod
def understand(cls, image_file):
with tifffile.TiffFile(image_file) as tif:
page = tif.pages[0]
if page.shape != (1500, 1500):
return False
return check_environment_variable(cls)
def _goniometer(self):
"""Dummy goniometer, 'vertical' as the images are viewed. Not completely
sure about the handedness yet"""
return self._goniometer_factory.known_axis((0, 1, 0))
def _beam(self):
"""Dummy beam, energy 200 keV"""
wavelength = 0.02508
return self._beam_factory.make_polarized_beam(
sample_to_source=(0.0, 0.0, 1.0),
wavelength=wavelength,
polarization=(0, 1, 0),
polarization_fraction=0.5,
probe=Probe.electron,
)
def _detector(self):
"""Dummy detector"""
pixel_size = 0.005, 0.005
image_size = (1500, 1500)
dyn_range = 20
trusted_range = (0, 2**dyn_range - 1)
beam_centre = [(p * i) / 2 for p, i in zip(pixel_size, image_size)]
d = self._detector_factory.simple(
"PAD", 2440, beam_centre, "+x", "-y", pixel_size, image_size, trusted_range
)
return d