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-35897: Minor gen2 and logging cleanup #230

Merged
merged 3 commits into from
Aug 16, 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
68 changes: 44 additions & 24 deletions bin.src/plot_photoCalib.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,11 @@
original calib(s).
"""

from collections import defaultdict
import numpy as np
import lsst.afw.cameraGeom.utils
import lsst.daf.persistence
import lsst.daf.butler
from lsst.pipe.base import Instrument

import matplotlib
matplotlib.use('Agg')
Expand All @@ -48,25 +50,27 @@
matplotlib.rcParams['figure.dpi'] = 300


def getValidDataIds(butler, tract, dataset_type='jointcal_photoCalib'):
def getValidDataIds(butler, tract, dataset_type='jointcal_photoCalib', collections=...):
"""Return a list of all dataIds that exist in this butler.

This exists here because the butler doesn't provide this functionality.

Parameters
----------
butler : `lsst.daf.persistence.Butler`
butler : `lsst.daf.butler.Butler`
The butler to search within.
tract : `int`
Tract id to include in search.
dataset_type : `str`
Dataset type to search for.
collections : Any
Collections to search for dataIds for this dataset type.
"""
data_ids = []
for data_ref in butler.subset(dataset_type, tract=tract):
if data_ref.datasetExists():
data_ids.append(data_ref.dataId)
return data_ids
# Need to know the skymap. Assume we can get it from the collection.
data_ids = set(butler.registry.queryDataIds(datasets=dataset_type,
dimensions=("tract", "visit", "detector"),
tract=tract))
return list(data_ids)


def colorbar(mappable):
Expand Down Expand Up @@ -99,7 +103,7 @@ def makePhotoCalibImages(visit, butler, step=8, chips=[], tract=None,
----------
visit : `int`
The visit id to plot.
butler : `lsst.daf.persistence.Butler`
butler : `lsst.daf.butler.Butler`
The butler instance to pull the data from.
step : `int`
Step size between samples.
Expand All @@ -121,16 +125,16 @@ def makePhotoCalibImages(visit, butler, step=8, chips=[], tract=None,
if meanCalib:
meanCalibScaling = 0
for ccd in chips:
calib = butler.get('calexp_photoCalib', dataId=dict(visit=int(visit), ccd=int(ccd), tract=tract))
meanCalibScaling += calib.getFluxMag0()[0]
calib = butler.get('calexp.photoCalib', dataId=dict(visit=int(visit), detector=int(ccd)))
meanCalibScaling += calib.getInstFluxAtZeroMagnitude()
meanCalibScaling /= len(chips)
if verbose:
print('calib mean: %s' % meanCalibScaling)
else:
meanCalibScaling = 1

for ccd in chips:
detector = butler.get('calexp_detector', ccd=int(ccd))
detector = butler.get('calexp.detector', detector=int(ccd), visit=int(visit))
bbox = detector.getBBox()

xx = np.linspace(bbox.getMinX(), bbox.getMaxX(), int(np.floor(bbox.getWidth()/step)))
Expand All @@ -139,13 +143,13 @@ def makePhotoCalibImages(visit, butler, step=8, chips=[], tract=None,

calibScaling = 1
if singleCalib:
calib = butler.get('calexp_photoCalib', dataId=dict(visit=int(visit), ccd=int(ccd), tract=tract))
calibScaling = calib.getFluxMag0()[0]
calib = butler.get('calexp.photoCalib', dataId=dict(visit=int(visit), detector=int(ccd),))
calibScaling = calib.getInstFluxAtZeroMagnitude()
if verbose:
print('calib ccd %s: %s' % (ccd, calibScaling))

photoCalib = butler.get('jointcal_photoCalib', dataId=dict(visit=int(visit),
ccd=int(ccd),
detector=int(ccd),
tract=tract))
if verbose:
print("photoCalib mean ccd %s: %s" % (ccd, photoCalib.getCalibrationMean()))
Expand Down Expand Up @@ -179,7 +183,7 @@ def getCcdImage(self, detector, imageFactory, binSize):
return self.images[detector.getId()], detector


def plotVisitPhotoCalib(visit, butler, step=8, percentile=0,
def plotVisitPhotoCalib(visit, butler, instrument, step=8, percentile=0,
tract=None, chips=[],
chipScaling=False, meanCalib=False, singleCalib=False,
colormap="magma", verbose=False):
Expand All @@ -189,8 +193,10 @@ def plotVisitPhotoCalib(visit, butler, step=8, percentile=0,
----------
visit : `int`
The visit id to plot.
butler : `lsst.daf.persistence.Butler`
butler : `lsst.daf.butler.Butler`
The butler instance to pull the data from.
instrument : `str`
Name of the instrument in use.
step : `int`
Step size between samples.
i.e. `np.linspace(0, bbox.getHeight(), bbox.getHeight()/binSize)`
Expand Down Expand Up @@ -231,7 +237,12 @@ def plotVisitPhotoCalib(visit, butler, step=8, percentile=0,
meanCalib=meanCalib,
singleCalib=singleCalib,
verbose=verbose))
image = lsst.afw.cameraGeom.utils.makeImageFromCamera(butler.get('camera'),

# Need the camera geometry. Use the default.
inst = Instrument.fromName(instrument, registry=butler.registry)
camera = inst.getCamera()

image = lsst.afw.cameraGeom.utils.makeImageFromCamera(camera,
imageSource=imageMaker,
detectorNameList=chips,
imageFactory=lsst.afw.image.ImageD,
Expand Down Expand Up @@ -265,6 +276,8 @@ def main():
formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument("repo", metavar="repo",
help="Directory to butler repository containing `jointcal_photoCalib` datasets.")
parser.add_argument("collection", metavar="collection",
help="Butler collection to search for photoCalibs.")
parser.add_argument("--step", default=8, type=int,
help="Steps between samples per image (i.e. larger number = coarser image).")
parser.add_argument("--tract", default=0, type=int,
Expand All @@ -277,27 +290,34 @@ def main():
help="matplotlib.Colormap name to use when generating the 2d plot.")
args = parser.parse_args()

butler = lsst.daf.persistence.Butler(inputs=args.repo)
butler = lsst.daf.butler.Butler(args.repo, collections=args.collection)
dataIds = getValidDataIds(butler, args.tract)
instrument = dataIds[0]['instrument']
visits = np.unique([dataId['visit'] for dataId in dataIds])
chips = np.unique([dataId['ccd'] for dataId in dataIds])
chips = np.unique([dataId['detector'] for dataId in dataIds])
if args.verbose:
print("Found these visits:", visits)
print(f"For instrument {instrument!r} found these visits: {visits}")
print("And these chips:", chips)

# Not all visits have all detectors.
visit_map = defaultdict(set)
for dataId in dataIds:
visit_map[dataId['visit']].add(dataId['detector'])

for visit in visits:
if args.verbose:
print()
print("Processing visit", visit)
plotVisitPhotoCalib(visit, butler, tract=args.tract, chips=chips,
detectors = list(sorted(visit_map[visit]))
plotVisitPhotoCalib(visit, butler, instrument, tract=args.tract, chips=detectors,
step=args.step, percentile=args.percentile,
chipScaling=False,
colormap=args.colormap, verbose=args.verbose)
plotVisitPhotoCalib(visit, butler, tract=args.tract, chips=chips,
plotVisitPhotoCalib(visit, butler, instrument, tract=args.tract, chips=detectors,
step=args.step, percentile=args.percentile,
chipScaling=True, meanCalib=True,
colormap=args.colormap, verbose=args.verbose)
plotVisitPhotoCalib(visit, butler, tract=args.tract, chips=chips,
plotVisitPhotoCalib(visit, butler, instrument, tract=args.tract, chips=detectors,
step=args.step, percentile=args.percentile,
chipScaling=True, singleCalib=True,
colormap=args.colormap, verbose=args.verbose)
Expand Down
4 changes: 2 additions & 2 deletions python/lsst/jointcal/cameraGeometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@
"""
__all__ = ["CameraModel"]

