From 743be9b0e985e91d664081e390b8b75429e340d6 Mon Sep 17 00:00:00 2001 From: "Matthew R. Becker" Date: Thu, 19 Oct 2023 07:06:47 -0500 Subject: [PATCH] ENH enable gsobjects from eval (#20) --- montara/__init__.py | 1 + montara/des_tile.py | 1 + montara/eastlake_step.py | 15 ++++++++++++++- montara/eval_gsobject.py | 10 ++++++++++ montara/tests/test_eval_gsobject.py | 16 ++++++++++++++++ ruff.toml | 2 ++ 6 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 montara/eval_gsobject.py create mode 100644 montara/tests/test_eval_gsobject.py create mode 100644 ruff.toml diff --git a/montara/__init__.py b/montara/__init__.py index cc3db68..87919fa 100644 --- a/montara/__init__.py +++ b/montara/__init__.py @@ -18,3 +18,4 @@ from . import mixed_scene_postop # noqa from . import badpixfromfits # noqa from . import eastlake_step # noqa +from . import eval_gsobject # noqa diff --git a/montara/des_tile.py b/montara/des_tile.py index fa74ef5..f47f38a 100644 --- a/montara/des_tile.py +++ b/montara/des_tile.py @@ -901,6 +901,7 @@ def buildImages( 'rejectlist_file', 'dither_scale', 'coadd_wcs', 'n_se_test', 'grid_border'] ignore += ['file_name', 'dir'] + ignore += ['analyze_with_interpimage_psf'] logger.debug("current mag_zp: %f" % base["eval_variables"]["fmag_zp"]) # We are making sure to call the OutputBuilder from galsim diff --git a/montara/eastlake_step.py b/montara/eastlake_step.py index 8c67313..a912f62 100644 --- a/montara/eastlake_step.py +++ b/montara/eastlake_step.py @@ -198,7 +198,20 @@ def update_stash(self, config, stash): stash["rejectlist"] = rejectlist.rejectlist_data # Add the PSF config - stash["psf_config"] = config["psf"] + if config["output"].get("analyze_with_interpimage_psf", False): + import copy + _psf, safe = galsim.config.BuildGSObject({'blah': copy.deepcopy(config["psf"])}, 'blah') + assert safe, "PSF model must be reusable (safe) to use as an InterpolatedImage" + _psf = _psf.withFlux(1.0).drawImage(nx=25, ny=25, scale=0.263) + _psf = galsim.InterpolatedImage(_psf, x_interpolant='lanczos15') + with np.printoptions(threshold=np.inf, precision=32): + _psf = repr(_psf) + stash["psf_config"] = { + "type": "Eval", + "str": _psf.replace("array(", "np.array("), + } + else: + stash["psf_config"] = config["psf"] # add draw_method if present if "draw_method" in config["stamp"]: stash["draw_method"] = config["stamp"]["draw_method"] diff --git a/montara/eval_gsobject.py b/montara/eval_gsobject.py new file mode 100644 index 0000000..5a6a2e6 --- /dev/null +++ b/montara/eval_gsobject.py @@ -0,0 +1,10 @@ +import galsim + + +def EvalGSObject(config, base, ignore, gsparams, logger): + req = {'str': str} + params, safe = galsim.config.GetAllParams(config, base, req=req, ignore=ignore) + return galsim.utilities.math_eval(params['str']).withGSParams(**gsparams), safe + + +galsim.config.RegisterObjectType('Eval', EvalGSObject) diff --git a/montara/tests/test_eval_gsobject.py b/montara/tests/test_eval_gsobject.py new file mode 100644 index 0000000..14b06cc --- /dev/null +++ b/montara/tests/test_eval_gsobject.py @@ -0,0 +1,16 @@ +import galsim +import numpy as np + + +def test_build_gsobject_evalrepr(): + g = galsim.Gaussian(fwhm=1.0).drawImage(nx=25, ny=25, scale=0.263) + ii = galsim.InterpolatedImage(g, x_interpolant='lanczos15') + with np.printoptions(threshold=np.inf, precision=32): + r = repr(ii) + cfg = { + "type": "Eval", + "str": r.replace("array(", "np.array("), + } + rii, safe = galsim.config.BuildGSObject({'blah': cfg}, 'blah') + assert safe + assert rii == ii diff --git a/ruff.toml b/ruff.toml new file mode 100644 index 0000000..6802919 --- /dev/null +++ b/ruff.toml @@ -0,0 +1,2 @@ +# Allow lines to be as long as 120 characters. +line-length = 120