Skip to content

Commit

Permalink
ENH enable gsobjects from eval (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
beckermr authored Oct 19, 2023
1 parent 7bdae5d commit 743be9b
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 1 deletion.
1 change: 1 addition & 0 deletions montara/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@
from . import mixed_scene_postop # noqa
from . import badpixfromfits # noqa
from . import eastlake_step # noqa
from . import eval_gsobject # noqa
1 change: 1 addition & 0 deletions montara/des_tile.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 14 additions & 1 deletion montara/eastlake_step.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
10 changes: 10 additions & 0 deletions montara/eval_gsobject.py
Original file line number Diff line number Diff line change
@@ -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)
16 changes: 16 additions & 0 deletions montara/tests/test_eval_gsobject.py
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions ruff.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Allow lines to be as long as 120 characters.
line-length = 120

0 comments on commit 743be9b

Please sign in to comment.