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-34208: Use getTraceLogger and remove lsstLog from python #213

Merged
merged 2 commits into from
Mar 28, 2022
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
6 changes: 3 additions & 3 deletions examples/compareLambdaTypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.

import logging
import os
import sys
import unittest
Expand All @@ -33,13 +34,12 @@
import lsst.afw.math as afwMath
import lsst.ip.diffim as ipDiffim
import lsst.ip.diffim.diffimTools as diffimTools
from lsst.log import Log
import lsst.utils.logging as logUtils
import lsst.pex.config as pexConfig

logUtils.trace_set_at("lsst.ip.diffim", 6)
logger = Log.getLogger("lsst.ip.diffim.compareLambdaTypes")
logger.setLevel(Log.DEBUG)
logger = logUtils.getLogger("lsst.ip.diffim.compareLambdaTypes")
logger.setLevel(logging.DEBUG)

display = True
writefits = False
Expand Down
3 changes: 1 addition & 2 deletions examples/jackknifeResampleSpatialKernel.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import lsst.afw.math as afwMath
import lsst.ip.diffim as ipDiffim
import lsst.ip.diffim.diffimTools as diffimTools
from lsst.log import Log
import lsst.utils.logging as logUtils
import lsst.pex.config as pexConfig

Expand All @@ -41,7 +40,7 @@
verbosity = 2
logUtils.trace_set_at("lsst.ip.diffim", verbosity)

logger = Log.getLogger("lsst.ip.diffim.JackknifeResampleKernel")
logger = logUtils.getLogger("lsst.ip.diffim.JackknifeResampleKernel")

display = False
writefits = False
Expand Down
4 changes: 2 additions & 2 deletions python/lsst/ip/diffim/diffimTools.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@
import lsst.afw.detection as afwDetect
import lsst.afw.math as afwMath
import lsst.geom as geom
from lsst.log import Log
import lsst.pex.config as pexConfig
from lsst.utils.logging import getLogger
from .makeKernelBasisList import makeKernelBasisList

# Helper functions for ipDiffim; mostly viewing of results and writing
Expand Down Expand Up @@ -323,7 +323,7 @@ def backgroundSubtract(config, maskedImages):
del backobj

t1 = time.time()
logger = Log.getLogger("lsst.ip.diffim.backgroundSubtract")
logger = getLogger("lsst.ip.diffim.backgroundSubtract")
logger.debug("Total time for background subtraction : %.2f s", (t1 - t0))
return backgrounds

Expand Down
4 changes: 2 additions & 2 deletions python/lsst/ip/diffim/dipoleMeasurement.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@
import lsst.afw.image as afwImage
import lsst.geom as geom
import lsst.pex.config as pexConfig
from lsst.log import Log
import lsst.meas.deblender.baseline as deblendBaseline
from lsst.meas.base.pluginRegistry import register
from lsst.meas.base import SingleFrameMeasurementTask, SingleFrameMeasurementConfig, \
SingleFramePluginConfig, SingleFramePlugin
import lsst.afw.display as afwDisplay
from lsst.utils.logging import getLogger

__all__ = ("DipoleMeasurementConfig", "DipoleMeasurementTask", "DipoleAnalysis", "DipoleDeblender",
"SourceFlagChecker", "ClassificationDipoleConfig", "ClassificationDipolePlugin")
Expand Down Expand Up @@ -514,7 +514,7 @@ def __init__(self):

# Always deblend as Psf
self.psfChisqCut1 = self.psfChisqCut2 = self.psfChisqCut2b = np.inf
self.log = Log.getLogger('lsst.ip.diffim.DipoleDeblender')
self.log = getLogger('lsst.ip.diffim.DipoleDeblender')
self.sigma2fwhm = 2. * np.sqrt(2. * np.log(2.))

