Coverage for metadetect / lsst / tests / test_lsst_conventions.py: 29%
52 statements
« prev ^ index » next coverage.py v7.13.1, created at 2026-01-29 02:05 -0800
« prev ^ index » next coverage.py v7.13.1, created at 2026-01-29 02:05 -0800
1"""
2test using lsst simple sim
3"""
4import pytest
5import sys
6import numpy as np
7import logging
8from metadetect.lsst.metadetect import get_fitter
9import metadetect.lsst.measure
10from metadetect.lsst import util
11from lsst.utils import getPackageDir
13logging.basicConfig(
14 stream=sys.stdout,
15 level=logging.INFO,
16)
18try:
19 getPackageDir('descwl_shear_sims')
20 getPackageDir('descwl_coadd')
21 skip_tests_on_simulations = False
22except LookupError:
23 skip_tests_on_simulations = True
26def make_lsst_sim(rng, g1, g2, mag=20, hlr=1.0):
27 import descwl_shear_sims
29 coadd_dim = 251
31 bands = ['i']
33 galaxy_catalog = descwl_shear_sims.galaxies.FixedGalaxyCatalog(
34 rng=rng,
35 coadd_dim=coadd_dim,
36 buff=20,
37 layout='grid',
38 mag=mag,
39 hlr=hlr,
40 )
42 psf = descwl_shear_sims.psfs.make_fixed_psf(psf_type='gauss')
44 sim_data = descwl_shear_sims.make_sim(
45 rng=rng,
46 galaxy_catalog=galaxy_catalog,
47 coadd_dim=coadd_dim,
48 g1=g1,
49 g2=g2,
50 psf=psf,
51 bands=bands,
52 )
53 return sim_data
56def do_coadding(rng, sim_data, nowarp):
57 from descwl_coadd.coadd import make_coadd
58 from descwl_coadd.coadd_nowarp import make_coadd_nowarp
60 bands = list(sim_data['band_data'].keys())
62 if nowarp:
63 coadd_data_list = [
64 make_coadd_nowarp(
65 exp=sim_data['band_data'][band][0],
66 psf_dims=sim_data['psf_dims'],
67 rng=rng,
68 remove_poisson=False,
69 )
70 for band in bands
71 ]
72 else:
73 coadd_data_list = [
74 make_coadd(
75 exps=sim_data['band_data'][band],
76 psf_dims=sim_data['psf_dims'],
77 rng=rng,
78 coadd_wcs=sim_data['coadd_wcs'],
79 coadd_bbox=sim_data['coadd_bbox'],
80 remove_poisson=False,
81 )
82 for band in bands
83 ]
85 return util.extract_multiband_coadd_data(coadd_data_list)
88@pytest.mark.skipif(
89 skip_tests_on_simulations,
90 reason='descwl_shear_sims and/or descwl_coadd not available'
91)
92@pytest.mark.parametrize('ginput', [
93 (0.1, 0.0),
94 (0.1, 0.1),
95 (0.1, -0.1),
96 (0.0, 0.1),
97 (0.0, -0.1),
98 (-0.1, 0.0),
99 (-0.1, 0.1),
100 (-0.1, -0.1),
101])
102def test_convention(ginput):
103 rng = np.random.RandomState(35)
105 g1in, g2in = ginput
107 sim_data = make_lsst_sim(rng, g1=g1in, g2=g2in)
109 data = do_coadding(rng=rng, sim_data=sim_data, nowarp=True)
111 sources, detexp = metadetect.lsst.measure.detect_and_deblend(
112 mbexp=data['mbexp'], rng=rng,
113 )
115 config = {'meas_type': 'wmom', 'weight': {'fwhm': 1.2}}
116 fitter = get_fitter(config, rng=rng)
118 res = metadetect.lsst.measure.measure(
119 mbexp=data['mbexp'], detexp=detexp, sources=sources, fitter=fitter,
120 stamp_size=47,
121 )
123 w, = np.where(res['wmom_flags'] == 0)
125 g1, g2 = res['wmom_g'][w].mean(axis=0)
127 if g1in > 0:
128 assert g1 > 0
129 if g1in < 0:
130 assert g1 < 0
131 if g2in > 0:
132 assert g2 > 0
133 if g2in < 0:
134 assert g2 < 0