import logging
import numpy as np

from lsst.afw import cameraGeom
import lsst.afw.geom
import astshim as ast
import lsst.log
from lsst.geom import SpherePoint, Point2D, radians

_LOG = lsst.log.Log.getLogger(__name__)
_LOG = logging.getLogger(__name__)


class CameraModel:
Expand Down
54 changes: 3 additions & 51 deletions python/lsst/jointcal/jointcal.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import dataclasses
import collections
import os
import logging

import astropy.time
import numpy as np
Expand All @@ -32,10 +33,8 @@
import lsst.pex.config as pexConfig
import lsst.pipe.base as pipeBase
from lsst.afw.image import fluxErrFromABMagErr
import lsst.pex.exceptions as pexExceptions
import lsst.afw.cameraGeom
import lsst.afw.table
import lsst.log
from lsst.pipe.base import Instrument
from lsst.pipe.tasks.colorterms import ColortermLibrary
from lsst.verify import Job, Measurement
Expand Down Expand Up @@ -503,7 +502,7 @@ def validate(self):
if self.doAstrometry and not self.doPhotometry and self.applyColorTerms:
msg = ("Only doing astrometry, but Colorterms are not applied for astrometry;"
"applyColorTerms=True will be ignored.")
lsst.log.warning(msg)
logging.getLogger("lsst.jointcal").warning(msg)

