LSST Applications g063fba187b+cac8b7c890,g0f08755f38+6aee506743,g1653933729+a8ce1bb630,g168dd56ebc+a8ce1bb630,g1a2382251a+b4475c5878,g1dcb35cd9c+8f9bc1652e,g20f6ffc8e0+6aee506743,g217e2c1bcf+73dee94bd0,g28da252d5a+1f19c529b9,g2bbee38e9b+3f2625acfc,g2bc492864f+3f2625acfc,g3156d2b45e+6e55a43351,g32e5bea42b+1bb94961c2,g347aa1857d+3f2625acfc,g35bb328faa+a8ce1bb630,g3a166c0a6a+3f2625acfc,g3e281a1b8c+c5dd892a6c,g3e8969e208+a8ce1bb630,g414038480c+5927e1bc1e,g41af890bb2+8a9e676b2a,g7af13505b9+809c143d88,g80478fca09+6ef8b1810f,g82479be7b0+f568feb641,g858d7b2824+6aee506743,g89c8672015+f4add4ffd5,g9125e01d80+a8ce1bb630,ga5288a1d22+2903d499ea,gb58c049af0+d64f4d3760,gc28159a63d+3f2625acfc,gcab2d0539d+b12535109e,gcf0d15dbbd+46a3f46ba9,gda6a2b7d83+46a3f46ba9,gdaeeff99f8+1711a396fd,ge79ae78c31+3f2625acfc,gef2f8181fd+0a71e47438,gf0baf85859+c1f95f4921,gfa517265be+6aee506743,gfa999e8aa5+17cd334064,w.2024.51
LSST Data Management Base Package
Loading...
Searching...
No Matches
Functions | Variables
test_model Namespace Reference

Functions

 channels ()
 
 data (channels)
 
 psfmodels (data)
 
 interptype_sersic_default ()
 
 sources (channels, interptype_sersic_default)
 
 model (data, psfmodels, sources)
 
 test_data (channels, data)
 
 test_model (channels, model)
 
 test_model_eval_jacobian (model)
 
 test_model_eval_loglike_grad (model)
 
 test_model_hessian (model)
 

Variables

int max_diff_ll = 1e-25
 

Function Documentation

◆ channels()

test_model.channels ( )

Definition at line 12 of file test_model.py.

12def channels():
13 return tuple(g2f.Channel(x) for x in "rgb")
14
15
16@pytest.fixture(scope="module")
An observational channel, usually representing some range of wavelengths of light.
Definition channel.h:29

◆ data()

test_model.data ( channels)

Definition at line 17 of file test_model.py.

17def data(channels):
18 n_x, n_y = 5, 5
19 return g2f.DataD(
20 [
21 g2f.ObservationD(
22 g2d.ImageD(np.zeros((n_y, n_x))),
23 g2d.ImageD(np.full((n_y, n_x), 1e3)),
24 g2d.ImageB(np.ones((n_y, n_x))),
25 channel,
26 )
27 for channel in channels
28 ]
29 )
30
31
32@pytest.fixture(scope="module")
char * data
Definition BaseRecord.cc:61

◆ interptype_sersic_default()

test_model.interptype_sersic_default ( )

Definition at line 53 of file test_model.py.

53def interptype_sersic_default():
55
56
57@pytest.fixture(scope="module")
A SersicIndexParameter for a Gaussian mixture Component.

◆ model()

test_model.model ( data,
psfmodels,
sources )

Definition at line 130 of file test_model.py.

130def model(data, psfmodels, sources):
131 model = g2f.ModelD(data, list(psfmodels), list(sources))
132 with pytest.raises(RuntimeError):
133 model.evaluate()
134 model.setup_evaluators()
135 model.evaluate()
136 result = model.evaluate()
137 for value in result:
138 assert value == 0
139 for data, output in zip(model.data, model.outputs):
140 data.image.data.flat = output.data.flat
141 return model
142
143

◆ psfmodels()

test_model.psfmodels ( data)

Definition at line 33 of file test_model.py.

33def psfmodels(data):
34 psfmodels = tuple(
36 [
38 g2f.GaussianParametricEllipse(1.0, 1.0, 0.0),
39 None,
40 g2f.LinearIntegralModel([(g2f.Channel.NONE, g2f.IntegralParameterD(1.0))]),
41 )
42 ]
43 )
44 for _ in range(len(data))
45 )
46 for psfmodel in psfmodels:
47 for param in psfmodel.parameters():
48 param.fixed = True
49 return psfmodels
50
51
52@pytest.fixture(scope="module")
A Component consisting of a 2D Gaussian.
A Parameter-based implementation of lsst::gauss2d::EllipseData and ParametricEllipse.
An IntegralModel with a single linear IntegralParameterD per Channel.
A Gaussian mixture model of a point spread function.
Definition psfmodel.h:26

◆ sources()

test_model.sources ( channels,
interptype_sersic_default )

Definition at line 58 of file test_model.py.

