From fea32f8eb094e6e2132d09f97487ad5eba7c6cd4 Mon Sep 17 00:00:00 2001 From: Stephen Bailey Date: Tue, 26 Feb 2019 16:22:41 -0800 Subject: [PATCH 1/2] fix desitarget.mock.build.targets_truth --- py/desitarget/mock/build.py | 16 +++----- py/desitarget/test/test_mock_build.py | 58 ++++++++++++++++++++++++++- 2 files changed, 63 insertions(+), 11 deletions(-) diff --git a/py/desitarget/mock/build.py b/py/desitarget/mock/build.py index 7675b1953..0018a7023 100644 --- a/py/desitarget/mock/build.py +++ b/py/desitarget/mock/build.py @@ -935,7 +935,7 @@ def targets_truth(params, healpixels=None, nside=None, output_dir='.', seed=healseed) def finish_catalog(targets, truth, objtruth, skytargets, skytruth, healpix, - nside, log, seed=None, survey='main'): + nside, log, seed=None): """Add hpxpixel, brick_objid, targetid, subpriority, priority, and numobs to the target catalog. @@ -959,11 +959,7 @@ def finish_catalog(targets, truth, objtruth, skytargets, skytruth, healpix, Logger object. seed : :class:`int`, optional Seed for the random number generation. Defaults to None. - survey : :class:`str` - Specifies which target masks yaml file to use. Options are `main`, `cmx` - and `sv` for the main survey, commissioning and SV, respectively. - Defaults to `main`. - + Returns ------- Updated versions of targets, truth, objtruth, skytargets, and skytruth. @@ -1013,8 +1009,8 @@ def finish_catalog(targets, truth, objtruth, skytargets, skytruth, healpix, log.warning('Mismatching TARGETIDs!') raise ValueError - targets['PRIORITY_INIT'], targets['NUMOBS_INIT'] = initial_priority_numobs( - targets, survey=survey) + targets['PRIORITY_INIT'], targets['NUMOBS_INIT'] = \ + initial_priority_numobs(targets) # Rename TYPE --> MORPHTYPE targets.rename_column('TYPE', 'MORPHTYPE') @@ -1028,8 +1024,8 @@ def finish_catalog(targets, truth, objtruth, skytargets, skytruth, healpix, skytargets['SUBPRIORITY'][:] = subpriority[nobj:] skytruth['TARGETID'][:] = targetid[nobj:] - skytargets['PRIORITY_INIT'], skytargets['NUMOBS_INIT'] = initial_priority_numobs( - skytargets, survey=survey) + skytargets['PRIORITY_INIT'], skytargets['NUMOBS_INIT'] = \ + initial_priority_numobs(skytargets) # Rename TYPE --> MORPHTYPE skytargets.rename_column('TYPE', 'MORPHTYPE') diff --git a/py/desitarget/test/test_mock_build.py b/py/desitarget/test/test_mock_build.py index fe46a4115..62db56314 100644 --- a/py/desitarget/test/test_mock_build.py +++ b/py/desitarget/test/test_mock_build.py @@ -3,19 +3,75 @@ """Test desitarget.mock.build, but only add_mock_shapes_and_fluxes for now. """ import unittest +import tempfile +import os +import shutil +from pkg_resources import resource_filename import numpy as np from astropy.table import Table import healpy as hp +import fitsio import desimodel.footprint from desitarget.mock.sky import random_sky +from desitarget.mock.build import targets_truth from desitarget.targetmask import desi_mask, bgs_mask, mws_mask class TestMockBuild(unittest.TestCase): def setUp(self): - pass + self.outdir = tempfile.mkdtemp() + print('Output directory {}'.format(self.outdir)) + + def tearDown(self): + if os.path.exists(self.outdir): + shutil.rmtree(self.outdir) + + @unittest.skipUnless('DESITARGET_RUN_MOCK_UNITTEST' in os.environ, '$DESITARGET_RUN_MOCK_UNITTEST not set; skipping expensive mock tests') + def test_targets_truth(self): + configfile = resource_filename('desitarget.mock', 'data/select-mock-targets.yaml') + + import yaml + with open(configfile) as fx: + params = yaml.load(fx) + + #- Trim for faster initial testing + px = dict() + px['targets'] = dict() + px['targets']['ELG'] = params['targets']['ELG'] + px['contaminants'] = dict() + px['contaminants']['targets'] = dict() + px['contaminants']['targets']['ELG'] = params['contaminants']['targets']['ELG'] + params = px + + for targettype in params['targets'].keys(): + mockfile = params['targets'][targettype]['mockfile'].format(**os.environ) + self.assertTrue(os.path.isfile(mockfile), 'Missing {}'.format(mockfile)) + + #- Test without spectra + targets_truth(px, healpixels=[99737,], nside=256, output_dir=self.outdir, no_spectra=True) + targetfile = self.outdir + '/997/99737/targets-256-99737.fits' + truthfile = self.outdir + '/997/99737/truth-256-99737.fits' + self.assertTrue(os.path.exists(targetfile)) + self.assertTrue(os.path.exists(truthfile)) + + with fitsio.FITS(truthfile) as fx: + self.assertTrue('TRUTH' in fx) + self.assertTrue('WAVE' not in fx) + self.assertTrue('FLUX' not in fx) + + #- Test with spectra + shutil.rmtree(self.outdir+'/997') + + targets_truth(px, healpixels=[99737,], nside=256, output_dir=self.outdir, no_spectra=False) + self.assertTrue(os.path.exists(targetfile)) + self.assertTrue(os.path.exists(truthfile)) + + with fitsio.FITS(truthfile) as fx: + self.assertTrue('TRUTH' in fx) + self.assertTrue('WAVE' in fx) + self.assertTrue('FLUX' in fx) @unittest.skip('This test is deprecated, so skip for now.') def test_shapes_and_fluxes(self): From 66d4a71a5449a9e5f87f83fa9c7c84265de99cca Mon Sep 17 00:00:00 2001 From: Stephen Bailey Date: Tue, 26 Feb 2019 16:57:50 -0800 Subject: [PATCH 2/2] cleanup test --- py/desitarget/test/test_mock_build.py | 21 ++++++--------------- 1 file changed, 6 insertions(+), 15 deletions(-) diff --git a/py/desitarget/test/test_mock_build.py b/py/desitarget/test/test_mock_build.py index 62db56314..288e6fd2b 100644 --- a/py/desitarget/test/test_mock_build.py +++ b/py/desitarget/test/test_mock_build.py @@ -22,7 +22,6 @@ class TestMockBuild(unittest.TestCase): def setUp(self): self.outdir = tempfile.mkdtemp() - print('Output directory {}'.format(self.outdir)) def tearDown(self): if os.path.exists(self.outdir): @@ -36,21 +35,12 @@ def test_targets_truth(self): with open(configfile) as fx: params = yaml.load(fx) - #- Trim for faster initial testing - px = dict() - px['targets'] = dict() - px['targets']['ELG'] = params['targets']['ELG'] - px['contaminants'] = dict() - px['contaminants']['targets'] = dict() - px['contaminants']['targets']['ELG'] = params['contaminants']['targets']['ELG'] - params = px - for targettype in params['targets'].keys(): mockfile = params['targets'][targettype]['mockfile'].format(**os.environ) - self.assertTrue(os.path.isfile(mockfile), 'Missing {}'.format(mockfile)) + self.assertTrue(os.path.exists(mockfile), 'Missing {}'.format(mockfile)) #- Test without spectra - targets_truth(px, healpixels=[99737,], nside=256, output_dir=self.outdir, no_spectra=True) + targets_truth(params, healpixels=[99737,], nside=256, output_dir=self.outdir, no_spectra=True) targetfile = self.outdir + '/997/99737/targets-256-99737.fits' truthfile = self.outdir + '/997/99737/truth-256-99737.fits' self.assertTrue(os.path.exists(targetfile)) @@ -58,13 +48,14 @@ def test_targets_truth(self): with fitsio.FITS(truthfile) as fx: self.assertTrue('TRUTH' in fx) - self.assertTrue('WAVE' not in fx) - self.assertTrue('FLUX' not in fx) + #- WAVE is there, and FLUX is there but with strange shape (n,0) + # self.assertTrue('WAVE' not in fx) + # self.assertTrue('FLUX' not in fx) #- Test with spectra shutil.rmtree(self.outdir+'/997') - targets_truth(px, healpixels=[99737,], nside=256, output_dir=self.outdir, no_spectra=False) + targets_truth(params, healpixels=[99737,], nside=256, output_dir=self.outdir, no_spectra=False) self.assertTrue(os.path.exists(targetfile)) self.assertTrue(os.path.exists(truthfile))