Skip to content

Commit

Permalink
move access of line variances down to where it is actually used, and
Browse files Browse the repository at this point in the history
put these uses together with setting the boxflux variance
  • Loading branch information
Jeremy Buhler committed Nov 16, 2024
1 parent a464667 commit 4fa3daf
Showing 1 changed file with 22 additions and 20 deletions.
42 changes: 22 additions & 20 deletions py/fastspecfit/emlines.py
Original file line number Diff line number Diff line change
Expand Up @@ -915,15 +915,9 @@ def _gaussian_lineflux(flux_perpixel, s, e, patchindx, gausscorr=1.):
values[line_sigma] = 0.
continue

# Monte Carlo uncertainties
if results_monte is not None:
modelamp_var = values_var[line_amp]
vshift_var = values_var[line_vshift]
sigma_var = values_var[line_sigma]
obsamp_var = obsamps_var[line_amp]

# number of pixels, chi2, and boxcar integration
patchindx = line_s + np.where(emlineivar_s[line_s:line_e] > 0.)[0]

npix = len(patchindx)
result[f'{linename}_NPIX'] = npix

Expand All @@ -943,17 +937,6 @@ def _gaussian_lineflux(flux_perpixel, s, e, patchindx, gausscorr=1.):
result[f'{linename}_BOXFLUX'] = boxflux # * u.erg/(u.second*u.cm**2)

if results_monte is not None:
if obsamp_var > TINY:
obsamp_ivar = 1. / obsamp_var
else:
obsamp_ivar = 0.
result[f'{linename}_AMP_IVAR'] = obsamp_ivar # * u.second**2*u.cm**4*u.Angstrom**2/u.erg**2
if vshift_var > TINY:
result[f'{linename}_VSHIFT_IVAR'] = 1. / vshift_var
if sigma_var > TINY:
result[f'{linename}_SIGMA_IVAR'] = 1. / sigma_var
#if modelamp_var > TINY:
# result[f'{linename}_MODELAMP_IVAR'] = 1. / modelamp_var

# Monte Carlo to get the uncertainties
# FIXME: merge with nearly identical code for boxflux above
Expand Down Expand Up @@ -987,6 +970,25 @@ def get_boxflux(values, emlineflux_s):
if boxflux_var > TINY:
result[f'{linename}_BOXFLUX_IVAR'] = 1. / boxflux_var

vshift_var = values_var[line_vshift]
if vshift_var > TINY:
result[f'{linename}_VSHIFT_IVAR'] = 1. / vshift_var

sigma_var = values_var[line_sigma]
if sigma_var > TINY:
result[f'{linename}_SIGMA_IVAR'] = 1. / sigma_var

# modelamp_var = values_var[line_amp]
#if modelamp_var > TINY:
# result[f'{linename}_MODELAMP_IVAR'] = 1. / modelamp_var

obsamp_var = obsamps_var[line_amp]
if obsamp_var > TINY:
obsamp_ivar = 1. / obsamp_var
result[f'{linename}_AMP_IVAR'] = obsamp_ivar # * u.second**2*u.cm**4*u.Angstrom**2/u.erg**2
else:
obsamp_ivar = 0. # needed below

else:
# Legacy algorithm: get the uncertainty in the
# line-amplitude based on the scatter in the pixel values
Expand All @@ -995,9 +997,9 @@ def get_boxflux(values, emlineflux_s):
obsamp_sigma = (n_hi - n_lo) / 1.349 # robust sigma
if obsamp_sigma > SQTINY:
obsamp_ivar = 1. / obsamp_sigma**2
result[f'{linename}_AMP_IVAR'] = obsamp_ivar # * u.second**2*u.cm**4*u.Angstrom**2/u.erg**2
else:
obsamp_ivar = 0.
result[f'{linename}_AMP_IVAR'] = obsamp_ivar # * u.second**2*u.cm**4*u.Angstrom**2/u.erg**2
obsamp_ivar = 0. # needed below

# formal (statistical) uncertainty
boxflux_ivar = 1. / np.sum(dwaves_patch**2 / emlineivar_patch)
Expand Down

0 comments on commit 4fa3daf

Please sign in to comment.