Skip to content

Commit

Permalink
Merge pull request #788 from desihub/mock-fixes
Browse files Browse the repository at this point in the history
restore select_mock_targets to working order
  • Loading branch information
moustakas committed Jan 18, 2022
2 parents d26a114 + 83c8a24 commit dace344
Show file tree
Hide file tree
Showing 24 changed files with 1,511 additions and 136 deletions.
176 changes: 90 additions & 86 deletions bin/select_mock_targets
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#!/usr/bin/env python

from __future__ import print_function, division

import os
import sys
import argparse
Expand All @@ -20,91 +18,97 @@ from desitarget.io import find_target_files
import multiprocessing
nproc = multiprocessing.cpu_count() // 2

parser = argparse.ArgumentParser()
parser.add_argument('-c', '--config', default='input.yaml')
parser.add_argument('-O', '--output_dir', help='Path to write the outputs', type=str, default='./')
parser.add_argument('-s', '--seed', help='Seed for random number generation', type=int, default=None)
parser.add_argument('-n', '--nproc', type=int, help='Number of concurrent processes to use [{}]'.format(nproc), default=nproc)
parser.add_argument('--survey', type=str, choices=['main', 'sv1'], help='Survey to simulate.', default='main')
parser.add_argument('--nside', help='Divide the DESI footprint into this healpix resolution', type=int, default=None)
parser.add_argument('--tiles', help='Path to file with tiles to cover', type=str)
parser.add_argument('--healpixels', help='Integer list of healpix pixels (corresponding to nside) to process.', type=int, nargs='*', default=None)
parser.add_argument('--join', action='store_true', help='Join the target and truth files in output_dir.')
parser.add_argument('-v','--verbose', action='store_true', help='Enable verbose output.')
parser.add_argument('--no-spectra', action='store_true', help='Do not generate spectra.')
parser.add_argument('--overwrite', action='store_true', help='Overwrite existing files.')

args = parser.parse_args()

if args.verbose:
log = get_logger(DEBUG)
else:
log = get_logger()

log.info('Starting select_mock_targets at {}'.format(time.asctime()))

if args.nside is None:
log.warning('NSIDE is a required input.')
raise ValueError

# Divide the DESI footprint into healpix pixels.
if args.nside < 2:
log.fatal('NSIDE = {} must be >=2'.format(args.nside))
raise Exception()

if args.tiles and args.healpixels:
log.error('use --tiles or --healpixels but not both')
sys.exit(1)
def main():
"""Main wrapper on select_mock_targets
"""
parser = argparse.ArgumentParser()
parser.add_argument('-c', '--config', default='input.yaml')
parser.add_argument('-O', '--output_dir', help='Path to write the outputs', type=str, default='./')
parser.add_argument('-s', '--seed', help='Seed for random number generation', type=int, default=None)
parser.add_argument('-n', '--nproc', type=int, help='Number of concurrent processes to use [{}]'.format(nproc), default=nproc)
parser.add_argument('--survey', type=str, choices=['main', 'sv1'], help='Survey to simulate.', default='main')
parser.add_argument('--nside', help='Divide the DESI footprint into this healpix resolution', type=int, default=None)
parser.add_argument('--tiles', help='Path to file with tiles to cover', type=str)
parser.add_argument('--healpixels', help='Integer list of healpix pixels (corresponding to nside) to process.', type=int, nargs='*', default=None)
parser.add_argument('--join', action='store_true', help='Join the target and truth files in output_dir.')
parser.add_argument('-v','--verbose', action='store_true', help='Enable verbose output.')
parser.add_argument('--no-spectra', action='store_true', help='Do not generate spectra.')
parser.add_argument('--overwrite', action='store_true', help='Overwrite existing files.')

if args.healpixels is None:
if args.tiles is not None:
if args.tiles.endswith('.ecsv'):
tiles = Table.read(args.tiles, format='ascii.ecsv')
args = parser.parse_args()

if args.verbose:
log = get_logger(DEBUG)
else:
log = get_logger()

log.info('Starting select_mock_targets at {}'.format(time.asctime()))

if args.nside is None:
log.warning('NSIDE is a required input.')
raise ValueError

# Divide the DESI footprint into healpix pixels.
if args.nside < 2:
log.fatal('NSIDE = {} must be >=2'.format(args.nside))
raise Exception()

if args.tiles and args.healpixels:
log.error('use --tiles or --healpixels but not both')
sys.exit(1)