def __call__(self, source, exposure):
Expand Down
3 changes: 1 addition & 2 deletions python/lsst/ip/diffim/imageDecorrelation.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import lsst.afw.image as afwImage
import lsst.afw.math as afwMath
import lsst.geom as geom
import lsst.log
import lsst.meas.algorithms as measAlg
import lsst.pex.config as pexConfig
import lsst.pipe.base as pipeBase
Expand Down Expand Up @@ -739,7 +738,7 @@ def run(self, subExposure, expandedSubExposure, fullBBox,

# Prevent too much log INFO verbosity from DecorrelateALKernelTask.run
logLevel = self.log.getLevel()
self.log.setLevel(lsst.log.WARN)
self.log.setLevel(self.log.WARNING)
res = DecorrelateALKernelTask.run(self, subExp2, subExp1, expandedSubExposure,
psfMatchingKernel, preConvKernel, **kwargs)
self.log.setLevel(logLevel) # reset the log level
Expand Down
5 changes: 3 additions & 2 deletions python/lsst/ip/diffim/makeKernelBasisList.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@
__all__ = ["makeKernelBasisList", "generateAlardLuptonBasisList"]

from . import diffimLib
from lsst.log import Log
import numpy as np

from lsst.utils.logging import getLogger

sigma2fwhm = 2. * np.sqrt(2. * np.log(2.))


Expand Down Expand Up @@ -186,7 +187,7 @@ def generateAlardLuptonBasisList(config, targetFwhmPix=None, referenceFwhmPix=No
if (kernelSize % 2) != 1:
raise ValueError("Only odd-sized Alard-Lupton bases allowed")

logger = Log.getLogger("lsst.ip.diffim.generateAlardLuptonBasisList")
logger = getLogger("lsst.ip.diffim.generateAlardLuptonBasisList")
if (targetFwhmPix is None) or (referenceFwhmPix is None) or (not config.scaleByFwhm):
logger.info("PSF sigmas are not available or scaling by fwhm disabled, "
"falling back to config values")
Expand Down
5 changes: 2 additions & 3 deletions python/lsst/ip/diffim/modelPsfMatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
import lsst.afw.image as afwImage
import lsst.afw.math as afwMath
import lsst.geom as geom
import lsst.log as log
import lsst.pex.config as pexConfig
import lsst.pipe.base as pipeBase
from lsst.utils.logging import getTraceLogger
from lsst.utils.timer import timeMethod
from .makeKernelBasisList import makeKernelBasisList
from .psfMatch import PsfMatchTask, PsfMatchConfigAL
Expand Down Expand Up @@ -495,8 +495,7 @@ def _buildCellSet(self, exposure, referencePsfModel):
# place at center of cell
posX = sizeCellX*col + sizeCellX//2 + scienceX0

log.log("TRACE4." + self.log.name, log.DEBUG,
"Creating Psf candidate at %.1f %.1f", posX, posY)
getTraceLogger(self.log, 4).debug("Creating Psf candidate at %.1f %.1f", posX, posY)

# reference kernel image, at location of science subimage
referenceMI = self._makePsfMaskedImage(referencePsfModel, posX, posY, dimensions=dimenR)
Expand Down
57 changes: 28 additions & 29 deletions python/lsst/ip/diffim/psfMatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@
import lsst.pex.config as pexConfig
import lsst.afw.math as afwMath
import lsst.afw.display as afwDisplay
import lsst.log as log
import lsst.pipe.base as pipeBase
from lsst.meas.algorithms import SubtractBackgroundConfig
from lsst.utils.logging import getTraceLogger
from lsst.utils.timer import timeMethod
from . import utils as diutils
from . import diffimLib
Expand Down Expand Up @@ -845,9 +845,9 @@ def _createPcaBasis(self, kernelCellSet, nStarPerCell, ps):
eSum = np.sum(eigenValues)
if eSum == 0.0:
raise RuntimeError("Eigenvalues sum to zero")
trace_logger = getTraceLogger(self.log.getChild("_solve"), 5)
for j in range(len(eigenValues)):
log.log("TRACE5." + self.log.name + "._solve", log.DEBUG,
"Eigenvalue %d : %f (%f)", j, eigenValues[j], eigenValues[j]/eSum)
trace_logger.debug("Eigenvalue %d : %f (%f)", j, eigenValues[j], eigenValues[j]/eSum)

nToUse = min(nComponents, len(eigenValues))
trimBasisList = []
Expand Down Expand Up @@ -921,6 +921,7 @@ def _solve(self, kernelCellSet, basisList, returnOnExcept=False):
ksv = diffimLib.KernelSumVisitorF(ps)

# Main loop
trace_loggers = [getTraceLogger(self.log.getChild("_solve"), i) for i in range(5)]
t0 = time.time()
try:
totalIterations = 0
Expand All @@ -930,13 +931,13 @@ def _solve(self, kernelCellSet, basisList, returnOnExcept=False):
# Make sure there are no uninitialized candidates as active occupants of Cell
nRejectedSkf = -1
while (nRejectedSkf != 0):
log.log("TRACE1." + self.log.name + "._solve", log.DEBUG,
"Building single kernels...")
trace_loggers[1].debug("Building single kernels...")
kernelCellSet.visitCandidates(singlekv, nStarPerCell)
nRejectedSkf = singlekv.getNRejected()
log.log("TRACE1." + self.log.name + "._solve", log.DEBUG,
"Iteration %d, rejected %d candidates due to initial kernel fit",
thisIteration, nRejectedSkf)
trace_loggers[1].debug(
"Iteration %d, rejected %d candidates due to initial kernel fit",
thisIteration, nRejectedSkf
)

# Reject outliers in kernel sum
ksv.resetKernelSum()
Expand All @@ -947,9 +948,10 @@ def _solve(self, kernelCellSet, basisList, returnOnExcept=False):
kernelCellSet.visitCandidates(ksv, nStarPerCell)

nRejectedKsum = ksv.getNRejected()
log.log("TRACE1." + self.log.name + "._solve", log.DEBUG,
"Iteration %d, rejected %d candidates due to kernel sum",
thisIteration, nRejectedKsum)
trace_loggers[1].debug(
"Iteration %d, rejected %d candidates due to kernel sum",
thisIteration, nRejectedKsum
)

# Do we jump back to the top without incrementing thisIteration?
if nRejectedKsum > 0:
Expand All @@ -962,13 +964,13 @@ def _solve(self, kernelCellSet, basisList, returnOnExcept=False):
# the spatial fit to these kernels

if (usePcaForSpatialKernel):
log.log("TRACE0." + self.log.name + "._solve", log.DEBUG,
"Building Pca basis")
trace_loggers[0].debug("Building Pca basis")

nRejectedPca, spatialBasisList = self._createPcaBasis(kernelCellSet, nStarPerCell, ps)
log.log("TRACE1." + self.log.name + "._solve", log.DEBUG,
"Iteration %d, rejected %d candidates due to Pca kernel fit",
thisIteration, nRejectedPca)
trace_loggers[1].debug(
"Iteration %d, rejected %d candidates due to Pca kernel fit",
thisIteration, nRejectedPca
)

# We don't want to continue on (yet) with the
# spatial modeling, because we have bad objects
Expand All @@ -990,20 +992,19 @@ def _solve(self, kernelCellSet, basisList, returnOnExcept=False):
spatialkv = diffimLib.BuildSpatialKernelVisitorF(spatialBasisList, regionBBox, ps)
kernelCellSet.visitCandidates(spatialkv, nStarPerCell)
spatialkv.solveLinearEquation()
log.log("TRACE2." + self.log.name + "._solve", log.DEBUG,
"Spatial kernel built with %d candidates", spatialkv.getNCandidates())
trace_loggers[2].debug("Spatial kernel built with %d candidates", spatialkv.getNCandidates())
spatialKernel, spatialBackground = spatialkv.getSolutionPair()

# Check the quality of the spatial fit (look at residuals)
assesskv = diffimLib.AssessSpatialKernelVisitorF(spatialKernel, spatialBackground, ps)
kernelCellSet.visitCandidates(assesskv, nStarPerCell)
nRejectedSpatial = assesskv.getNRejected()
nGoodSpatial = assesskv.getNGood()
log.log("TRACE1." + self.log.name + "._solve", log.DEBUG,
"Iteration %d, rejected %d candidates due to spatial kernel fit",
thisIteration, nRejectedSpatial)
log.log("TRACE1." + self.log.name + "._solve", log.DEBUG,
"%d candidates used in fit", nGoodSpatial)
trace_loggers[1].debug(
"Iteration %d, rejected %d candidates due to spatial kernel fit",
thisIteration, nRejectedSpatial
)
trace_loggers[1].debug("%d candidates used in fit", nGoodSpatial)

# If only nGoodSpatial == 0, might be other candidates in the cells
if nGoodSpatial == 0 and nRejectedSpatial == 0:
Expand All @@ -1018,28 +1019,26 @@ def _solve(self, kernelCellSet, basisList, returnOnExcept=False):

# Final fit if above did not converge
if (nRejectedSpatial > 0) and (thisIteration == maxSpatialIterations):
log.log("TRACE1." + self.log.name + "._solve", log.DEBUG, "Final spatial fit")
trace_loggers[1].debug("Final spatial fit")
if (usePcaForSpatialKernel):
nRejectedPca, spatialBasisList = self._createPcaBasis(kernelCellSet, nStarPerCell, ps)
regionBBox = kernelCellSet.getBBox()
spatialkv = diffimLib.BuildSpatialKernelVisitorF(spatialBasisList, regionBBox, ps)
kernelCellSet.visitCandidates(spatialkv, nStarPerCell)
spatialkv.solveLinearEquation()
log.log("TRACE2." + self.log.name + "._solve", log.DEBUG,
"Spatial kernel built with %d candidates", spatialkv.getNCandidates())
trace_loggers[2].debug("Spatial kernel built with %d candidates", spatialkv.getNCandidates())
spatialKernel, spatialBackground = spatialkv.getSolutionPair()

spatialSolution = spatialkv.getKernelSolution()

except Exception as e:
self.log.error("ERROR: Unable to calculate psf matching kernel")

log.log("TRACE1." + self.log.name + "._solve", log.DEBUG, "%s", e)
trace_loggers[1].debug("%s", e)
raise e

t1 = time.time()
log.log("TRACE0." + self.log.name + "._solve", log.DEBUG,
"Total time to compute the spatial kernel : %.2f s", (t1 - t0))
trace_loggers[0].debug("Total time to compute the spatial kernel : %.2f s", (t1 - t0))

if display:
self._displayDebug(kernelCellSet, spatialKernel, spatialBackground)
Expand Down
5 changes: 3 additions & 2 deletions python/lsst/ip/diffim/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@
import lsst.afw.image as afwImage
import lsst.afw.math as afwMath
import lsst.afw.table as afwTable
from lsst.log import Log
import lsst.meas.algorithms as measAlg
import lsst.meas.base as measBase
from lsst.utils.logging import getLogger
from .dipoleFitTask import DipoleFitAlgorithm
from . import diffimLib
from . import diffimTools
Expand Down Expand Up @@ -724,7 +724,8 @@ def plotPixelResiduals(exposure, warpedTemplateExposure, diffExposure, kernelCel
allResids = fullIm[sidx]/np.sqrt(fullVar[sidx])

testFootprints = diffimTools.sourceToFootprintList(testSources, warpedTemplateExposure,
exposure, config, Log.getDefaultLogger())
exposure, config,
getLogger(__name__).getChild("plotPixelResiduals"))
for fp in testFootprints:
subexp = diffExposure.Factory(diffExposure, fp["footprint"].getBBox())
subim = subexp.getMaskedImage().getImage()
Expand Down
4 changes: 2 additions & 2 deletions tests/test_subtractExposures.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import logging
import os
import unittest

Expand All @@ -7,15 +8,14 @@
import lsst.afw.image as afwImage
import lsst.afw.math as afwMath
import lsst.geom as geom
from lsst.log import Log
import lsst.utils.logging as logUtils
import lsst.meas.algorithms as measAlg
import lsst.ip.diffim as ipDiffim
import lsst.ip.diffim.diffimTools as diffimTools

verbosity = 4
logUtils.trace_set_at("lsst.ip.diffim", verbosity)
Log.getLogger('psfMatch').setLevel(Log.INFO)
logUtils.getLogger('lsst.psfMatch').setLevel(logging.INFO)

display = False

Expand Down