Skip to content

Commit

Permalink
internally rename stackfit-->fitstack keyword to avoid conflict with …
Browse files Browse the repository at this point in the history
…function name
  • Loading branch information
moustakas committed Nov 14, 2024
1 parent 7103f50 commit dfcde89
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 60 deletions.
31 changes: 18 additions & 13 deletions py/fastspecfit/fastspecfit.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@


def fastspec_one(iobj, data, meta, out_dtype, broadlinefit=True, fastphot=False,
constrain_age=False, no_smooth_continuum=False, debug_plots=False,
uncertainty_floor=0.01, minsnr_balmer_broad=2.5, nmonte=50, seed=1):
fitstack=False, constrain_age=False, no_smooth_continuum=False,
debug_plots=False, uncertainty_floor=0.01, minsnr_balmer_broad=2.5,
nmonte=50, seed=1):
"""Run :func:`fastspec` on a single object.
"""
Expand All @@ -37,15 +38,15 @@ def fastspec_one(iobj, data, meta, out_dtype, broadlinefit=True, fastphot=False,
log.info(f'Continuum- and emission-line fitting object {iobj} [{phot.uniqueid_col.lower()} ' + \
f'{data["uniqueid"]}, z={data["redshift"]:.6f}].')

if stackfit:
if fitstack:
one_stacked_spectrum(data, meta, synthphot=True, debug_plots=debug_plots)
else:
one_spectrum(data, meta, uncertainty_floor=uncertainty_floor, fastphot=fastphot,
synthphot=True, debug_plots=debug_plots)

# Copy parsed photometry from the 'data' dictionary to the 'meta' table.
if not fastphot:
if not stackfit:
if not fitstack:
flux = data['photometry']['nanomaggies']
fluxivar = data['photometry']['nanomaggies_ivar']
for iband, band in enumerate(phot.bands):
Expand Down Expand Up @@ -78,7 +79,7 @@ def fastspec_one(iobj, data, meta, out_dtype, broadlinefit=True, fastphot=False,
return out.value, meta, emmodel


def fastspec(fastphot=False, stackfit=False, args=None, comm=None, verbose=False):
def fastspec(fastphot=False, fitstack=False, args=None, comm=None, verbose=False):
"""Main fastspec script.
This script is the engine to model one or more DESI spectra. It initializes
Expand Down Expand Up @@ -117,7 +118,7 @@ def fastspec(fastphot=False, stackfit=False, args=None, comm=None, verbose=False
log.critical(errmsg)
raise KeyError(errmsg)

if stackfit:
if fitstack:
args.ignore_photometry = True

if verbose:
Expand All @@ -140,7 +141,7 @@ def fastspec(fastphot=False, stackfit=False, args=None, comm=None, verbose=False
'emlines_file': args.emlinesfile,
'fphotofile': args.fphotofile,
'fastphot': fastphot,
'stackfit': stackfit,
'fitstack': fitstack,
'ignore_photometry': args.ignore_photometry,
'template_file': args.templates,
'template_version': args.templateversion,
Expand Down Expand Up @@ -172,7 +173,7 @@ def fastspec(fastphot=False, stackfit=False, args=None, comm=None, verbose=False
Spec = DESISpectra(phot=sc_data.photometry, cosmo=sc_data.cosmology,
fphotodir=args.fphotodir, mapdir=args.mapdir)

if stackfit:
if fitstack:
data, meta = Spec.read_stacked(args.redrockfiles, firsttarget=args.firsttarget,
stackids=targetids, ntargets=args.ntargets,
constrain_age=args.constrain_age)
Expand All @@ -190,12 +191,16 @@ def fastspec(fastphot=False, stackfit=False, args=None, comm=None, verbose=False

ntargets = len(meta)
ncoeff = sc_data.templates.ntemplates
if fastphot:
cameras = None
else:
cameras = data[0]['cameras']

out_dtype, out_units = get_output_dtype(
Spec.specprod, phot=sc_data.photometry,
linetable=sc_data.emlines.table, ncoeff=ncoeff,
cameras=data[0]['cameras'], fastphot=fastphot,
stackfit=stackfit)
cameras=cameras, fastphot=fastphot,
fitstack=fitstack)

# Fit in parallel
t0 = time.time()
Expand All @@ -219,10 +224,10 @@ def fastspec(fastphot=False, stackfit=False, args=None, comm=None, verbose=False
out = list(zip(*_out))

meta = create_output_meta(vstack(out[1]), phot=sc_data.photometry,
fastphot=fastphot, stackfit=stackfit)
fastphot=fastphot, fitstack=fitstack)

results = create_output_table(out[0], meta, out_units,
stackfit=stackfit)
fitstack=fitstack)

if fastphot:
modelspectra = None
Expand Down Expand Up @@ -276,7 +281,7 @@ def stackfit(args=None, comm=None):
Intracommunicator used with MPI parallelism.
"""
fastspec(stackfit=True, args=args, comm=comm)
fastspec(fitstack=True, args=args, comm=comm)


def parse(options=None):
Expand Down
14 changes: 7 additions & 7 deletions py/fastspecfit/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -1742,7 +1742,7 @@ def select(fastfit, metadata, coadd_type, healpixels=None, tiles=None,

def get_output_dtype(specprod, phot, linetable, ncoeff,
cameras=['B', 'R', 'Z'], fastphot=False,
stackfit=False):
fitstack=False):
"""
Get type of one fastspecfit output data record, along
with dictionary of units for any fields that have them.
Expand Down Expand Up @@ -1922,7 +1922,7 @@ def add_field(name, dtype, shape=None, unit=None):
return np.dtype(out_dtype, align=True), out_units


def create_output_meta(input_meta, phot, fastphot=False, stackfit=False):
def create_output_meta(input_meta, phot, fastphot=False, fitstack=False):
"""Create the fastspecfit output metadata table.
"""
Expand All @@ -1934,7 +1934,7 @@ def create_output_meta(input_meta, phot, fastphot=False, stackfit=False):

# The information stored in the metadata table depends on which spectra
# were fitted (exposures, nightly coadds, deep coadds).
if stackfit:
if fitstack:
fluxcols = ['PHOTSYS']
else:
fluxcols = []
Expand All @@ -1960,7 +1960,7 @@ def create_output_meta(input_meta, phot, fastphot=False, stackfit=False):

skipcols = fluxcols + ['OBJTYPE', 'TARGET_RA', 'TARGET_DEC', 'BRICKNAME', 'BRICKID', 'BRICK_OBJID', 'RELEASE']

if stackfit:
if fitstack:
redrockcols = ('Z')
else:
redrockcols = ('Z', 'ZWARN', 'DELTACHI2', 'SPECTYPE', 'Z_RR', 'TSNR2_BGS',
Expand All @@ -1971,7 +1971,7 @@ def create_output_meta(input_meta, phot, fastphot=False, stackfit=False):

# All of this business is so we can get the columns in the order we want
# (i.e., the order that matches the data model).
if stackfit:
if fitstack:
for metacol in ('STACKID', 'SURVEY', 'PROGRAM'):
if metacol in metacols:
meta[metacol] = input_meta[metacol]
Expand Down Expand Up @@ -2016,14 +2016,14 @@ def create_output_meta(input_meta, phot, fastphot=False, stackfit=False):
return meta


def create_output_table(result_records, meta, units, stackfit=False):
def create_output_table(result_records, meta, units, fitstack=False):

from astropy.table import hstack

# Initialize the output table from the metadata table.
metacols = set(meta.colnames)

if stackfit:
if fitstack:
initcols = ('STACKID', 'SURVEY', 'PROGRAM')
else:
initcols = ('TARGETID', 'SURVEY', 'PROGRAM', 'HEALPIX', 'TILEID', 'NIGHT', 'FIBER', 'EXPID')
Expand Down
4 changes: 2 additions & 2 deletions py/fastspecfit/photometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Photometry(object):
"""Class to load filters and containing filter- and dust-related methods.
"""
def __init__(self, fphotofile=None, stackfit=False, ignore_photometry=False):
def __init__(self, fphotofile=None, fitstack=False, ignore_photometry=False):

"""
Parameters
Expand All @@ -33,7 +33,7 @@ def __init__(self, fphotofile=None, stackfit=False, ignore_photometry=False):

if fphotofile is None:
from importlib import resources
if stackfit:
if fitstack:
fphotofile = resources.files('fastspecfit').joinpath('data/stacked-phot.yaml')
else:
fphotofile = resources.files('fastspecfit').joinpath('data/legacysurvey-dr9.yaml')
Expand Down
Loading

0 comments on commit dfcde89

Please sign in to comment.