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-11539: Fix test for pytest and make flake8 clean #90

Merged
merged 5 commits into from
Aug 13, 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
3 changes: 1 addition & 2 deletions examples/runSingleFrameTask.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ def run(display=False):
ds9.dot('o', *xy, size=config.plugins["base_CircularApertureFlux"].radii[0],
ctype=ds9.YELLOW, frame=frame)

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

if __name__ == "__main__":
import argparse
Expand All @@ -124,7 +123,7 @@ def run(display=False):

if args.debug:
try:
import debug
import debug # noqa F401
except ImportError as e:
print(e, file=sys.stderr)

Expand Down
1 change: 0 additions & 1 deletion python/lsst/meas/base/applyApCorr.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,6 @@ def __init__(self, schema, **kwds):
continue
self.apCorrInfoDict[name] = ApCorrInfo(schema=schema, model=model, name=name)


def run(self, catalog, apCorrMap):
"""Apply aperture corrections to a catalog of sources

Expand Down
19 changes: 9 additions & 10 deletions python/lsst/meas/base/baseMeasurement.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
#
"""Base measurement task, which subclassed by the single frame and forced measurement tasks.
"""
from lsst.log import Log
import lsst.pipe.base
import lsst.pex.config

Expand Down Expand Up @@ -234,7 +233,7 @@ def initializePlugins(self, **kwds):
# The task will use this name to log plugin errors, regardless.
if hasattr(PluginClass, "hasLogName") and PluginClass.hasLogName:
self.plugins[name] = PluginClass(config, name, metadata=self.algMetadata,
logName=self.getPluginLogName(name), **kwds)
logName=self.getPluginLogName(name), **kwds)
else:
self.plugins[name] = PluginClass(config, name, metadata=self.algMetadata, **kwds)

Expand Down Expand Up @@ -300,13 +299,13 @@ def doMeasurement(self, plugin, measRecord, *args, **kwds):
raise
except MeasurementError as error:
lsst.log.Log.getLogger(self.getPluginLogName(plugin.name)).debug(
"MeasurementError in %s.measure on record %s: %s"
% (plugin.name, measRecord.getId(), error))
"MeasurementError in %s.measure on record %s: %s"
% (plugin.name, measRecord.getId(), error))
plugin.fail(measRecord, error)
except Exception as error:
lsst.log.Log.getLogger(self.getPluginLogName(plugin.name)).debug(
"Exception in %s.measure on record %s: %s"
% (plugin.name, measRecord.getId(), error))
"Exception in %s.measure on record %s: %s"
% (plugin.name, measRecord.getId(), error))
plugin.fail(measRecord)

def callMeasureN(self, measCat, *args, **kwds):
Expand Down Expand Up @@ -368,12 +367,12 @@ def doMeasurementN(self, plugin, measCat, *args, **kwds):
except MeasurementError as error:
for measRecord in measCat:
lsst.log.Log.getLogger(self.getPluginLogName(plugin.name)).debug(
"MeasurementError in %s.measureN on records %s-%s: %s"
% (plugin.name, measCat[0].getId(), measCat[-1].getId(), error))
"MeasurementError in %s.measureN on records %s-%s: %s"
% (plugin.name, measCat[0].getId(), measCat[-1].getId(), error))
plugin.fail(measRecord, error)
except Exception as error:
for measRecord in measCat:
plugin.fail(measRecord)
lsst.log.Log.getLogger(self.getPluginLogName(plugin.name)).debug(
"Exception in %s.measureN on records %s-%s: %s"
% (plugin.name, measCat[0].getId(), measCat[-1].getId(), error))
"Exception in %s.measureN on records %s-%s: %s"
% (plugin.name, measCat[0].getId(), measCat[-1].getId(), error))
1 change: 0 additions & 1 deletion python/lsst/meas/base/footprintArea.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,3 @@ def fail(self, measRecord, error=None):
# Should be impossible for this algorithm to fail unless there is no
# Footprint (and that's a precondition for measurement).
pass

3 changes: 2 additions & 1 deletion python/lsst/meas/base/forcedPhotCoadd.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from builtins import zip
#!/usr/bin/env python
#
# LSST Data Management System
Expand All @@ -21,6 +20,8 @@
# the GNU General Public License along with this program. If not,
# see <https://www.lsstcorp.org/LegalNotices/>.
#
from builtins import zip

import lsst.pex.config
import lsst.pipe.base
import lsst.coadd.utils
Expand Down
5 changes: 3 additions & 2 deletions python/lsst/meas/base/noiseReplacer.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from builtins import str
from builtins import object
#!/usr/bin/env python
#
# LSST Data Management System
Expand All @@ -22,6 +20,9 @@
# the GNU General Public License along with this program. If not,
# see <https://www.lsstcorp.org/LegalNotices/>.
#
from builtins import str
from builtins import object

import math

import lsst.afw.detection as afwDet
Expand Down
8 changes: 5 additions & 3 deletions python/lsst/meas/base/plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,12 @@
from .gaussianFlux import GaussianFluxAlgorithm, GaussianFluxControl, GaussianFluxTransform
from .exceptions import MeasurementError
from .naiveCentroid import NaiveCentroidAlgorithm, NaiveCentroidControl, NaiveCentroidTransform
from .peakLikelihoodFlux import PeakLikelihoodFluxAlgorithm, PeakLikelihoodFluxControl, PeakLikelihoodFluxTransform
from .peakLikelihoodFlux import PeakLikelihoodFluxAlgorithm, PeakLikelihoodFluxControl, \
PeakLikelihoodFluxTransform
from .pixelFlags import PixelFlagsAlgorithm, PixelFlagsControl
from .psfFlux import PsfFluxAlgorithm, PsfFluxControl, PsfFluxTransform
from .scaledApertureFlux import ScaledApertureFluxAlgorithm, ScaledApertureFluxControl, ScaledApertureFluxTransform
from .scaledApertureFlux import ScaledApertureFluxAlgorithm, ScaledApertureFluxControl, \
ScaledApertureFluxTransform
from .sdssCentroid import SdssCentroidAlgorithm, SdssCentroidControl, SdssCentroidTransform
from .sdssShape import SdssShapeAlgorithm, SdssShapeControl, SdssShapeTransform

Expand Down Expand Up @@ -238,7 +240,7 @@ def measure(self, measRecord, exposure):
measRecord.set(self.varValue, medVar)
else:
raise MeasurementError("Footprint empty, or all pixels are masked, can't compute median",
self.FAILURE_EMPTY_FOOTPRINT)
self.FAILURE_EMPTY_FOOTPRINT)

def fail(self, measRecord, error=None):
# Check that we have a error object and that it is of type MeasurementError
Expand Down
4 changes: 1 addition & 3 deletions python/lsst/meas/base/sfm.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@
to avoid information loss (this should, of course, be indicated in the field documentation).
"""

