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

Tickets/dm 2961 - psfex port #1

Merged
merged 25 commits into from
Aug 14, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
0ebadc7
Updated pex exceptions to format in DM-746.
natelust Jul 6, 2015
bc20bb8
Removed dependency on atlas
natelust Jul 6, 2015
039fe8b
update config to use slot strings
natelust Jul 17, 2015
aa9ff0d
Unset the VERSION variable from IFDEFINE in psfex.
natelust Jul 17, 2015
b2eac69
eupspkg build script created
natelust Jul 17, 2015
f74a977
updated compound fields to functor keys (DM-2535)
natelust Jul 17, 2015
3adea1e
Code to successfully run on LSST stack
natelust Jul 17, 2015
725772a
Fix for a dangling pointer issue.
natelust Jul 17, 2015
45837ce
Update test with appropriate LSST configs
natelust Jul 17, 2015
f271090
Updated table file with required dependancies
Jul 21, 2015
cc52389
Added meas_base in addition to algorithms
natelust Jul 27, 2015
15f55ec
Removed unneeded eupspkg script
natelust Jul 28, 2015
1ac6006
Inserted spacing for readability
natelust Jul 28, 2015
4586f37
Updated argument description
natelust Aug 11, 2015
252ca17
Assigned descriptive variable name to bool object
natelust Aug 11, 2015
618727c
[Added comments describing why a vector is created
natelust Aug 11, 2015
aecb8d6
Remove unneeded comments
natelust Aug 11, 2015
64aca56
Whitespace changes
natelust Aug 11, 2015
ea300d1
Remove unneeded comments
natelust Aug 11, 2015
d229054
Change config file to depend on fftw package
natelust Aug 11, 2015
9b6219b
Update explicit dependancies
natelust Aug 11, 2015
89ba518
Added comment on wrapping headers
natelust Aug 11, 2015
d8ec3d2
Removed un-needed variables from python script
natelust Aug 11, 2015
2ffdbe2
updated slot according to DM-3108
natelust Aug 11, 2015
6ff9fc8
Only import what is needed, not *, and formatting
natelust Aug 12, 2015
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
8 changes: 4 additions & 4 deletions bin/psfex.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
import argparse
import os
import sys
from lsst.meas.extensions.psfex import *
from lsst.meas.extensions.psfex import psfex, readPrefs, makeitLsst, makeit, showPsf, dafBase

#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

if __name__ == "__main__":
parser = argparse.ArgumentParser(description="PSFEX")
parser = argparse.ArgumentParser(description="Wrapper for Point Spread Function Extractor (PSFEX)")

parser.add_argument('catalogs', type=str, nargs="+", help="Input catalogues from SExtractor")
parser.add_argument('-c', type=str, dest="defaultsFile",
Expand All @@ -17,12 +17,12 @@
help="Overrides for default parameters", default=[])
parser.add_argument('--plot', type=str, nargs="+",
help="Desired plots", default=[])
parser.add_argument('--ds9', type=int,
parser.add_argument('--ds9', type=int,
help="Show the PSF on ds9", default=None)
parser.add_argument('--diagnostics', action="store_true",
help="Write output diagnostic plots to outDir")
parser.add_argument('--verbose', action="store_true", help="How chatty should I be?", default=False)

argv = sys.argv[:] # argparse will mess with sys.argv
args = parser.parse_args()

Expand Down
8 changes: 4 additions & 4 deletions config/default-lsst.psfex
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ PSF_ACCURACY 0.01 # Accuracy to expect from PSF "pixel" values

#------------------------- Point source measurements -------------------------

CENTER_KEYS initial.centroid.sdss.x,initial.centroid.sdss.y # Catalogue parameters for source pre-centering
PHOTFLUX_KEY initial.flux.sinc # Catalogue parameter for photometric norm.
PHOTFLUXERR_KEY initial.flux.sinc.err # Catalogue parameter for photometric error
CENTER_KEYS slot_Centroid_x,slot_Centroid_y # Catalogue parameters for source pre-centering
PHOTFLUX_KEY slot_ApFlux_flux # Catalogue parameter for photometric norm.
PHOTFLUXERR_KEY slot_ApFlux_fluxErr # Catalogue parameter for photometric error

#----------------------------- PSF variability -------------------------------

PSFVAR_KEYS initial.centroid.sdss.x,initial.centroid.sdss.y # Catalogue or FITS (preceded by :) params
PSFVAR_KEYS slot_Centroid_x,slot_Centroid_y # Catalogue or FITS (preceded by :) params
PSFVAR_GROUPS 1,1 # Group tag for each context key
PSFVAR_DEGREES 2 # Polynom degree for each group

Expand Down
2 changes: 2 additions & 0 deletions include/lsst/meas/extensions/psfex/Field.hh
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// -*- lsst-C++ -*-
// Wrapper and extension of the psfex field.h header
#if !defined(ASTROMATIC_PSFEX_FIELD_HH)
#define ASTROMATIC_PSFEX_FIELD_HH

Expand All @@ -21,6 +22,7 @@ struct structtab; typedef struct structtab tabstruct;

#include "define.h"
#include "field.h"
#undef VERSION
}

namespace lsst { namespace meas { namespace extensions { namespace psfex {
Expand Down
2 changes: 2 additions & 0 deletions include/lsst/meas/extensions/psfex/prefs.hh
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// -*- lsst-C++ -*-
// Wrapper and extension of the psfex prefs.h header
#if !defined(ASTROMATIC_PSFEX_PREFS_HH)
#define ASTROMATIC_PSFEX_PREFS_HH

Expand All @@ -15,6 +16,7 @@ struct structtab; typedef struct structtab tabstruct;

#include "define.h"
#include "prefs.h"
#undef VERSION
}

namespace lsst { namespace meas { namespace extensions { namespace psfex {
Expand Down
9 changes: 1 addition & 8 deletions python/lsst/meas/extensions/psfex/psfex.py
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,6 @@ def load_samples(prefs, context, ext=psfex.Prefs.ALL_EXTENSIONS, next=1, plot=di
ncat = len(filenames)
fwhmmin = np.empty(ncat)
fwhmmax = np.empty(ncat)
fwhmode = np.empty(ncat)

if not prefs.getAutoselectFlag():
fwhmmin = np.zeros(ncat) + prefs.getFwhmrange()[0]
Expand Down Expand Up @@ -747,7 +746,6 @@ def read_samplesLsst(prefs, set, filename, frmin, frmax, ext, next, catindex, co
shape = tab.getShapeDefinition()
ixx = tab.get("%s.xx" % shape)
iyy = tab.get("%s.yy" % shape)
ixy = tab.get("%s.xy" % shape)

rmsSize = np.sqrt(0.5*(ixx + iyy))
elong = 0.5*(ixx - iyy)/(ixx + iyy)
Expand All @@ -756,7 +754,6 @@ def read_samplesLsst(prefs, set, filename, frmin, frmax, ext, next, catindex, co
fluxErr = tab.get(prefs.getPhotfluxerrRkey())
flags = getLsstFlags(tab)

nobj = len(xm)
#
# Now the VIGNET data
#
Expand All @@ -783,7 +780,7 @@ def read_samplesLsst(prefs, set, filename, frmin, frmax, ext, next, catindex, co
try:
pstamp = mi[x - vigw//2:x + vigw//2 + 1, y - vigh//2:y + vigh//2 + 1]
vignet[i] = pstamp.getImage().getArray().transpose()
except Exception, e:
except Exception:
flags[i] |= edgeBit # mark star as bad

# Try to load the set of context keys
Expand Down Expand Up @@ -871,7 +868,6 @@ def load_samplesLsst(prefs, context, ext=psfex.Prefs.ALL_EXTENSIONS, next=1, plo
ncat = len(filenames)
fwhmmin = np.empty(ncat)
fwhmmax = np.empty(ncat)
fwhmode = np.empty(ncat)

if not prefs.getAutoselectFlag():
fwhmmin = np.zeros(ncat) + prefs.getFwhmrange()[0]
Expand All @@ -896,7 +892,6 @@ def load_samplesLsst(prefs, context, ext=psfex.Prefs.ALL_EXTENSIONS, next=1, plo
shape = tab.getShapeDefinition()
ixx = tab.get("%s.xx" % shape)
iyy = tab.get("%s.yy" % shape)
ixy = tab.get("%s.xy" % shape)

rmsSize = np.sqrt(0.5*(ixx + iyy))
elong = 0.5*(ixx - iyy)/(ixx + iyy)
Expand Down Expand Up @@ -1020,9 +1015,7 @@ def makeit(prefs, context, saveWcs=False, plot=dict()):
field.finalize()
fields.append(field)

next = fields[0].getNext() # number of extensions

psfstep = prefs.getPsfStep()

sets = psfex.vectorSet()
for set in load_samples(prefs, context, plot=plot):
Expand Down
8 changes: 4 additions & 4 deletions python/lsst/meas/extensions/psfex/psfexPsfDeterminer.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,18 +253,18 @@ def determinePsf(self, exposure, psfCandidateList, metadata=None, flagKey=None):
prefs.addCatalog("psfexPsfDeterminer")

prefs.use()

principalComponentExclusionFlag = bool(bool(psfex.Context.REMOVEHIDDEN)
if False else psfex.Context.KEEPHIDDEN)
context = psfex.Context(prefs.getContextName(), prefs.getContextGroup(),
prefs.getGroupDeg(),
psfex.Context.REMOVEHIDDEN if False else psfex.Context.KEEPHIDDEN)
prefs.getGroupDeg(),principalComponentExclusionFlag)
set = psfex.Set(context)
set.setVigSize(pixKernelSize, pixKernelSize)
set.setFwhm(2*np.sqrt(2*np.log(2))*np.median(sizes))
set.setRecentroid(self.config.recentroid)

catindex, ext = 0, 0
backnoise2 = afwMath.makeStatistics(mi.getImage(), afwMath.VARIANCECLIP).getValue()
ccd = afwCG.cast_Ccd(exposure.getDetector())
ccd = exposure.getDetector()
if ccd:
gain = np.mean(np.array([a.getElectronicParams().getGain() for a in ccd]))
else:
Expand Down
10 changes: 5 additions & 5 deletions src/PsfexPsf.cc
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ extern "C" {
}
#include "lsst/meas/extensions/psfex/PsfexPsf.h"
#include "lsst/meas/algorithms/KernelPsfFactory.h"
#include "lsst/afw/table/aggregates.h"

namespace lsst { namespace meas { namespace extensions { namespace psfex {

Expand Down Expand Up @@ -98,7 +99,7 @@ PsfexPsf::getKernel(afw::geom::Point2D position) const
double pos[MAXCONTEXT];
int const ndim = _context.size();
if (ndim != 2) { // we're only handling spatial variation for now
throw LSST_EXCEPT(lsst::pex::exceptions::InvalidParameterException,
throw LSST_EXCEPT(lsst::pex::exceptions::InvalidParameterError,
str(boost::format("Only spatial variation (ndim == 2) is supported; saw %d")
% ndim));

Expand Down Expand Up @@ -188,7 +189,7 @@ PsfexPsf::_doComputeImage(afw::geom::Point2D const& position,
double pos[MAXCONTEXT];
int const ndim = _context.size();
if (ndim != 2) { // we're only handling spatial variation for now
throw LSST_EXCEPT(lsst::pex::exceptions::InvalidParameterException,
throw LSST_EXCEPT(lsst::pex::exceptions::InvalidParameterError,
str(boost::format("Only spatial variation (ndim == 2) is supported; saw %d")
% ndim));

Expand Down Expand Up @@ -279,8 +280,7 @@ class PsfexPsfSchema1 {
_comp_size(schema.addField<int>("_comp_size", "Size of _comp array")),
_context_size(schema.addField<int>("_context_size", "Size of _context array")),
// Other scalars
averagePosition(schema.addField< afw::table::Point<double> >(
"averagePosition", "average position of stars used to make the PSF")),
averagePosition(afw::table::PointKey<double>::addFields(schema,"averagePosition","average position of stars used to make the PSF","pixels")),
_pixstep(schema.addField<float>("_pixstep", "oversampling", "pixel"))
{
;
Expand All @@ -297,7 +297,7 @@ class PsfexPsfSchema1 {
table::Key<int> _comp_size;
table::Key<int> _context_size;
// Other scalars
table::Key<table::Point<double> > averagePosition;
table::PointKey<double> averagePosition;
table::Key<float> _pixstep;
};

Expand Down
11 changes: 9 additions & 2 deletions src/prefsImpl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,18 @@ Prefs::Prefs(std::string const& filename, lsst::daf::base::PropertySet const* va
std::vector<char *> argkey(narg);
std::vector<char *> argval(narg);
std::vector<std::string> const names = values->paramNames();
// Next, decalre a vector to store string values. These strings must
// live long enough such that pointers to them (returned by c_str) stay
// valid until the readprefs function can read their values. Once this
// happens, the srings and pointers are no longer needed, and will be
// cleaned up when the vector goes out of scope
std::vector<std::string> paramvalues(narg);

for (int i = 0; i != narg; ++i) {
std::string const& name = names[i];
argkey[i] = const_cast<char *>(name.c_str());
argval[i] = const_cast<char *>(values->getAsString(name).c_str());
paramvalues[i] = values->getAsString(name);
argval[i] = const_cast<char *>(paramvalues[i].c_str());
}

readprefs(cfilename, &argkey[0], &argval[0], narg);
Expand Down Expand Up @@ -56,7 +63,7 @@ Prefs::setCommandLine(std::vector<std::string> const& argv)
void
Prefs::addCatalog(std::string const& filename) {
if (prefs.ncat >= MAXFILE) {
throw LSST_EXCEPT(lsst::pex::exceptions::LengthErrorException, "Too many input catalogues");
throw LSST_EXCEPT(lsst::pex::exceptions::LengthError, "Too many input catalogues");
}
_catalogs.push_back(filename);
prefs.incat_name[prefs.ncat++] = const_cast<char *>((_catalogs.end() - 1)->c_str());
Expand Down
4 changes: 2 additions & 2 deletions src/psfImpl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ void Sample::setVig(ndarray::Array<float,2,2> const& img)
int const w = img.getSize<0>(), h = img.getSize<1>();

if (_vigsize[0]*_vigsize[1] != w*h) {
throw LSST_EXCEPT(lsst::pex::exceptions::LengthErrorException,
throw LSST_EXCEPT(lsst::pex::exceptions::LengthError,
str(boost::format("Expected %dx%d array, but was passed %dx%d")
% _vigsize[0] % _vigsize[1] % w % h));
}
Expand Down Expand Up @@ -146,7 +146,7 @@ Psf::build(double x, double y,
}

psf_build(impl, &pos[0]);
}
}

RETURN_IMAGE_FIELD(Psf::getLoc, loc, impl->size)
RETURN_IMAGE_FIELD(Psf::getResi, resi, impl->size)
Expand Down
2 changes: 1 addition & 1 deletion src/psfexAdaptors.cc
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ makeit(std::vector<boost::shared_ptr<Field> > &fields_,
)
{
if (sets.size() > MAXFILE) {
throw LSST_EXCEPT(lsst::pex::exceptions::LengthErrorException,
throw LSST_EXCEPT(lsst::pex::exceptions::LengthError,
(boost::format("Too many sets: %d") % sets.size()).str());
}

Expand Down
49 changes: 14 additions & 35 deletions tests/testPsfexPsf.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@
import lsst.meas.algorithms.utils as maUtils
import lsst.afw.cameraGeom as cameraGeom
import lsst.meas.extensions.psfex.psfexPsfDeterminer as psfexPsfDeterminer

from lsst.meas.base import SingleFrameMeasurementTask
from lsst.meas.algorithms.detection import SourceDetectionTask
try:
type(verbose)
except NameError:
Expand Down Expand Up @@ -84,32 +85,21 @@ class SpatialModelPsfTestCase(unittest.TestCase):
@staticmethod
def measure(footprintSet, exposure):
"""Measure a set of Footprints, returning a SourceCatalog"""
config = measAlg.SourceMeasurementConfig()
config.prefix = "initial."
config.algorithms.names = ["flags.pixel", "flux.psf", "flux.sinc", "flux.gaussian", "shape.sdss"]
config.centroider.name = "centroid.sdss"
config.algorithms["flux.naive"].radius = 3.0
config.slots.centroid = "initial.centroid.sdss"
config.slots.psfFlux = "initial.flux.psf"
config.slots.apFlux = "initial.flux.sinc"
config.slots.modelFlux = None
config.slots.instFlux = None
config.slots.calibFlux = None
config.slots.shape = "initial.shape.sdss"

config = SingleFrameMeasurementTask.ConfigClass()
config.slots.apFlux = 'base_CircularApertureFlux_12_0'
schema = afwTable.SourceTable.makeMinimalSchema()
measureSources = config.makeMeasureSources(schema)

measureSources = SingleFrameMeasurementTask(schema,config=config)

tab = afwTable.SourceTable.make(schema)
catalog = afwTable.SourceCatalog(schema)
config.slots.setupTable(catalog.table)

if display:
ds9.mtv(exposure, title="Original", frame=0)

footprintSet.makeSources(catalog)

for i, source in enumerate(catalog):
measureSources.applyWithPeak(source, exposure)

measureSources.run(catalog,exposure)
return catalog

def setUp(self):
Expand All @@ -134,14 +124,6 @@ def setUp(self):
wcs = afwImage.makeWcs(crval, afwGeom.PointD(0, 0), 1.0, 0, 0, 1.0)
self.exposure.setWcs(wcs)

ccd = cameraGeom.Ccd(cameraGeom.Id(1))
ccd.addAmp(cameraGeom.Amp(cameraGeom.Id(0),
afwGeom.BoxI(afwGeom.PointI(0,0), self.exposure.getDimensions()),
afwGeom.BoxI(afwGeom.PointI(0,0), afwGeom.ExtentI(0,0)),
afwGeom.BoxI(afwGeom.PointI(0,0), self.exposure.getDimensions()),
cameraGeom.ElectronicParams(1.0, 100.0, 65535)))
self.exposure.setDetector(ccd)
self.exposure.getDetector().setDistortion(None)
#
# Make a kernel with the exactly correct basis functions. Useful for debugging
#
Expand Down Expand Up @@ -241,13 +223,12 @@ def setupDeterminer(exposure):
starSelectorFactory = measAlg.starSelectorRegistry["objectSize"]
starSelectorConfig = starSelectorFactory.ConfigClass()

starSelectorConfig.sourceFluxField = "initial.flux.gaussian"
starSelectorConfig.badFlags = ["initial.flags.pixel.edge",
"initial.flags.pixel.interpolated.center",
"initial.flags.pixel.saturated.center",
"initial.flags.pixel.cr.center",
starSelectorConfig.sourceFluxField = "base_GaussianFlux_flux"
starSelectorConfig.badFlags = ["base_PixelFlags_flag_edge",
"base_PixelFlags_flag_interpolatedCenter",
"base_PixelFlags_flag_saturatedCenter",
"base_PixelFlags_flag_crCenter",
]
starSelectorConfig.widthStdAllowed = 0.5

starSelector = starSelectorFactory(starSelectorConfig)

Expand All @@ -270,7 +251,6 @@ def subtractStars(self, exposure, catalog, chi_lim=-1):
mi, psf = exposure.getMaskedImage(), exposure.getPsf()

subtracted = mi.Factory(mi, True)

for s in catalog:
xc, yc = s.getX(), s.getY()
bbox = subtracted.getBBox(afwImage.PARENT)
Expand All @@ -279,7 +259,6 @@ def subtractStars(self, exposure, catalog, chi_lim=-1):
measAlg.subtractPsf(psf, subtracted, xc, yc)
except:
pass

chi = subtracted.Factory(subtracted, True)
var = subtracted.getVariance()
np.sqrt(var.getArray(), var.getArray()) # inplace sqrt
Expand Down
4 changes: 2 additions & 2 deletions ups/meas_extensions_psfex.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import lsst.sconsUtils

dependencies = {
"required": [],
"buildRequired": ["afw", "boost_test", "daf_base", "meas_algorithms",
"swig", "utils", "psfex"],
"buildRequired": ["afw", "boost_test", "daf_base", "fftw",
"meas_base","meas_algorithms","swig", "utils", "psfex"],
}

config = lsst.sconsUtils.Configuration(
Expand Down
8 changes: 7 additions & 1 deletion ups/meas_extensions_psfex.table
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
setupRequired(afw)
setupRequired(boost)
setupRequired(daf_base)
Copy link
Member

Choose a reason for hiding this comment

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

Either delete it or uncomment it please.

Copy link
Contributor

Choose a reason for hiding this comment

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

Strange, I don't have that on my local copy. I wonder If I forgot to push that change

setupRequired(fftw)
setupRequired(swig)
setupRequired(utils)
setupRequired(meas_base)
setupRequired(meas_algorithms)
setupRequired(psfex)
#setupRequired(pyfits)

envPrepend(PATH, ${PRODUCT_DIR}/bin)

Expand Down