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-8467: Wrap lsst_distrib with pybind11 #30

Merged
merged 1 commit into from
Mar 7, 2017
Merged
Show file tree
Hide file tree
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
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ config.log
*.so
*.cfgc
*.pyc
*_wrap.cc
*Lib.py
doc/html
doc/*.tag
doc/*.inc
Expand Down
2 changes: 1 addition & 1 deletion include/lsst/ip/isr/isr.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ namespace isr {
size_t maskNans(
afw::image::MaskedImage<PixelT> const& mi, ///< Input image
afw::image::MaskPixel maskVal, ///< Bit mask value to give a NaN
afw::image::MaskPixel allow=0 ///< Allow NANs with this bit mask (0 to disallow all NANs)
afw::image::MaskPixel allow=0 ///< Retain NANs with this bit mask (0 to mask all NANs)
);


Expand Down
4 changes: 2 additions & 2 deletions python/lsst/ip/isr/SConscript
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# -*- python -*-
## -*- python -*-
from lsst.sconsUtils import scripts
scripts.BasicSConscript.python(['isrLib'])
scripts.BasicSConscript.pybind11(["applyLookupTable", "isr"], addUnderscore=False)
11 changes: 7 additions & 4 deletions python/lsst/ip/isr/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#
# LSST Data Management System
# Copyright 2008, 2009, 2010 LSST Corporation.
#
# This product includes software developed by the
# LSST Project (http://www.lsst.org/).
# See the COPYRIGHT file
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
Expand All @@ -17,12 +17,15 @@
#
# You should have received a copy of the LSST License Statement and
# the GNU General Public License along with this program. If not,
# see <http://www.lsstcorp.org/LegalNotices/>.
# see <https://www.lsstcorp.org/LegalNotices/>.
#

from .version import *
from .isrLib import *
from __future__ import absolute_import

from .applyLookupTable import *
from .isr import *
from .version import *
from .isrFunctions import *
from .assembleCcdTask import *
from .isrTask import *
from .linearize import *
61 changes: 61 additions & 0 deletions python/lsst/ip/isr/applyLookupTable.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* LSST Data Management System
*
* This product includes software developed by the
* LSST Project (http://www.lsst.org/).
* See the COPYRIGHT file
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the LSST License Statement and
* the GNU General Public License along with this program. If not,
* see <https://www.lsstcorp.org/LegalNotices/>.
*/
#include "pybind11/pybind11.h"
#include "pybind11/stl.h"
#include "numpy/arrayobject.h"
#include "ndarray/pybind11.h"

#include "lsst/ip/isr/applyLookupTable.h"

namespace py = pybind11;
using namespace pybind11::literals;

namespace lsst {
namespace ip {
namespace isr {

namespace {

template <typename PixelT>
static void declareApplyLookupTable(py::module& mod) {
mod.def("applyLookupTable", &applyLookupTable<PixelT>, "image"_a, "table"_a, "indOffset"_a);
}

} // namespace lsst::ip::isr::<anonymous>

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

// Need to import numpy for ndarray and eigen conversions
if (_import_array() < 0) {
PyErr_SetString(PyExc_ImportError, "numpy.core.multiarray failed to import");
return nullptr;
}

declareApplyLookupTable<float>(mod);
declareApplyLookupTable<double>(mod);

return mod.ptr();
}
}
}
} // lsst::ip::isr
3 changes: 2 additions & 1 deletion python/lsst/ip/isr/assembleCcdTask.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@
# the GNU General Public License along with this program. If not,
# see <http://www.lsstcorp.org/LegalNotices/>.
#
from __future__ import division, print_function, absolute_import
import lsst.afw.cameraGeom as cameraGeom
import lsst.afw.cameraGeom.utils as cameraGeomUtils
import lsst.afw.image as afwImage
import lsst.pex.config as pexConfig
import lsst.pipe.base as pipeBase
from lsstDebug import getDebugFrame
from lsst.afw.display import getDisplay
from .isr import calcEffectiveGain
from .isrFunctions import calcEffectiveGain

__all__ = ["AssembleCcdTask"]

Expand Down
4 changes: 2 additions & 2 deletions python/lsst/ip/isr/fringe.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
# see <http://www.lsstcorp.org/LegalNotices/>.
#