if args.healpixels is None:
if args.tiles is not None:
if args.tiles.endswith('.ecsv'):
tiles = Table.read(args.tiles, format='ascii.ecsv')
else:
tiles = Table.read(args.tiles)
log.info('{} tiles'.format(len(tiles)))
else:
tiles = Table.read(args.tiles)
log.info('{} tiles'.format(len(tiles)))
tiles = None
log.info('Running on the full DESI footprint')
healpixels = desimodel.footprint.tiles2pix(args.nside, tiles)
else:
tiles = None
log.info('Running on the full DESI footprint')
healpixels = desimodel.footprint.tiles2pix(args.nside, tiles)
else:
healpixels = np.array(args.healpixels)

if args.overwrite:
log.info('Processing {} pixel(s).'.format(len(healpixels)))
else:
keep = list()
for i, pixnum in enumerate(healpixels):
truth_dark = find_target_files(args.output_dir, flavor="truth", obscon='dark',
hp=pixnum, nside=args.nside, mock=True)
truth_bright = find_target_files(args.output_dir, flavor="truth", obscon='bright',
hp=pixnum, nside=args.nside, mock=True)
if os.path.exists(truth_dark) or os.path.exists(truth_bright):
continue
keep.append(i)

log.info('{}/{} pixels remaining to do'.format(len(keep), len(healpixels)))
healpixels = healpixels[keep]

if args.join:
from desitarget.mock.build import join_targets_truth
join_targets_truth(mockdir=args.output_dir, outdir=args.output_dir, overwrite=args.overwrite)

# Construct Targets and Truth files
if not os.path.exists(args.config):
log.fatal('No configuration file {} found.'.format(args.config))
raise Exception()

if len(healpixels)>0:
# Read parameters from yaml file.
log.info('Reading configuration file {}'.format(args.config))
with open(args.config, 'r') as pfile:
params = yaml.safe_load(pfile)

log.info('Calling targets_truth with survey={} at {}'.format(args.survey, time.asctime()))
targets_truth(params, healpixels=healpixels, nside=args.nside, seed=args.seed,
output_dir=args.output_dir, nproc=args.nproc, verbose=args.verbose,
no_spectra=args.no_spectra, survey=args.survey)

log.info('All done at {}'.format(time.asctime()))
healpixels = np.array(args.healpixels)

if args.overwrite:
log.info('Processing {} pixel(s).'.format(len(healpixels)))
else:
keep = list()
for i, pixnum in enumerate(healpixels):
truth_dark = find_target_files(args.output_dir, flavor="truth", obscon='dark',
hp=pixnum, nside=args.nside, mock=True)
truth_bright = find_target_files(args.output_dir, flavor="truth", obscon='bright',
hp=pixnum, nside=args.nside, mock=True)
if os.path.exists(truth_dark) or os.path.exists(truth_bright):
continue
keep.append(i)

log.info('{}/{} pixels remaining to do'.format(len(keep), len(healpixels)))
healpixels = healpixels[keep]

if args.join:
from desitarget.mock.build import join_targets_truth
join_targets_truth(mockdir=args.output_dir, outdir=args.output_dir, overwrite=args.overwrite)

# Construct Targets and Truth files
if not os.path.exists(args.config):
log.fatal('No configuration file {} found.'.format(args.config))
raise Exception()

if len(healpixels)>0:
# Read parameters from yaml file.
log.info('Reading configuration file {}'.format(args.config))
with open(args.config, 'r') as pfile:
params = yaml.safe_load(pfile)

log.info('Calling targets_truth with survey={} at {}'.format(args.survey, time.asctime()))
targets_truth(params, healpixels=healpixels, nside=args.nside, seed=args.seed,
output_dir=args.output_dir, nproc=args.nproc, verbose=args.verbose,
no_spectra=args.no_spectra, survey=args.survey)

log.info('All done at {}'.format(time.asctime()))

if __name__ == '__main__':
main()
2 changes: 2 additions & 0 deletions doc/changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ desitarget Change Log
* Turn off BACKUP MTL processing for the Main Survey (for now).
* Allow the file format for override ledgers to be forced.
* Addresses `issue #784`_.
* Restore ``select_mock_targets`` to a working state [`PR #788`_].
* Add optional ``[grz]fiberflux`` arguments to the ``ELG``, ``LRG``, and
``BGS`` color-cut selection functions to support the `desisim`
template-generating code (see `desisim/PR #556`_) [`PR #786`_].
Expand All @@ -25,6 +26,7 @@ desitarget Change Log
.. _`desisim/PR #556`: https://github.com/desihub/desisim/pull/556
.. _`issue #784`: https://github.com/desihub/desitarget/issues/784
.. _`PR #786`: https://github.com/desihub/desitarget/pull/786
.. _`PR #788`: https://github.com/desihub/desitarget/pull/788
.. _`PR #789`: https://github.com/desihub/desitarget/pull/789

2.3.0 (2021-12-14)
Expand Down
Loading

0 comments on commit dace344

Please sign in to comment.