Skip to content

Commit

Permalink
use double precision only for wavelength
Browse files Browse the repository at this point in the history
Wavelength needs to be in double precision in order to preserve
the precision in the measurements, but other quantities (esp.
flux) don't need double precision, and datamodel I/O has been
reverted to single-precision for these to save disk space.
This commit adapts to these changes.
  • Loading branch information
PaulPrice committed May 3, 2022
1 parent f5ead1a commit 228ecc3
Show file tree
Hide file tree
Showing 15 changed files with 42 additions and 39 deletions.
2 changes: 1 addition & 1 deletion include/pfs/drp/stella/FiberTrace.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ class FiberTrace {
ndarray::Array<double, 2, 1> const& profiles,
ndarray::Array<bool, 2, 1> const& good,
ndarray::Array<double, 1, 1> const& centers,
ndarray::Array<double, 1, 1> const& norm=ndarray::Array<double, 1, 1>()
ndarray::Array<Spectrum::ImageT, 1, 1> const& norm=ndarray::Array<Spectrum::ImageT, 1, 1>()
);

private:
Expand Down
4 changes: 2 additions & 2 deletions include/pfs/drp/stella/Spectrum.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ namespace pfs { namespace drp { namespace stella {
*/
class Spectrum {
public:
using ImageT = double;
using VarianceT = double;
using ImageT = float;
using VarianceT = float;
using Mask = lsst::afw::image::Mask<lsst::afw::image::MaskPixel>;
using ImageArray = ndarray::Array<ImageT, 1, 1>;
using ConstImageArray = ndarray::Array<const ImageT, 1, 1>;
Expand Down
4 changes: 2 additions & 2 deletions include/pfs/drp/stella/SpectrumSet.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ class SpectrumSet {
using SpectrumPtr = std::shared_ptr<Spectrum>;
using ConstSpectrumPtr = std::shared_ptr<Spectrum const>;
using Collection = std::vector<SpectrumPtr>;
using ImageArray = ndarray::Array<double, 2, 1>;
using CovarianceArray = ndarray::Array<double, 3, 1>;
using ImageArray = ndarray::Array<Spectrum::ImageT, 2, 1>;
using CovarianceArray = ndarray::Array<Spectrum::ImageT, 3, 1>;
using MaskArray = ndarray::Array<lsst::afw::image::MaskPixel, 2, 1>;
using WavelengthArray = ndarray::Array<double, 2, 1>;
using iterator = Collection::iterator;
Expand Down
8 changes: 5 additions & 3 deletions python/pfs/drp/stella/fiberProfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class FiberProfile:
profiles : array_like of `float`, shape ``(N, M)``
Profiles for each swath, each of width
``M = 2*(radius + 1)*oversample + 1``.
norm : array_like of `float`, optional
norm : array_like of `np.float32`, optional
Normalisation for each spectral pixel.
"""
def __init__(self, radius, oversample, rows, profiles, norm=None):
Expand Down Expand Up @@ -220,7 +220,8 @@ def makeFiberTraceFromDetectorMap(self, detectorMap, fiberId):
"""
return FiberTrace.fromProfile(fiberId, detectorMap.bbox.getDimensions(), self.radius,
self.oversample, self.rows, self.profiles, ~self.profiles.mask,
detectorMap.getXCenter(fiberId), self.norm)
detectorMap.getXCenter(fiberId),
self.norm.astype(np.float32) if self.norm is not None else None)

def makeFiberTrace(self, dimensions, centerFunc, fiberId):
"""Make a FiberTrace object
Expand All @@ -244,7 +245,8 @@ def makeFiberTrace(self, dimensions, centerFunc, fiberId):
"""
rows = np.arange(dimensions.getY(), dtype=float)
return FiberTrace.fromProfile(fiberId, dimensions, self.radius, self.oversample, self.rows,
self.profiles, ~self.profiles.mask, centerFunc(rows), self.norm)
self.profiles, ~self.profiles.mask, centerFunc(rows),
self.norm.astype(np.float32) if self.norm is not None else None)

def extractSpectrum(self, maskedImage, detectorMap, fiberId):
"""Extract a single spectrum from an image
Expand Down
2 changes: 1 addition & 1 deletion python/pfs/drp/stella/findLines.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def convolve(self, spectrum, continuum=None):
size = 2*halfSize + 1
xx = np.arange(size, dtype=float) - halfSize
sigma = self.config.width
kernel = np.exp(-0.5*xx**2/sigma**2)/sigma/np.sqrt(2.0*np.pi)
kernel = (np.exp(-0.5*xx**2/sigma**2)/sigma/np.sqrt(2.0*np.pi)).astype(spectrum.flux.dtype)

flux = np.convolve(spectrum.normFlux if continuum is None else spectrum.normFlux - continuum,
kernel, mode="same")
Expand Down
6 changes: 3 additions & 3 deletions python/pfs/drp/stella/fitContinuum.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,13 +141,13 @@ def _fitContinuumImpl(self, values, good):
fit : `numpy.ndarray`, floating-point
Fit array.
"""
indices = np.arange(len(values), dtype=float)
indices = np.arange(len(values), dtype=values.dtype)
knots, binned = binData(indices, values, good, self.config.numKnots)
use = np.isfinite(knots) & np.isfinite(binned)
if not np.any(use):
raise FitContinuumError("No finite knots when fitting continuum")
interp = makeInterpolate(knots[use], binned[use], self.fitType)
fit = np.array(interp.interpolate(indices))
fit = np.array(interp.interpolate(indices)).astype(values.dtype)

if lsstDebug.Info(__name__).plot:
import matplotlib.pyplot as plt
Expand Down Expand Up @@ -285,4 +285,4 @@ def binData(xx, yy, good, numBins):
select = good[low:high]
xBinned[ii] = np.median(xx[low:high][select]) if np.any(select) else np.nan
yBinned[ii] = np.median(yy[low:high][select]) if np.any(select) else np.nan
return xBinned, yBinned
return xBinned.astype(xx.dtype), yBinned.astype(yy.dtype)
5 changes: 3 additions & 2 deletions python/pfs/drp/stella/fitLine.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@ PYBIND11_PLUGIN(fitLine) {
lsst::utils::python::addOutputOp(cls, "__repr__");

mod.def("fitLine",
py::overload_cast<ndarray::Array<double const, 1, 1> const&,
py::overload_cast<ndarray::Array<Spectrum::ImageT const, 1, 1> const&,
ndarray::Array<lsst::afw::image::MaskPixel const, 1, 1> const&,
float, float, lsst::afw::image::MaskPixel, std::size_t>(&fitLine<double>),
float, float, lsst::afw::image::MaskPixel,
std::size_t>(&fitLine<Spectrum::ImageT>),
"flux"_a, "mask"_a, "peakPosition"_a, "rmsSize"_a, "badBitMask"_a,
"fittingHalfSize"_a=0);

Expand Down
2 changes: 1 addition & 1 deletion python/pfs/drp/stella/mergeArms.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ def normalizeSpectra(self, spectra):
assert all(np.all(ss.fiberId == fiberId) for ss in spectra) # Consistent fibers

# Collect normalisations from each arm, interpolated to common wavelength sampling
norm = np.zeros((fiberId.size, wavelength.size))
norm = np.zeros((fiberId.size, wavelength.size), dtype=np.float32)
for ss in spectra:
badBitmask = ss.flags.get(*self.config.mask)
for ii in range(len(ss)):
Expand Down
2 changes: 1 addition & 1 deletion src/FiberTrace.cc
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ FiberTrace<ImageT, MaskT, VarianceT> FiberTrace<ImageT, MaskT, VarianceT>::fromP
ndarray::Array<double, 2, 1> const& profiles,
ndarray::Array<bool, 2, 1> const& good,
ndarray::Array<double, 1, 1> const& centers,
ndarray::Array<double, 1, 1> const& norm
ndarray::Array<Spectrum::ImageT, 1, 1> const& norm
) {
int const width = dims.getX();
std::size_t const height = dims.getY();
Expand Down
14 changes: 7 additions & 7 deletions tests/test_PfsArm.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ def setUp(self):
self.identity = Identity(self.visit, self.arm, self.spectrograph, self.pfsDesignId)
self.fiberId = self.synthConfig.fiberId
self.wavelength = np.vstack([np.linspace(600, 900, self.synthConfig.height)]*num)
self.flux = self.rng.uniform(size=shape)
self.mask = self.rng.uniform(high=2**31, size=shape).astype(np.int32)
self.sky = self.rng.uniform(size=shape)
self.norm = self.rng.uniform(size=shape)
self.covar = self.rng.uniform(size=(num, 3, self.synthConfig.height))
self.flux = self.rng.uniform(size=shape).astype(np.float32)
self.mask = self.rng.uniform(high=2**31, size=shape).astype(np.uint32)
self.sky = self.rng.uniform(size=shape).astype(np.float32)
self.norm = self.rng.uniform(size=shape).astype(np.float32)
self.covar = self.rng.uniform(size=(num, 3, self.synthConfig.height)).astype(np.float32)
self.flags = MaskHelper()
self.metadata = dict(FOO=12345, BAR=0.9876) # FITS keywords get capitalised

Expand All @@ -48,9 +48,9 @@ def makeSpectra(self):
def assertSpectra(self, spectra):
"""Check that the spectra match what's expected"""
for name in ("visit", "arm", "spectrograph", "pfsDesignId"):
self.assertEqual(getattr(spectra.identity, name), getattr(self, name))
self.assertEqual(getattr(spectra.identity, name), getattr(self, name), msg=name)
for name in ("fiberId", "wavelength", "flux", "mask", "sky", "norm", "covar"):
self.assertFloatsEqual(getattr(spectra, name), getattr(self, name))
self.assertFloatsEqual(getattr(spectra, name), getattr(self, name), msg=name)
self.assertDictEqual(spectra.flags.flags, self.flags.flags)
self.assertDictEqual({**self.metadata, **spectra.metadata}, spectra.metadata)

Expand Down
10 changes: 5 additions & 5 deletions tests/test_Spectrum.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ def setUp(self):
# Spectrum
self.length = 123
self.fiberId = 456
self.image = self.rng.uniform(size=self.length).astype(float)
self.image = self.rng.uniform(size=self.length).astype(np.float32)
self.mask = lsst.afw.image.Mask(self.length, 1)
self.mask.array[:] = self.rng.randint(0, 2**30, self.length).astype(np.int32)
self.background = self.rng.uniform(size=self.length).astype(float)
self.norm = self.rng.uniform(size=self.length).astype(float)
self.covariance = self.rng.uniform(size=(3, self.length)).astype(float)
self.background = self.rng.uniform(size=self.length).astype(np.float32)
self.norm = self.rng.uniform(size=self.length).astype(np.float32)
self.covariance = self.rng.uniform(size=(3, self.length)).astype(np.float32)
self.wavelengthArray = np.arange(1, self.length + 1, dtype=float)

def makeSpectrum(self):
Expand Down Expand Up @@ -145,7 +145,7 @@ def testBasics(self):
# Set the variance and ensure it changes the covariance
spectrum.covariance[:] = self.covariance
self.assertFloatsEqual(spectrum.covariance, self.covariance)
variance = self.rng.uniform(size=self.length)
variance = self.rng.uniform(size=self.length).astype(np.float32)
spectrum.variance = variance
self.covariance[0] = variance
self.assertFloatsEqual(spectrum.covariance, self.covariance)
Expand Down
8 changes: 4 additions & 4 deletions tests/test_SpectrumSet.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ def setUp(self):
# Spectrum
self.length = 123
self.fiberId = 456
self.image = self.rng.uniform(size=self.length).astype(float)
self.image = self.rng.uniform(size=self.length).astype(np.float32)
self.mask = lsst.afw.image.Mask(self.length, 1)
self.mask.array[:] = self.rng.randint(0, 2**30, self.length).astype(np.int32)
self.background = self.rng.uniform(size=self.length).astype(float)
self.norm = self.rng.uniform(size=self.length).astype(float)
self.covariance = self.rng.uniform(size=(3, self.length)).astype(float)
self.background = self.rng.uniform(size=self.length).astype(np.float32)
self.norm = self.rng.uniform(size=self.length).astype(np.float32)
self.covariance = self.rng.uniform(size=(3, self.length)).astype(np.float32)
self.wavelengthArray = np.arange(1, self.length + 1, dtype=float)

def makeSpectrum(self):
Expand Down
10 changes: 5 additions & 5 deletions tests/test_extractSpectra.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def setUp(self):
lsst.afw.image.makeExposure(self.image), detectorMap=self.detMap
).profiles
for fiberId in self.fiberProfiles:
self.fiberProfiles[fiberId].norm = np.full(self.synthConfig.height, self.flux, dtype=float)
self.fiberProfiles[fiberId].norm = np.full(self.synthConfig.height, self.flux, dtype=np.float32)
self.fiberTraces = self.fiberProfiles.makeFiberTracesFromDetectorMap(self.detMap)
self.assertEqual(len(self.fiberTraces), self.synthConfig.numFibers)

Expand Down Expand Up @@ -78,7 +78,7 @@ def assertSpectra(self, spectra, flux=None, mask=None):
expectMask = 0

bbox = ft.trace.getBBox()
expectNorm = np.zeros(self.synthConfig.height, dtype=float)
expectNorm = np.zeros(self.synthConfig.height, dtype=np.float32)
expectNorm[bbox.getMinY():bbox.getMaxY() + 1] = ft.trace.image.array.sum(axis=1)

self.assertEqual(ss.fiberId, ft.fiberId)
Expand Down Expand Up @@ -126,7 +126,7 @@ def testUndersizedFiberTrace(self):
newTrace = type(trace)(trace.trace.Factory(trace.trace, newBox), trace.fiberId)
self.fiberTraces[index] = newTrace
spectra = self.fiberTraces.extractSpectra(self.image)
expectFlux = np.zeros(self.synthConfig.height, dtype=float)
expectFlux = np.zeros(self.synthConfig.height, dtype=np.float32)
expectFlux[middle:] = self.flux
expectMask = np.zeros(self.synthConfig.height, dtype=np.int32)
expectMask[:middle] = trace.trace.mask.getPlaneBitMask("NO_DATA")
Expand All @@ -137,7 +137,7 @@ def testUndersizedFiberTrace(self):
newTrace = type(trace)(trace.trace.Factory(trace.trace, newBox), trace.fiberId)
self.fiberTraces[index] = newTrace
spectra = self.fiberTraces.extractSpectra(self.image)
expectFlux = np.zeros(self.synthConfig.height, dtype=float)
expectFlux = np.zeros(self.synthConfig.height, dtype=np.float32)
expectFlux[:middle] = self.flux
expectMask = np.zeros(self.synthConfig.height, dtype=np.int32)
expectMask[middle:] = trace.trace.mask.getPlaneBitMask("NO_DATA")
Expand All @@ -154,7 +154,7 @@ def testSingular(self):
index = self.synthConfig.numFibers//2
row = self.synthConfig.height//2
fiberId = self.synthConfig.fiberId[index]
expectFlux = np.full(self.synthConfig.height, self.flux, dtype=float)
expectFlux = np.full(self.synthConfig.height, self.flux, dtype=np.float32)
expectMask = np.zeros(self.synthConfig.height, dtype=np.int32)
expectFlux[row] = 0.0
expectMask[row] = self.image.mask.getPlaneBitMask(["BAD_FIBERTRACE", "NO_DATA"])
Expand Down
2 changes: 1 addition & 1 deletion tests/test_fitLine.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def makeSpectrum(length, center, amplitude, rmsSize, bgConst=0.0, bgSlope=0.0):
return spectrum


class FindLinesTestCase(lsst.utils.tests.TestCase):
class FitLinesTestCase(lsst.utils.tests.TestCase):
def setUp(self):
self.length = 256
self.center = 123.45
Expand Down
2 changes: 1 addition & 1 deletion tests/test_profileNorm.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def testProfileNorm(self):
newSpectra = traces.extractSpectra(image)
for new, old in zip(newSpectra, spectra):
self.assertFloatsAlmostEqual(new.norm, old.normFlux, rtol=1.0e-7)
self.assertFloatsAlmostEqual(new.normFlux, 1.0, atol=1.0e-7)
self.assertFloatsAlmostEqual(new.normFlux, 1.0, atol=1.0e-6)


class TestMemory(lsst.utils.tests.MemoryTestCase):
Expand Down

0 comments on commit 228ecc3

Please sign in to comment.