def setDefaults(self):
# Use only stars because aperture fluxes of galaxies are biased and depend on seeing.
Expand Down Expand Up @@ -1529,53 +1528,6 @@ def _make_output(self, ccdImageList, model, func):
output[(visit, ccd)] = getattr(model, func)(ccdImage)
return output

def _write_astrometry_results(self, associations, model, visit_ccd_to_dataRef):
"""
Write the fitted astrometric results to a new 'jointcal_wcs' dataRef.

Parameters
----------
associations : `lsst.jointcal.Associations`
The star/reference star associations to fit.
model : `lsst.jointcal.AstrometryModel`
The astrometric model that was fit.
visit_ccd_to_dataRef : `dict` of Key: `lsst.daf.persistence.ButlerDataRef`
Dict of ccdImage identifiers to dataRefs that were fit.
"""
ccdImageList = associations.getCcdImageList()
output = self._make_output(ccdImageList, model, "makeSkyWcs")
for key, skyWcs in output.items():
dataRef = visit_ccd_to_dataRef[key]
try:
dataRef.put(skyWcs, 'jointcal_wcs')
except pexExceptions.Exception as e:
self.log.fatal('Failed to write updated Wcs: %s', str(e))
raise e

def _write_photometry_results(self, associations, model, visit_ccd_to_dataRef):
"""
Write the fitted photometric results to a new 'jointcal_photoCalib' dataRef.

Parameters
----------
associations : `lsst.jointcal.Associations`
The star/reference star associations to fit.
model : `lsst.jointcal.PhotometryModel`
The photoometric model that was fit.
visit_ccd_to_dataRef : `dict` of Key: `lsst.daf.persistence.ButlerDataRef`
Dict of ccdImage identifiers to dataRefs that were fit.
"""

ccdImageList = associations.getCcdImageList()
output = self._make_output(ccdImageList, model, "toPhotoCalib")
for key, photoCalib in output.items():
dataRef = visit_ccd_to_dataRef[key]
try:
dataRef.put(photoCalib, 'jointcal_photoCalib')
except pexExceptions.Exception as e:
self.log.fatal('Failed to write updated PhotoCalib: %s', str(e))
raise e


def make_schema_table():
"""Return an afw SourceTable to use as a base for creating the
Expand Down Expand Up @@ -1674,7 +1626,7 @@ def extract_detector_catalog_from_visit_catalog(table, visitCatalog, detectorId,
Names of the ixx/iyy/ixy columns in the catalog.
sourceFluxType : `str`
Name of the catalog field to load instFluxes from.
log : `lsst.log.Log`
log : `logging.Logger`
Logging instance to log to.

Returns
Expand Down