Coverage for metadetect / lsst / tests / test_lsst_conventions.py: 30%
51 statements
« prev ^ index » next coverage.py v7.13.4, created at 2026-03-23 22:27 -0700
« prev ^ index » next coverage.py v7.13.4, created at 2026-03-23 22:27 -0700
1"""
2test using lsst simple sim
3"""
4import pytest
5import sys
6import numpy as np
7import logging
8from metadetect.lsst.configs import get_config
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 = get_config()
117 res = metadetect.lsst.measure.measure(
118 mbexp=data['mbexp'],
119 detexp=detexp,
120 sources=sources,
121 config=config,
122 rng=rng,
123 )
125 w, = np.where(res['gauss_flags'] == 0)
127 g1, g2 = res['gauss_g'][w].mean(axis=0)
129 if g1in > 0:
130 assert g1 > 0
131 if g1in < 0:
132 assert g1 < 0
133 if g2in > 0:
134 assert g2 > 0
135 if g2in < 0:
136 assert g2 < 0