LSST Applications g0f08755f38+9c285cab97,g1635faa6d4+13f3999e92,g1653933729+a8ce1bb630,g1a0ca8cf93+bf6eb00ceb,g28da252d5a+0829b12dee,g29321ee8c0+5700dc9eac,g2bbee38e9b+9634bc57db,g2bc492864f+9634bc57db,g2cdde0e794+c2c89b37c4,g3156d2b45e+41e33cbcdc,g347aa1857d+9634bc57db,g35bb328faa+a8ce1bb630,g3a166c0a6a+9634bc57db,g3e281a1b8c+9f2c4e2fc3,g414038480c+077ccc18e7,g41af890bb2+fde0dd39b6,g5fbc88fb19+17cd334064,g781aacb6e4+a8ce1bb630,g80478fca09+55a9465950,g82479be7b0+d730eedb7d,g858d7b2824+9c285cab97,g9125e01d80+a8ce1bb630,g9726552aa6+10f999ec6a,ga5288a1d22+2a84bb7594,gacf8899fa4+c69c5206e8,gae0086650b+a8ce1bb630,gb58c049af0+d64f4d3760,gc28159a63d+9634bc57db,gcf0d15dbbd+4b7d09cae4,gda3e153d99+9c285cab97,gda6a2b7d83+4b7d09cae4,gdaeeff99f8+1711a396fd,ge2409df99d+5e831397f4,ge79ae78c31+9634bc57db,gf0baf85859+147a0692ba,gf3967379c6+41c94011de,gf3fb38a9a8+8f07a9901b,gfb92a5be7c+9c285cab97,w.2024.46
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.