from lsst.pex.config import Field, ListField

from .pluginRegistry import PluginRegistry, PluginMap
from .pluginRegistry import PluginRegistry
from .baseMeasurement import (BaseMeasurementPluginConfig, BaseMeasurementPlugin,
BaseMeasurementConfig, BaseMeasurementTask)
from .noiseReplacer import NoiseReplacer, DummyNoiseReplacer
Expand Down
13 changes: 8 additions & 5 deletions python/lsst/meas/base/wrappers.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class WrappedSingleFramePlugin(SingleFramePlugin):

def __init__(self, config, name, schema, metadata, logName=None):
SingleFramePlugin.__init__(self, config, name, schema, metadata, logName=logName)
if hasattr(self, "hasLogName") and self.hasLogName and not logName is None:
if hasattr(self, "hasLogName") and self.hasLogName and logName is not None:
self.cpp = self.factory(config, name, schema, metadata, logName=logName)
else:
self.cpp = self.factory(config, name, schema, metadata)
Expand All @@ -30,7 +30,7 @@ class WrappedForcedPlugin(ForcedPlugin):

def __init__(self, config, name, schemaMapper, metadata, logName=None):
ForcedPlugin.__init__(self, config, name, schemaMapper, metadata, logName=logName)
if hasattr(self, "hasLogName") and self.hasLogName and not logName is None:
if hasattr(self, "hasLogName") and self.hasLogName and logName is not None:
self.cpp = self.factory(config, name, schemaMapper, metadata, logName=logName)
else:
self.cpp = self.factory(config, name, schemaMapper, metadata)
Expand Down Expand Up @@ -323,9 +323,11 @@ def wrapForcedAlgorithm(AlgClass, executionOrder, name=None, needsMetadata=False
and the logName comes last of the three
"""
if needsSchemaOnly:
extractSchemaArg = lambda m: m.editOutputSchema()
def extractSchemaArg(m):
return m.editOutputSchema()
else:
extractSchemaArg = lambda m: m
def extractSchemaArg(m):
return m
if hasMeasureN:
if needsMetadata:
def factory(config, name, schemaMapper, metadata, **kwargs):
Expand Down Expand Up @@ -416,7 +418,8 @@ def wrapSimpleAlgorithm(AlgClass, executionOrder, name=None, needsMetadata=False
return (wrapSingleFrameAlgorithm(AlgClass, executionOrder=executionOrder, name=name,
needsMetadata=needsMetadata, hasLogName=hasLogName, **kwds),
wrapForcedAlgorithm(AlgClass, executionOrder=executionOrder, name=name,
needsMetadata=needsMetadata, hasLogName=hasLogName, needsSchemaOnly=True, **kwds))
needsMetadata=needsMetadata, hasLogName=hasLogName,
needsSchemaOnly=True, **kwds))


def wrapTransform(transformClass, hasLogName=False):
Expand Down
16 changes: 16 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[flake8]
max-line-length = 110
ignore = E133, E226, E228, N802, N803, W503
exclude = __init__.py, tests/testLib.py

[tool:pytest]
addopts = --flake8
flake8-ignore = E133 E226 E228 N802 N803 W503
# These will not be needed when we use numpydoc
baseMeasurement.py E266
forcedMeasurement.py E266
forcedPhotCcd.py E266
forcedPhotCoadd.py E266
forcedPhotImage.py E266
noiseReplacer.py E266
sfm.py E266
17 changes: 15 additions & 2 deletions tests/test_ApCorrNameSet.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,19 @@
import lsst.meas.base


class MinimalistTestPlugin(lsst.meas.base.SingleFramePlugin):
"""This class is used below to test registration. It is set up as the
minimal class that still has a valid implementation. Whilst these
methods are not needed in this test file, the class registered here
might appear in other tests that scan the registry."""
@classmethod
def getExecutionOrder(cls):
return cls.CENTROID_ORDER

def fail(self, measRecord, error=None):
pass


class ApCorrNameTestCase(lsst.utils.tests.TestCase):

def testDefaultNames(self):
Expand Down Expand Up @@ -60,11 +73,11 @@ def testCopy(self):
def testRegisterDecorator(self):
"""Test the shouldApCorr argument of the register decorator for measurement plugins."""
@lsst.meas.base.register("test_ApCorrPlugin", shouldApCorr=True)
class ApCorrPlugin(lsst.meas.base.SingleFramePlugin):
class ApCorrPlugin(MinimalistTestPlugin):
pass

@lsst.meas.base.register("test_NonApCorrPlugin")
class NonApCorrPlugin(lsst.meas.base.SingleFramePlugin):
class NonApCorrPlugin(MinimalistTestPlugin):
pass

apCorrSet = lsst.meas.base.getApCorrNameSet()
Expand Down
4 changes: 2 additions & 2 deletions tests/test_ApertureFlux.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def testNaive(self):
position)
area = self.computeNaiveArea(position, radius)
# test that this isn't the same as the sinc flux
self.assertNotClose(
self.assertFloatsNotEqual(
Copy link
Contributor

Choose a reason for hiding this comment

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

Wasn't immediately clear why you did this — I had to go and look up the definition of assertNotClose to find out that it's deprecated. Maybe worth noting that in the commit message?

ApertureFluxAlgorithm.computeSincFlux(self.exposure.getMaskedImage().getImage(),
ellipse, self.ctrl).flux, area)

Expand Down Expand Up @@ -112,7 +112,7 @@ def testSinc(self):
position)
area = ellipse.getCore().getArea()
# test that this isn't the same as the naive flux
self.assertNotClose(
self.assertFloatsNotEqual(
ApertureFluxAlgorithm.computeNaiveFlux(self.exposure.getMaskedImage().getImage(),
ellipse, self.ctrl).flux, area)

Expand Down
25 changes: 13 additions & 12 deletions tests/test_PluginLogs.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,29 +22,27 @@
#

from __future__ import absolute_import, division, print_function
from builtins import object
from contextlib import contextmanager
import unittest
import os
import numpy
import lsst.afw.table
import lsst.daf.base
import lsst.meas.base
import lsst.utils.tests
from lsst.meas.base import (SingleFrameMeasurementTask, SingleFrameMeasurementConfig)
from lsst.meas.base.tests import (AlgorithmTestCase, )
from lsst.meas.base.apCorrRegistry import addApCorrName
from lsst.meas.base.sfm import SingleFramePluginConfig, SingleFramePlugin
from lsst.meas.base.forcedMeasurement import ForcedPlugin
from lsst.meas.base.pluginRegistry import register
from lsst.meas.base import FlagDefinitionList, FlagHandler, MeasurementError


class LoggingPluginConfig(SingleFramePluginConfig):
"""
Configuration for Sample Plugin with a FlagHandler.
"""
pass


@register("test_LoggingPlugin")
class LoggingPlugin(SingleFramePlugin):
"""
Expand Down Expand Up @@ -98,10 +96,11 @@ def fail(self, measRecord, error=None):
else:
self.flagHandler.handleFailure(measRecord, error.cpp)

# Direct the log given to a file, or to the console if no file is specified

def directLog(log, file=None):
"""Direct the log given to a file, or to the console if no file is specified"""
props = "log4j.rootLogger=INFO, FA\n"
if file == None:
if file is None:
props += "log4j.appender.FA=ConsoleAppender\n"
else:
props += "log4j.appender.FA=FileAppender\n"
Expand All @@ -113,6 +112,7 @@ def directLog(log, file=None):
props += "log4j.logger.main.a=DEBUG\n"
log.configure_prop(props)


class RegisteredPluginsTestCase(AlgorithmTestCase, lsst.utils.tests.TestCase):
"""
Test all the registered Plugins to see if their logName is set as expected.
Expand All @@ -124,7 +124,7 @@ class RegisteredPluginsTestCase(AlgorithmTestCase, lsst.utils.tests.TestCase):
def testSingleFramePlugins(self):
center = lsst.afw.geom.Point2D(50, 50)
bbox = lsst.afw.geom.Box2I(lsst.afw.geom.Point2I(0, 0),
lsst.afw.geom.Extent2I(100,100))
lsst.afw.geom.Extent2I(100, 100))
dataset = lsst.meas.base.tests.TestDataset(bbox)
dataset.addSource(1000000.0, center)
registry = SingleFramePlugin.registry
Expand All @@ -147,7 +147,7 @@ def testForcedPlugins(self):
# Test all the ForcedPlugins registered to see if their logName is set as expected.
center = lsst.afw.geom.Point2D(50, 50)
bbox = lsst.afw.geom.Box2I(lsst.afw.geom.Point2I(0, 0),
lsst.afw.geom.Extent2I(100,100))
lsst.afw.geom.Extent2I(100, 100))
dataset = lsst.meas.base.tests.TestDataset(bbox)
dataset.addSource(1000000.0, center)
registry = ForcedPlugin.registry
Expand Down Expand Up @@ -208,7 +208,6 @@ def testLoggingPythonPlugin(self):
pluginLogName = os.path.join(lsst.utils.getPackageDir('meas_base'), 'tests', 'plugin.log')
directLog(log, pluginLogName)
exposure, cat = self.dataset.realize(noise=0.0, schema=schema)
source = cat[0]
task.run(cat, exposure)
directLog(log, None)
# direct back to console, closing log files
Expand All @@ -235,7 +234,6 @@ def testLoggingCppPlugin(self):
pluginLogName = os.path.join(lsst.utils.getPackageDir('meas_base'), 'tests', 'plugin.log')
directLog(log, pluginLogName)
exposure, cat = self.dataset.realize(noise=0.0, schema=schema)
source = cat[0]
exposure.setPsf(None)
# This call throws an error, so be prepared for it
try:
Expand All @@ -251,13 +249,14 @@ def testLoggingCppPlugin(self):
# test that the sample plugin has correctly logged to where we expected it to.
self.assertTrue(lines.find("ERROR") >= 0)


class SingleFrameTestCase(AlgorithmTestCase, lsst.utils.tests.TestCase):

def setUp(self):
# object in corner to trigger EDGE error
self.center = lsst.afw.geom.Point2D(5, 5)
self.bbox = lsst.afw.geom.Box2I(lsst.afw.geom.Point2I(0, 0),
lsst.afw.geom.Extent2I(100,100))
lsst.afw.geom.Extent2I(100, 100))
self.dataset = lsst.meas.base.tests.TestDataset(self.bbox)
self.dataset.addSource(1000000.0, self.center)
self.task = self.makeSingleFrameMeasurementTask("base_SdssCentroid")
Expand Down Expand Up @@ -311,13 +310,14 @@ def testSetPluginLevel(self):
os.unlink(pluginLogName)
self.assertTrue(lines.find("MeasurementError") < 0)


class ForcedTestCase(AlgorithmTestCase, lsst.utils.tests.TestCase):

def setUp(self):
# object in corner to trigger EDGE error
self.center = lsst.afw.geom.Point2D(0, 0)
self.bbox = lsst.afw.geom.Box2I(lsst.afw.geom.Point2I(0, 0),
lsst.afw.geom.Extent2I(100,100))
lsst.afw.geom.Extent2I(100, 100))
self.dataset = lsst.meas.base.tests.TestDataset(self.bbox)
self.dataset.addSource(1000000.0, self.center)
self.task = self.makeForcedMeasurementTask("base_SdssCentroid")
Expand Down Expand Up @@ -379,6 +379,7 @@ def testSetPluginLevel(self):
os.unlink(pluginLogName)
self.assertTrue(lines.find("MeasurementError") < 0)


class TestMemory(lsst.utils.tests.MemoryTestCase):
pass

Expand Down