forked from slaclab/psgeom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
executable file
·664 lines (431 loc) · 24.1 KB
/
test.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
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
#!/usr/bin/env python
import numpy as np
import os
import unittest
import h5py
from psgeom import moveable
from psgeom import sensors
from psgeom import translate
from psgeom import camera
from psgeom import basisgrid
from psgeom import fitting
from psgeom.translate import _cheetah_to_twobyones
import warnings
#camera._STRICT = True
PIXEL_TOLERANCE_um = 10.0
# ---- moveable.py -------------------------------------------------------------
def test_translation_matrix_from_vector():
x = np.random.randint(0,5,size=(3))
y = np.random.randint(0,5,size=(3))
yp = np.ones(4)
yp[:3] = y
T = moveable._translation_matrix_from_vector(x)
assert np.all(np.dot(T, yp)[:3] == x + y)
assert np.dot(T, yp)[3] == 1.0
def test_rotation_matrix_from_angles():
x = np.array([1.0, 0.0, 0.0]) # vector pointing at x
Rz = moveable._rotation_matrix_from_angles(90.0, 0.0, 0.0)
y = np.dot(Rz, x)
np.testing.assert_array_almost_equal(y, np.array([0.0, 1.0, 0.0]),
err_msg='Rz err')
Rx = moveable._rotation_matrix_from_angles(0.0, 0.0, 90.0)
z = np.dot(Rx, y)
np.testing.assert_array_almost_equal(z, np.array([0.0, 0.0, 1.0]),
err_msg='Rx err')
Ry = moveable._rotation_matrix_from_angles(0.0, -90.0, 0.0)
x = np.dot(Ry, z)
np.testing.assert_array_almost_equal(x, x, err_msg='Ry err')
def test_angle_retrieval():
for i in range(100):
alpha = np.random.rand() * 180.0
beta = np.random.rand() * 180.0
gamma = np.random.rand() * 180.0
R = moveable._rotation_matrix_from_angles(gamma, beta, alpha)
I = np.eye(3)
Ip = np.dot(R, I)
gp, bp, ap = moveable._angles_from_rotated_frame(Ip[:,0], Ip[:,1], Ip[:,2])
#print np.array([gamma, beta, alpha]), np.array([gp, bp, ap])
# test to make sure they rotate to the same thing
R2 = moveable._rotation_matrix_from_angles(gp, bp, ap)
assert np.sum( np.abs( R - R2 ) ) < 1e-12
# ---- sensors.py --------------------------------------------------------------
class TestPixelArraySensor(object):
def setup(self):
self.shape = (185, 388)
self.pixel_shape = (1.0, 1.0)
self.rotation_angles = np.random.rand(3) * 360.0
self.translation = np.random.randn(3)
self.PAS = sensors.PixelArraySensor(self.shape, self.pixel_shape,
type_name='Test', id_num=0, parent=None,
rotation_angles=self.rotation_angles,
translation=self.translation)
def test_local_transform(self):
R = moveable._rotation_matrix_from_angles(*self.rotation_angles,
dummy_dimension=True)
T = moveable._translation_matrix_from_vector(self.translation)
L_ref = np.dot(T, R)
L_ans = self.PAS.local_transform
np.testing.assert_array_almost_equal(L_ref, L_ans)
def test_global_transform(self):
ra_p = np.random.rand(3) * 360.0
t_p = np.random.randn(3)
Rp = moveable._rotation_matrix_from_angles(*ra_p, dummy_dimension=True)
Tp = moveable._translation_matrix_from_vector(t_p)
parent_obj = camera.CompoundCamera(type_name='daddy',
id_num=0, parent=None,
rotation_angles=ra_p,
translation=t_p)
self.PAS.set_parent(parent_obj)
R = moveable._rotation_matrix_from_angles(*self.rotation_angles,
dummy_dimension=True)
T = moveable._translation_matrix_from_vector(self.translation)
G_ref = np.dot( np.dot( np.dot(Tp, Rp), T), R) # T_1 . R_1 . T_2 . R_2
G_ans = self.PAS.global_transform
np.testing.assert_array_almost_equal(G_ref, G_ans)
def test_evaluate_transform(self):
# for rotation
x = np.array([[1.0, 0.0, 0.0]]) # vector pointing at x
Rz = moveable._rotation_matrix_from_angles(90.0, 0.0, 0.0,
dummy_dimension=True)
ref = np.array([[0.0, 1.0, 0.0]])
ans = camera.CompoundCamera._evaluate_transform(Rz, x)
np.testing.assert_array_almost_equal(ans, ref, err_msg='rotation')
# for translation
x = np.random.randint(0,5,size=(1,3))
y = np.random.randint(0,5,size=(1,3))
T = moveable._translation_matrix_from_vector(x)
assert np.all( camera.CompoundCamera._evaluate_transform(T, y) == x + y )
def test_untransformed_xyz(self):
uxyz = self.PAS.untransformed_xyz
assert uxyz.shape[:-1] == self.shape, '%s %s' % (uxyz.shape, self.shape)
# remember, slow is x convention... (changed 8-28-16 by TJL)
assert np.max(uxyz[...,0]) == (self.shape[0]-1) * self.pixel_shape[0]
assert np.max(uxyz[...,1]) == (self.shape[1]-1) * self.pixel_shape[1]
def test_xyz(self):
uxyz = self.PAS.untransformed_xyz
buff = np.ones( list(uxyz.shape[:-1]) + [1], dtype=uxyz.dtype)
uxyzd = np.concatenate([uxyz, buff], axis=-1)
R = moveable._rotation_matrix_from_angles(*self.rotation_angles,
dummy_dimension=True)
T = moveable._translation_matrix_from_vector(self.translation)
xyz_ans = np.dot(uxyzd, np.dot(T, R).T)
np.testing.assert_array_almost_equal(self.PAS.xyz, xyz_ans[...,:3])
class TestSens2x1(TestPixelArraySensor):
def test_2x1_central_gap(self):
# regression test for the size of the big pixels
s = sensors.Cspad2x1()
small1 = s.xyz[0,193,0] - s.xyz[0,192,0]
big = s.xyz[0,194,0] - s.xyz[0,193,0]
small2 = s.xyz[0,195,0] - s.xyz[0,194,0]
np.testing.assert_array_almost_equal(small1, 109.92) # px size
np.testing.assert_array_almost_equal(small2, 109.92)
np.testing.assert_array_almost_equal(big, 439.68) # gap size
# ---- camera.py -------------------------------------------------------------
class TestCompoundCamera(object):
def setup(self):
self.geom = camera.CompoundCamera.from_psana_file('ref_files/refgeom_psana.data')
def test_read_write(self):
self.geom.to_psana_file('test.data')
geom2 = camera.CompoundCamera.from_psana_file('test.data')
assert self.geom.xyz.shape == geom2.xyz.shape, 'shape/element mismatch'
np.testing.assert_allclose(self.geom.xyz, geom2.xyz, rtol=1e-3)
os.remove('test.data')
def test_xyz_vs_old_implementation(self):
# ---- get the geometry Mikhail-style
try:
from PSCalib.GeometryAccess import GeometryAccess
ga = GeometryAccess('ref_files/refgeom_psana.data')
xyz_old = ga.get_pixel_coords()
except:
# if that don't work, load a pre-saved answer
print 'could not use GeometryAccess, loading saved xyz'
xyz_old = np.load('ref_files/GA_saved_1-end.npy')
# some np-foo to move the 3-d x,y,z axis from first dim to last
xyz_old = np.rollaxis(np.array(xyz_old), 0, 7) # send 0 --> 7
xyz_old = np.squeeze(xyz_old)
geom = camera.CompoundCamera.from_psana_file('ref_files/refgeom_psana.data')
xyz_new = np.squeeze(geom.xyz)
assert xyz_new.shape == xyz_old.shape, 'shape mismatch'
err = np.sum( np.abs(xyz_new - xyz_old) ) / float(np.product(xyz_new.shape))
print 'Mean Absolute Error: %f um / px' % err
num_more_than_1px_err = np.sum( np.abs(xyz_new - xyz_old) > 109.92 )
assert err < 10.0, 'error greater than 10um avg per px (%f)' % err
assert num_more_than_1px_err < 7500, '>7500 pix w err > 1 px'
#np.testing.assert_allclose(xyz_new, xyz_old, atol=2 * 110.0)
def test_leaves(self):
assert len(self.geom.leaves) == 32, 'got: %d' % len(self.geom.leaves)
class TestCompoundAreaCamera(TestCompoundCamera):
def setup(self):
self.geom = camera.CompoundAreaCamera.from_psana_file('ref_files/refgeom_psana.data')
self.klass = camera.CompoundAreaCamera
def test_hdf5_file(self):
self.geom.to_hdf5('ref_files/tmp_hdf.h5')
f = h5py.File('ref_files/tmp_hdf.h5', 'r')
xyz2 = np.array(f['/xyz'])
f.close()
np.testing.assert_allclose(self.geom.xyz, xyz2)
#self.cspad.to_hdf5('ref_files/tmp_hdf.h5')
#cd2 = camera.CompoundCamera.from_hdf5('ref_files/tmp_hdf.h5')
#np.testing.assert_allclose(self.cd.xyz, cd2.xyz)
os.remove('ref_files/tmp_hdf.h5')
def test_rayonix_vs_geometry_access(self):
# ---- get the geometry Mikhail-style
try:
from PSCalib.GeometryAccess import GeometryAccess
ga = GeometryAccess('ref_files/rayonix.data')
xyz_old = ga.get_pixel_coords()
except:
# if that don't work, load a pre-saved answer
print 'could not use GeometryAccess, loading saved xyz'
xyz_old = np.load('ref_files/rayonix_saved.npy')
xyz_old = np.rollaxis(np.array(xyz_old), 0, 5) # send 0 --> end
xyz_old = np.squeeze(xyz_old)
geom = camera.CompoundAreaCamera.from_psana_file('ref_files/rayonix.data')
xyz_new = np.squeeze(geom.xyz)
assert xyz_new.shape == xyz_old.shape, 'shape mismatch %s / %s' % (xyz_new.shape, xyz_old.shape)
err = np.sum( np.abs(xyz_new - xyz_old) ) / float(np.product(xyz_new.shape))
print 'Mean Absolute Error: %f um / px' % err
num_more_than_1px_err = np.sum( np.abs(xyz_new - xyz_old) > 89.0 )
print 'new', xyz_new
print 'old', xyz_old
assert err < 10.0, 'error greater than 10 um avg per px (%f)' % err
assert num_more_than_1px_err < 7500, '>7500 pix w err > 1 px'
def test_rayonix_crystfel(self):
geom = camera.CompoundAreaCamera.from_psana_file('ref_files/rayonix.data')
geom.to_crystfel_file('ref_files/tmp_rayonix.geom')
# compare to reference created by make_pixelmap
f = h5py.File('ref_files/rayonix.h5')
x = np.array(f['/x']) * 1e6
y = np.array(f['/y']) * 1e6
#z = np.array(f['/z']) # z currently is not populated by make_pixelmap
f.close()
# NOTE: x is flipped in the crystFEL/cheetah convention
# as there, z points towards the source of x-rays (not along travel)
np.testing.assert_allclose( np.squeeze(geom.xyz[...,0]), -x, atol=10 )
np.testing.assert_allclose( np.squeeze(geom.xyz[...,1]), y, atol=10 )
geom2 = camera.CompoundAreaCamera.from_crystfel_file('ref_files/tmp_rayonix.geom')
np.testing.assert_allclose(geom.xyz, geom2.xyz, atol=0.001)
os.remove('ref_files/tmp_rayonix.geom')
def test_pnccd_vs_geometry_access(self):
# ---- get the geometry Mikhail-style
try:
from PSCalib.GeometryAccess import GeometryAccess
ga = GeometryAccess('ref_files/pnccd.data')
xyz_old = ga.get_pixel_coords()
except:
# if that don't work, load a pre-saved answer
print 'could not use GeometryAccess, loading saved xyz'
xyz_old = np.load('ref_files/pnccd_saved.npy')
xyz_old = np.rollaxis(np.array(xyz_old), 0, 6) # send 0 --> end
xyz_old = np.squeeze(xyz_old)
geom = camera.CompoundAreaCamera.from_psana_file('ref_files/pnccd.data')
xyz_new = np.squeeze(geom.xyz)
assert xyz_new.shape == xyz_old.shape, 'shape mismatch'
err = np.sum( np.abs(xyz_new - xyz_old) ) / float(np.product(xyz_new.shape))
print 'Mean Absolute Error: %f um / px' % err
num_more_than_1px_err = np.sum( np.abs(xyz_new - xyz_old) > 75.0 )
assert err < 10.0, 'error greater than 10um avg per px (%f)' % err
assert num_more_than_1px_err < 7500, '>7500 pix w err > 1 px'
class TestCspad(TestCompoundCamera):
def setup(self):
self.geom = camera.Cspad.from_psana_file('ref_files/refgeom_psana.data')
self.klass = camera.Cspad
def test_to_basis_grid(self):
bg = self.geom.to_basisgrid()
xyz = np.squeeze(self.geom.xyz)
for i in range(4):
for j in range(8):
bg_xyz_ij_1 = bg.grid_as_explicit(i*16 + j*2)
bg_xyz_ij_2 = bg.grid_as_explicit(i*16 + j*2 + 1)
cd_xyz_ij = xyz[i,j,:,:,:]
np.testing.assert_allclose(bg_xyz_ij_1, cd_xyz_ij[:,:194,:],
atol=PIXEL_TOLERANCE_um)
np.testing.assert_allclose(bg_xyz_ij_2, cd_xyz_ij[:,194:,:],
atol=PIXEL_TOLERANCE_um)
def test_basisgrid_roundtrip(self):
bg = self.geom.to_basisgrid()
new = self.klass.from_basisgrid(bg)
ref_xyz = np.squeeze(self.geom.xyz)
new_xyz = new.xyz.reshape(ref_xyz.shape)
assert self.geom.num_pixels == new.num_pixels
np.testing.assert_allclose(ref_xyz,
new_xyz,
atol=PIXEL_TOLERANCE_um)
def test_hdf5_file(self):
self.geom.to_hdf5('ref_files/tmp_hdf.h5')
f = h5py.File('ref_files/tmp_hdf.h5', 'r')
xyz2 = np.array(f['/xyz'])
f.close()
# we re-shape the xyz to match the way psana
# presents CSPAD data
assert xyz2.shape == (32, 185, 388, 3)
np.testing.assert_allclose(self.geom.xyz[0,0,0], xyz2[0])
os.remove('ref_files/tmp_hdf.h5')
# ---- translate.py ------------------------------------------------------------
class TestTranslate(object):
"""
Will test read/write from external software packages
"""
def setup(self):
self.cd = camera.CompoundCamera.from_psana_file('ref_files/refgeom_psana.data')
self.cspad = camera.Cspad.from_psana_file('ref_files/refgeom_psana.data')
def test_psf_text(self):
raise unittest.SkipTest
self.cd.to_text_file('ref_files/cd_psf.txt')
self.cspad.to_text_file('ref_files/cspad_psf.txt')
# todo : load & ensure consistent
cd2 = camera.CompoundCamera.from_text_file('ref_files/cd_psf.txt')
cspad2 = camera.CompoundCamera.from_text_file('ref_files/cspad_psf.txt')
np.testing.assert_allclose(self.cd.xyz, cd2.xyz)
np.testing.assert_allclose(self.cspad.xyz, cspad2.xyz)
os.remove('ref_files/cd_psf.txt')
os.remove('ref_files/cspad_psf.txt')
def test_cheetah_roundtrip(self):
self.cspad.to_cheetah_file('ref_files/tmp_cheetah_geom.h5')
cspad2 = camera.Cspad.from_cheetah_file('ref_files/tmp_cheetah_geom.h5')
np.testing.assert_allclose( np.squeeze(self.cspad.xyz),
np.squeeze(cspad2.xyz),
err_msg='round trip fail',
atol=PIXEL_TOLERANCE_um)
os.remove('ref_files/tmp_cheetah_geom.h5')
def test_cheetah_values(self):
# the cheetah pixels are just the xyz, so we can load them and use them
# to compare to our implementation
f = h5py.File('ref_files/refgeom_cheetah.h5', 'r')
x = -1.0 * _cheetah_to_twobyones( np.array(f['x']) * 1000000.0 )
y = _cheetah_to_twobyones( np.array(f['y']) * 1000000.0 )
z = _cheetah_to_twobyones( np.array(f['z']) )
f.close()
ref_xyz = np.rollaxis( np.array([x,y,z]), 0, 4) # to shape (32,...,3)
tst = camera.Cspad.from_cheetah_file('ref_files/refgeom_cheetah.h5')
tst_xyz = tst.xyz.reshape(32,185,388,3)
np.testing.assert_allclose(tst_xyz[:,0,0,:],
ref_xyz[:,0,0,:],
atol=PIXEL_TOLERANCE_um,
err_msg='panel 1st pixels off')
# cheetah does not deal correctly with the large center pixels, so
# we test around that
assert np.max( np.abs(tst_xyz - ref_xyz) ) < 500.0
np.testing.assert_allclose(tst_xyz[:,:,:,:], ref_xyz[:,:,:,:],
atol=PIXEL_TOLERANCE_um,
err_msg='panels off in general')
def test_cheetah_crystfel_consistency(self):
# the following two files were generated using the "make_pixelmap"
# program by TAW, the cheetah file being generated from the CrystFEL
# conversion done by TJL 7/10/15
# very close but ~1/2 pixel off. Does cheetah plot pixel corners or
# centers? -- TJL 7/8/15
cheetah = camera.Cspad.from_cheetah_file('ref_files/refgeom_cheetah.h5')
crystfel = camera.Cspad.from_crystfel_file('ref_files/refgeom_crystfel.geom')
print np.squeeze(cheetah.xyz) - np.squeeze(crystfel.xyz)
# cheetah z-values are screwed up, so ignore those
np.testing.assert_allclose(np.squeeze(cheetah.xyz)[...,:2],
np.squeeze(crystfel.xyz)[...,:2],
atol=PIXEL_TOLERANCE_um)
def test_crystfel_roundtrip(self):
# first test with no weird z stuff
self.cspad.to_crystfel_file('ref_files/tmp_crystfel.geom')
cd2 = camera.Cspad.from_crystfel_file('ref_files/tmp_crystfel.geom')
print self.cspad.xyz[...,2], cd2.xyz[...,:2]
# be sure error is less than 1 micron in x/y, 0.2 mm in z
assert np.max(np.abs( np.squeeze(self.cspad.xyz[...,:2]) - np.squeeze(cd2.xyz[...,:2]) )) < 1.0
assert np.max(np.abs( np.squeeze(self.cspad.xyz) - np.squeeze(cd2.xyz) )) < 200.0
# CrystFEL's geometry assumes all panels are orthogonal to the beam. To
# do a fair comparison, therefore, we set all the z-rotations to zero
# note: we remove rotations around the x- & y-axes to get rid of z
for c in self.cspad.children:
c._rotation_angles[1:] = 0.0
for q in c.children:
q._rotation_angles[1:] = 0.0
for t in q.children:
t._rotation_angles[1:] = 0.0
self.cspad._rotation_angles[1:] = 0.0
for l in self.cspad.leaves:
assert [v[2] == 0.0 for v in l.psf[1:]]
self.cspad.to_crystfel_file('ref_files/tmp_crystfel.geom')
cd2 = camera.Cspad.from_crystfel_file('ref_files/tmp_crystfel.geom')
assert np.max(np.abs( np.squeeze(self.cspad.xyz) - np.squeeze(cd2.xyz) )) < 1.0
# finally just check all pixels are straight up reasonable
np.testing.assert_allclose(np.squeeze(self.cd.xyz),
np.squeeze(cd2.xyz),
err_msg='round trip fail',
atol=PIXEL_TOLERANCE_um,
rtol=1e-3)
os.remove('ref_files/tmp_crystfel.geom')
def test_crystfel_coffset(self):
geom = camera.Cspad.from_psana_file('ref_files/refgeom_psana.data')
geom.to_crystfel_file('ref_files/temp.geom', coffset=2.0)
geom2 = camera.Cspad.from_crystfel_file('ref_files/temp.geom')
assert np.all(np.abs(geom2.xyz[...,2] - 2e6) < 0.01)
np.testing.assert_allclose(np.squeeze(geom.xyz[...,:2]),
np.squeeze(geom2.xyz[...,:2]),
err_msg='round trip fail',
atol=PIXEL_TOLERANCE_um,
rtol=1e-3)
os.remove('ref_files/temp.geom')
def test_2x2_cheetah_roundtrip(self):
cheetah = camera.Cspad.from_cheetah_file('ref_files/cspad-2x2-approx1.h5')
cheetah.to_psana_file('ref_files/tmp_psana.data')
cheetah2 = camera.Cspad.from_psana_file('ref_files/tmp_psana.data')
np.testing.assert_allclose(np.squeeze(cheetah.xyz),
np.squeeze(cheetah2.xyz),
atol=PIXEL_TOLERANCE_um)
os.remove('ref_files/tmp_psana.data')
def test_2x2_crystfel_roundtrip(self):
crystfel = camera.Cspad.from_crystfel_file('ref_files/cspad-2x2-approx1.geom')
crystfel.to_psana_file('ref_files/tmp_psana.data')
crystfel2 = camera.Cspad.from_psana_file('ref_files/tmp_psana.data')
np.testing.assert_allclose(np.squeeze(crystfel.xyz),
np.squeeze(crystfel2.xyz),
atol=PIXEL_TOLERANCE_um)
os.remove('ref_files/tmp_psana.data')
def test_2x2_consistency(self):
# TJL: I have not verified that these are in fact the same geom
# they were provided by users
crystfel = camera.Cspad.from_crystfel_file('ref_files/cspad-2x2-approx1.geom')
cheetah = camera.Cspad.from_cheetah_file('ref_files/cspad-2x2-approx1.h5')
# compare only x/y, not z
np.testing.assert_allclose(np.squeeze(crystfel.xyz)[...,:2],
np.squeeze(cheetah.xyz)[...,:2],
atol=100.0)
class TestFitting(object):
def test_basis_grid_interpolator(self):
new_z = 0.75
# load 3x geometires
filenames = ['origin.geom', 'coffset05.geom', 'coffset10.geom']
cameras = [camera.Cspad.from_crystfel_file('ref_files/distance_series/' + f) for f in filenames]
motor_z = np.array([0.0, 0.5, 1.0])
bgi = fitting.BasisGridInterpolator([g.to_basisgrid() for g in cameras], motor_z)
prediction = bgi.predict( np.array([new_z]) )
#print 'm:x (p/s/f)', bgi._coefficient_matrix[0,0::3]
#print 'm:y (p/s/f)', bgi._coefficient_matrix[0,1::3]
#print 'm:z (p/s/f)', bgi._coefficient_matrix[0,2::3]
#print 'b:x (p/s/f)', bgi._coefficient_matrix[1,0::3]
#print 'b:y (p/s/f)', bgi._coefficient_matrix[1,1::3]
#print 'b:z (p/s/f)', bgi._coefficient_matrix[1,2::3]
# the predicted bg should be the same as any other in x/y
bg0 = cameras[0].to_basisgrid()
xy_diff = np.sum(np.abs( prediction.xyz[...,:2] - bg0.xyz[...,:2] ))
assert xy_diff < 1.0
# and have a specific z-value
#print prediction.xyz[...,2]
bg2 = cameras[2].to_basisgrid()
z_diff = (bg2.xyz[...,2] - bg0.xyz[...,2])
z_expt = z_diff * 0.75 + bg0.xyz[...,2]
#print z_expt
z_diff = np.sum(np.abs(z_expt - prediction.xyz[...,2]))
assert z_diff < 1.0
return
def test_bg_as_array():
# prob not necessary
geom = camera.Cspad.from_psana_file('ref_files/refgeom_psana.data')
bg = geom.to_basisgrid()
assert bg.as_array().shape == (64, 11)
def test_bg_from_array():
geom = camera.Cspad.from_psana_file('ref_files/refgeom_psana.data')
bg = geom.to_basisgrid()
bg2 = basisgrid.BasisGrid.from_array( bg.as_array() )
assert np.all( bg.to_explicit() == bg2.to_explicit() )
if __name__ == '__main__':
#test_create_cspad()
test_xyz_vs_old_implementation()