From bd8963b0bac31900eefc48738e0256bb41226bfe Mon Sep 17 00:00:00 2001 From: Simon Birrer Date: Fri, 17 May 2024 16:14:11 -0400 Subject: [PATCH 1/4] minor documentation change for light_3d --- lenstronomy/LightModel/light_model_base.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/lenstronomy/LightModel/light_model_base.py b/lenstronomy/LightModel/light_model_base.py index 0582557b2..4cb17de07 100644 --- a/lenstronomy/LightModel/light_model_base.py +++ b/lenstronomy/LightModel/light_model_base.py @@ -223,10 +223,15 @@ def surface_brightness(self, x, y, kwargs_list, k=None): return flux def light_3d(self, r, kwargs_list, k=None): - """Computes 3d density at radius r :param r: 3d radius units of arcsec relative - to the center of the light profile :param kwargs_list: keyword argument list of - light profile :param k: integer or list of integers for selecting subsets of - light profiles.""" + """Computes 3d density at radius r (3D radius) + such that integrated in projection in units of angle results in the projected surface brightness + + :param r: 3d radius units of arcsec relative to the center of the light profile + :param kwargs_list: keyword argument list of light profile + :param k: integer or list of integers for selecting subsets of light profiles. + + :return: flux density + """ kwargs_list_standard = self._transform_kwargs(kwargs_list) r = np.array(r, dtype=float) flux = np.zeros_like(r) From f1b150e39b1ff65ede210eac7d22fcd96451675b Mon Sep 17 00:00:00 2001 From: Simon Birrer Date: Wed, 22 May 2024 16:56:35 -0400 Subject: [PATCH 2/4] explicit tests for centroiding of the convolutions with all the different methods --- .../test_Numerics/test_convolution.py | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/test/test_ImSim/test_Numerics/test_convolution.py b/test/test_ImSim/test_Numerics/test_convolution.py index 31e7f7c4c..6c5b3ea92 100644 --- a/test/test_ImSim/test_Numerics/test_convolution.py +++ b/test/test_ImSim/test_Numerics/test_convolution.py @@ -45,6 +45,54 @@ def test_pixel_kernel(self): npt.assert_equal(pixel_conv.pixel_kernel(), kernel) npt.assert_equal(pixel_conv.pixel_kernel(num_pix=3), kernel[1:-1, 1:-1]) + def test_centroiding(self): + """ + this test convolves a Gaussian source centered in a centered pixel with a Gaussian and checks whether the + pixelated FFT convolution returns a centered image as well + + :return: + """ + # constructing a source + lightModel = LightModel(light_model_list=["GAUSSIAN"]) + delta_pix = 1 + x, y = util.make_grid(11, deltapix=delta_pix) + kwargs_model = [{"amp": 1, "sigma": 2, "center_x": 0, "center_y": 0}] + flux = lightModel.surface_brightness(x, y, kwargs_model) + model = util.array2image(flux) + model /= np.sum(flux) + + # compute moments of the source to check that it is centered in the center pixel + npt.assert_almost_equal(np.sum(flux * x), 0, decimal=5) + npt.assert_almost_equal(np.sum(flux * y), 0, decimal=5) + + # PSF + x_psf, y_psf = util.make_grid(21, deltapix=delta_pix) + kwargs_model = [{"amp": 1, "sigma": 2, "center_x": 0, "center_y": 0}] + psf_1d = lightModel.surface_brightness(x_psf, y_psf, kwargs_model) + psf = util.array2image(psf_1d) + psf /= np.sum(psf) + + npt.assert_almost_equal(np.sum(psf_1d * x_psf), 0, decimal=5) + npt.assert_almost_equal(np.sum(psf_1d * y_psf), 0, decimal=5) + + conv = PixelKernelConvolution(kernel=psf, convolution_type="fft_static") + model_conv = conv.convolution2d(model) + model_conv_1d = util.image2array(model_conv) + npt.assert_almost_equal(np.sum(model_conv_1d * x), 0, decimal=5) + npt.assert_almost_equal(np.sum(model_conv_1d * y), 0, decimal=5) + + conv = PixelKernelConvolution(kernel=psf, convolution_type="fft") + model_conv = conv.convolution2d(model) + model_conv_1d = util.image2array(model_conv) + npt.assert_almost_equal(np.sum(model_conv_1d * x), 0, decimal=5) + npt.assert_almost_equal(np.sum(model_conv_1d * y), 0, decimal=5) + + conv = PixelKernelConvolution(kernel=psf, convolution_type="grid") + model_conv = conv.convolution2d(model) + model_conv_1d = util.image2array(model_conv) + npt.assert_almost_equal(np.sum(model_conv_1d * x), 0, decimal=5) + npt.assert_almost_equal(np.sum(model_conv_1d * y), 0, decimal=5) + class TestSubgridKernelConvolution(object): def setup_method(self): From 573d8d0130ca89b95195030fb3181789d60bf7fc Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 22 May 2024 20:58:45 +0000 Subject: [PATCH 3/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- lenstronomy/LightModel/light_model_base.py | 5 ++--- test/test_ImSim/test_Numerics/test_convolution.py | 6 +++--- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/lenstronomy/LightModel/light_model_base.py b/lenstronomy/LightModel/light_model_base.py index a83a9058e..d37a255f7 100644 --- a/lenstronomy/LightModel/light_model_base.py +++ b/lenstronomy/LightModel/light_model_base.py @@ -228,13 +228,12 @@ def surface_brightness(self, x, y, kwargs_list, k=None): return flux def light_3d(self, r, kwargs_list, k=None): - """Computes 3d density at radius r (3D radius) - such that integrated in projection in units of angle results in the projected surface brightness + """Computes 3d density at radius r (3D radius) such that integrated in + projection in units of angle results in the projected surface brightness. :param r: 3d radius units of arcsec relative to the center of the light profile :param kwargs_list: keyword argument list of light profile :param k: integer or list of integers for selecting subsets of light profiles. - :return: flux density """ kwargs_list_standard = self._transform_kwargs(kwargs_list) diff --git a/test/test_ImSim/test_Numerics/test_convolution.py b/test/test_ImSim/test_Numerics/test_convolution.py index 6c5b3ea92..51980ec9f 100644 --- a/test/test_ImSim/test_Numerics/test_convolution.py +++ b/test/test_ImSim/test_Numerics/test_convolution.py @@ -46,9 +46,9 @@ def test_pixel_kernel(self): npt.assert_equal(pixel_conv.pixel_kernel(num_pix=3), kernel[1:-1, 1:-1]) def test_centroiding(self): - """ - this test convolves a Gaussian source centered in a centered pixel with a Gaussian and checks whether the - pixelated FFT convolution returns a centered image as well + """This test convolves a Gaussian source centered in a centered pixel with a + Gaussian and checks whether the pixelated FFT convolution returns a centered + image as well. :return: """ From e6b075519fe0f1ab833ab3a989dc863aee7098f8 Mon Sep 17 00:00:00 2001 From: Simon Birrer Date: Thu, 23 May 2024 21:32:20 -0400 Subject: [PATCH 4/4] addresses issue #613 --- lenstronomy/Workflow/fitting_sequence.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lenstronomy/Workflow/fitting_sequence.py b/lenstronomy/Workflow/fitting_sequence.py index dc0f30454..86cf24c5f 100644 --- a/lenstronomy/Workflow/fitting_sequence.py +++ b/lenstronomy/Workflow/fitting_sequence.py @@ -214,7 +214,7 @@ def best_fit_likelihood(self): :return: log likelihood, float """ - kwargs_result = self.best_fit(bijective=False) + kwargs_result = self.best_fit(bijective=True) param_class = self.param_class likelihoodModule = self.likelihoodModule logL = likelihoodModule.logL(param_class.kwargs2args(**kwargs_result))