-
Notifications
You must be signed in to change notification settings - Fork 120
Expand file tree
/
Copy pathtest_shear_position.py
More file actions
128 lines (111 loc) · 4.09 KB
/
test_shear_position.py
File metadata and controls
128 lines (111 loc) · 4.09 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
# Copyright (c) 2012-2023 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 numpy as np
import galsim
from galsim_test_helpers import timer
@timer
def test_shear_position():
# zero position
shear = galsim.Shear(g1=-0.03, g2=0.7)
pos = galsim.PositionD(x=0, y=0)
sheared_pos = pos.shear(shear)
np.testing.assert_almost_equal(
sheared_pos.x, 0,
err_msg="x coord is wrong for shear with zero pos"
)
np.testing.assert_almost_equal(
sheared_pos.y, 0,
err_msg="y coord is wrong for shear with zero pos"
)
# zero shear
shear = galsim.Shear(g1=0, g2=0)
pos = galsim.PositionD(x=0.1, y=-0.5)
sheared_pos = pos.shear(shear)
np.testing.assert_almost_equal(
sheared_pos.x, pos.x,
err_msg="x coord is wrong for zero shear with pos"
)
np.testing.assert_almost_equal(
sheared_pos.y, pos.y,
err_msg="y coord is wrong for zero shear with pos"
)
# full thing
shear = galsim.Shear(g1=-0.1, g2=0.1)
pos = galsim.PositionD(x=0.1, y=-0.5)
sheared_pos = pos.shear(shear)
mat = shear.getMatrix()
np.testing.assert_almost_equal(
sheared_pos.x,
pos.x * mat[0, 0] + pos.y * mat[0, 1],
err_msg="x coord is wrong for shear with pos"
)
np.testing.assert_almost_equal(
sheared_pos.y,
pos.x * mat[1, 0] + pos.y * mat[1, 1],
err_msg="y coord is wrong for shear with pos"
)
@timer
def test_shear_position_image_integration_pixelwcs():
wcs = galsim.PixelScale(0.3)
obj1 = galsim.Gaussian(sigma=3)
obj2 = galsim.Gaussian(sigma=2)
pos2 = galsim.PositionD(3, 5)
sum = obj1 + obj2.shift(pos2)
shear = galsim.Shear(g1=0.1, g2=0.18)
im1 = galsim.Image(50, 50, wcs=wcs)
sum.shear(shear).drawImage(im1, center=im1.center)
# Equivalent to shear each object separately and drawing at the sheared position.
im2 = galsim.Image(50, 50, wcs=wcs)
obj1.shear(shear).drawImage(im2, center=im2.center)
obj2.shear(shear).drawImage(
im2,
add_to_image=True,
center=im2.center + wcs.toImage(pos2.shear(shear)),
)
print("err:", np.max(np.abs(im1.array - im2.array)))
np.testing.assert_allclose(im1.array, im2.array, rtol=0, atol=5e-8)
@timer
def test_shear_position_image_integration_offsetwcs():
wcs = galsim.OffsetShearWCS(
0.3,
galsim.Shear(g1=-0.2, g2=0.4),
origin=galsim.PositionD(x=-1.1, y=2.7),
world_origin=galsim.PositionD(x=0.6, y=-2.5),
)
obj1 = galsim.Gaussian(sigma=3)
obj2 = galsim.Gaussian(sigma=2)
pos2 = galsim.PositionD(3, 5)
pos1 = galsim.PositionD(0, 0)
sum_obj = obj1 + obj2.shift(pos2)
shear = galsim.Shear(g1=0.1, g2=0.18)
im1 = galsim.Image(50, 50, wcs=wcs)
sum_obj.shear(shear).drawImage(im1, center=wcs.toImage(pos1))
# Equivalent to shear each object separately and drawing at the sheared position.
im2 = galsim.Image(50, 50, wcs=wcs)
obj1.shear(shear).drawImage(im2, center=wcs.toImage(pos1))
obj2.shear(shear).drawImage(
im2,
add_to_image=True,
center=wcs.toImage(pos2.shear(shear)),
)
print("err:", np.max(np.abs(im1.array - im2.array)))
np.testing.assert_allclose(im1.array, im2.array, rtol=0, atol=2e-7)
if __name__ == "__main__":
testfns = [v for k, v in vars().items() if k[:5] == 'test_' and callable(v)]
for testfn in testfns:
testfn()