58def sources(channels, interptype_sersic_default):
59 last = None
60 n_sources, n_components = 2, 2
61 sources = [None] * n_sources
62 n_comp_max = n_components - 1
63 log10 = g2f.Log10TransformD()
64 for i in range(n_sources):
65 components = [None] * n_components
66 for c in range(n_components):
67 is_last = c == n_comp_max
69 [
70 (
71 channel,
73 (is_last == 1) or 0.25 * (1 + idx_channel), fixed=is_last
74 ),
75 )
76 for idx_channel, channel in enumerate(channels)
77 ],
79 [(channel, g2f.IntegralParameterD(0.5 + 0.5 * (i + 1))) for channel in channels]
80 )
81 if (c == 0)
82 else last,
83 is_last,
84 )
85 components[c] = g2f.GaussianComponent(
86 centroid=None,
87 ellipse=g2f.GaussianParametricEllipse(1.0, 1.0, 0.0),
88 integral=last,
89 )
90 components.append(
92 centroid=None,
94 size_x=g2f.ReffXParameterD(2.6, transform=log10, fixed=False),
95 size_y=g2f.ReffYParameterD(3.5, transform=log10, fixed=False),
97 -0.1,
98 fixed=False,
99 transform=g2f.LogitLimitedTransformD(
100 limits=g2f.LimitsD(min=-0.999, max=0.999, name="ref_logit_sersic[0.5, 6.0]"),
101 ),
102 ),
103 ),
105 [
106 (
107 channel,
108 g2f.IntegralParameterD(1.0, transform=log10, label=f"Sersic {channel}-band"),
109 )
110 for channel in channels
111 ]
112 ),
113 # Linear interpolation fails at exact knot values
114 # Adding a small offset solves the problem
116 value=1.0 + 5e-4 * (interptype_sersic_default == g2f.InterpType.linear),
117 fixed=False,
118 transform=g2f.LogitLimitedTransformD(
119 limits=g2f.LimitsD(min=0.5, max=6.0, name="ref_logit_sersic[0.5, 6.0]"),
120 ),
121 ),
122 ),
123 )
124 sources[i] = g2f.Source(components)
125
126 return sources
127
128
129@pytest.fixture(scope="module")
An IntegralModel that returns a Parameter-dependent fraction of the flux of another IntegralModel.
A Gaussian mixture approximation to a Sersic profile Component.
A ParametericEllipse with effective radius Parameters.
An association of physically-related Component instances.
Definition source.h:22

◆ test_data()

test_model.test_data ( channels,
data )

Definition at line 144 of file test_model.py.

144def test_data(channels, data):
145 assert len(data) == len(channels)
146
147

◆ test_model()

test_model.test_model ( channels,
model )

Definition at line 148 of file test_model.py.

148def test_model(channels, model):
149 assert str(model) != ""
150 gaussians = model.gaussians(channels[0])
151 assert len(gaussians) == 12
152 params = model.parameters()
153 assert len(params) == 80
154 assert len(set(params)) == 68
155
156

◆ test_model_eval_jacobian()

test_model.test_model_eval_jacobian ( model)

Definition at line 157 of file test_model.py.

157def test_model_eval_jacobian(model):
158 # Force is needed in case other tests use a jacobian evaluator
159 # with different parameters
160 model.setup_evaluators(g2f.EvaluatorMode.jacobian, force=True)
161 result = np.array(model.evaluate())
162 # TODO: Investigate why clang/osx builds have non-zero values in DM-45308
163 # GCC/linux are exactly 0. Hopefully it's not something like ffast-math
164 assert (np.abs(result) <= max_diff_ll).all()
165 # TODO: Investigate why this rtol needs to be so high
166 errors = model.verify_jacobian(atol=1e-3, rtol=5e-3, max_diff_ll=max_diff_ll)
167 # All of the IntegralParameters got double-counted - see DM-40674
168 # ... but also, linear interpolators will fail the Sersic index params
169 assert len(errors) == 6
170
171

◆ test_model_eval_loglike_grad()

test_model.test_model_eval_loglike_grad ( model)

Definition at line 172 of file test_model.py.

172def test_model_eval_loglike_grad(model):
173 model.setup_evaluators(g2f.EvaluatorMode.loglike_grad, force=True)
174 result = np.array(model.evaluate())
175 # TODO: Investigate why clang/osx builds have non-zero values in DM-45308
176 assert (np.abs(result) <= max_diff_ll).all()
177 dloglike = np.array(model.compute_loglike_grad(True))
178 assert (np.abs(dloglike) < max_diff_ll).all()
179
180

◆ test_model_hessian()

test_model.test_model_hessian ( model)

Definition at line 181 of file test_model.py.

181def test_model_hessian(model):
182 options = g2f.HessianOptions(return_negative=False)
183 params_fixed = set()
184
185 for idx in range(2):
186 hessians = {
187 is_transformed: model.compute_hessian(transformed=is_transformed, options=options)
188 for is_transformed in (False, True)
189 }
190 # Note that since there is no noise in the image and none of the
191 # param values have changed, the Hessian terms are not all negative -
192 # the sign corrections are based on the sign of loglike_grad,
193 # and those are all exactly zero
194 assert all(np.isfinite(hessian.data).all() for hessian in hessians.values())
195 if idx == 0:
196 for param in set(model.parameters()):
197 # TODO: Remove this when DM-40674 is fixed
198 if isinstance(param, g2f.IntegralParameterD) and param.free:
199 param.free = False
200 params_fixed.add(param)
201
202 for is_transformed in (False, True):
203 # can set print=True for debugging, but the output is too verbose for
204 # pytest output even on a failure
205 hessian_from_jac = model.compute_hessian(transformed=is_transformed, options=None).data
206 # TODO: investigate these unreasonably large differences after DM-40674
207 assert np.all(np.isclose(hessians[is_transformed].data, hessian_from_jac, rtol=1e-3, atol=1))
208
209 for param in params_fixed:
210 param.free = True

Variable Documentation

◆ max_diff_ll

int test_model.max_diff_ll = 1e-25

Definition at line 8 of file test_model.py.