Skip to content

Commit

Permalink
* remove trailing whitespace from lines in all files
Browse files Browse the repository at this point in the history
* remove shell commands from tops of files that do not contain main or test for __main__
  • Loading branch information
jdbuhler committed Sep 5, 2024
1 parent 9db0531 commit ea8db89
Show file tree
Hide file tree
Showing 12 changed files with 95 additions and 90 deletions.
4 changes: 2 additions & 2 deletions py/fastspecfit/continuum.py
Original file line number Diff line number Diff line change
Expand Up @@ -857,13 +857,13 @@ def _stellar_objective(self, params, templateflux,
spec_resid = resid[:resid_split]
np.subtract(modelflux, specflux, spec_resid)
spec_resid *= specistd

if synthphot:
modelflam = self.continuum_to_photometry(fullmodel)
phot_resid = resid[resid_split:]
np.subtract(modelflam, objflam, phot_resid)
phot_resid *= objflamistd

return resid


Expand Down
12 changes: 6 additions & 6 deletions py/fastspecfit/emlines.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,33 +134,33 @@ def compute_inrange_lines(self, redshift, wavelims=(3600., 9900.), wavepad=5*0.8
((zlinewave > (wavelims[0] + wavepad)) & \
(zlinewave < (wavelims[1] - wavepad)))


def build_linemodels(self, separate_oiii_fit=True):
"""Build emission line model tables, with and without
suppression of broad lines. Establish fixed (i.e., forced to
zero) params, tying relationships between params, and doublet
relationships for each model, as well as the relationship
between lines and their parameters, which we record in the
line table.
Parameter fixing needs to know which lines are within the
observed wavelength ranges of the cameras, so we first add
this information to the line table.
"""

def create_model(tying_info, forceFixed=[]):
"""Given the tying info for the model and the list of
in-range lines, determine which parameters of the model
are fixed and free, and create the model's table.
We fix all parameters for out-of-range lines to zero,
unless the parameter is tied to a parameter for an
in-range line. We also allow the caller to specify
parameters that should be fixed regardless.
"""

n_params = len(self.param_table)
isfixed = np.full(n_params, False, dtype=bool)

Expand Down Expand Up @@ -355,7 +355,7 @@ def tie_line(tying_info, line_params, source_linename, amp_factor=None):

def summarize_linemodel(self, linemodel):
"""Simple function to summarize an input linemodel."""

def _print(line_mask):
for line in np.where(line_mask)[0]:
line_name = self.line_table['name'][line]
Expand Down
21 changes: 18 additions & 3 deletions py/fastspecfit/fastspecfit.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#!/usr/bin/env python
"""
fastspecfit.fastspecfit
=======================
Expand All @@ -15,6 +14,10 @@
from fastspecfit.singlecopy import sc_data
from fastspecfit.util import BoxedScalar, MPPool

import cProfile as profile
import pstats
pr = profile.Profile()
shot = 0

def fastspec_one(iobj, data, out_dtype, broadlinefit=True, fastphot=False,
constrain_age=False, no_smooth_continuum=False,
Expand All @@ -41,11 +44,20 @@ def fastspec_one(iobj, data, out_dtype, broadlinefit=True, fastphot=False,
# output structure
out = BoxedScalar(out_dtype)

global shot
if shot > 0:
pr.enable()

continuummodel, smooth_continuum = continuum_specfit(
data, out, templates, igm, phot, constrain_age=constrain_age,
no_smooth_continuum=no_smooth_continuum,
fastphot=fastphot, debug_plots=debug_plots)

if shot > 0:
pr.disable()
else:
shot = 1

# Optionally fit the emission-line spectrum.
if fastphot:
emmodel = None
Expand Down Expand Up @@ -212,6 +224,10 @@ def fastspec(fastphot=False, stackfit=False, args=None, comm=None, verbose=False

log.info(f'Fitting {Spec.ntargets} object(s) took {time.time()-t0:.2f} seconds.')

st = pstats.Stats(pr).strip_dirs().sort_stats("cumulative")
st.print_stats()
st.print_callees()

write_fastspecfit(results, meta, modelspectra=modelspectra, outfile=args.outfile,
specprod=Spec.specprod, coadd_type=Spec.coadd_type,
fphotofile=sc_data.photometry.fphotofile,
Expand All @@ -235,7 +251,7 @@ def fastphot(args=None, comm=None):
Parameters
----------
args : :class:`argparse.Namespace` or ``None``
Required and optional arguments parsed via inputs to the command line.
Required and optional arguments parsed via inputs to the command line.
comm : :class:`mpi4py.MPI.MPI.COMM_WORLD` or `None`
Intracommunicator used with MPI parallelism.
Expand Down Expand Up @@ -606,4 +622,3 @@ def create_output_table(result_records, meta, units, stackfit=False):
results = hstack((results, Table(np.array(result_records), units=units)))

return results

3 changes: 1 addition & 2 deletions py/fastspecfit/igm.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#!/usr/bin/env python
"""
fastspecfit.igm
===============
Expand Down Expand Up @@ -198,7 +197,7 @@ def _tLCDLA(zS, lobs):
0.04696 * z**3 - \
0.01779 * z**3.3 * (lobs[i]/lamL) ** -0.3 - \
0.1347 * (lobs[i]/lamL) ** 2 - \
0.2905 * (lobs[i]/lamL) ** -0.3
0.2905 * (lobs[i]/lamL) ** -0.3
else:
r[i] = \
0.04696 * z**3 - \
Expand Down
13 changes: 6 additions & 7 deletions py/fastspecfit/io.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#!/usr/bin/env python
"""
fastspecfit.io
==============
Expand Down Expand Up @@ -36,8 +35,8 @@
# fibermap and exp_fibermap columns to read
FMCOLS = ('TARGETID', 'TARGET_RA', 'TARGET_DEC', 'COADD_FIBERSTATUS', 'OBJTYPE',
'PHOTSYS', 'RELEASE', 'BRICKNAME', 'BRICKID', 'BRICK_OBJID',
#'FIBERFLUX_G', 'FIBERFLUX_R', 'FIBERFLUX_Z',
#'FIBERTOTFLUX_G', 'FIBERTOTFLUX_R', 'FIBERTOTFLUX_Z',
#'FIBERFLUX_G', 'FIBERFLUX_R', 'FIBERFLUX_Z',
#'FIBERTOTFLUX_G', 'FIBERTOTFLUX_R', 'FIBERTOTFLUX_Z',
#'FLUX_G', 'FLUX_R', 'FLUX_Z', 'FLUX_W1', 'FLUX_W2',
#'FLUX_IVAR_G', 'FLUX_IVAR_R', 'FLUX_IVAR_Z', 'FLUX_IVAR_W1', 'FLUX_IVAR_W2'
)
Expand Down Expand Up @@ -945,7 +944,7 @@ def one_spectrum(iobj, specdata, meta, ebv, fastphot,
specdata['mask'].append(specdata['mask0'][icam])

specdata['res'].append(Resolution(res))

if len(cameras) == 0:
errmsg = 'No good data, which should never happen.'
log.critical(errmsg)
Expand All @@ -956,7 +955,7 @@ def one_spectrum(iobj, specdata, meta, ebv, fastphot,
for key in ('wave', 'flux', 'ivar', 'mask', 'res'):
del specdata[key + '0']
specdata[key] = tuple(specdata[key])

# Pre-compute some convenience variables for "un-hstacking"
# an "hstacked" spectrum.
specdata['cameras'] = np.array(cameras)
Expand All @@ -968,7 +967,7 @@ def one_spectrum(iobj, specdata, meta, ebv, fastphot,
specdata['camerapix'] = np.zeros((ncam, 2), np.int16)
specdata['camerapix'][:, 0] = c_starts
specdata['camerapix'][:, 1] = c_ends

# use the coadded spectrum to build a robust emission-line mask
LM = LineMasker(emline_table)
pix = LM.build_linemask(
Expand Down Expand Up @@ -1351,7 +1350,7 @@ def one_stacked_spectrum(iobj, specdata, synthphot):
for key in ('wave', 'flux', 'ivar', 'mask', 'res'):
del specdata[key + '0']
specdata[key] = tuple(specdata[key])

# Pre-compute some convenience variables for "un-hstacking"
# an "hstacked" spectrum.
specdata['cameras'] = np.array(cameras)
Expand Down
5 changes: 2 additions & 3 deletions py/fastspecfit/linemasker.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def _get_contpix(zlinewaves, sigmas, nsigma_factor=2., linemask=None, lya=False)
for linename, zlinewave, sigma in zip(linenames, zlinewaves, linesigmas_ang):
# skip fixed (e.g., hbeta_broad) lines
if sigma <= 0.:
continue
continue
I = _get_linepix(zlinewave, sigma)
if len(I) > 0:
linemask[I] = True
Expand Down Expand Up @@ -424,7 +424,7 @@ def _fit_patches(continuum_patches, patchMap, linemodel, debug_plots=False,
else:
isNarrow = EMFit.isNarrow * Ifree
if np.any(isNarrow):
linesigma_narrow = np.atleast_1d(linesigmas[isNarrow])[0]
linesigma_narrow = np.atleast_1d(linesigmas[isNarrow])[0]
linevshift_narrow = np.atleast_1d(linevshifts[isNarrow])[0]
maxsnr_narrow = np.max(linesnrs[isNarrow])
else:
Expand Down Expand Up @@ -785,4 +785,3 @@ def _niceline(line):
}

return out

50 changes: 24 additions & 26 deletions py/fastspecfit/qa.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#!/usr/bin/env python
"""
fastspecfit.qa
==============
Expand Down Expand Up @@ -300,7 +299,7 @@ def major_formatter(x, pos):
elif fastspec['HGAMMA_BROAD_AMP']*np.sqrt(fastspec['HGAMMA_BROAD_AMP_IVAR']) > snrcut:
leg_broad['ewbalmer_broad'] = r'EW(H$\gamma)_{\mathrm{broad}}=$'+r'${:.1f}$'.format(fastspec['HGAMMA_BROAD_EW'])+r' $\AA$'

if (fastspec['OII_3726_AMP']*np.sqrt(fastspec['OII_3726_AMP_IVAR']) > snrcut or
if (fastspec['OII_3726_AMP']*np.sqrt(fastspec['OII_3726_AMP_IVAR']) > snrcut or
fastspec['OII_3729_AMP']*np.sqrt(fastspec['OII_3729_AMP_IVAR']) > snrcut):
leg_narrow['ewoii'] = r'EW([OII])'+r'$={:.1f}$'.format(fastspec['OII_3726_EW']+fastspec['OII_3729_EW'])+r' $\AA$'

Expand All @@ -315,24 +314,24 @@ def major_formatter(x, pos):
elif fastspec['HGAMMA_AMP']*np.sqrt(fastspec['HGAMMA_AMP_IVAR']) > snrcut:
leg_narrow['ewbalmer_narrow'] = r'EW(H$\gamma)=$'+r'${:.1f}$'.format(fastspec['HGAMMA_EW'])+r' $\AA$'

if (fastspec['HALPHA_AMP']*np.sqrt(fastspec['HALPHA_AMP_IVAR']) > snrcut and
if (fastspec['HALPHA_AMP']*np.sqrt(fastspec['HALPHA_AMP_IVAR']) > snrcut and
fastspec['HBETA_AMP']*np.sqrt(fastspec['HBETA_AMP_IVAR']) > snrcut):
leg_narrow['hahb'] = r'$\mathrm{H}\alpha/\mathrm{H}\beta=$'+r'${:.3f}$'.format(fastspec['HALPHA_FLUX']/fastspec['HBETA_FLUX'])
if 'hahb' not in leg_narrow.keys() and (fastspec['HBETA_AMP']*np.sqrt(fastspec['HBETA_AMP_IVAR']) > snrcut and
if 'hahb' not in leg_narrow.keys() and (fastspec['HBETA_AMP']*np.sqrt(fastspec['HBETA_AMP_IVAR']) > snrcut and
fastspec['HGAMMA_AMP']*np.sqrt(fastspec['HGAMMA_AMP_IVAR']) > snrcut):
leg_narrow['hbhg'] = r'$\mathrm{H}\beta/\mathrm{H}\gamma=$'+r'${:.3f}$'.format(fastspec['HBETA_FLUX']/fastspec['HGAMMA_FLUX'])
if (fastspec['HBETA_AMP']*np.sqrt(fastspec['HBETA_AMP_IVAR']) > snrcut and
fastspec['OIII_5007_AMP']*np.sqrt(fastspec['OIII_5007_AMP_IVAR']) > snrcut and
if (fastspec['HBETA_AMP']*np.sqrt(fastspec['HBETA_AMP_IVAR']) > snrcut and
fastspec['OIII_5007_AMP']*np.sqrt(fastspec['OIII_5007_AMP_IVAR']) > snrcut and
fastspec['HBETA_FLUX'] > 0 and fastspec['OIII_5007_FLUX'] > 0):
leg_narrow['oiiihb'] = r'$\log_{10}(\mathrm{[OIII]/H}\beta)=$'+r'${:.3f}$'.format(
np.log10(fastspec['OIII_5007_FLUX']/fastspec['HBETA_FLUX']))
if (fastspec['HALPHA_AMP']*np.sqrt(fastspec['HALPHA_AMP_IVAR']) > snrcut and
fastspec['NII_6584_AMP']*np.sqrt(fastspec['NII_6584_AMP_IVAR']) > snrcut and
if (fastspec['HALPHA_AMP']*np.sqrt(fastspec['HALPHA_AMP_IVAR']) > snrcut and
fastspec['NII_6584_AMP']*np.sqrt(fastspec['NII_6584_AMP_IVAR']) > snrcut and
fastspec['HALPHA_FLUX'] > 0 and fastspec['NII_6584_FLUX'] > 0):
leg_narrow['niiha'] = r'$\log_{10}(\mathrm{[NII]/H}\alpha)=$'+r'${:.3f}$'.format(
np.log10(fastspec['NII_6584_FLUX']/fastspec['HALPHA_FLUX']))

if (fastspec['OII_3726_AMP']*np.sqrt(fastspec['OII_3726_AMP_IVAR']) > snrcut or
if (fastspec['OII_3726_AMP']*np.sqrt(fastspec['OII_3726_AMP_IVAR']) > snrcut or
fastspec['OII_3729_AMP']*np.sqrt(fastspec['OII_3729_AMP_IVAR']) > snrcut):
#if fastspec['OII_DOUBLET_RATIO'] != 0:
leg_narrow['oii_doublet'] = r'[OII] $\lambda3726/\lambda3729={:.3f}$'.format(fastspec['OII_DOUBLET_RATIO'])
Expand All @@ -347,7 +346,7 @@ def major_formatter(x, pos):
sedmodel, sedphot = CTools.templates2data(
templates.flux, templates.wave,
redshift=redshift, dluminosity=dlum, photsys=metadata['PHOTSYS'],
synthphot=True, coeff=fastspec['COEFF'] * CTools.massnorm,
synthphot=True, coeff=fastspec['COEFF'] * CTools.massnorm,
get_abmag=True)
else:
sedmodel = CTools.build_stellar_continuum(
Expand Down Expand Up @@ -398,7 +397,7 @@ def major_formatter(x, pos):
else:
contmodel = CTools.build_stellar_continuum(
templates.flux_nolines, fastspec['COEFF'],
vdisp=fastspec['VDISP'], conv_pre=templates.conv_pre_nolines,
vdisp=fastspec['VDISP'], conv_pre=templates.conv_pre_nolines,
ebv=fastspec['AV'] / Templates.klambda(5500.)
)

Expand All @@ -421,7 +420,7 @@ def major_formatter(x, pos):
fullsmoothcontinuum = np.zeros_like(fullwave)
else:
fullsmoothcontinuum = CTools.smooth_continuum(
fullwave, np.hstack(desiresiduals), np.hstack(data['ivar']),
fullwave, np.hstack(desiresiduals), np.hstack(data['ivar']),
np.hstack(data['linemask']), camerapix=data['camerapix'])

desismoothcontinuum = []
Expand Down Expand Up @@ -460,7 +459,7 @@ def major_formatter(x, pos):
if not os.path.isfile(cutoutjpeg):
wait = 5 # seconds
cmd = 'wget -q -O {outfile} https://www.legacysurvey.org/viewer/jpeg-cutout?ra={ra}&dec={dec}&width={width}&height={height}&layer={layer}'
cmd = cmd.format(outfile=cutoutjpeg, ra=metadata['RA'], dec=metadata['DEC'],
cmd = cmd.format(outfile=cutoutjpeg, ra=metadata['RA'], dec=metadata['DEC'],
width=width, height=height, layer=layer)
log.info(cmd)
try:
Expand Down Expand Up @@ -646,7 +645,7 @@ def major_formatter(x, pos):
sedmodel_abmag = -2.5*np.log10(sedmodel * factor)
sedax.plot(sedwave / 1e4, sedmodel_abmag, color='slategrey', alpha=0.9, zorder=1)

sedax.scatter(sedphot['lambda_eff']/1e4, sedphot['abmag'], marker='s',
sedax.scatter(sedphot['lambda_eff']/1e4, sedphot['abmag'], marker='s',
s=400, color='k', facecolor='none', linewidth=2, alpha=1.0, zorder=2)

if not fastphot:
Expand Down Expand Up @@ -694,11 +693,11 @@ def major_formatter(x, pos):
raise('Problem here!')
#print(sed_ymin, sed_ymax)

#sedax.set_xlabel(r'Observed-frame Wavelength ($\mu$m)')
#sedax.set_xlabel(r'Observed-frame Wavelength ($\mu$m)')
sedax.set_xlim(phot_wavelims[0], phot_wavelims[1])
sedax.set_xscale('log')
sedax.set_ylabel('AB mag')
#sedax.set_ylabel(r'Apparent Brightness (AB mag)')
sedax.set_ylabel('AB mag')
#sedax.set_ylabel(r'Apparent Brightness (AB mag)')
sedax.set_ylim(sed_ymin, sed_ymax)

sedax.xaxis.set_major_formatter(major_formatter)
Expand All @@ -712,7 +711,7 @@ def major_formatter(x, pos):
sedax_twin = sedax.twiny()
sedax_twin.set_xlim(phot_wavelims[0]/(1+redshift), phot_wavelims[1]/(1+redshift))
sedax_twin.set_xscale('log')
#sedax_twin.set_xlabel(r'Rest-frame Wavelength ($\mu$m)')
#sedax_twin.set_xlabel(r'Rest-frame Wavelength ($\mu$m)')

sedax_twin.xaxis.set_major_formatter(major_formatter)
restticks = np.array([0.02, 0.05, 0.1, 0.2, 0.5, 1.0, 1.5, 3.0, 5.0, 10.0, 15.0, 20.0])
Expand Down Expand Up @@ -865,7 +864,7 @@ def major_formatter(x, pos):
thislinename = thislinename.replace('-', ' ')
if 'CIII]' in thislinename:
thislinename = thislinename+'\n'
specax.text(oneline['restwave']*(1+redshift)/1e4, spec_ymax*0.97, thislinename,
specax.text(oneline['restwave']*(1+redshift)/1e4, spec_ymax*0.97, thislinename,
ha='center', va='top', rotation=270, fontsize=12, alpha=0.5)
else:
if '4363' in linename:
Expand Down Expand Up @@ -932,7 +931,7 @@ def major_formatter(x, pos):
plotline[s:e] = oneline
# stupid hack; where cameras overlap (e.g.,
# Halpha on sv1-bright-22923-39627731570268174),
# the wavelengths are out of order.
# the wavelengths are out of order.
srt = np.argsort(fullwave)
xx.plot(fullwave[srt] / 1e4, plotline[srt], lw=1, alpha=0.8, color=col2[icam])

Expand Down Expand Up @@ -1131,23 +1130,23 @@ def major_formatter(x, pos):
_ = leg_broad.pop('dv_broad')

ibox = 0
#fig.text(startpos, toppos, '\n'.join(txt), ha='left', va='top', fontsize=legfntsz,
fig.text(boxpos[ibox], toppos, '\n'.join(txt), ha='left', va='top', fontsize=legfntsz,
#fig.text(startpos, toppos, '\n'.join(txt), ha='left', va='top', fontsize=legfntsz,
fig.text(boxpos[ibox], toppos, '\n'.join(txt), ha='left', va='top', fontsize=legfntsz,
bbox=bbox, linespacing=1.4)
ibox += 1

txt = [
r'{}'.format(leg['absmag_r']),
r'{}'.format(leg['absmag_gr']),
r'{}'.format(leg['absmag_rz']),
r'{}'.format(leg['absmag_rz']),
'',
r'{}'.format(leg['dn4000_model'])
]
if 'dn4000_spec' in legkeys:
txt += [r'{}'.format(leg['dn4000_spec'])]

#fig.text(startpos+deltapos, toppos, '\n'.join(txt), ha='left', va='top',
fig.text(boxpos[ibox], toppos, '\n'.join(txt), ha='left', va='top',
#fig.text(startpos+deltapos, toppos, '\n'.join(txt), ha='left', va='top',
fig.text(boxpos[ibox], toppos, '\n'.join(txt), ha='left', va='top',
fontsize=legfntsz, bbox=bbox, linespacing=1.4)
ibox += 1

Expand Down Expand Up @@ -1488,4 +1487,3 @@ def _wrap_qa(redrockfile, indx=None, stackfit=False):

# if multiprocessing, clean up workers
mp_pool.close()

Loading

0 comments on commit ea8db89

Please sign in to comment.