Skip to content

Commit

Permalink
Simplify vector version of statisticsStack
Browse files Browse the repository at this point in the history
Make it accept a vector of vectors instead of a vector of
shared_ptr to vectors,
and return a vector instead of a shared pointer to a vector.
  • Loading branch information
r-owen committed Jul 18, 2018
1 parent de4d469 commit 57132a5
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 29 deletions.
13 changes: 6 additions & 7 deletions examples/simpleStacker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ namespace math = lsst::afw::math;
typedef image::Image<float> ImageF;
typedef image::MaskedImage<float> MImageF;
typedef std::vector<float> VecF;
typedef std::shared_ptr<VecF> VecFPtr;

int main(int argc, char **argv) {
int const nImg = 10;
Expand Down Expand Up @@ -85,13 +84,13 @@ int main(int argc, char **argv) {
std::cout << "MaskedImage (const weight): " << (*wmimgStack->getImage())(nX / 2, nY / 2) << std::endl;

// std::vector, and also with a constant weight vector
std::vector<VecFPtr> vecList;
std::vector<VecF> vecList;
for (int iImg = 0; iImg < nImg; ++iImg) {
VecFPtr v = VecFPtr(new VecF(nX * nY, iImg));
VecF v(nX * nY, iImg);
vecList.push_back(v);
}
VecFPtr vecStack = math::statisticsStack<float>(vecList, math::MEAN);
std::cout << "Vector: " << (*vecStack)[nX * nY / 2] << std::endl;
VecFPtr wvecStack = math::statisticsStack<float>(vecList, math::MEAN, sctrl, wvec);
std::cout << "Vector (const weight): " << (*wvecStack)[nX * nY / 2] << std::endl;
VecF vecStack = math::statisticsStack<float>(vecList, math::MEAN);
std::cout << "Vector: " << (vecStack)[nX * nY / 2] << std::endl;
VecF wvecStack = math::statisticsStack<float>(vecList, math::MEAN, sctrl, wvec);
std::cout << "Vector (const weight): " << (wvecStack)[nX * nY / 2] << std::endl;
}
4 changes: 2 additions & 2 deletions include/lsst/afw/math/Stack.h
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,8 @@ void statisticsStack(lsst::afw::image::MaskedImage<PixelT>& out, ///< Output im
* A function to compute some statistics of a stack of std::vectors
*/
template <typename PixelT>
std::shared_ptr<std::vector<PixelT>> statisticsStack(
std::vector<std::shared_ptr<std::vector<PixelT>>>& vectors, ///< Vectors to process
std::vector<PixelT> statisticsStack(
std::vector<std::vector<PixelT>>& vectors, ///< Vectors to process
Property flags, ///< statistics requested
StatisticsControl const& sctrl = StatisticsControl(), ///< control structure
std::vector<lsst::afw::image::VariancePixel> const& wvector =
Expand Down
11 changes: 9 additions & 2 deletions python/lsst/afw/math/stack.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
* see <https://www.lsstcorp.org/LegalNotices/>.
*/

#include <memory>
#include <vector>

#include <pybind11/pybind11.h>
//#include <pybind11/operators.h>
#include <pybind11/stl.h>
Expand All @@ -32,6 +35,8 @@ using namespace py::literals;

using namespace lsst::afw::math;

namespace {

template <typename PixelT>
void declareStatisticsStack(py::module &mod) {
mod.def("statisticsStack", (std::shared_ptr<lsst::afw::image::MaskedImage<PixelT>>(*)(
Expand Down Expand Up @@ -93,13 +98,15 @@ void declareStatisticsStack(py::module &mod) {
))statisticsStack<PixelT>,
"images"_a, "flags"_a, "sctrl"_a, "wvector"_a, "clipped"_a, "maskMap"_a);
mod.def("statisticsStack",
(std::shared_ptr<std::vector<PixelT>>(*)(
std::vector<std::shared_ptr<std::vector<PixelT>>> &, Property, StatisticsControl const &,
(std::vector<PixelT>(*)(
std::vector<std::vector<PixelT>> &, Property, StatisticsControl const &,
std::vector<lsst::afw::image::VariancePixel> const &))statisticsStack<PixelT>,
"vectors"_a, "flags"_a, "sctrl"_a = StatisticsControl(),
"wvector"_a = std::vector<lsst::afw::image::VariancePixel>(0));
}

} // namespace

PYBIND11_PLUGIN(stack) {
py::module mod("stack");

Expand Down
22 changes: 11 additions & 11 deletions src/math/Stack.cc
Original file line number Diff line number Diff line change
Expand Up @@ -375,12 +375,12 @@ namespace {
* to handle cases when we are, or are not, weighting
*/
template <typename PixelT, bool isWeighted>
std::shared_ptr<std::vector<PixelT>> computeVectorStack(
std::vector<std::shared_ptr<std::vector<PixelT>>> &vectors, Property flags,
std::vector<PixelT> computeVectorStack(
std::vector<std::vector<PixelT>> &vectors, Property flags,
StatisticsControl const &sctrl, WeightVector const &wvector = WeightVector()) {
// create the image to be returned
typedef std::vector<PixelT> Vect;
std::shared_ptr<Vect> vecStack(new Vect(vectors[0]->size(), 0.0));
Vect vecStack(vectors[0].size(), 0.0);

MaskedVector<PixelT> pixelSet(vectors.size()); // values from a given pixel of each image

Expand All @@ -393,16 +393,16 @@ std::shared_ptr<std::vector<PixelT>> computeVectorStack(
}

// collect elements from the stack into the MaskedVector to do stats
for (unsigned int x = 0; x < vectors[0]->size(); ++x) {
for (unsigned int x = 0; x < vectors[0].size(); ++x) {
typename MaskedVector<PixelT>::iterator psPtr = pixelSet.begin();
for (unsigned int i = 0; i < vectors.size(); ++i, ++psPtr) {
psPtr.value() = (*vectors[i])[x];
psPtr.value() = (vectors[i])[x];
}

if (isWeighted) {
(*vecStack)[x] = makeStatistics(pixelSet, wvector, flags, sctrlTmp).getValue(flags);
(vecStack)[x] = makeStatistics(pixelSet, wvector, flags, sctrlTmp).getValue(flags);
} else {
(*vecStack)[x] = makeStatistics(pixelSet, flags, sctrlTmp).getValue(flags);
(vecStack)[x] = makeStatistics(pixelSet, flags, sctrlTmp).getValue(flags);
}
}

Expand All @@ -412,8 +412,8 @@ std::shared_ptr<std::vector<PixelT>> computeVectorStack(
} // end anonymous namespace

template <typename PixelT>
std::shared_ptr<std::vector<PixelT>> statisticsStack(
std::vector<std::shared_ptr<std::vector<PixelT>>> &vectors, Property flags,
std::vector<PixelT> statisticsStack(
std::vector<std::vector<PixelT>> &vectors, Property flags,
StatisticsControl const &sctrl, WeightVector const &wvector) {
checkObjectsAndWeights(vectors, wvector);
checkOnlyOneFlag(flags);
Expand Down Expand Up @@ -546,8 +546,8 @@ std::shared_ptr<image::MaskedImage<PixelT>> statisticsStack(image::MaskedImage<P
image::MaskedImage<TYPE> & out, std::vector<std::shared_ptr<image::MaskedImage<TYPE>>> & images, \
Property flags, StatisticsControl const &sctrl, WeightVector const &wvector, image::MaskPixel, \
std::vector<std::pair<image::MaskPixel, image::MaskPixel>> const &); \
template std::shared_ptr<std::vector<TYPE>> statisticsStack<TYPE>( \
std::vector<std::shared_ptr<std::vector<TYPE>>> & vectors, Property flags, \
template std::vector<TYPE> statisticsStack<TYPE>( \
std::vector<std::vector<TYPE>> & vectors, Property flags, \
StatisticsControl const &sctrl, WeightVector const &wvector); \
template std::shared_ptr<image::MaskedImage<TYPE>> statisticsStack(image::Image<TYPE> const &image, \
Property flags, char dimension, \
Expand Down
13 changes: 6 additions & 7 deletions tests/stacker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ namespace math = lsst::afw::math;
typedef image::Image<float> ImageF;
typedef image::MaskedImage<float> MImageF;
typedef std::vector<float> VecF;
typedef std::shared_ptr<VecF> VecFPtr;

BOOST_AUTO_TEST_CASE(
MeanStack) { /* parasoft-suppress LsstDm-3-2a LsstDm-3-4a LsstDm-4-6 LsstDm-5-25 "Boost non-Std" */
Expand Down Expand Up @@ -111,13 +110,13 @@ BOOST_AUTO_TEST_CASE(

// ====================================================
// std::vector, and also with a constant weight vector
std::vector<VecFPtr> vecList;
std::vector<VecF> vecList;
for (int iImg = 0; iImg < nImg; ++iImg) {
VecFPtr v = VecFPtr(new VecF(nX * nY, iImg));
VecF v(nX * nY, iImg);
vecList.push_back(v);
}
VecFPtr vecStack = math::statisticsStack<float>(vecList, math::MEAN);
VecFPtr wvecStack = math::statisticsStack<float>(vecList, math::MEAN, sctrl, wvec);
BOOST_CHECK_EQUAL((*vecStack)[nX * nY / 2], knownMean);
BOOST_CHECK_EQUAL((*wvecStack)[nX * nY / 2], knownWeightMean);
VecF vecStack = math::statisticsStack<float>(vecList, math::MEAN);
VecF wvecStack = math::statisticsStack<float>(vecList, math::MEAN, sctrl, wvec);
BOOST_CHECK_EQUAL((vecStack)[nX * nY / 2], knownMean);
BOOST_CHECK_EQUAL((wvecStack)[nX * nY / 2], knownWeightMean);
}

0 comments on commit 57132a5

Please sign in to comment.