forked from GalSim-developers/GalSim
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_draw.py
More file actions
1761 lines (1591 loc) · 87.6 KB
/
test_draw.py
File metadata and controls
1761 lines (1591 loc) · 87.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
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
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Copyright (c) 2012-2026 by the GalSim developers team on GitHub
# https://github.com/GalSim-developers
#
# This file is part of GalSim: The modular galaxy image simulation toolkit.
# https://github.com/GalSim-developers/GalSim
#
# GalSim is free software: redistribution and use in source and binary forms,
# with or without modification, are permitted provided that the following
# conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this
# list of conditions, and the disclaimer given in the accompanying LICENSE
# file.
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions, and the disclaimer given in the documentation
# and/or other materials provided with the distribution.
#
import os
import numpy as np
import galsim
from galsim_test_helpers import *
# for flux normalization tests
test_flux = 1.8
# A helper function used by both test_draw and test_drawk to check that the drawn image
# is a radially symmetric exponential with the right scale.
def CalculateScale(im):
# We just determine the scale radius of the drawn exponential by calculating
# the second moments of the image.
# int r^2 exp(-r/s) 2pir dr = 12 s^4 pi
# int exp(-r/s) 2pir dr = 2 s^2 pi
x, y = np.meshgrid(np.arange(np.shape(im.array)[0]), np.arange(np.shape(im.array)[1]))
if np.iscomplexobj(im.array):
T = complex
else:
T = float
flux = im.array.astype(T).sum()
mx = (x * im.array.astype(T)).sum() / flux
my = (y * im.array.astype(T)).sum() / flux
mxx = (((x-mx)**2) * im.array.astype(T)).sum() / flux
myy = (((y-my)**2) * im.array.astype(T)).sum() / flux
mxy = ((x-mx) * (y-my) * im.array.astype(T)).sum() / flux
s2 = mxx+myy
print(flux,mx,my,mxx,myy,mxy)
np.testing.assert_almost_equal((mxx-myy)/s2, 0, 5, "Found e1 != 0 for Exponential draw")
np.testing.assert_almost_equal(2*mxy/s2, 0, 5, "Found e2 != 0 for Exponential draw")
return np.sqrt(s2/6) * im.scale
@timer
def test_drawImage():
"""Test the various optional parameters to the drawImage function.
In particular test the parameters image and dx in various combinations.
"""
# We use a simple Exponential for our object:
obj = galsim.Exponential(flux=test_flux, scale_radius=2)
# First test drawImage() with method='no_pixel'. It should:
# - create a new image
# - return the new image
# - set the scale to obj.nyquist_scale
# - set the size large enough to contain 99.5% of the flux
im1 = obj.drawImage(method='no_pixel')
nyq_scale = obj.nyquist_scale
np.testing.assert_almost_equal(im1.scale, nyq_scale, 9,
"obj.drawImage() produced image with wrong scale")
np.testing.assert_equal(im1.bounds, galsim.BoundsI(1,56,1,56),
"obj.drawImage() produced image with wrong bounds")
np.testing.assert_almost_equal(CalculateScale(im1), 2, 1,
"Measured wrong scale after obj.drawImage()")
# The flux is only really expected to come out right if the object has been
# convoled with a pixel:
obj2 = galsim.Convolve([ obj, galsim.Pixel(im1.scale) ])
im2 = obj2.drawImage(method='no_pixel')
nyq_scale = obj2.nyquist_scale
np.testing.assert_almost_equal(im2.scale, nyq_scale, 9,
"obj2.drawImage() produced image with wrong scale")
np.testing.assert_almost_equal(im2.array.astype(float).sum(), test_flux, 2,
"obj2.drawImage() produced image with wrong flux")
np.testing.assert_equal(im2.bounds, galsim.BoundsI(1,56,1,56),
"obj2.drawImage() produced image with wrong bounds")
np.testing.assert_almost_equal(CalculateScale(im2), 2, 1,
"Measured wrong scale after obj2.drawImage()")
# This should be the same as obj with method='auto'
im2 = obj.drawImage()
np.testing.assert_almost_equal(im2.scale, nyq_scale, 9,
"obj2.drawImage() produced image with wrong scale")
np.testing.assert_almost_equal(im2.array.astype(float).sum(), test_flux, 2,
"obj2.drawImage() produced image with wrong flux")
np.testing.assert_equal(im2.bounds, galsim.BoundsI(1,56,1,56),
"obj2.drawImage() produced image with wrong bounds")
np.testing.assert_almost_equal(CalculateScale(im2), 2, 1,
"Measured wrong scale after obj2.drawImage()")
# Test if we provide an image argument. It should:
# - write to the existing image
# - also return that image
# - set the scale to obj2.nyquist_scale
# - zero out any existing data
im3 = galsim.ImageD(56,56)
im4 = obj.drawImage(im3)
np.testing.assert_almost_equal(im3.scale, nyq_scale, 9,
"obj.drawImage(im3) produced image with wrong scale")
np.testing.assert_almost_equal(im3.array.sum(), test_flux, 2,
"obj.drawImage(im3) produced image with wrong flux")
np.testing.assert_almost_equal(im3.array.sum(), im2.array.astype(float).sum(), 6,
"obj.drawImage(im3) produced image with different flux than im2")
np.testing.assert_almost_equal(CalculateScale(im3), 2, 1,
"Measured wrong scale after obj.drawImage(im3)")
np.testing.assert_array_equal(im3.array, im4.array,
"im4 = obj.drawImage(im3) produced im4 != im3")
im3.fill(9.8)
np.testing.assert_array_equal(im3.array, im4.array,
"im4 = obj.drawImage(im3) produced im4 is not im3")
im4 = obj.drawImage(im3)
np.testing.assert_almost_equal(im3.array.sum(), im2.array.astype(float).sum(), 6,
"obj.drawImage(im3) doesn't zero out existing data")
# Test if we provide an image with undefined bounds. It should:
# - resize the provided image
# - also return that image
# - set the scale to obj2.nyquist_scale
im5 = galsim.ImageD()
obj.drawImage(im5)
np.testing.assert_almost_equal(im5.scale, nyq_scale, 9,
"obj.drawImage(im5) produced image with wrong scale")
np.testing.assert_almost_equal(im5.array.sum(), test_flux, 2,
"obj.drawImage(im5) produced image with wrong flux")
np.testing.assert_almost_equal(CalculateScale(im5), 2, 1,
"Measured wrong scale after obj.drawImage(im5)")
np.testing.assert_almost_equal(
im5.array.sum(), im2.array.astype(float).sum(), 6,
"obj.drawImage(im5) produced image with different flux than im2")
np.testing.assert_equal(im5.bounds, galsim.BoundsI(1,56,1,56),
"obj.drawImage(im5) produced image with wrong bounds")
# Test if we provide a dx to use. It should:
# - create a new image using that dx for the scale
# - return the new image
# - set the size large enough to contain 99.5% of the flux
scale = 0.51 # Just something different from 1 or dx_nyq
im7 = obj.drawImage(scale=scale,method='no_pixel')
np.testing.assert_almost_equal(im7.scale, scale, 9,
"obj.drawImage(dx) produced image with wrong scale")
np.testing.assert_almost_equal(im7.array.astype(float).sum(), test_flux, 2,
"obj.drawImage(dx) produced image with wrong flux")
np.testing.assert_almost_equal(CalculateScale(im7), 2, 1,
"Measured wrong scale after obj.drawImage(dx)")
np.testing.assert_equal(im7.bounds, galsim.BoundsI(1,68,1,68),
"obj.drawImage(dx) produced image with wrong bounds")
# If also providing center, then same size, but centered near that center.
for center in [(3,3), (210.2, 511.9), (10.55, -23.8), (0.5,0.5)]:
im8 = obj.drawImage(scale=scale, center=center)
np.testing.assert_almost_equal(im8.scale, scale, 9)
# Note: it doesn't have to come out 68,68. If the offset is zero from the integer center,
# it drops down to (66, 66)
if center == (3,3):
np.testing.assert_equal(im8.array.shape, (66, 66))
else:
np.testing.assert_equal(im8.array.shape, (68, 68))
np.testing.assert_almost_equal(im8.array.astype(float).sum(), test_flux, 2)
print('center, true = ',center,im8.true_center)
assert abs(center[0] - im8.true_center.x) <= 0.5
assert abs(center[1] - im8.true_center.y) <= 0.5
# Test if we provide an image with a defined scale. It should:
# - write to the existing image
# - use the image's scale
nx = 200 # Some randome size
im9 = galsim.ImageD(nx,nx, scale=scale)
obj.drawImage(im9)
np.testing.assert_almost_equal(im9.scale, scale, 9,
"obj.drawImage(im9) produced image with wrong scale")
np.testing.assert_almost_equal(im9.array.sum(), test_flux, 4,
"obj.drawImage(im9) produced image with wrong flux")
np.testing.assert_almost_equal(CalculateScale(im9), 2, 2,
"Measured wrong scale after obj.drawImage(im9)")
# Test if we provide an image with a defined scale <= 0. It should:
# - write to the existing image
# - set the scale to obj2.nyquist_scale
im9.scale = -scale
im9.setZero()
obj.drawImage(im9)
np.testing.assert_almost_equal(im9.scale, nyq_scale, 9,
"obj.drawImage(im9) produced image with wrong scale")
np.testing.assert_almost_equal(im9.array.sum(), test_flux, 4,
"obj.drawImage(im9) produced image with wrong flux")
np.testing.assert_almost_equal(CalculateScale(im9), 2, 2,
"Measured wrong scale after obj.drawImage(im9)")
im9.scale = 0
im9.setZero()
obj.drawImage(im9)
np.testing.assert_almost_equal(im9.scale, nyq_scale, 9,
"obj.drawImage(im9) produced image with wrong scale")
np.testing.assert_almost_equal(im9.array.sum(), test_flux, 4,
"obj.drawImage(im9) produced image with wrong flux")
np.testing.assert_almost_equal(CalculateScale(im9), 2, 2,
"Measured wrong scale after obj.drawImage(im9)")
# Test if we provide an image and dx. It should:
# - write to the existing image
# - use the provided dx
# - write the new dx value to the image's scale
im9.scale = 0.73
im9.setZero()
obj.drawImage(im9, scale=scale)
np.testing.assert_almost_equal(im9.scale, scale, 9,
"obj.drawImage(im9,dx) produced image with wrong scale")
np.testing.assert_almost_equal(im9.array.sum(), test_flux, 4,
"obj.drawImage(im9,dx) produced image with wrong flux")
np.testing.assert_almost_equal(CalculateScale(im9), 2, 2,
"Measured wrong scale after obj.drawImage(im9,dx)")
# Test if we provide an image and dx <= 0. It should:
# - write to the existing image
# - set the scale to obj2.nyquist_scale
im9.scale = 0.73
im9.setZero()
obj.drawImage(im9, scale=-scale)
np.testing.assert_almost_equal(im9.scale, nyq_scale, 9,
"obj.drawImage(im9,dx<0) produced image with wrong scale")
np.testing.assert_almost_equal(im9.array.sum(), test_flux, 4,
"obj.drawImage(im9,dx<0) produced image with wrong flux")
np.testing.assert_almost_equal(CalculateScale(im9), 2, 2,
"Measured wrong scale after obj.drawImage(im9,dx<0)")
im9.scale = 0.73
im9.setZero()
obj.drawImage(im9, scale=0)
np.testing.assert_almost_equal(im9.scale, nyq_scale, 9,
"obj.drawImage(im9,scale=0) produced image with wrong scale")
np.testing.assert_almost_equal(im9.array.sum(), test_flux, 4,
"obj.drawImage(im9,scale=0) produced image with wrong flux")
np.testing.assert_almost_equal(CalculateScale(im9), 2, 2,
"Measured wrong scale after obj.drawImage(im9,scale=0)")
# Test if we provide nx, ny, and scale. It should:
# - create a new image with the right size
# - set the scale
ny = 100 # Make it non-square
im10 = obj.drawImage(nx=nx, ny=ny, scale=scale)
np.testing.assert_equal(im10.array.shape, (ny, nx),
"obj.drawImage(nx,ny,scale) produced image with wrong size")
np.testing.assert_almost_equal(im10.scale, scale, 9,
"obj.drawImage(nx,ny,scale) produced image with wrong scale")
np.testing.assert_almost_equal(im10.array.sum(), test_flux, 4,
"obj.drawImage(nx,ny,scale) produced image with wrong flux")
mom = galsim.utilities.unweighted_moments(im10)
np.testing.assert_almost_equal(
mom['Mx'], (nx+1.)/2., 4, "obj.drawImage(nx,ny,scale) (even) did not center in x correctly")
np.testing.assert_almost_equal(
mom['My'], (ny+1.)/2., 4, "obj.drawImage(nx,ny,scale) (even) did not center in y correctly")
# Repeat with odd nx,ny
im10 = obj.drawImage(nx=nx+1, ny=ny+1, scale=scale)
np.testing.assert_equal(im10.array.shape, (ny+1, nx+1),
"obj.drawImage(nx,ny,scale) produced image with wrong size")
np.testing.assert_almost_equal(im10.scale, scale, 9,
"obj.drawImage(nx,ny,scale) produced image with wrong scale")
np.testing.assert_almost_equal(im10.array.sum(), test_flux, 4,
"obj.drawImage(nx,ny,scale) produced image with wrong flux")
mom = galsim.utilities.unweighted_moments(im10)
np.testing.assert_almost_equal(
mom['Mx'], (nx+1.+1.)/2., 4,
"obj.drawImage(nx,ny,scale) (odd) did not center in x correctly")
np.testing.assert_almost_equal(
mom['My'], (ny+1.+1.)/2., 4,
"obj.drawImage(nx,ny,scale) (odd) did not center in y correctly")
# Test if we provide nx, ny, and no scale. It should:
# - create a new image with the right size
# - set the scale to obj2.nyquist_scale
im10 = obj.drawImage(nx=nx, ny=ny)
np.testing.assert_equal(im10.array.shape, (ny, nx),
"obj.drawImage(nx,ny) produced image with wrong size")
np.testing.assert_almost_equal(im10.scale, nyq_scale, 9,
"obj.drawImage(nx,ny) produced image with wrong scale")
np.testing.assert_almost_equal(im10.array.sum(), test_flux, 4,
"obj.drawImage(nx,ny) produced image with wrong flux")
mom = galsim.utilities.unweighted_moments(im10)
np.testing.assert_almost_equal(
mom['Mx'], (nx+1.)/2., 4, "obj.drawImage(nx,ny) (even) did not center in x correctly")
np.testing.assert_almost_equal(
mom['My'], (ny+1.)/2., 4, "obj.drawImage(nx,ny) (even) did not center in y correctly")
# Repeat with odd nx,ny
im10 = obj.drawImage(nx=nx+1, ny=ny+1)
np.testing.assert_equal(im10.array.shape, (ny+1, nx+1),
"obj.drawImage(nx,ny) produced image with wrong size")
np.testing.assert_almost_equal(im10.scale, nyq_scale, 9,
"obj.drawImage(nx,ny) produced image with wrong scale")
np.testing.assert_almost_equal(im10.array.sum(), test_flux, 4,
"obj.drawImage(nx,ny) produced image with wrong flux")
mom = galsim.utilities.unweighted_moments(im10)
np.testing.assert_almost_equal(
mom['Mx'], (nx+1.+1.)/2., 4, "obj.drawImage(nx,ny) (odd) did not center in x correctly")
np.testing.assert_almost_equal(
mom['My'], (ny+1.+1.)/2., 4, "obj.drawImage(nx,ny) (odd) did not center in y correctly")
# Test if we provide bounds and scale. It should:
# - create a new image with the right size
# - set the scale
bounds = galsim.BoundsI(1,nx,1,ny+1)
im10 = obj.drawImage(bounds=bounds, scale=scale)
np.testing.assert_equal(im10.array.shape, (ny+1, nx),
"obj.drawImage(bounds,scale) produced image with wrong size")
np.testing.assert_almost_equal(im10.scale, scale, 9,
"obj.drawImage(bounds,scale) produced image with wrong scale")
np.testing.assert_almost_equal(im10.array.sum(), test_flux, 4,
"obj.drawImage(bounds,scale) produced image with wrong flux")
mom = galsim.utilities.unweighted_moments(im10)
np.testing.assert_almost_equal(mom['Mx'], (nx+1.)/2., 4,
"obj.drawImage(bounds,scale) did not center in x correctly")
np.testing.assert_almost_equal(mom['My'], (ny+1.+1.)/2., 4,
"obj.drawImage(bounds,scale) did not center in y correctly")
# Test if we provide bounds and no scale. It should:
# - create a new image with the right size
# - set the scale to obj2.nyquist_scale
bounds = galsim.BoundsI(1,nx,1,ny+1)
im10 = obj.drawImage(bounds=bounds)
np.testing.assert_equal(im10.array.shape, (ny+1, nx),
"obj.drawImage(bounds) produced image with wrong size")
np.testing.assert_almost_equal(im10.scale, nyq_scale, 9,
"obj.drawImage(bounds) produced image with wrong scale")
np.testing.assert_almost_equal(im10.array.sum(), test_flux, 4,
"obj.drawImage(bounds) produced image with wrong flux")
mom = galsim.utilities.unweighted_moments(im10)
np.testing.assert_almost_equal(mom['Mx'], (nx+1.)/2., 4,
"obj.drawImage(bounds) did not center in x correctly")
np.testing.assert_almost_equal(mom['My'], (ny+1.+1.)/2., 4,
"obj.drawImage(bounds) did not center in y correctly")
# Test if we provide nx, ny, scale, and center. It should:
# - create a new image with the right size
# - set the scale
# - set the center to be as close as possible to center
for center in [(3,3), (10.2, 11.9), (10.55, -23.8)]:
im11 = obj.drawImage(nx=nx, ny=ny, scale=scale, center=center)
np.testing.assert_equal(im11.array.shape, (ny, nx))
np.testing.assert_almost_equal(im11.scale, scale, 9)
np.testing.assert_almost_equal(im11.array.sum(), test_flux, 4)
print('center, true = ',center,im8.true_center)
assert abs(center[0] - im11.true_center.x) <= 0.5
assert abs(center[1] - im11.true_center.y) <= 0.5
# Repeat with odd nx,ny
im11 = obj.drawImage(nx=nx+1, ny=ny+1, scale=scale, center=center)
np.testing.assert_equal(im11.array.shape, (ny+1, nx+1))
np.testing.assert_almost_equal(im11.scale, scale, 9)
np.testing.assert_almost_equal(im11.array.sum(), test_flux, 4)
assert abs(center[0] - im11.true_center.x) <= 0.5
assert abs(center[1] - im11.true_center.y) <= 0.5
# Combinations that raise errors:
assert_raises(TypeError, obj.drawImage, image=im10, bounds=bounds)
assert_raises(TypeError, obj.drawImage, image=im10, dtype=int)
assert_raises(TypeError, obj.drawImage, nx=3, ny=4, image=im10, scale=scale)
assert_raises(TypeError, obj.drawImage, nx=3, ny=4, image=im10)
assert_raises(TypeError, obj.drawImage, nx=3, ny=4, bounds=bounds)
assert_raises(TypeError, obj.drawImage, nx=3, ny=4, add_to_image=True)
assert_raises(TypeError, obj.drawImage, nx=3, ny=4, center=True)
assert_raises(TypeError, obj.drawImage, nx=3, ny=4, center=23)
assert_raises(TypeError, obj.drawImage, bounds=bounds, add_to_image=True)
assert_raises(TypeError, obj.drawImage, image=galsim.Image(), add_to_image=True)
assert_raises(TypeError, obj.drawImage, nx=3)
assert_raises(TypeError, obj.drawImage, ny=3)
assert_raises(TypeError, obj.drawImage, nx=3, ny=3, invalid=True)
assert_raises(TypeError, obj.drawImage, bounds=bounds, scale=scale, wcs=galsim.PixelScale(3))
assert_raises(TypeError, obj.drawImage, bounds=bounds, wcs=scale)
assert_raises(TypeError, obj.drawImage, image=im10.array)
assert_raises(TypeError, obj.drawImage, wcs=galsim.FitsWCS(
os.path.join(os.path.dirname(__file__), 'fits_files/tpv.fits')))
assert_raises(ValueError, obj.drawImage, bounds=galsim.BoundsI())
assert_raises((ValueError, Exception), obj.drawImage, image=im10, gain=0.)
assert_raises((ValueError, Exception), obj.drawImage, image=im10, gain=-1.)
assert_raises((ValueError, Exception), obj.drawImage, image=im10, area=0.)
assert_raises((ValueError, Exception), obj.drawImage, image=im10, area=-1.)
assert_raises((ValueError, Exception), obj.drawImage, image=im10, exptime=0.)
assert_raises((ValueError, Exception), obj.drawImage, image=im10, exptime=-1.)
assert_raises(ValueError, obj.drawImage, image=im10, method='invalid')
# These options are invalid unless metho=phot
assert_raises(TypeError, obj.drawImage, image=im10, n_photons=3)
assert_raises(TypeError, obj.drawImage, rng=galsim.BaseDeviate(234))
assert_raises(ValueError, obj.drawImage, max_extra_noise=23)
assert_raises(TypeError, obj.drawImage, poisson_flux=True)
assert_raises(TypeError, obj.drawImage, maxN=10000)
assert_raises(TypeError, obj.drawImage, save_photons=True)
@timer
def test_draw_methods():
"""Test the the different method options do the right thing.
"""
# We use a simple Exponential for our object:
obj = galsim.Exponential(flux=test_flux, scale_radius=1.09)
test_scale = 0.28
pix = galsim.Pixel(scale=test_scale)
obj_pix = galsim.Convolve(obj, pix)
N = 64
im1 = galsim.ImageD(N, N, scale=test_scale)
# auto and fft should be equivalent to drawing obj_pix with no_pixel
im1 = obj.drawImage(image=im1)
im2 = obj_pix.drawImage(image=im1.copy(), method='no_pixel')
print('im1 flux diff = ',abs(im1.array.sum() - test_flux))
np.testing.assert_almost_equal(
im1.array.sum(), test_flux, 2,
"obj.drawImage() produced image with wrong flux")
print('im2 flux diff = ',abs(im2.array.sum() - test_flux))
np.testing.assert_almost_equal(
im2.array.sum(), test_flux, 2,
"obj_pix.drawImage(no_pixel) produced image with wrong flux")
print('im1, im2 max diff = ',abs(im1.array - im2.array).max())
np.testing.assert_array_almost_equal(
im1.array, im2.array, 6,
"obj.drawImage() differs from obj_pix.drawImage(no_pixel)")
im3 = obj.drawImage(image=im1.copy(), method='fft')
print('im1, im3 max diff = ',abs(im1.array - im3.array).max())
np.testing.assert_array_almost_equal(
im1.array, im3.array, 6,
"obj.drawImage(fft) differs from obj.drawImage")
# real_space should be similar, but not precisely equal.
im4 = obj.drawImage(image=im1.copy(), method='real_space')
print('im1, im4 max diff = ',abs(im1.array - im4.array).max())
np.testing.assert_array_almost_equal(
im1.array, im4.array, 4,
"obj.drawImage(real_space) differs from obj.drawImage")
# sb should match xValue for pixel centers. And be scale**2 factor different from no_pixel.
im5 = obj.drawImage(image=im1.copy(), method='sb', use_true_center=False)
im5.setCenter(0,0)
print('im5(0,0) = ',im5(0,0))
print('obj.xValue(0,0) = ',obj.xValue(0.,0.))
np.testing.assert_almost_equal(
im5(0,0), obj.xValue(0.,0.), 6,
"obj.drawImage(sb) values do not match surface brightness given by xValue")
np.testing.assert_almost_equal(
im5(3,2), obj.xValue(3*test_scale, 2*test_scale), 6,
"obj.drawImage(sb) values do not match surface brightness given by xValue")
im5 = obj.drawImage(image=im5, method='sb')
print('im5(0,0) = ',im5(0,0))
print('obj.xValue(dx/2,dx/2) = ',obj.xValue(test_scale/2., test_scale/2.))
np.testing.assert_almost_equal(
im5(0,0), obj.xValue(0.5*test_scale, 0.5*test_scale), 6,
"obj.drawImage(sb) values do not match surface brightness given by xValue")
np.testing.assert_almost_equal(
im5(3,2), obj.xValue(3.5*test_scale, 2.5*test_scale), 6,
"obj.drawImage(sb) values do not match surface brightness given by xValue")
im6 = obj.drawImage(image=im1.copy(), method='no_pixel')
print('im6, im5*scale**2 max diff = ',abs(im6.array - im5.array*test_scale**2).max())
np.testing.assert_array_almost_equal(
im5.array * test_scale**2, im6.array, 6,
"obj.drawImage(sb) * scale**2 differs from obj.drawImage(no_pixel)")
# Drawing a truncated object, auto should be identical to real_space
obj = galsim.Sersic(flux=test_flux, n=3.7, half_light_radius=2, trunc=4)
obj_pix = galsim.Convolve(obj, pix)
# auto and real_space should be equivalent to drawing obj_pix with no_pixel
im1 = obj.drawImage(image=im1)
im2 = obj_pix.drawImage(image=im1.copy(), method='no_pixel')
print('im1 flux diff = ',abs(im1.array.sum() - test_flux))
np.testing.assert_almost_equal(
im1.array.sum(), test_flux, 2,
"obj.drawImage() produced image with wrong flux")
print('im2 flux diff = ',abs(im2.array.sum() - test_flux))
np.testing.assert_almost_equal(
im2.array.sum(), test_flux, 2,
"obj_pix.drawImage(no_pixel) produced image with wrong flux")
print('im1, im2 max diff = ',abs(im1.array - im2.array).max())
np.testing.assert_array_almost_equal(
im1.array, im2.array, 6,
"obj.drawImage() differs from obj_pix.drawImage(no_pixel)")
im4 = obj.drawImage(image=im1.copy(), method='real_space')
print('im1, im4 max diff = ',abs(im1.array - im4.array).max())
np.testing.assert_array_almost_equal(
im1.array, im4.array, 6,
"obj.drawImage(real_space) differs from obj.drawImage")
# fft should be similar, but not precisely equal.
with assert_warns(galsim.GalSimWarning):
# This emits a warning about convolving two things with hard edges.
im3 = obj.drawImage(image=im1.copy(), method='fft')
print('im1, im3 max diff = ',abs(im1.array - im3.array).max())
np.testing.assert_array_almost_equal(
im1.array, im3.array, 3, # Should be close, but not exact.
"obj.drawImage(fft) differs from obj.drawImage")
# sb should match xValue for pixel centers. And be scale**2 factor different from no_pixel.
im5 = obj.drawImage(image=im1.copy(), method='sb')
im5.setCenter(0,0)
print('im5(0,0) = ',im5(0,0))
print('obj.xValue(dx/2,dx/2) = ',obj.xValue(test_scale/2., test_scale/2.))
np.testing.assert_almost_equal(
im5(0,0), obj.xValue(0.5*test_scale, 0.5*test_scale), 6,
"obj.drawImage(sb) values do not match surface brightness given by xValue")
np.testing.assert_almost_equal(
im5(3,2), obj.xValue(3.5*test_scale, 2.5*test_scale), 6,
"obj.drawImage(sb) values do not match surface brightness given by xValue")
im6 = obj.drawImage(image=im1.copy(), method='no_pixel')
print('im6, im5*scale**2 max diff = ',abs(im6.array - im5.array*test_scale**2).max())
np.testing.assert_array_almost_equal(
im5.array * test_scale**2, im6.array, 6,
"obj.drawImage(sb) * scale**2 differs from obj.drawImage(no_pixel)")
@timer
def test_drawKImage():
"""Test the various optional parameters to the drawKImage function.
In particular test the parameters image, and scale in various combinations.
"""
# We use a Moffat profile with beta = 1.5, since its real-space profile is
# flux / (2 pi rD^2) * (1 + (r/rD)^2)^3/2
# and the 2-d Fourier transform of that is
# flux * exp(-rD k)
# So this should draw in Fourier space the same image as the Exponential drawn in
# test_drawImage().
obj = galsim.Moffat(flux=test_flux, beta=1.5, scale_radius=0.5)
obj = obj.withGSParams(maxk_threshold=1.e-4)
# First test drawKImage() with no kwargs. It should:
# - create new images
# - return the new images
# - set the scale to 2pi/(N*obj.nyquist_scale)
im1 = obj.drawKImage()
N = 1174
np.testing.assert_equal(im1.bounds, galsim.BoundsI(-N/2,N/2,-N/2,N/2),
"obj.drawKImage() produced image with wrong bounds")
stepk = obj.stepk
np.testing.assert_almost_equal(im1.scale, stepk, 9,
"obj.drawKImage() produced image with wrong scale")
np.testing.assert_almost_equal(CalculateScale(im1), 2, 1,
"Measured wrong scale after obj.drawKImage()")
# The flux in Fourier space is just the value at k=0
np.testing.assert_equal(im1.bounds.center, galsim.PositionI(0,0))
np.testing.assert_almost_equal(im1(0,0), test_flux, 2,
"obj.drawKImage() produced image with wrong flux")
# Imaginary component should all be 0.
np.testing.assert_almost_equal(im1.imag.array.sum(), 0., 3,
"obj.drawKImage() produced non-zero imaginary image")
# Test if we provide an image argument. It should:
# - write to the existing image
# - also return that image
# - set the scale to obj.stepk
# - zero out any existing data
im3 = galsim.ImageCD(1149,1149)
im4 = obj.drawKImage(im3)
np.testing.assert_almost_equal(im3.scale, stepk, 9,
"obj.drawKImage(im3) produced image with wrong scale")
np.testing.assert_almost_equal(im3(0,0), test_flux, 2,
"obj.drawKImage(im3) produced real image with wrong flux")
np.testing.assert_almost_equal(im3.imag.array.sum(), 0., 3,
"obj.drawKImage(im3) produced non-zero imaginary image")
np.testing.assert_almost_equal(CalculateScale(im3), 2, 1,
"Measured wrong scale after obj.drawKImage(im3)")
np.testing.assert_array_equal(im3.array, im4.array,
"im4 = obj.drawKImage(im3) produced im4 != im3")
im3.fill(9.8)
np.testing.assert_array_equal(im3.array, im4.array,
"im4 = obj.drawKImage(im3) produced im4 is not im3")
# Test if we provide an image with undefined bounds. It should:
# - resize the provided image
# - also return that image
# - set the scale to obj.stepk
im5 = galsim.ImageCD()
obj.drawKImage(im5)
np.testing.assert_almost_equal(im5.scale, stepk, 9,
"obj.drawKImage(im5) produced image with wrong scale")
np.testing.assert_almost_equal(im5(0,0), test_flux, 2,
"obj.drawKImage(im5) produced image with wrong flux")
np.testing.assert_almost_equal(im5.imag.array.sum(), 0., 3,
"obj.drawKImage(im5) produced non-zero imaginary image")
np.testing.assert_almost_equal(CalculateScale(im5), 2, 1,
"Measured wrong scale after obj.drawKImage(im5)")
np.testing.assert_equal(im5.bounds, galsim.BoundsI(-N/2,N/2,-N/2,N/2),
"obj.drawKImage(im5) produced image with wrong bounds")
# Test if we provide a scale to use. It should:
# - create a new image using that scale for the scale
# - return the new image
# - set the size large enough to contain 99.5% of the flux
scale = 0.51 # Just something different from 1 or stepk
im7 = obj.drawKImage(scale=scale)
np.testing.assert_almost_equal(im7.scale, scale, 9,
"obj.drawKImage(dx) produced image with wrong scale")
np.testing.assert_almost_equal(im7(0,0), test_flux, 2,
"obj.drawKImage(dx) produced image with wrong flux")
np.testing.assert_almost_equal(im7.imag.array.astype(float).sum(), 0., 2,
"obj.drawKImage(dx) produced non-zero imaginary image")
np.testing.assert_almost_equal(CalculateScale(im7), 2, 1,
"Measured wrong scale after obj.drawKImage(dx)")
# This image is smaller because not using nyquist scale for stepk
np.testing.assert_equal(im7.bounds, galsim.BoundsI(-37,37,-37,37),
"obj.drawKImage(dx) produced image with wrong bounds")
# Test if we provide an image with a defined scale. It should:
# - write to the existing image
# - use the image's scale
nx = 401
im9 = galsim.ImageCD(nx,nx, scale=scale)
obj.drawKImage(im9)
np.testing.assert_almost_equal(im9.scale, scale, 9,
"obj.drawKImage(im9) produced image with wrong scale")
np.testing.assert_almost_equal(im9(0,0), test_flux, 4,
"obj.drawKImage(im9) produced image with wrong flux")
np.testing.assert_almost_equal(im9.imag.array.sum(), 0., 5,
"obj.drawKImage(im9) produced non-zero imaginary image")
np.testing.assert_almost_equal(CalculateScale(im9), 2, 1,
"Measured wrong scale after obj.drawKImage(im9)")
# Test if we provide an image with a defined scale <= 0. It should:
# - write to the existing image
# - set the scale to obj.stepk
im3.scale = -scale
im3.setZero()
obj.drawKImage(im3)
np.testing.assert_almost_equal(im3.scale, stepk, 9,
"obj.drawKImage(im3) produced image with wrong scale")
np.testing.assert_almost_equal(im3(0,0), test_flux, 4,
"obj.drawKImage(im3) produced image with wrong flux")
np.testing.assert_almost_equal(im3.imag.array.sum(), 0., 5,
"obj.drawKImage(im3) produced non-zero imaginary image")
np.testing.assert_almost_equal(CalculateScale(im3), 2, 1,
"Measured wrong scale after obj.drawKImage(im3)")
im3.scale = 0
im3.setZero()
obj.drawKImage(im3)
np.testing.assert_almost_equal(im3.scale, stepk, 9,
"obj.drawKImage(im3) produced image with wrong scale")
np.testing.assert_almost_equal(im3(0,0), test_flux, 4,
"obj.drawKImage(im3) produced image with wrong flux")
np.testing.assert_almost_equal(im3.imag.array.sum(), 0., 5,
"obj.drawKImage(im3) produced non-zero imaginary image")
np.testing.assert_almost_equal(CalculateScale(im3), 2, 1,
"Measured wrong scale after obj.drawKImage(im3)")
# Test if we provide an image and dx. It should:
# - write to the existing image
# - use the provided dx
# - write the new dx value to the image's scale
im9.scale = scale + 0.3 # Just something other than scale
im9.setZero()
obj.drawKImage(im9, scale=scale)
np.testing.assert_almost_equal(
im9.scale, scale, 9,
"obj.drawKImage(im9,scale) produced image with wrong scale")
np.testing.assert_almost_equal(
im9(0,0), test_flux, 4,
"obj.drawKImage(im9,scale) produced image with wrong flux")
np.testing.assert_almost_equal(
im9.imag.array.sum(), 0., 5,
"obj.drawKImage(im9,scale) produced non-zero imaginary image")
np.testing.assert_almost_equal(CalculateScale(im9), 2, 1,
"Measured wrong scale after obj.drawKImage(im9,scale)")
# Test if we provide an image and scale <= 0. It should:
# - write to the existing image
# - set the scale to obj.stepk
im3.scale = scale + 0.3
im3.setZero()
obj.drawKImage(im3, scale=-scale)
np.testing.assert_almost_equal(
im3.scale, stepk, 9,
"obj.drawKImage(im3,scale<0) produced image with wrong scale")
np.testing.assert_almost_equal(
im3(0,0), test_flux, 4,
"obj.drawKImage(im3,scale<0) produced image with wrong flux")
np.testing.assert_almost_equal(
im3.imag.array.sum(), 0., 5,
"obj.drawKImage(im3,scale<0) produced non-zero imaginary image")
np.testing.assert_almost_equal(CalculateScale(im3), 2, 1,
"Measured wrong scale after obj.drawKImage(im3,scale<0)")
im3.scale = scale + 0.3
im3.setZero()
obj.drawKImage(im3, scale=0)
np.testing.assert_almost_equal(
im3.scale, stepk, 9,
"obj.drawKImage(im3,scale=0) produced image with wrong scale")
np.testing.assert_almost_equal(
im3(0,0), test_flux, 4,
"obj.drawKImage(im3,scale=0) produced image with wrong flux")
np.testing.assert_almost_equal(
im3.imag.array.sum(), 0., 5,
"obj.drawKImage(im3,scale=0) produced non-zero imaginary image")
np.testing.assert_almost_equal(CalculateScale(im3), 2, 1,
"Measured wrong scale after obj.drawKImage(im3,scale=0)")
# Test if we provide nx, ny, and scale. It should:
# - create a new image with the right size
# - set the scale
nx = 200 # Some randome non-square size
ny = 100
im4 = obj.drawKImage(nx=nx, ny=ny, scale=scale)
np.testing.assert_almost_equal(
im4.scale, scale, 9,
"obj.drawKImage(nx,ny,scale) produced image with wrong scale")
np.testing.assert_equal(
im4.array.shape, (ny, nx),
"obj.drawKImage(nx,ny,scale) produced image with wrong shape")
# Test if we provide nx, ny, and no scale. It should:
# - create a new image with the right size
# - set the scale to obj.stepk
im4 = obj.drawKImage(nx=nx, ny=ny)
np.testing.assert_almost_equal(
im4.scale, stepk, 9,
"obj.drawKImage(nx,ny) produced image with wrong scale")
np.testing.assert_equal(
im4.array.shape, (ny, nx),
"obj.drawKImage(nx,ny) produced image with wrong shape")
# Test if we provide bounds and no scale. It should:
# - create a new image with the right size
# - set the scale to obj.stepk
bounds = galsim.BoundsI(1,nx,1,ny)
im4 = obj.drawKImage(bounds=bounds)
np.testing.assert_almost_equal(
im4.scale, stepk, 9,
"obj.drawKImage(bounds) produced image with wrong scale")
np.testing.assert_equal(
im4.array.shape, (ny, nx),
"obj.drawKImage(bounds) produced image with wrong shape")
# Test if we provide bounds and scale. It should:
# - create a new image with the right size
# - set the scale
bounds = galsim.BoundsI(1,nx,1,ny)
im4 = obj.drawKImage(bounds=bounds, scale=scale)
np.testing.assert_almost_equal(
im4.scale, scale, 9,
"obj.drawKImage(bounds,scale) produced image with wrong scale")
np.testing.assert_equal(
im4.array.shape, (ny, nx),
"obj.drawKImage(bounds,scale) produced image with wrong shape")
# Test recenter = False option
bounds6 = galsim.BoundsI(0, nx//3, 0, ny//4)
im6 = obj.drawKImage(bounds=bounds6, scale=scale, recenter=False)
np.testing.assert_equal(
im6.bounds, bounds6,
"obj.drawKImage(bounds,scale,recenter=False) produced image with wrong bounds")
np.testing.assert_almost_equal(
im6.scale, scale, 9,
"obj.drawKImage(bounds,scale,recenter=False) produced image with wrong scale")
np.testing.assert_equal(
im6.array.shape, (ny//4+1, nx//3+1),
"obj.drawKImage(bounds,scale,recenter=False) produced image with wrong shape")
np.testing.assert_array_almost_equal(
im6.array, im4[bounds6].array, 9,
"obj.drawKImage(recenter=False) produced different values than recenter=True")
# Test recenter = False option
im6.setZero()
obj.drawKImage(im6, recenter=False)
np.testing.assert_almost_equal(
im6.scale, scale, 9,
"obj.drawKImage(image,recenter=False) produced image with wrong scale")
np.testing.assert_array_almost_equal(
im6.array, im4[bounds6].array, 9,
"obj.drawKImage(image,recenter=False) produced different values than recenter=True")
# Can add to image if recenter is False
im6.setZero()
obj.drawKImage(im6, recenter=False, add_to_image=True)
np.testing.assert_almost_equal(
im6.scale, scale, 9,
"obj.drawKImage(image,add_to_image=True) produced image with wrong scale")
np.testing.assert_array_almost_equal(
im6.array, im4[bounds6].array, 9,
"obj.drawKImage(image,add_to_image=True) produced different values than recenter=True")
# .. or if image is centered.
im7 = im4.copy()
im7.setZero()
im7.setCenter(0,0)
obj.drawKImage(im7, add_to_image=True)
np.testing.assert_almost_equal(
im7.scale, scale, 9,
"obj.drawKImage(image,add_to_image=True) produced image with wrong scale")
np.testing.assert_array_almost_equal(
im7.array, im4.array, 9,
"obj.drawKImage(image,add_to_image=True) produced different values than recenter=True")
# .. but otherwise not.
with assert_raises((Exception, galsim.GalSimIncompatibleValuesError)):
obj.drawKImage(image=im6, add_to_image=True)
# Other error combinations:
assert_raises(TypeError, obj.drawKImage, image=im6, bounds=bounds)
assert_raises(TypeError, obj.drawKImage, image=im6, dtype=int)
assert_raises(TypeError, obj.drawKImage, nx=3, ny=4, image=im6, scale=scale)
assert_raises(TypeError, obj.drawKImage, nx=3, ny=4, image=im6)
assert_raises(TypeError, obj.drawKImage, nx=3, ny=4, add_to_image=True)
assert_raises(TypeError, obj.drawKImage, nx=3, ny=4, bounds=bounds)
assert_raises(TypeError, obj.drawKImage, bounds=bounds, add_to_image=True)
assert_raises(TypeError, obj.drawKImage, image=galsim.Image(dtype=complex), add_to_image=True)
assert_raises(TypeError, obj.drawKImage, nx=3)
assert_raises(TypeError, obj.drawKImage, ny=3)
assert_raises(TypeError, obj.drawKImage, nx=3, ny=3, invalid=True)
assert_raises(TypeError, obj.drawKImage, bounds=bounds, wcs=galsim.PixelScale(3))
assert_raises(TypeError, obj.drawKImage, image=im6.array)
assert_raises(ValueError, obj.drawKImage, image=galsim.ImageF(3,4))
assert_raises(ValueError, obj.drawKImage, bounds=galsim.BoundsI())
@timer
def test_drawKImage_Gaussian():
"""Test the drawKImage function using known symmetries of the Gaussian Hankel transform.
See http://en.wikipedia.org/wiki/Hankel_transform.
"""
test_flux = 2.3 # Choose a non-unity flux
test_sigma = 17. # ...likewise for sigma
test_imsize = 45 # Dimensions of comparison image, doesn't need to be large
# Define a Gaussian GSObject
gal = galsim.Gaussian(sigma=test_sigma, flux=test_flux)
# Then define a related object which is in fact the opposite number in the Hankel transform pair
# For the Gaussian this is straightforward in our definition of the Fourier transform notation,
# and has sigma -> 1/sigma and flux -> flux * 2 pi / sigma**2
gal_hankel = galsim.Gaussian(sigma=1./test_sigma, flux=test_flux*2.*np.pi/test_sigma**2)
# Do a basic flux test: the total flux of the gal should equal gal_Hankel(k=(0, 0))
np.testing.assert_almost_equal(
gal.flux, gal_hankel.xValue(galsim.PositionD(0., 0.)), decimal=12,
err_msg="Test object flux does not equal k=(0, 0) mode of its Hankel transform conjugate.")
image_test = galsim.ImageD(test_imsize, test_imsize)
kimage_test = galsim.ImageCD(test_imsize, test_imsize)
# Then compare these two objects at a couple of different scale (reasonably matched for size)
for scale_test in (0.03 / test_sigma, 0.4 / test_sigma):
gal.drawKImage(image=kimage_test, scale=scale_test)
gal_hankel.drawImage(image_test, scale=scale_test, use_true_center=False, method='sb')
np.testing.assert_array_almost_equal(
kimage_test.real.array, image_test.array, decimal=12,
err_msg="Test object drawKImage() and drawImage() from Hankel conjugate do not match "
"for grid spacing scale = "+str(scale_test))
np.testing.assert_array_almost_equal(
kimage_test.imag.array, 0., decimal=12,
err_msg="Non-zero imaginary part for drawKImage from test object that is purely "
"centred on the origin.")
@timer
def test_drawKImage_Exponential_Moffat():
"""Test the drawKImage function using known symmetries of the Exponential Hankel transform
(which is a Moffat with beta=1.5).
See http://mathworld.wolfram.com/HankelTransform.html.
"""
test_flux = 4.1 # Choose a non-unity flux
test_scale_radius = 13. # ...likewise for scale_radius
test_imsize = 45 # Dimensions of comparison image, doesn't need to be large
# Define an Exponential GSObject
gal = galsim.Exponential(scale_radius=test_scale_radius, flux=test_flux)
# Then define a related object which is in fact the opposite number in the Hankel transform pair
# For the Exponential we need a Moffat, with scale_radius=1/scale_radius. The total flux under
# this Moffat with unit amplitude at r=0 is is pi * scale_radius**(-2) / (beta - 1)
# = 2. * pi * scale_radius**(-2) in this case, so it works analagously to the Gaussian above.
gal_hankel = galsim.Moffat(beta=1.5, scale_radius=1. / test_scale_radius,
flux=test_flux * 2. * np.pi / test_scale_radius**2)
# Do a basic flux test: the total flux of the gal should equal gal_Hankel(k=(0, 0))
np.testing.assert_almost_equal(
gal.flux, gal_hankel.xValue(galsim.PositionD(0., 0.)), decimal=12,
err_msg="Test object flux does not equal k=(0, 0) mode of its Hankel transform conjugate.")
image_test = galsim.ImageD(test_imsize, test_imsize)
kimage_test = galsim.ImageCD(test_imsize, test_imsize)
# Then compare these two objects at a couple of different scale (reasonably matched for size)
for scale_test in (0.15 / test_scale_radius, 0.6 / test_scale_radius):
gal.drawKImage(image=kimage_test, scale=scale_test)
gal_hankel.drawImage(image_test, scale=scale_test, use_true_center=False, method='sb')
np.testing.assert_array_almost_equal(
kimage_test.real.array, image_test.array, decimal=12,
err_msg="Test object drawKImageImage() and drawImage() from Hankel conjugate do not "+
"match for grid spacing scale = "+str(scale_test))
np.testing.assert_array_almost_equal(
kimage_test.imag.array, 0., decimal=12,
err_msg="Non-zero imaginary part for drawKImage from test object that is purely "+
"centred on the origin.")
@timer
def test_offset():
"""Test the offset parameter to the drawImage function.
"""
scale = 0.23
# Use some more exact GSParams. We'll be comparing FFT images to real-space convolved values,
# so we don't want to suffer from our overall accuracy being only about 10^-3.
# Update: It turns out the only one I needed to reduce to obtain the accuracy I wanted
# below is maxk_threshold. Perhaps this is a sign that we ought to lower it in general?
params = galsim.GSParams(maxk_threshold=1.e-4)
# We use a simple Exponential for our object:
gal = galsim.Exponential(flux=test_flux, scale_radius=0.5, gsparams=params)
pix = galsim.Pixel(scale, gsparams=params)
obj = galsim.Convolve([gal,pix], gsparams=params)
# The shapes of the images we will build
# Make sure all combinations of odd/even are represented.
shape_list = [ (256,256), (256,243), (249,260), (255,241), (270,260) ]
# Some reasonable (x,y) values at which to test the xValues (near the center)
xy_list = [ (128,128), (123,131), (126,124) ]
# The offsets to test
offset_list = [ (1,-3), (0.3,-0.1), (-2.3,-1.2) ]
# Make the images somewhat large so the moments are measured accurately.
for nx,ny in shape_list:
# First check that the image agrees with our calculation of the center
cenx = (nx+1.)/2.
ceny = (ny+1.)/2.
im = galsim.ImageD(nx,ny, scale=scale)
true_center = im.bounds.true_center
np.testing.assert_almost_equal(
cenx, true_center.x, 6,
"im.bounds.true_center.x is wrong for (nx,ny) = %d,%d"%(nx,ny))
np.testing.assert_almost_equal(
ceny, true_center.y, 6,
"im.bounds.true_center.y is wrong for (nx,ny) = %d,%d"%(nx,ny))
# Check that the default draw command puts the centroid in the center of the image.
obj.drawImage(im, method='sb')
mom = galsim.utilities.unweighted_moments(im)
np.testing.assert_almost_equal(
mom['Mx'], cenx, 5,
"obj.drawImage(im) not centered correctly for (nx,ny) = %d,%d"%(nx,ny))
np.testing.assert_almost_equal(
mom['My'], ceny, 5,
"obj.drawImage(im) not centered correctly for (nx,ny) = %d,%d"%(nx,ny))
# Can also use center to explicitly say we want to use the true_center.
im3 = obj.drawImage(im.copy(), method='sb', center=im.true_center)
np.testing.assert_array_almost_equal(im3.array, im.array)
# Test that a few pixel values match xValue.
# Note: we don't expect the FFT drawn image to match the xValues precisely, since the
# latter use real-space convolution, so they should just match to our overall accuracy
# requirement, which is something like 1.e-3 or so. But an image of just the galaxy
# should use real-space drawing, so should be pretty much exact.
im2 = galsim.ImageD(nx,ny, scale=scale)
gal.drawImage(im2, method='sb')
for x,y in xy_list:
u = (x-cenx) * scale
v = (y-ceny) * scale
np.testing.assert_almost_equal(
im(x,y), obj.xValue(galsim.PositionD(u,v)), 2,
"im(%d,%d) does not match xValue(%f,%f)"%(x,y,u,v))
np.testing.assert_almost_equal(
im2(x,y), gal.xValue(galsim.PositionD(u,v)), 6,
"im2(%d,%d) does not match xValue(%f,%f)"%(x,y,u,v))
# Check that offset moves the centroid by the right amount.
for offx, offy in offset_list:
# For integer offsets, we expect the centroids to come out pretty much exact.
# (Only edge effects of the image should produce any error, and those are very small.)
# However, for non-integer effects, we don't actually expect the centroids to be
# right, even with perfect image rendering. To see why, imagine using a delta function
# for the galaxy. The centroid changes discretely, not continuously as the offset
# varies. The effect isn't as severe of course for our Exponential, but the effect
# is still there in part. Hence, only use 2 decimal places for non-integer offsets.
if offx == int(offx) and offy == int(offy):
decimal = 4
else:
decimal = 2
offset = galsim.PositionD(offx,offy)
obj.drawImage(im, method='sb', offset=offset)
mom = galsim.utilities.unweighted_moments(im)
np.testing.assert_almost_equal(
mom['Mx'], cenx+offx, decimal,
"obj.drawImage(im,offset) not centered correctly for (nx,ny) = %d,%d"%(nx,ny))
np.testing.assert_almost_equal(
mom['My'], ceny+offy, decimal,
"obj.drawImage(im,offset) not centered correctly for (nx,ny) = %d,%d"%(nx,ny))
# Test that a few pixel values match xValue
gal.drawImage(im2, method='sb', offset=offset)
for x,y in xy_list: