Skip to content

Commit

Permalink
* fix markup error in changes.rst
Browse files Browse the repository at this point in the history
* attempt to clean up some formatting warnings
* add new-style docstrings to emline_fit/utils.py
  • Loading branch information
jdbuhler committed Sep 3, 2024
1 parent 2df2666 commit d5d8f64
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 42 deletions.
2 changes: 1 addition & 1 deletion doc/changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Change Log
.. _`PR #177`: https://github.com/desihub/fastspecfit/pull/177
.. _`PR #179`: https://github.com/desihub/fastspecfit/pull/179
.. _`PR #180`: https://github.com/desihub/fastspecfit/pull/180
`` _`PR #181`: https://github.com/desihub/fastspecfit/pull/181
.. _`PR #181`: https://github.com/desihub/fastspecfit/pull/181

2.5.2 (2024-04-28)
------------------
Expand Down
13 changes: 6 additions & 7 deletions py/fastspecfit/emline_fit/jacobian.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,21 +49,21 @@ def emline_model_jacobian(line_parameters,
SQRT_2PI = np.sqrt(2*np.pi)

nbins = len(log_obs_bin_edges) - 1

line_amplitudes, line_vshifts, line_sigmas = \
np.split(line_parameters, 3)

# buffers for per-parameter calculations, sized large
# enough for max possible range [s .. e), plus extra
# padding as directed by caller.
max_width = max_buffer_width(log_obs_bin_edges, line_sigmas, padding)

nlines = len(line_wavelengths)
dd = np.empty((3 * nlines, max_width), dtype=line_amplitudes.dtype)
endpts = np.zeros((3 * nlines, 2), dtype=np.int32)

starts = endpts[:,0]
ends = endpts[:,1]
starts = endpts[:, 0]
ends = endpts[:, 1]

# compute partial derivatives for avg values of all Gaussians
# inside each bin. For each Gaussian, we only compute
Expand Down Expand Up @@ -94,7 +94,7 @@ def emline_model_jacobian(line_parameters,
if hi == 0 or lo == len(log_obs_bin_edges):
continue

nedges = hi - lo + 2 # compute values at edges [lo - 1 ... hi]
nedges = hi - lo + 2 # compute values at edges [lo - 1 ... hi]

# Compute contribs of each line to each partial derivative in place.
# No sharing of params between peaks means that we never have to
Expand Down Expand Up @@ -180,7 +180,6 @@ def emline_model_jacobian(line_parameters,
return (endpts, dd)



@staticmethod
@jit(nopython=True, nogil=True)
def patch_jacobian(obs_bin_centers,
Expand Down
3 changes: 2 additions & 1 deletion py/fastspecfit/emline_fit/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
norm_cdf
)


@jit(nopython=True, nogil=True)
def emline_model(line_wavelengths,
line_parameters,
Expand Down Expand Up @@ -239,7 +240,7 @@ def emline_model_core(line_wavelength,

# vals[i] --> edge i + lo - 1

vals[0] = 0. # edge lo - 1
vals[0] = 0. # edge lo - 1

for i in range(1, nedges-1):

Expand Down
14 changes: 8 additions & 6 deletions py/fastspecfit/emline_fit/params_mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from numba import jit


class ParamsMapping(object):
"""Compute a mapping from the free (line) parameters of a spectrum
fitting problem to the full set of parameters for the EMLine
Expand Down Expand Up @@ -99,11 +100,11 @@ def _mapFreeToFull(freeParms, nParms, sources, factors,
for j, src_j_free in doubletPatches:
factors[j] = freeParms[src_j_free] if patchDoublets else 1.

if fullParms == None:
if fullParms is None:
fullParms = np.empty(nParms, dtype=freeParms.dtype)

for j, src_j_free in enumerate(sources):
fullParms[j] = factors[j] # copy fixed zeros
fullParms[j] = factors[j] # copy fixed zeros
if src_j_free != -1:
fullParms[j] *= freeParms[src_j_free]

Expand Down Expand Up @@ -152,7 +153,7 @@ def _matvec(J_S, v, w):

shape, elts, factors = J_S

for j in range(shape[0]): # total params
for j in range(shape[0]): # total params
w[j] = 0.

for i, (dst, src) in enumerate(elts):
Expand Down Expand Up @@ -180,9 +181,10 @@ def _add_rmatvec(J_S, v, w):
for i, (dst, src) in enumerate(elts):
w[src] += factors[i] * v[dst]


###########################################################



def _precomputeMapping(self, isFree, pFree,
tiedSources, tiedFactors,
doubletTargets, doubletSources):
Expand Down Expand Up @@ -293,7 +295,7 @@ def _precomputeJacobian(self):

# jacElts already has coefficient (j, p[j]).
# its factor should be patched from v[ p[src_j] ]
jacDoubletPatches[2*i, :] = (liveOffsets[j], p_src_j)
jacDoubletPatches[2*i, :] = (liveOffsets[j], p_src_j)

# add a second coefficient (j, p[src_j]).
# its factor should be patched from v[ p[j] ]
Expand Down
8 changes: 4 additions & 4 deletions py/fastspecfit/emline_fit/sparse_rep.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ class EMLineJacobian(LinearOperator):
"""

def __init__(self, shape, nLineFreeParms, camerapix, jacs, J_S,
J_P = None):
def __init__(self, shape, nLineFreeParms, camerapix, jacs,
J_S, J_P=None):
"""
Parameters
----------
Expand Down Expand Up @@ -63,7 +63,7 @@ def __init__(self, shape, nLineFreeParms, camerapix, jacs, J_S,
nPatchParms = J_P[0].shape[0]

dtype = jacs[0][1].dtype
nParms = jacs[0][1].shape[0] # num line params in full set
nParms = jacs[0][1].shape[0] # num line params in full set
self.vFull = np.empty(nParms, dtype=dtype)

super().__init__(dtype, shape)
Expand Down Expand Up @@ -124,7 +124,7 @@ def _matmat(self, M):

nBins = self.shape[0]
nVecs = M.shape[1]
R = np.empty((nVecs, nBins), dtype=M.dtype) # transpose of result
R = np.empty((nVecs, nBins), dtype=M.dtype) # transpose of result

for i in range(nVecs):
w = R[i,:]
Expand Down
56 changes: 33 additions & 23 deletions py/fastspecfit/emline_fit/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,37 @@

from fastspecfit.util import C_LIGHT


# Do not bother computing normal PDF/CDF if more than this many
# standard deviations from mean.
MAX_SDEV = 5.

#
# norm_pdf()
# PDF of standard normal distribution at a point a
#
@jit(nopython=True, fastmath=False, nogil=True)
def norm_pdf(a):

@jit(nopython=True, nogil=True)
def norm_pdf(a):
"""
PDF of standard normal distribution at a point a
"""

SQRT_2PI = np.sqrt(2 * np.pi)

return 1/SQRT_2PI * np.exp(-0.5 * a**2)

#
# norm_cdf()
# Approximate the integral of a standard normal PDF from -infty to a.
#
# Optimization (currently disabled because it is not needed): If
# |a| > MAX_SDEV, treat the value as extreme and return 0 or 1 as
# appropriate.
#
@jit(nopython=True, fastmath=False, nogil=True)

@jit(nopython=True, nogil=True)
def norm_cdf(a):
"""
Approximate the integral of a standard normal PDF from -infty to a.
"""

SQRT1_2 = 1.0 / np.sqrt(2)

z = np.abs(a)

# Optimization (currently disabled because it is not needed): If
# |a| > MAX_SDEV, treat the value as extreme and return 0 or 1 as
# appropriate.

#if z > MAX_SDEV: # short-circuit extreme values
# if a > 0:
# y = 1.
Expand All @@ -50,16 +51,25 @@ def norm_cdf(a):
return y


#
# max_buffer_width()
# Compute a safe estimate of the number of nonzero bin fluxes possible
# for a line spanning a subrange of bins with edges log_obs_bin_edges,
# assuming the line's width is one of the values in line_sigmas.
# Optionally add 2*padding to allow future expansion to left and right.
#
@jit(nopython=True, fastmath=False, nogil=True)
@jit(nopython=True, nogil=True)
def max_buffer_width(log_obs_bin_edges, line_sigmas, padding=0):
"""
Compute a safe estimate of the number of nonzero bin fluxes possible
for a line spanning a subrange of bins with edges log_obs_bin_edges,
assuming the line's width is one of the values in line_sigmas.
Optionally add 2*padding to allow future expansion to left and right.
Parameters
----------
log_obs_bin_edges : :class:`np.ndarray` [# obs wavelength bins + 1]
log of wavelengths of all observed bin edges.
line_sigmas : :class:`np.ndarray` [# nlines]
Gaussian widths of all spectral lines.
padding : :class:`int`
Padding parameter to add to width for future use.
"""

# Find the largest width sigma for any line, and
# allocate enough space for twice that much width
# in bins, given the smallest observed bin width.
Expand Down

0 comments on commit d5d8f64

Please sign in to comment.