Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DM-14740: Stop using ndarray::EigenView indirectly in C++ code #119

Merged
merged 3 commits into from
Jun 18, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 15 additions & 14 deletions src/PsfFlux.cc
Original file line number Diff line number Diff line change
Expand Up @@ -93,22 +93,23 @@ void PsfFluxAlgorithm::measure(afw::table::SourceRecord& measRecord,
throw LSST_EXCEPT(MeasurementError, NO_GOOD_PIXELS.doc, NO_GOOD_PIXELS.number);
}
typedef afw::detection::Psf::Pixel PsfPixel;
auto model = fitRegion.getSpans()
->flatten(psfImage->getArray(), psfImage->getXY0())
.asEigen<Eigen::ArrayXpr>();
auto data = fitRegion.getSpans()
->flatten(exposure.getMaskedImage().getImage()->getArray(), exposure.getXY0())
.asEigen<Eigen::ArrayXpr>();
auto variance = fitRegion.getSpans()
->flatten(exposure.getMaskedImage().getVariance()->getArray(), exposure.getXY0())
.asEigen<Eigen::ArrayXpr>();
PsfPixel alpha = model.matrix().squaredNorm();
// SpanSet::flatten returns a new ndarray::Array, which must stay in scope
// while we use an Eigen::Map view of it
auto modelNdArray = fitRegion.getSpans()->flatten(psfImage->getArray(), psfImage->getXY0());
auto dataNdArray = fitRegion.getSpans()->flatten(exposure.getMaskedImage().getImage()->getArray(),
exposure.getXY0());
auto varianceNdArray = fitRegion.getSpans()->flatten(exposure.getMaskedImage().getVariance()->getArray(),
exposure.getXY0());
auto model = ndarray::asEigenMatrix(modelNdArray);
auto data = ndarray::asEigenMatrix(dataNdArray);
auto variance = ndarray::asEigenMatrix(varianceNdArray);
PsfPixel alpha = model.squaredNorm();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are these asEigenMatrix rather than asEigenArray? The original code used asEigen<Eigen::ArrayXpr>.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because in almost all cases in the old code the array EigenView is used as a matrix by appending .matrix(). This eliminates all those calls to .matrix() at the expense of adding one call to .array(). See the changes below.

FluxResult result;
result.flux = model.matrix().dot(data.matrix().cast<PsfPixel>()) / alpha;
result.flux = model.dot(data.cast<PsfPixel>()) / alpha;
// If we're not using per-pixel weights to compute the flux, we'll still want to compute the
// variance as if we had, so we'll apply the weights to the model vector now, and update alpha.
result.fluxSigma = std::sqrt(model.square().matrix().dot(variance.matrix().cast<PsfPixel>())) / alpha;
measRecord.set(_areaKey, model.matrix().sum() / alpha);
// variance as if we had, so we'll apply the weights to the model now, and update alpha.
result.fluxSigma = std::sqrt(model.array().square().matrix().dot(variance.cast<PsfPixel>())) / alpha;
measRecord.set(_areaKey, model.sum() / alpha);
if (!std::isfinite(result.flux) || !std::isfinite(result.fluxSigma)) {
throw LSST_EXCEPT(PixelValueError, "Invalid pixel value detected in image.");
}
Expand Down