LSST Applications 24.1.6,g063fba187b+e7121a6b04,g0f08755f38+4e0faf0f7f,g12f32b3c4e+7915c4de30,g1653933729+a8ce1bb630,g168dd56ebc+a8ce1bb630,g28da252d5a+94d9f37a33,g2bbee38e9b+ae03bbfc84,g2bc492864f+ae03bbfc84,g3156d2b45e+6e55a43351,g347aa1857d+ae03bbfc84,g35bb328faa+a8ce1bb630,g3a166c0a6a+ae03bbfc84,g3e281a1b8c+c5dd892a6c,g414038480c+6b9177ef31,g41af890bb2+9e154f3e8d,g6b1c1869cb+adc49b6f1a,g781aacb6e4+a8ce1bb630,g7af13505b9+3363a39af3,g7f202ee025+406ba613a5,g80478fca09+8fbba356e2,g82479be7b0+0d223595df,g858d7b2824+4e0faf0f7f,g89c8672015+f4add4ffd5,g9125e01d80+a8ce1bb630,g9726552aa6+414189b318,ga5288a1d22+32d6120315,gacef1a1666+7f85da65db,gb58c049af0+d64f4d3760,gbcfae0f0a0+a8c62e8bb6,gc28159a63d+ae03bbfc84,gcf0d15dbbd+412a8a6f35,gda6a2b7d83+412a8a6f35,gdaeeff99f8+1711a396fd,ge79ae78c31+ae03bbfc84,gf0baf85859+c1f95f4921,gfa517265be+4e0faf0f7f,gfa999e8aa5+17cd334064,gfb92a5be7c+4e0faf0f7f
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)