Skip to content

Commit

Permalink
Improve pep8 compliance
Browse files Browse the repository at this point in the history
  • Loading branch information
nmearl committed Sep 16, 2019
1 parent 9589695 commit 7eef14f
Showing 1 changed file with 38 additions and 30 deletions.
68 changes: 38 additions & 30 deletions specutils/analysis/template_comparison.py
Original file line number Diff line number Diff line change
@@ -1,37 +1,41 @@
import logging

import numpy as np

from ..manipulation import (FluxConservingResampler,
LinearInterpolatedResampler,
SplineInterpolatedResampler)
from ..spectra.spectrum1d import Spectrum1D
from ..spectra.spectrum_collection import SpectrumCollection

def _normalize_for_template_matching(observed_spectrum, template_spectrum):
"""
Calculate a scale factor to be applied to the template spectrum so the total flux in both spectra will be the same.
Calculate a scale factor to be applied to the template spectrum so the
total flux in both spectra will be the same.
Parameters
----------
observed_spectrum : :class:`~specutils.Spectrum1D`
the observed spectrum
The observed spectrum.
template_spectrum : :class:`~specutils.Spectrum1D`
the template spectrum, which needs to be normalized in order to be compared with the observed_spectrum
The template spectrum, which needs to be normalized in order to be
compared with the observed spectrum.
Returns
-------
`float`
A float which will normalize the template spectrum's flux so that it can be compared to the observed spectrum
A float which will normalize the template spectrum's flux so that it
can be compared to the observed spectrum.
"""
num = np.sum((observed_spectrum.flux*template_spectrum.flux)/(observed_spectrum.uncertainty.array**2))
denom = np.sum((template_spectrum.flux/observed_spectrum.uncertainty.array)**2)
num = np.sum((observed_spectrum.flux*template_spectrum.flux)/
(observed_spectrum.uncertainty.array**2))
denom = np.sum((template_spectrum.flux/
observed_spectrum.uncertainty.array)**2)

return num/denom


def _resample(resample_method):
"""
Find the user preferred method of resampling the template spectrum to fit the observed spectrum.
Find the user preferred method of resampling the template spectrum to fit
the observed spectrum.
Parameters
----------
Expand All @@ -45,38 +49,41 @@ def _resample(resample_method):
"""
if resample_method == "flux_conserving":
return FluxConservingResampler()
elif resample_method == "linear_interpolated":

if resample_method == "linear_interpolated":
return LinearInterpolatedResampler()
elif resample_method == "spline_interpolated":

if resample_method == "spline_interpolated":
return SplineInterpolatedResampler()
else:
return None

return None


def _template_match(observed_spectrum, template_spectrum, resample_method):
"""
Resample the template spectrum to match the wavelength of the observed spectrum.
Then, calculate chi2 on the flux of the two spectra.
Resample the template spectrum to match the wavelength of the observed
spectrum. Then, calculate chi2 on the flux of the two spectra.
Parameters
----------
observed_spectrum : :class:`~specutils.Spectrum1D`
the observed spectrum
The observed spectrum.
template_spectrum : :class:`~specutils.Spectrum1D`
the template spectrum, which will be resampled to match the wavelength of the observed_spectrum
The template spectrum, which will be resampled to match the wavelength
of the observed spectrum.
Returns
-------
normalized_template_spectrum : :class:`~specutils.Spectrum1D`
The normalized spectrum template.
chi2 : `float`
The chi2 of the flux of the observed spectrum and the flux of the
The chi2 of the flux of the observed spectrum and the flux of the
normalized template spectrum.
"""
# Resample template
if _resample(resample_method) != 0:
fluxc_resample = _resample(resample_method)
template_obswavelength = fluxc_resample(template_spectrum,
template_obswavelength = fluxc_resample(template_spectrum,
observed_spectrum.wavelength)

# Normalize spectra
Expand All @@ -94,7 +101,7 @@ def _template_match(observed_spectrum, template_spectrum, resample_method):
result = (num/denom)**2
chi2 = np.sum(result.value)

# Create normalized template spectrum, which will be returned with
# Create normalized template spectrum, which will be returned with
# corresponding chi2
normalized_template_spectrum = Spectrum1D(
spectral_axis=template_spectrum.spectral_axis,
Expand All @@ -103,28 +110,29 @@ def _template_match(observed_spectrum, template_spectrum, resample_method):
return normalized_template_spectrum, chi2


def template_match(observed_spectrum, spectral_templates, resample_method="flux_conserving"):
def template_match(observed_spectrum, spectral_templates,
resample_method="flux_conserving"):
"""
Find which spectral templates is the best fit to an observed spectrum by
computing the chi-squared. If two template_spectra have the same chi2, the
Find which spectral templates is the best fit to an observed spectrum by
computing the chi-squared. If two template_spectra have the same chi2, the
first template is returned.
Parameters
----------
observed_spectrum : :class:`~specutils.Spectrum1D`
The observed spectrum.
spectral_templates : :class:`~specutils.Spectrum1D` or :class:`~specutils.SpectrumCollection` or `list`
That will give a single :class:`~specutils.Spectrum1D` when iterated
over. The template spectra, which will be resampled, normalized, and
compared to the observed spectrum, where the smallest chi2 and
That will give a single :class:`~specutils.Spectrum1D` when iterated
over. The template spectra, which will be resampled, normalized, and
compared to the observed spectrum, where the smallest chi2 and
normalized template spectrum will be returned.
Returns
-------
normalized_template_spectrum : :class:`~specutils.Spectrum1D`
The template spectrum that has been normalized.
chi2 : `float`
The chi2 of the flux of the observed_spectrum and the flux of the
The chi2 of the flux of the observed_spectrum and the flux of the
normalized template spectrum.
smallest_chi_index : `int`
The index of the spectrum with the smallest chi2 in spectral templates.
Expand All @@ -136,8 +144,8 @@ def template_match(observed_spectrum, spectral_templates, resample_method="flux_
return normalized_spectral_template, chi2

# At this point, the template spectrum is either a ``SpectrumCollection``
# or a multi-dimensional``Spectrum1D``. Loop through the object and return
# the template spectrum with the lowest chi square and its corresponding
# or a multi-dimensional``Spectrum1D``. Loop through the object and return
# the template spectrum with the lowest chi square and its corresponding
# chi square.
chi2_min = None
smallest_chi_spec = None
Expand Down

0 comments on commit 7eef14f

Please sign in to comment.