forked from GalSim-developers/GalSim
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_chromatic.py
More file actions
3431 lines (3008 loc) · 162 KB
/
Copy pathtest_chromatic.py
File metadata and controls
3431 lines (3008 loc) · 162 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 copy
import os
import copy
import numpy as np
import galsim
from galsim_test_helpers import *
bppath = os.path.join(galsim.meta_data.share_dir, "bandpasses")
sedpath = os.path.join(galsim.meta_data.share_dir, "SEDs")
# from pylab import *
# def plotme(image):
# imshow(image.array)
# show()
# liberal use of globals here...
zenith_angle = 20 * galsim.degrees
R500 = galsim.dcr.get_refraction(500.0, zenith_angle) # normalize refraction to 500nm
# some profile parameters to test with
bulge_n = 4.0
bulge_hlr = 0.5
bulge_e1 = 0.2
bulge_e2 = 0.2
disk_n = 1.0
disk_hlr = 1.0
disk_e1 = 0.4
disk_e2 = 0.2
PSF_hlr = 0.3
PSF_beta = 3.0
PSF_e1 = 0.01
PSF_e2 = 0.06
shear_g1 = 0.01
shear_g2 = 0.02
# load a filter
bandpass = (galsim.Bandpass(os.path.join(bppath, 'LSST_r.dat'), 'nm'))
bandpass_g = (galsim.Bandpass(os.path.join(bppath, 'LSST_g.dat'), 'nm'))
bandpass_z = (galsim.Bandpass(os.path.join(bppath, 'LSST_z.dat'), 'nm'))
# load some spectra
bulge_SED = (galsim.SED(os.path.join(sedpath, 'CWW_E_ext.sed'), wave_type='ang',
flux_type='flambda')
.thin(rel_err=1e-3)
.withFluxDensity(target_flux_density=0.3, wavelength=500.0))
disk_SED = (galsim.SED(os.path.join(sedpath, 'CWW_Sbc_ext.sed'), wave_type='ang',
flux_type='flambda')
.thin(rel_err=1e-3)
.withFluxDensity(target_flux_density=0.3, wavelength=500.0))
# define the directory containing some reference images
refdir = os.path.join(".", "chromatic_reference_images") # Directory containing the reference
@timer
def test_draw_add_commutativity():
"""Compare two chromatic images, one generated by adding up GSObject profiles before drawing,
and one generated (via galsim.chromatic) by drawing image summands wavelength-by-wavelength
while updating the profile and adding as you go.
"""
import time
stamp_size = 32
pixel_scale = 0.2
#------------------------------------------------------------------------------
# Use galsim.base functions to generate chromaticity by creating an effective
# PSF by adding together weighted monochromatic PSFs.
# Profiles are added together before drawing.
#------------------------------------------------------------------------------
# make galaxy
GS_gal = galsim.Sersic(n=bulge_n, half_light_radius=bulge_hlr)
GS_gal = GS_gal.shear(e1=bulge_e1, e2=bulge_e2)
GS_gal = GS_gal.shear(g1=shear_g1, g2=shear_g2)
# make effective PSF with Riemann sum midpoint rule
mPSFs = [] # list of flux-scaled monochromatic PSFs
N = 50
h = (bandpass.red_limit * 1.0 - bandpass.blue_limit) / N
ws = [bandpass.blue_limit + h*(i+0.5) for i in range(N)]
shift_fn = lambda w:(0, ((galsim.dcr.get_refraction(w, zenith_angle) - R500)
* (galsim.radians / galsim.arcsec)))
dilate_fn = lambda w:(w/500.0)**(-0.2)
for w in ws:
flux = bulge_SED(w) * bandpass(w) * h
mPSF = galsim.Moffat(flux=flux, beta=PSF_beta, half_light_radius=PSF_hlr*dilate_fn(w))
mPSF = mPSF.withGSParams(maxk_threshold=1.e-4)
mPSF = mPSF.shear(e1=PSF_e1, e2=PSF_e2)
mPSF = mPSF.shift(shift_fn(w))
mPSFs.append(mPSF)
PSF = galsim.Add(mPSFs)
# final profile
final = galsim.Convolve([GS_gal, PSF])
GS_image = galsim.ImageD(stamp_size, stamp_size, scale=pixel_scale)
GS_kimage = galsim.ImageCD(stamp_size, stamp_size, scale=pixel_scale)
t2 = time.time()
GS_image = final.drawImage(image=GS_image)
GS_kimage = final.drawKImage(image=GS_kimage)
t3 = time.time()
print('GS_object drawImage, drawKImage took {0} seconds.'.format(t3-t2))
# plotme(GS_image)
#------------------------------------------------------------------------------
# Use galsim.chromatic to generate chromaticity. Internally, this module draws
# the result at each wavelength and adds the results together. I.e., drawing
# and adding happen in the reverse order of the above.
#------------------------------------------------------------------------------
# make galaxy
mono_gal = galsim.Sersic(n=bulge_n, half_light_radius=bulge_hlr)
chromatic_gal = mono_gal * bulge_SED
check_pickle(bulge_SED)
check_pickle(chromatic_gal, lambda x: x.drawImage(bandpass, method='no_pixel',
nx=10, ny=10, scale=1))
check_pickle(chromatic_gal)
# Shear object
chromatic_gal = chromatic_gal.shear(e1=bulge_e1, e2=bulge_e2)
chromatic_gal = chromatic_gal.shear(g1=shear_g1, g2=shear_g2)
check_pickle(chromatic_gal, lambda x: x.drawImage(bandpass, method='no_pixel',
nx=10, ny=10, scale=1))
check_pickle(chromatic_gal)
# make chromatic PSF
mono_PSF = galsim.Moffat(beta=PSF_beta, half_light_radius=PSF_hlr)
mono_PSF = mono_PSF.withGSParams(maxk_threshold=1.e-4)
mono_PSF = mono_PSF.shear(e1=PSF_e1, e2=PSF_e2)
chromatic_PSF = galsim.ChromaticTransformation(mono_PSF, flux_ratio=1.0)
check_pickle(chromatic_PSF, lambda x: (x.evaluateAtWavelength(bandpass.effective_wavelength)
.drawImage(method='no_pixel', nx=10, ny=10, scale=1)))
check_pickle(chromatic_PSF)
chromatic_PSF = chromatic_PSF.dilate(dilate_fn)
chromatic_PSF = chromatic_PSF.shift(shift_fn)
# final profile
chromatic_final = galsim.Convolve([chromatic_gal, chromatic_PSF])
chromatic_image = galsim.ImageD(stamp_size, stamp_size, scale=pixel_scale)
chromatic_kimage = galsim.ImageCD(stamp_size, stamp_size, scale=pixel_scale)
# use chromatic parent class to draw without ChromaticConvolution acceleration...
t4 = time.time()
integrator = galsim.integ.ContinuousIntegrator(galsim.integ.midptRule, N=N, use_endpoints=False)
# NB. You cannot use ChromaticObject.drawImage() here, since it will automatically farm out to
# the ChromaticConvolution version of drawImage rather than respecting the
# ChromaticObject specification. Using super() doesn't seem to work either. So I just
# went ahead and converted this statement to the new format. There are a couple other
# similar times in the test suite where we want to force it to use the base class
# implementation, so those had to be switched as well.
galsim.ChromaticObject.drawImage(chromatic_final, bandpass, image=chromatic_image,
integrator=integrator, add_to_image=True)
galsim.ChromaticObject.drawKImage(chromatic_final, bandpass, image=chromatic_kimage,
integrator=integrator)
t5 = time.time()
print('ChromaticObject drawImage, drawKImage took {0} seconds.'.format(t5-t4))
# plotme(chromatic_image)
peak = chromatic_image.array.max()
printval(GS_image, chromatic_image)
np.testing.assert_array_almost_equal(
chromatic_image.array/peak, GS_image.array/peak, 6,
err_msg="Directly computed chromatic image disagrees with image created using "
+"galsim.chromatic")
kpeak = chromatic_kimage.array.real.max()
np.testing.assert_array_almost_equal(
chromatic_kimage.array/kpeak, GS_kimage.array/kpeak, 6,
err_msg="Directly computed chromatic kimage disagrees with kimage created using "
+"galsim.chromatic")
# Repeat with multiple inseparable profiles.
delta = galsim.ChromaticObject(galsim.DeltaFunction()).rotate(lambda wave: wave*galsim.degrees)
chromatic_final2 = galsim.Convolve(chromatic_gal, chromatic_PSF, delta)
chromatic_final2.drawImage(bandpass, image=chromatic_image, integrator=integrator)
chromatic_final2.drawKImage(bandpass, image=chromatic_kimage, integrator=integrator)
# Note: fft vs real space differences now, so only accurate to 1.e-3
np.testing.assert_array_almost_equal(chromatic_image.array/peak, GS_image.array/peak, 3)
np.testing.assert_array_almost_equal(chromatic_kimage.array/kpeak, GS_kimage.array/kpeak, 6)
# Check error handling of too few sample points
integrator = galsim.integ.ContinuousIntegrator(galsim.integ.midptRule, N=1, use_endpoints=False)
with assert_raises(ValueError):
chromatic_final.drawImage(bandpass, integrator=integrator)
integrator = galsim.integ.ContinuousIntegrator(galsim.integ.trapzRule, N=1, use_endpoints=False)
with assert_raises(ValueError):
chromatic_final.drawImage(bandpass, integrator=integrator)
# As an aside, check for appropriate tests of 'integrator' argument.
assert_raises(ValueError, chromatic_final.drawImage, bandpass, method='no_pixel',
integrator='midp') # minor misspelling
assert_raises(ValueError, chromatic_final.drawKImage, bandpass,
integrator='midp') # minor misspelling
assert_raises(TypeError, chromatic_final.drawImage, bandpass, method='no_pixel',
integrator=galsim.integ.midptRule)
assert_raises(TypeError, chromatic_final.drawKImage, bandpass,
integrator=galsim.integ.midptRule)
# Can't use base class directly.
assert_raises(NotImplementedError, galsim.integ.ImageIntegrator)
@timer
def test_ChromaticConvolution_InterpolatedImage():
"""Check that we can interchange the order of integrating over wavelength and convolving for
separable ChromaticObjects. This involves storing the results of integrating first in an
InterpolatedImage.
"""
pixel_scale = 0.2
stamp_size = 32
# stars are fundamentally delta-fns with an SED
star = galsim.Gaussian(fwhm=1.e-8) * bulge_SED
mono_PSF = galsim.Gaussian(half_light_radius=PSF_hlr)
PSF = galsim.ChromaticAtmosphere(mono_PSF, base_wavelength=500.0,
zenith_angle=zenith_angle)
final = galsim.Convolve(star, PSF)
image = galsim.ImageD(stamp_size, stamp_size, scale=pixel_scale)
check_pickle(star)
check_pickle(PSF)
check_pickle(final)
# draw image using speed tricks in ChromaticConvolution.draw
# For this particular test, need to set iimult=4 in order to pass.
II_image = final.drawImage(bandpass, image=image, iimult=4)
II_flux = II_image.array.sum()
image2 = image.copy()
# draw image without any speed tricks using ChromaticObject.drawImage
D_image = galsim.ChromaticObject.drawImage(final, bandpass, image=image2)
D_flux = D_image.array.sum()
#compare
print('Flux when integrating first, convolving second: {0}'.format(II_flux))
print('Flux when convolving first, integrating second: {0}'.format(D_flux))
printval(II_image, D_image)
# This used to work with decimal=5, but is apparently sensitive to the particular details of the
# bandpass thinning used. decimal=4 is still good though.
np.testing.assert_array_almost_equal(
II_image.array, D_image.array, 4,
err_msg="ChromaticConvolution draw not equivalent to regular draw")
# Check flux scaling
II_image2 = (final * 2.).drawImage(bandpass, image=image, iimult=4)
II_flux2 = II_image2.array.sum()
np.testing.assert_array_almost_equal(
II_flux2, 2.*II_flux, 5,
err_msg="ChromaticConvolution * 2 resulted in wrong flux.")
@timer
def test_chromatic_add():
"""Test the `+` operator on ChromaticObjects"""
stamp_size = 32
pixel_scale = 0.2
# create galaxy profiles
mono_bulge = galsim.Sersic(n=bulge_n, half_light_radius=bulge_hlr)
bulge = mono_bulge * bulge_SED
bulge = bulge.shear(e1=bulge_e1, e2=bulge_e2)
mono_disk = galsim.Sersic(n=disk_n, half_light_radius=disk_hlr)
disk = mono_disk * disk_SED
disk = disk.shear(e1=disk_e1, e2=disk_e2)
# test `+` operator
bdgal = bulge + disk
bdgal = bdgal.shear(g1=shear_g1, g2=shear_g2)
# now shear the indiv profiles
bulge = bulge.shear(g1=shear_g1, g2=shear_g2)
disk = disk.shear(g1=shear_g1, g2=shear_g2)
# create PSF
mono_PSF = galsim.Moffat(beta=PSF_beta, half_light_radius=PSF_hlr)
mono_PSF = mono_PSF.shear(e1=PSF_e1, e2=PSF_e2)
chromatic_PSF = galsim.ChromaticAtmosphere(mono_PSF, base_wavelength=500.0,
zenith_angle=zenith_angle)
# create final profile
final = galsim.Convolve(bdgal, chromatic_PSF)
image = galsim.ImageD(stamp_size, stamp_size, scale=pixel_scale)
image = final.drawImage(bandpass, image=image)
check_pickle(bulge)
check_pickle(disk)
check_pickle(bdgal)
check_pickle(chromatic_PSF)
check_pickle(final)
bulge_image = galsim.ImageD(stamp_size, stamp_size, scale=pixel_scale)
bulge_part = galsim.Convolve([bulge, chromatic_PSF])
bulge_image = bulge_part.drawImage(bandpass, image=bulge_image)
disk_image = galsim.ImageD(stamp_size, stamp_size, scale=pixel_scale)
disk_part = galsim.Convolve([disk, chromatic_PSF])
disk_image = disk_part.drawImage(bandpass, image=disk_image)
piecewise_image = bulge_image + disk_image
print('bulge image flux: {0}'.format(bulge_image.array.sum()))
print('disk image flux: {0}'.format(disk_image.array.sum()))
print('piecewise image flux: {0}'.format(piecewise_image.array.sum()))
print('bdimage flux: {0}'.format(image.array.sum()))
printval(image, piecewise_image)
np.testing.assert_array_almost_equal(
image.array, piecewise_image.array, 6,
err_msg="`+` operator doesn't match manual image addition")
# Check flux scaling
flux = image.array.sum()
image = (final * 2.).drawImage(bandpass, image=image)
flux2 = image.array.sum()
np.testing.assert_array_almost_equal(
flux2, 2.*flux, 5,
err_msg="ChromaticConvolution with sum * 2 resulted in wrong flux.")
# apply flux scaling to ChromaticSum
final2 = galsim.Convolve(bdgal*2, chromatic_PSF)
image = final2.drawImage(bandpass, image=image)
flux2 = image.array.sum()
np.testing.assert_array_almost_equal(
flux2, 2.*flux, 5,
err_msg="ChromaticSum * 2 resulted in wrong flux.")
# also check that a - b == a + (-b)
c = bulge_part - disk_part
d = bulge_part + (-disk_part)
assert c == d
cimage = c.drawImage(bandpass, nx=stamp_size, ny=stamp_size, scale=pixel_scale)
dimage = d.drawImage(bandpass, cimage.copy())
np.testing.assert_equal(cimage, dimage, "chromatic a-b != a+(-b)")
@timer
def test_dcr_moments():
"""Check that zenith-direction surface brightness distribution first and second moments obey
expected behavior for differential chromatic refraction when comparing objects drawn with
different SEDs."""
stamp_size = 256
pixel_scale = 0.025
# stars are fundamentally delta-fns with an SED
star1 = galsim.Gaussian(fwhm=1.e-8) * bulge_SED
star2 = galsim.Gaussian(fwhm=1.e-8) * disk_SED
shift_fn = lambda w:(0, ((galsim.dcr.get_refraction(w, zenith_angle) - R500)
* (galsim.radians / galsim.arcsec)))
mono_PSF = galsim.Moffat(beta=PSF_beta, half_light_radius=PSF_hlr)
PSF = galsim.ChromaticObject(mono_PSF).shift(shift_fn)
final1 = galsim.Convolve([star1, PSF])
final2 = galsim.Convolve([star2, PSF])
image1 = galsim.ImageD(stamp_size, stamp_size, scale=pixel_scale)
image2 = galsim.ImageD(stamp_size, stamp_size, scale=pixel_scale)
image1 = final1.drawImage(bandpass, image=image1)
image2 = final2.drawImage(bandpass, image=image2)
# plotme(image1)
mom1 = galsim.utilities.unweighted_moments(image1)
mom2 = galsim.utilities.unweighted_moments(image2)
dR_image = (mom1['My'] - mom2['My']) * pixel_scale
dV_image = (mom1['Myy'] - mom2['Myy']) * (pixel_scale)**2
# analytic moment differences
R_bulge, V_bulge = bulge_SED.calculateDCRMomentShifts(bandpass, zenith_angle=zenith_angle)
R_disk, V_disk = disk_SED.calculateDCRMomentShifts(bandpass, zenith_angle=zenith_angle)
dR_analytic = (R_bulge[1] - R_disk[1]) * 180.0/np.pi * 3600
dV_analytic = (V_bulge[1,1] - V_disk[1,1]) * (180.0/np.pi * 3600)**2
# also compute dR_analytic using ChromaticObject.calculateCentroid()
centroid1 = final1.calculateCentroid(bandpass)
centroid2 = final2.calculateCentroid(bandpass)
dR_centroid = (centroid1 - centroid2).y
print('image delta R: {0}'.format(dR_image))
print('analytic delta R: {0}'.format(dR_analytic))
print('centroid delta R: {0}'.format(dR_centroid))
print('image delta V: {0}'.format(dV_image))
print('analytic delta V: {0}'.format(dV_analytic))
np.testing.assert_almost_equal(dR_image, dR_analytic, 5,
err_msg="dRbar Shift from DCR doesn't match analytic formula")
np.testing.assert_almost_equal(dR_analytic, dR_centroid, 10,
err_msg="direct dRbar calculation doesn't match"
+" ChromaticObject.calculateCentroid()")
np.testing.assert_almost_equal(dV_image, dV_analytic, 5,
err_msg="dV Shift from DCR doesn't match analytic formula")
@timer
def test_chromatic_seeing_moments():
"""Check that surface brightness distribution second moments obey expected behavior
for chromatic seeing when comparing stars drawn with different SEDs."""
pixel_scale = 0.0075
stamp_size = 1024
# stars are fundamentally delta-fns with an SED
star1 = galsim.Gaussian(fwhm=1e-8) * bulge_SED
star2 = galsim.Gaussian(fwhm=1e-8) * disk_SED
indices = [-0.2, 0.6, 1.0]
for index in indices:
mono_PSF = galsim.Gaussian(half_light_radius=PSF_hlr)
PSF = galsim.ChromaticObject(mono_PSF).dilate(lambda w:(w/500.0)**index)
final1 = galsim.Convolve([star1, PSF])
final2 = galsim.Convolve([star2, PSF])
image1 = galsim.ImageD(stamp_size, stamp_size, scale=pixel_scale)
image2 = galsim.ImageD(stamp_size, stamp_size, scale=pixel_scale)
image1 = final1.drawImage(bandpass, image=image1)
image2 = final2.drawImage(bandpass, image=image2)
shape1 = galsim.utilities.unweighted_shape(image1)
shape2 = galsim.utilities.unweighted_shape(image2)
dr2byr2_image = (shape1['rsqr'] - shape2['rsqr']) / shape1['rsqr']
# analytic moment differences
r2_1 = bulge_SED.calculateSeeingMomentRatio(bandpass, alpha=index)
r2_2 = disk_SED.calculateSeeingMomentRatio(bandpass, alpha=index)
dr2byr2_analytic = (r2_1 - r2_2) / r2_1
np.testing.assert_almost_equal(dr2byr2_image, dr2byr2_analytic, 5,
err_msg="Moment Shift from chromatic seeing doesn't"+
" match analytic formula")
print('image delta(r^2) / r^2: {0}'.format(dr2byr2_image))
print('analytic delta(r^2) / r^2: {0}'.format(dr2byr2_analytic))
@timer
def test_monochromatic_filter():
"""Check that ChromaticObject drawn through a very narrow band filter matches analogous
GSObject.
"""
pixel_scale = 0.2
stamp_size = 32
chromatic_gal = galsim.Gaussian(fwhm=1.0) * bulge_SED
GS_gal = galsim.Gaussian(fwhm=1.0)
shift_fn = lambda w:(0, ((galsim.dcr.get_refraction(w, zenith_angle) - R500)
* (galsim.radians / galsim.arcsec)))
dilate_fn = lambda wave: (wave/500.0)**(-0.2)
mono_PSF = galsim.Gaussian(half_light_radius=PSF_hlr)
mono_PSF = mono_PSF.shear(e1=PSF_e1, e2=PSF_e2)
chromatic_PSF = galsim.ChromaticObject(mono_PSF).dilate(dilate_fn).shift(shift_fn)
chromatic_final = galsim.Convolve([chromatic_gal, chromatic_PSF])
fws = [350, 475, 625, 750, 875, 975] # approximate ugrizy filter central wavelengths
for fw in fws:
chromatic_image = galsim.ImageD(stamp_size, stamp_size, scale=pixel_scale)
narrow_filter = galsim.Bandpass(galsim.LookupTable([fw-0.01, fw, fw+0.01],
[1.0, 1.0, 1.0],
interpolant='linear'), 'nm')
chromatic_image = chromatic_final.drawImage(narrow_filter, image=chromatic_image)
# take out normalization
chromatic_image /= 0.02
chromatic_image /= bulge_SED(fw)
# now do non-chromatic version
GS_PSF = galsim.Gaussian(half_light_radius=PSF_hlr)
GS_PSF = GS_PSF.shear(e1=PSF_e1, e2=PSF_e2)
GS_PSF = GS_PSF.dilate(dilate_fn(fw))
GS_PSF = GS_PSF.shift(shift_fn(fw))
GS_final = galsim.Convolve([GS_gal, GS_PSF])
GS_image = galsim.ImageD(stamp_size, stamp_size, scale=pixel_scale)
GS_final.drawImage(image=GS_image)
# plotme(GS_image)
printval(chromatic_image, GS_image)
np.testing.assert_array_almost_equal(chromatic_image.array, GS_image.array, 5,
err_msg="ChromaticObject.drawImage() with monochromatic filter doesn't match"+
"GSObject.drawImage()")
@timer
def test_monochromatic_sed(run_slow):
# Similar to the above test, but this time with a broad bandpass and a narrow sed.
bandpass = galsim.Bandpass(galsim.LookupTable([500,1000], [1,1], 'linear'), wave_type='nm')
flux = 1.e6
rng = galsim.BaseDeviate(1234)
pixel_scale = 0.01
gal_achrom = galsim.Sersic(n=2.8, half_light_radius=0.03, flux=flux)
diam = 3.1 # meters
obscuration = 0.11
nstruts = 5
aberrations = np.array([0,0,0,0, 0.02, -0.05, -0.15, -0.02, 0.13, 0.06, -0.09, 0.11])
if run_slow:
wave_list = [515, 690, 900]
else:
wave_list = [515]
for wave in wave_list:
# First do the achromatic version at the given wavelength.
psf_achrom = galsim.OpticalPSF(lam=wave, diam=diam,
aberrations=aberrations, obscuration=obscuration,
nstruts=nstruts)
obj_achrom = galsim.Convolve(gal_achrom, psf_achrom)
im1 = galsim.ImageD(50, 50, scale=pixel_scale)
print('obj_achrom = ',obj_achrom)
print('im1 = ',im1)
obj_achrom.drawImage(image=im1)
print('im1.max,sum = ', im1.array.max(), im1.array.sum())
# Next do this chromatically using an emission line SED.
psf_chrom = galsim.ChromaticOpticalPSF(lam=wave, diam=diam,
aberrations=aberrations, obscuration=obscuration,
nstruts=nstruts)
sed = galsim.EmissionLine(wave)
gal_chrom = (gal_achrom * sed).withFlux(flux, bandpass=bandpass)
obj_chrom = galsim.Convolve(gal_chrom, psf_chrom)
im2 = obj_chrom.drawImage(bandpass, image=im1.copy())
print('im2.max,sum = ', im2.array.max(), im2.array.sum())
print('max diff/flux = ',np.max(np.abs(im1.array-im2.array)/flux))
np.testing.assert_allclose(im2.array/flux, im1.array/flux, rtol=1.e-4, atol=1.e-5)
# Now achromatic with phot
im3 = obj_achrom.drawImage(image=im1.copy(), method='phot', rng=rng)
print('im3.max,sum = ', im3.array.max(), im3.array.sum())
print('max diff/flux = ',np.max(np.abs(im1.array-im3.array)/flux))
np.testing.assert_allclose(im3.array/flux, im1.array/flux, atol=3.e-4)
# Finally, chromatic with phot
im4 = obj_chrom.drawImage(bandpass, image=im1.copy(), method='phot', rng=rng)
print('im4.max,sum = ', im4.array.max(), im4.array.sum())
print('max diff/flux = ',np.max(np.abs(im1.array-im4.array)/flux))
np.testing.assert_allclose(im4.array/flux, im1.array/flux, atol=3.e-4)
@timer
def test_chromatic_flux():
"""Test that the total drawn flux is equal to the integral of bandpass * sed over wavelength.
"""
pixel_scale = 0.5
stamp_size = 64
# stars are fundamentally delta-fns with an SED
star = galsim.Gaussian(fwhm=1e-8) * bulge_SED
mono_PSF = galsim.Gaussian(half_light_radius=PSF_hlr)
PSF = galsim.ChromaticAtmosphere(mono_PSF, base_wavelength=500,
zenith_angle=zenith_angle)
final = galsim.Convolve([star, PSF])
image = galsim.ImageD(stamp_size, stamp_size, scale=pixel_scale)
image2 = galsim.ImageD(stamp_size, stamp_size, scale=pixel_scale)
final.drawImage(bandpass, image=image)
ChromaticConvolve_flux = image.array.sum()
galsim.ChromaticObject.drawImage(final, bandpass, image=image2)
ChromaticObject_flux = image2.array.sum()
# analytic integral...
analytic_flux = bulge_SED.calculateFlux(bandpass)
printval(image, image2)
np.testing.assert_almost_equal(ChromaticObject_flux/analytic_flux, 1.0, 5,
err_msg="Drawn ChromaticObject flux doesn't match " +
"analytic prediction")
np.testing.assert_almost_equal(ChromaticConvolve_flux/analytic_flux, 1.0, 5,
err_msg="Drawn ChromaticConvolve flux doesn't match " +
"analytic prediction")
# Also check that the flux is okay and the image fairly consistent when using interpolation
# for the ChromaticAtmosphere.
PSF = PSF.interpolate(waves=np.linspace(bandpass.blue_limit, bandpass.red_limit, 30))
final_int = galsim.Convolve([star, PSF])
image3 = galsim.ImageD(stamp_size, stamp_size, scale=pixel_scale)
final_int.drawImage(bandpass, image=image3)
int_flux = image3.array.sum()
# Be *slightly* less stringent in this test given that we did use interpolation.
printval(image, image3)
np.testing.assert_almost_equal(
int_flux/analytic_flux, 1.0, 3,
err_msg="Drawn ChromaticConvolve flux (interpolated) doesn't match analytic prediction")
# As an aside, check for appropriate tests of 'integrator' argument.
assert_raises(ValueError, final_int.drawImage, bandpass, integrator='midp') # minor misspelling
assert_raises(ValueError, final_int.drawImage, bandpass, integrator=galsim.integ.midptRule)
check_pickle(PSF)
# Check option to not use exact SED
PSF = PSF.deinterpolated
PSF = PSF * 1.0
PSF = PSF.interpolate(waves=np.linspace(bandpass.blue_limit, bandpass.red_limit, 30),
use_exact_sed=False)
final_int = galsim.Convolve([star, PSF])
image3 = galsim.ImageD(stamp_size, stamp_size, scale=pixel_scale)
final_int.drawImage(bandpass, image=image3)
int_flux = image3.array.sum()
# Be *slightly* less stringent in this test given that we did use interpolation.
printval(image, image3)
np.testing.assert_almost_equal(
int_flux/analytic_flux, 1.0, 3,
err_msg="Drawn ChromaticConvolve flux (interpolated) doesn't match analytic prediction")
# As an aside, check for appropriate tests of 'integrator' argument.
assert_raises(ValueError, final_int.drawImage, bandpass, integrator='midp') # minor misspelling
assert_raises(ValueError, final_int.drawImage, bandpass, integrator=galsim.integ.midptRule)
check_pickle(PSF)
# Go back to no interpolation (this will effect the PSFs that are used below).
PSF = PSF.deinterpolated
# Try adjusting flux to something else.
target_flux = 2.63
bulge_SED2 = bulge_SED.withFlux(target_flux, bandpass)
star2 = galsim.Gaussian(fwhm=1e-8) * bulge_SED2
final = galsim.Convolve([star2, PSF])
final.drawImage(bandpass, image=image)
np.testing.assert_almost_equal(image.array.sum()/target_flux, 1.0, 5,
err_msg="Drawn ChromaticConvolve flux doesn't match " +
"using SED.withFlux()")
# Use flux_ratio instead.
flux_ratio = target_flux / analytic_flux
bulge_SED3 = bulge_SED * flux_ratio
star3 = galsim.Gaussian(fwhm=1e-8) * bulge_SED3
final = galsim.Convolve([star3, PSF])
final.drawImage(bandpass, image=image)
np.testing.assert_almost_equal(image.array.sum()/target_flux, 1.0, 5,
err_msg="Drawn ChromaticConvolve flux doesn't match " +
"using SED * flux_ratio")
# This should be equivalent.
bulge_SED3 = flux_ratio * bulge_SED
star3 = galsim.Gaussian(fwhm=1e-8) * bulge_SED3
final = galsim.Convolve([star3, PSF])
final.drawImage(bandpass, image=image)
np.testing.assert_almost_equal(image.array.sum()/target_flux, 1.0, 5,
err_msg="Drawn ChromaticConvolve flux doesn't match " +
"using flux_ratio * SED")
# Use flux_ratio on the chromatic object instead.
star4 = star * flux_ratio
final = galsim.Convolve([star4, PSF])
final.drawImage(bandpass, image=image)
np.testing.assert_almost_equal(image.array.sum()/target_flux, 1.0, 5,
err_msg="Drawn ChromaticConvolve flux doesn't match " +
"using ChromaticObject * flux_ratio")
# This should be equivalent.
star4 = flux_ratio * star
final = galsim.Convolve([star4, PSF])
final.drawImage(bandpass, image=image)
np.testing.assert_almost_equal(image.array.sum()/target_flux, 1.0, 5,
err_msg="Drawn ChromaticConvolve flux doesn't match " +
"using flux_ratio * ChromaticObject")
# As should this.
star4 = star.withScaledFlux(lambda wave: flux_ratio)
final = galsim.Convolve([star4, PSF])
final.drawImage(bandpass, image=image)
np.testing.assert_almost_equal(image.array.sum()/target_flux, 1.0, 5,
err_msg="Drawn ChromaticConvolve flux doesn't match " +
"using ChromaticObject.withScaledFlux(flux_ratio)")
# Can't scale GSObject by function (just SED)
with assert_raises(TypeError):
galsim.Gaussian(fwhm=1e-8).withScaledFlux(lambda wave: flux)
with assert_raises(TypeError):
galsim.Gaussian(fwhm=1e-8) * (lambda wave: flux)
# Test ChromaticObject.withFlux
star5 = star.withFlux(1.0, bandpass)
final = galsim.Convolve([star5, PSF])
final.drawImage(bandpass, image=image)
np.testing.assert_almost_equal(image.array.sum(), 1.0, 5,
err_msg="Drawn ChromaticConvolve flux doesn't match "
"using ChromaticObject.withFlux(1.0)")
# Test withMagnitude. The zeropoint is the magnitude at which the object produces
# 1 photon/sec/cm^2 in the given filter. So drawing with mag == zeropoint should yield a flux
# of 1.0
bandpass2 = bandpass.withZeropoint(25.0)
star6 = star.withMagnitude(25.0, bandpass2)
final = galsim.Convolve([star6, PSF])
final.drawImage(bandpass2, image=image)
np.testing.assert_almost_equal(image.array.sum(), 1.0, 5,
err_msg="Drawn ChromaticConvolve flux doesn't match "
"using ChromaticObject.withMagnitude(0.0)")
assert_raises(galsim.GalSimError, star.withMagnitude, 25.0, bandpass)
# Some very simple tests of withFluxDensity.
star7 = star.withFluxDensity(5.0, 500)
final = galsim.Convolve([star7, PSF])
final.evaluateAtWavelength(500).drawImage(image=image)
np.testing.assert_almost_equal(image.array.sum(), 5.0, 4,
err_msg="Drawn ChromaticConvolve flux density doesn't match "
"using ChromaticObject.withFluxDensity(5.0, 500)")
np.testing.assert_almost_equal(5.0, final.sed(500), 7,
err_msg="ChromaticObject.sed(500) doesn't match "
"withFluxDensity.")
from astropy import units
star8 = star.withFluxDensity(5.0, 5000*units.AA)
assert star7 == star8
star9 = star.withFluxDensity(0.5*units.astrophys.photon/(units.s*units.cm**2*units.AA), 500)
assert star7 == star9
# Test with non-unit initial flux
star9 = galsim.Gaussian(fwhm=1e-8, flux=flux_ratio) * bulge_SED
final = galsim.Convolve([star9, PSF])
final.drawImage(bandpass, image=image)
np.testing.assert_almost_equal(image.array.sum()/target_flux, 1.0, 5,
err_msg="obj * SED doesn't respect obj.flux")
# Check withFlux again
print('star9 flux = ',star9.calculateFlux(bandpass))
star10 = star9.withFlux(1.0, bandpass)
print('star10 flux = ',star10.calculateFlux(bandpass))
final = galsim.Convolve([star10, PSF])
final.drawImage(bandpass, image=image)
np.testing.assert_almost_equal(image.array.sum(), 1.0, 5,
err_msg="withFlux doesn't work when obj has flux.")
@timer
def test_double_ChromaticSum():
''' Test logic section of ChromaticConvolve that splits apart ChromaticSums for the case that
more than one ChromaticSum's are convolved together.
'''
a = galsim.Gaussian(fwhm=1.0) * bulge_SED
b = galsim.Gaussian(fwhm=2.0) * bulge_SED
c = galsim.Gaussian(fwhm=3.0)
d = galsim.Gaussian(fwhm=4.0)
image = galsim.ImageD(16, 16, scale=0.2)
obj = galsim.Convolve(a+b, c+d)
obj.drawImage(bandpass, image=image, method='no_pixel')
check_pickle(obj)
image_a = galsim.ImageD(16, 16, scale=0.2)
image_b = galsim.ImageD(16, 16, scale=0.2)
obj_a = galsim.Convolve(a, c+d)
obj_b = galsim.Convolve(b, c+d)
obj_a.drawImage(bandpass, image = image_a, method='no_pixel')
obj_b.drawImage(bandpass, image = image_b, method='no_pixel')
printval(image, image_a+image_b)
np.testing.assert_almost_equal(image.array, (image_a+image_b).array, 5,
err_msg="Convolving two ChromaticSums failed")
@timer
def test_ChromaticSum_nphot():
"""Test n_photons parameter with ChromaticSum photon shooting
"""
# in response to issue #1156
rng = galsim.BaseDeviate(12345)
sed1 = galsim.SED("CWW_E_ext.sed", wave_type='A', flux_type='flambda')
sed2 = galsim.SED("CWW_Sbc_ext.sed", wave_type='A', flux_type='flambda')
bandpass = galsim.Bandpass("LSST_r.dat", wave_type="nm")
flux1 = 990
flux2 = 20
flux = flux1 + flux2
obj1 = (galsim.Gaussian(fwhm=0.6) * sed1).withFlux(flux1, bandpass)
obj2 = (galsim.Gaussian(fwhm=0.3) * sed2).withFlux(flux2, bandpass)
obj = obj1 + obj2
class Counter(galsim.PhotonOp):
def __init__(self):
self.nphot = []
self.meanflux = []
def applyTo(self, photon_array, local_wcs=None, rng=None):
self.nphot.append(len(photon_array))
self.meanflux.append(np.mean(photon_array.flux))
# Looks okay when n_photons unspecified
counter = Counter()
img = obj.drawImage(
bandpass, nx=24, ny=24, scale=0.2, method='phot', rng=rng, poisson_flux=False,
photon_ops=[counter]
)
print("n_photons = 0, poisson_flux=False:")
print("counter.nphot = ",counter.nphot)
print("counter.meanflux = ",counter.meanflux)
print("img.array.sum() = ",img.array.sum())
print("img.added_flux = ",img.added_flux)
assert np.isclose(np.sum(counter.nphot), flux)
assert np.isclose(img.array.sum(), flux)
assert np.isclose(img.added_flux, flux)
assert np.isclose(img.array.sum(), np.sum(counter.nphot))
assert np.isclose(counter.nphot[0], flux1)
assert np.isclose(counter.nphot[1], flux2)
np.testing.assert_allclose(counter.meanflux, 1.0)
counter = Counter()
img = obj.drawImage(
bandpass, nx=24, ny=24, scale=0.2, method='phot', rng=rng, poisson_flux=True,
photon_ops=[counter]
)
print("n_photons = 0, poisson_flux=True:")
print("counter.nphot = ",counter.nphot)
print("counter.meanflux = ",counter.meanflux)
print("img.array.sum() = ",img.array.sum())
print("img.added_flux = ",img.added_flux)
assert np.isclose(np.sum(counter.nphot), flux, rtol=0.1)
assert np.isclose(img.array.sum(), flux, rtol=0.1)
assert np.isclose(img.added_flux, flux, rtol=0.1)
assert np.isclose(img.array.sum(), np.sum(counter.nphot))
assert np.isclose(counter.nphot[1]/counter.nphot[0], flux2/flux1, rtol=0.3)
np.testing.assert_allclose(counter.meanflux, 1.0)
# When n_photons is explicit, used to shoot too many photons. cf. #1156
counter = Counter()
img = obj.drawImage(
bandpass, nx=24, ny=24, scale=0.2, method='phot', rng=rng,
photon_ops=[counter], n_photons=101, poisson_flux=False,
)
print("n_photons = 101, poisson_flux=False:")
print("counter.nphot = ",counter.nphot)
print("counter.meanflux = ",counter.meanflux)
print("img.array.sum() = ",img.array.sum())
print("img.added_flux = ",img.added_flux)
assert np.sum(counter.nphot) == 101
assert np.isclose(img.array.sum(), flux)
assert np.isclose(img.added_flux, flux)
assert np.isclose(counter.nphot[1]/counter.nphot[0], flux2/flux1, rtol=1)
np.testing.assert_allclose(counter.meanflux, np.mean(counter.meanflux))
# Repeat with poisson_flux=True
counter = Counter()
img = obj.drawImage(
bandpass, nx=24, ny=24, scale=0.2, method='phot', rng=rng,
photon_ops=[counter], n_photons=101, poisson_flux=True,
)
print("n_photons = 101, poisson_flux=True:")
print("counter.nphot = ",counter.nphot)
print("counter.meanflux = ",counter.meanflux)
print("img.array.sum() = ",img.array.sum())
print("img.added_flux = ",img.added_flux)
assert np.sum(counter.nphot) == 101
assert np.isclose(img.array.sum(), flux, rtol=0.1)
assert np.isclose(img.added_flux, flux, rtol=0.1)
assert np.isclose(counter.nphot[1]/counter.nphot[0], flux2/flux1, rtol=1)
np.testing.assert_allclose(counter.meanflux, np.mean(counter.meanflux))
# Do few enough that one component gets no photons.
rng = galsim.BaseDeviate(1234)
counter = Counter()
img = obj.drawImage(
bandpass, nx=24, ny=24, scale=0.2, method='phot', rng=rng,
photon_ops=[counter], n_photons=11, poisson_flux=False,
)
print("n_photons = 11, poisson_flux=False:")
print("counter.nphot = ",counter.nphot)
print("counter.meanflux = ",counter.meanflux)
print("img.array.sum() = ",img.array.sum())
print("img.added_flux = ",img.added_flux)
assert np.sum(counter.nphot) == 11
assert len(counter.nphot) == 1 # Not a priori required, but works for this rng.
assert np.isclose(img.array.sum(), flux)
assert np.isclose(img.added_flux, flux)
print('Start #1170 test')
# If multiple objects at the end has zero flux, it used to cause a ZeroDivisionError.
# (cf. #1170)
# Note: they need to all have different seds to avoid ChromaticSum collapsing them.
obj1 = (galsim.Gaussian(fwhm=0.6) * sed1).withFlux(1.0, bandpass)
sed3 = galsim.SED("CWW_Im_ext.sed", wave_type='A', flux_type='flambda')
obj3 = (galsim.Gaussian(fwhm=0.3) * sed3).withFlux(0, bandpass)
obj = galsim.ChromaticSum([obj1, obj2 * 0, obj3])
rng = galsim.BaseDeviate(1234)
counter = Counter()
img = obj.drawImage(
bandpass, nx=24, ny=24, scale=0.2, method='phot', rng=rng,
photon_ops=[counter], n_photons=11, poisson_flux=False,
)
print("3 objects, two with 0 flux, n_photons=11, poisson_flux=False:")
print("counter.nphot = ",counter.nphot)
print("counter.meanflux = ",counter.meanflux)
print("img.array.sum() = ",img.array.sum())
print("img.added_flux = ",img.added_flux)
assert np.sum(counter.nphot) == 11
assert np.isclose(img.array.sum(), 1.0)
assert np.isclose(img.added_flux, 1.0)
# Also check if the first two have zero flux.
obj = galsim.ChromaticSum([obj3, obj1, obj2 * 0])
counter = Counter()
img = obj.drawImage(
bandpass, nx=24, ny=24, scale=0.2, method='phot', rng=rng,
photon_ops=[counter], n_photons=11, poisson_flux=False,
)
print("3 objects, last two with 0 flux, n_photons=11, poisson_flux=False:")
print("counter.nphot = ",counter.nphot)
print("counter.meanflux = ",counter.meanflux)
print("img.array.sum() = ",img.array.sum())
print("img.added_flux = ",img.added_flux)
assert np.sum(counter.nphot) == 11
assert np.isclose(img.array.sum(), 1.0)
@timer
def test_ChromaticConvolution_of_ChromaticConvolution():
"""Check that the __init__ of ChromaticConvolution properly expands arguments that are already
ChromaticConvolutions.
"""
a = galsim.Gaussian(fwhm=1.0) * bulge_SED
b = galsim.Gaussian(fwhm=2.0)
c = galsim.Gaussian(fwhm=3.0)
d = galsim.Gaussian(fwhm=4.0)
e = galsim.Convolve(a, b)
f = galsim.Convolve(c, d)
g = galsim.Convolve(e, f)
if any(isinstance(h, galsim.ChromaticConvolution) for h in g.obj_list):
raise AssertionError("ChromaticConvolution did not expand ChromaticConvolution argument")
assert_raises(TypeError, galsim.ChromaticConvolution)
assert_raises(TypeError, galsim.ChromaticConvolution, bulge_SED)
assert_raises(TypeError, galsim.ChromaticConvolution, [a,b], invalid=True)
assert_raises(NotImplementedError, galsim.ChromaticConvolution, [a,b], real_space=True)
@timer
def test_ChromaticAutoConvolution():
a = galsim.Gaussian(fwhm=1.0)
b = galsim.Gaussian(fwhm=2.0) * bulge_SED
im1 = galsim.ImageD(32, 32, scale=0.2)
im2 = galsim.ImageD(32, 32, scale=0.2)
c = galsim.Convolve(a, a, b)
c.drawImage(bandpass, image=im1, method='no_pixel')
d = galsim.Convolve(galsim.AutoConvolve(a), b)
d.drawImage(bandpass, image=im2, method='no_pixel')
printval(im1, im2)
np.testing.assert_array_almost_equal(im1.array, im2.array, 5,
"ChromaticAutoConvolution(a) not equal to "
"ChromaticConvolution(a,a)")
# Check flux scaling
flux = im2.array.sum()
im2 = (d * 2.).drawImage(bandpass, image=im2, method='no_pixel')
flux2 = im2.array.sum()
np.testing.assert_array_almost_equal(
flux2, 2.*flux, 5,
err_msg="ChromaticAutoConvolution * 2 resulted in wrong flux.")
@timer
def test_ChromaticAutoCorrelation():
a = galsim.Gaussian(fwhm=1.0)
b = galsim.Gaussian(fwhm=2.0) * bulge_SED
im1 = galsim.ImageD(32, 32, scale=0.2)
im2 = galsim.ImageD(32, 32, scale=0.2)
c = galsim.Convolve(a, a.rotate(180.0 * galsim.degrees), b)
c.drawImage(bandpass, image=im1, method='no_pixel')
d = galsim.Convolve(galsim.AutoCorrelate(a), b)
d.drawImage(bandpass, image=im2, method='no_pixel')
printval(im1, im2)
np.testing.assert_array_almost_equal(im1.array, im2.array, 5,
"ChromaticAutoCorrelate(a) not equal to "
"ChromaticConvolution(a,a.rotate(180.0*galsim.degrees)")
# Check flux scaling
flux = im2.array.sum()
im2 = (d * 2.).drawImage(bandpass, image=im2, method='no_pixel')
flux2 = im2.array.sum()
np.testing.assert_array_almost_equal(
flux2/(2.*flux), 1.0, 5,
err_msg="ChromaticAutoCorrelation * 2 resulted in wrong flux.")
@timer
def test_ChromaticObject_expand():
im1 = galsim.ImageD(32, 32, scale=0.2)
im2 = galsim.ImageD(32, 32, scale=0.2)
a = galsim.Gaussian(fwhm=1.0).expand(1.1) * bulge_SED
b = (galsim.Gaussian(fwhm=1.0) * bulge_SED).expand(1.1)