def continuum_correct(spectrum, nodes=None, method='linear'):
"""
Apply a continuum correction to a given spectrum
Parameters
==========
spectrum : pd.Series
A pandas series or Spectrum object
nodes: list
A list of the nodes between which piecewise continuum
will be fit
method : {'linear', 'regresison', 'cubic'}
The type of regression to be fit, where 'linear' is a piecewise
linear fit, 'regression' is an Ordinary Least Squares fit, and
'cubic' is a 2nd order polynomial fit.
Returns
=======
: pd.Series
The continuum corrected Spectrum
: pd.Series
The continuum line
"""
x = spectrum.index
y = spectrum
if not nodes:
nodes = [x[0], x[-1]]
return_length = len(y)
corrected = np.empty(return_length)
continuum = np.empty(return_length)
start = 0
nlist = list(zip(nodes, nodes[1:]))
for i, n in enumerate(nlist):
# Define indices into sub-series
ny = y[n[0]:n[1]]
nx = ny.index
if i == 0:
stop = start + len(y[:n[1]])
c = correction_methods[method](nx, ny, ex=y[:n[1]].index.values)
ey = y[:n[1]]
elif i == len(nlist) - 1:
stop = start + len(y[n[0]:])
c = correction_methods[method](nx, ny, ex=y[n[0]:].index.values)
ey = y[n[0]:]
else:
stop = start + len(ny)
c = correction_methods[method](nx, ny)
ey = ny
continuum[start:stop] = c
corrected[start:stop] = ey / c
start = stop
return pd.Series(corrected, index=x), pd.Series(continuum, index=x)
评论列表
文章目录