from __future__ import print_function
from __future__ import division, print_function, absolute_import
from builtins import zip
from builtins import input
from builtins import range
Expand All @@ -46,7 +46,7 @@ def getFrame():
class FringeStatisticsConfig(Config):
"""Options for measuring fringes on an exposure"""
badMaskPlanes = ListField(dtype=str, default=["SAT"], doc="Ignore pixels with these masks")
stat = Field(dtype=int, default=afwMath.MEDIAN, doc="Statistic to use")
stat = Field(dtype=int, default=int(afwMath.MEDIAN), doc="Statistic to use")
clip = Field(dtype=float, default=3.0, doc="Sigma clip threshold")
iterations = Field(dtype=int, default=3, doc="Number of fitting iterations")
rngSeedOffset = Field(dtype=int, default=0,
Expand Down
78 changes: 78 additions & 0 deletions python/lsst/ip/isr/isr.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* LSST Data Management System
*
* This product includes software developed by the
* LSST Project (http://www.lsst.org/).
* See the COPYRIGHT file
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the LSST License Statement and
* the GNU General Public License along with this program. If not,
* see <https://www.lsstcorp.org/LegalNotices/>.
*/
#include <memory>

#include "pybind11/pybind11.h"

#include "lsst/ip/isr/isr.h"

namespace py = pybind11;
using namespace pybind11::literals;

namespace lsst {
namespace ip {
namespace isr {

namespace {

template <typename PixelT>
static void declareCountMaskedPixels(py::module& mod, std::string const& suffix) {
py::class_<CountMaskedPixels<PixelT>, std::shared_ptr<CountMaskedPixels<PixelT>>> cls(
mod, ("CountMaskedPixels" + suffix).c_str());

cls.def("reset", &CountMaskedPixels<PixelT>::reset);
cls.def("apply", &CountMaskedPixels<PixelT>::apply, "image"_a, "bitmask"_a);
cls.def("getCount", &CountMaskedPixels<PixelT>::getCount);
}

/**
* Wrap all code in Isr.h for a given template parameter
*
* @tparam PixelT Pixel type; typically `float` or `double` (potentially could also be
* and integer class, but so far we have not needed those)
* @param mod pybind11 module to which to add the wrappers.
* @param[in] suffix Class name suffix associated with `PixelT`, e.g. "F" for `float` and "D" for `double`
*
* Note that the second (function type) template parameter of `fitOverscanImage` is always `double`.
*/
template <typename PixelT>
static void declareAll(py::module& mod, std::string const& suffix) {
declareCountMaskedPixels<PixelT>(mod, suffix);

mod.def("maskNans", &maskNans<PixelT>, "maskedImage"_a, "maskVal"_a, "allow"_a = 0);
mod.def("fitOverscanImage", &fitOverscanImage<PixelT, double>, "overscanFunction"_a, "overscan"_a,
"stepSize"_a = 1.1, "sigma"_a = 1);
}

} // namespace lsst::ip::isr::<anonymous>

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

declareAll<float>(mod, "F");
declareAll<double>(mod, "D");

return mod.ptr();
}
}
}
} // lsst::ip::isr
22 changes: 11 additions & 11 deletions python/lsst/ip/isr/isr.py → python/lsst/ip/isr/isrFunctions.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from __future__ import print_function
from __future__ import division, print_function, absolute_import
from builtins import input
from builtins import range
#
Expand Down Expand Up @@ -93,9 +93,9 @@ def defectListFromFootprintList(fpList, growFootprints=1):

@param[in] fpList footprint list
@param[in] growFootprints amount by which to grow footprints of detected regions
@return meas.algorithms.DefectListT
@return a list of defects (meas.algorithms.Defect)
"""
defectList = measAlg.DefectListT()
defectList = []
for fp in fpList:
if growFootprints > 0:
# if "True", growing requires a convolution
Expand All @@ -105,30 +105,30 @@ def defectListFromFootprintList(fpList, growFootprints=1):
fpGrow = fp
for bbox in afwDetection.footprintToBBoxList(fpGrow):
defect = measAlg.Defect(bbox)
defectList.push_back(defect)
defectList.append(defect)
return defectList


def transposeDefectList(defectList):
"""Make a transposed copy of a defect list

@param[in] defectList defect list
@return meas.algorithms.DefectListT with transposed defects
@param[in] defectList a list of defects (afw.meas.algorithms.Defect)
@return a defect list with transposed defects
"""
retDefectList = measAlg.DefectListT()
retDefectList = []
for defect in defectList:
bbox = defect.getBBox()
nbbox = afwGeom.Box2I(afwGeom.Point2I(bbox.getMinY(), bbox.getMinX()),
afwGeom.Extent2I(bbox.getDimensions()[1], bbox.getDimensions()[0]))
retDefectList.push_back(measAlg.Defect(nbbox))
retDefectList.append(measAlg.Defect(nbbox))
return retDefectList


def maskPixelsFromDefectList(maskedImage, defectList, maskName='BAD'):
"""Set mask plane based on a defect list

@param[in,out] maskedImage afw.image.MaskedImage to process; mask plane is updated
@param[in] defectList meas.algorithms.DefectListT
@param[in] defectList a list of defects (afw.meas.algorithms.Defect)
@param[in] maskName mask plane name
"""
# mask bad pixels
Expand All @@ -145,7 +145,7 @@ def getDefectListFromMask(maskedImage, maskName, growFootprints=1):
@param[in] maskedImage masked image to process
@param[in] maskName mask plane name, or list of names
@param[in] growFootprints amount by which to grow footprints of detected regions
@return meas.algrithms.DefectListT of regions in mask
@return a list of defects (each an meas.algrithms.Defect) of regions in mask
"""
mask = maskedImage.getMask()
thresh = afwDetection.Threshold(mask.getPlaneBitMask(maskName), afwDetection.Threshold.BITMASK)
Expand All @@ -160,7 +160,7 @@ def makeThresholdMask(maskedImage, threshold, growFootprints=1, maskName='SAT'):
@param[in] threshold detection threshold
@param[in] growFootprints amount by which to grow footprints of detected regions
@param[in] maskName mask plane name
@return meas.algorihtms.DefectListT of regions set in the mask.
@return a list of defects (meas.algrithms.Defect) of regions set in the mask.
"""
# find saturated regions
thresh = afwDetection.Threshold(threshold)
Expand Down
79 changes: 0 additions & 79 deletions python/lsst/ip/isr/isrLib.i

This file was deleted.