LSST Applications g1653933729+34a971ddd9,g1a997c3884+34a971ddd9,g2160c40384+da0d0eec6b,g28da252d5a+1236b942f7,g2bbee38e9b+e5a1bc5b38,g2bc492864f+e5a1bc5b38,g2ca4be77d2+192fe503f0,g2cdde0e794+704103fe75,g3156d2b45e+6e87dc994a,g347aa1857d+e5a1bc5b38,g35bb328faa+34a971ddd9,g3a166c0a6a+e5a1bc5b38,g3e281a1b8c+8ec26ec694,g4005a62e65+ba0306790b,g414038480c+9f5be647b3,g41af890bb2+c3a10c924f,g5065538af8+e7237db731,g5a0bb5165c+eae055db26,g717e5f8c0f+b65b5c3ae4,g80478fca09+4ce5a07937,g82479be7b0+08790af60f,g858d7b2824+b65b5c3ae4,g9125e01d80+34a971ddd9,ga5288a1d22+5df949a35e,gae0086650b+34a971ddd9,gb58c049af0+ace264a4f2,gbd397ab92a+2141afb137,gc28159a63d+e5a1bc5b38,gc805d3fbd4+b65b5c3ae4,gcf0d15dbbd+97632ccc20,gd6b7c0dfd1+de826e8718,gda6a2b7d83+97632ccc20,gdaeeff99f8+7774323b41,ge2409df99d+e6cadbf968,ge33fd446bb+b65b5c3ae4,ge79ae78c31+e5a1bc5b38,gf0baf85859+890af219f9,gf5289d68f6+a27069ed62,w.2024.37
LSST Data Management Base Package
Loading...
Searching...
No Matches
test_image.py
Go to the documentation of this file.
1# This file is part of gauss2d.
2#
3# Developed for the LSST Data Management System.
4# This product includes software developed by the LSST Project
5# (https://www.lsst.org).
6# See the COPYRIGHT file at the top-level directory of this distribution
7# for details of code ownership.
8#
9# This program is free software: you can redistribute it and/or modify
10# it under the terms of the GNU General Public License as published by
11# the Free Software Foundation, either version 3 of the License, or
12# (at your option) any later version.
13#
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17# GNU General Public License for more details.
18#
19# You should have received a copy of the GNU General Public License
20# along with this program. If not, see <https://www.gnu.org/licenses/>.
21
22import pytest
23
24import lsst.gauss2d as g2d
25import numpy as np
26import sys
27
28prefix_namespace = "lsst.gauss2d."
29
30check_str_repr = "linux" in sys.platform
31
32
34 img = g2d.ImageD(1, 1)
35 img.fill(1)
36 assert img.get_value(0, 0) == 1
37 img += 1
38 assert img.get_value(0, 0) == 2
39 img.set_value_unchecked(0, 0, -1)
40 assert img.get_value_unchecked(0, 0) == -1
41 str_coordsys = str(img.coordsys)
42 if check_str_repr:
43 assert str(img) == f"ImageD(coordsys={str_coordsys}, n_rows=1, n_cols=1)"
44 assert repr(img) == (
45 f"{prefix_namespace}python.ImageD(coordsys={prefix_namespace}{str_coordsys}, n_rows=1, n_cols=1)"
46 )
47 n_rows, n_cols = 11, 13
48 coordsys = g2d.CoordinateSystem(1.3, -0.7, -11.6, 365.1)
49 img = g2d.ImageF(n_rows=n_rows, n_cols=n_cols, coordsys=coordsys)
50 if check_str_repr:
51 assert str(img) == f"ImageF(coordsys={coordsys}, n_rows={n_rows}, n_cols={n_cols})"
52
53
55 data = np.arange(12).astype(np.int32).reshape((3, 4))
56 img = g2d.ImageI(data)
57
58 data[0, 0] = -1
59 assert img.get_value(0, 0) == -1
60 assert img.get_value_unchecked(0, 2) == 2
61 assert data[0, 2] == 2
62
63 assert (img.n_rows, img.n_cols) == data.shape
64 assert tuple(img.shape) == data.shape
65 assert img.size == data.size
66
67 with pytest.raises(IndexError):
68 img.get_value(3, 0)
69 with pytest.raises(IndexError):
70 img.get_value(0, 4)
71 with pytest.raises(TypeError):
72 img.get_value(0, -1)
73 with pytest.raises(TypeError):
74 img.get_value(-1, 0)
75
76
78 imgs = [g2d.ImageD(3, 2), g2d.ImageD(3, 2)]
79 img_arr = g2d.ImageArrayD(imgs)
80 assert img_arr.at(0) is imgs[0]
81 assert img_arr.size == len(imgs)
82 assert len(img_arr) == len(imgs)
83 assert img_arr[0] is imgs[0]
84
85 with pytest.raises(ValueError):
86 g2d.ImageArrayD([imgs[0], g2d.ImageD(2, 3)])
87
88 with pytest.raises(ValueError):
89 g2d.ImageArrayD([imgs[0], None])
90
91 if check_str_repr:
92 assert str(img_arr) == f"ImageArrayD(data=[{str(imgs[0])}, {str(imgs[1])}])"
93 assert repr(img_arr) == (
94 f"{prefix_namespace}ImageArrayD(data=[{repr(imgs[0])}, {repr(imgs[1])}])"
95 )
96
97
98@pytest.fixture(scope="module")
100 gauss1 = g2d.Gaussian(centroid=None, ellipse=g2d.Ellipse(3, 6, 0))
101 gauss2 = g2d.Gaussian(centroid=None, ellipse=g2d.Ellipse(4, 8, 0))
102 return gauss1, gauss2
103
104
105@pytest.fixture(scope="module")
106def convolved_gaussian(gaussians):
107 gauss_conv = g2d.ConvolvedGaussian(gaussians[0], gaussians[1])
108 return gauss_conv
109
110
111@pytest.fixture(scope="module")
112def convolved_gaussians(convolved_gaussian):
113 gaussians_conv = g2d.ConvolvedGaussians([
114 convolved_gaussian, g2d.ConvolvedGaussian(convolved_gaussian.kernel, convolved_gaussian.source)
115 ])
116 return gaussians_conv
117
118
119def test_GaussianEvaluator(convolved_gaussians):
120 output = g2d.ImageD(coordsys=g2d.CoordinateSystem(x_min=-5, y_min=-5), n_rows=11, n_cols=9)
121 evaluator = g2d.GaussianEvaluatorD(gaussians=convolved_gaussians, output=output)
122 result = evaluator.loglike_pixel()
123
124 assert result == 0
125 assert np.sum(output.data) > 0
126
127 sigma_inv = g2d.ImageD(coordsys=output.coordsys, data=np.ones_like(output.data))
128 evaluator_ll = g2d.GaussianEvaluatorD(gaussians=convolved_gaussians, data=output, sigma_inv=sigma_inv)
129
130 result = evaluator_ll.loglike_pixel()
131 assert result == 0
132
133 output += 1e-6
134 assert evaluator_ll.loglike_pixel() < 0
135
136 if check_str_repr:
137 assert str(evaluator_ll) == (
138 f"GaussianEvaluatorD(gaussians={str(convolved_gaussians)}, "
139 f"do_extra=0, do_output=0, do_residual=0, has_background=0, "
140 f"is_sigma_image=1, backgroundtype=0, get_likelihood=1, "
141 f"data={str(output)}, sigma_inv={str(sigma_inv)}, output=None, residual=None, "
142 f"grads=None, grad_param_map=None, grad_param_factor=None, "
143 f"extra_param_map=None, extra_param_factor=None, grad_extra=None, grad_param_idx=[], "
144 f"n_cols={output.n_cols}, n_rows={output.n_rows}, coordsys={str(output.coordsys)})"
145 )
146 assert repr(evaluator_ll) == (
147 f"{prefix_namespace}GaussianEvaluatorD(gaussians={repr(convolved_gaussians)}, "
148 f"data={repr(output)}, sigma_inv={repr(sigma_inv)}, output=None, residual=None, "
149 f"grads=None, grad_param_map=None, grad_param_factor=None, "
150 f"extra_param_map=None, extra_param_factor=None, background=None)"
151 )
152
153
154def test_make_gaussians_pixel(convolved_gaussians):
155 output = g2d.make_gaussians_pixel_D(convolved_gaussians, n_rows=5, n_cols=7)
156 assert np.sum(output.data) > 0
A convolution of a Gaussian source and kernel.
Definition gaussian.h:220
A collection of ConvolvedGaussian objects.
Definition gaussian.h:249
A coordinate system specifying image scale and orientation.
An Ellipse with sigma_x, sigma_y, and rho values.
Definition ellipse.h:283
A 2D Gaussian with a Centroid, Ellipse, and integral.
Definition gaussian.h:99
test_ImageArray()
Definition test_image.py:77
test_numpy_Image()
Definition test_image.py:54
convolved_gaussian(gaussians)
test_make_gaussians_pixel(convolved_gaussians)
convolved_gaussians(convolved_gaussian)
test_GaussianEvaluator(convolved_gaussians)