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-23008: Add a script to define DCR subfilters in a Gen 3 registry. #338

Merged
merged 1 commit into from
Jan 31, 2020
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
53 changes: 53 additions & 0 deletions bin.src/makeGen3DcrSubfilters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#!/usr/bin/env python
# This file is part of pipe_tasks.
#
# Developed for the LSST Data Management System.
# This product includes software developed by the LSST Project
# (https://www.lsst.org).
# See the COPYRIGHT file at the top-level directory of this distribution
# for details of code ownership.
#
# 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 GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.

import argparse
import os.path
import sys

from lsst.daf.butler import Butler
from lsst.pipe.tasks.makeGen3DcrSubfilters import MakeGen3DcrSubfiltersConfig, MakeGen3DcrSubfiltersTask

# Build a parser for command line arguments
parser = argparse.ArgumentParser(description="Define the set of subfilters for chromatic modeling.")
parser.add_argument("butler", metavar="Butler", type=str, help="Path to a gen3 butler")
parser.add_argument("-C", "--config-file", dest="configFile",
help="Path to a config file overrides file")

args = parser.parse_args()

# Verify any supplied paths actually exist on disk
if not os.path.exists(args.butler):
print("Butler path specified does not exists")
sys.exit(1)

config = MakeGen3DcrSubfiltersConfig()
if args.configFile:
if not os.path.exists(args.configFile):
print("Path to config file specified does not exist")
sys.exit(1)
config.load(args.configFile)

# Construct the SkyMap Creation task and run it
subfilterTask = MakeGen3DcrSubfiltersTask(config=config)
butler = Butler(args.butler, run=args.collection)
subfilterTask.run(butler)
93 changes: 93 additions & 0 deletions python/lsst/pipe/tasks/makeGen3DcrSubfilters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
# This file is part of pipe_tasks.
#
# Developed for the LSST Data Management System.
# This product includes software developed by the LSST Project
# (https://www.lsst.org).
# See the COPYRIGHT file at the top-level directory of this distribution
# for details of code ownership.
#
# 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 GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
import lsst.pex.config as pexConfig
import lsst.pipe.base as pipeBase

from sqlalchemy.exc import IntegrityError


class MakeGen3DcrSubfiltersConfig(pexConfig.Config):
"""Config for MakeGen3DcrSubfiltersTask.
"""
numSubfilters = pexConfig.Field(
doc="The number of subfilters to be used for chromatic modeling.",
dtype=int,
default=3,
optional=False
)
filterNames = pexConfig.ListField(
doc="The filters to add chromatic subfilters to in the registry.",
dtype=str,
default=["g"],
optional=False
)


class MakeGen3DcrSubfiltersTask(pipeBase.Task):
Copy link
Contributor

Choose a reason for hiding this comment

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

Will you be running this command multiple times for multiple filters to add subbands for them all? Then I think it might be worth it to use ListFields in the config and do them all at once here. At the very least it gives you the option to do it all at once, and still have a single entry if that is all you need.

ConfigClass = MakeGen3DcrSubfiltersConfig
_DefaultName = "makeGen3DcrSubfilters"

"""This is a task to construct the set of subfilters for chromatic modeling.

Parameters
----------
config : `MakeGen3DcrSubfiltersConfig` or None
Instance of a configuration class specifying task options, a default
config is created if value is None
"""

def __init__(self, *, config=None, **kwargs):
super().__init__(config=config, **kwargs)
Copy link
Contributor

Choose a reason for hiding this comment

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

Do you need this init if you are doing nothing?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm afraid I was just following what you did in MakeGen3SkyMapTask.
I assumed there was a reason we needed to strip positional arguments from __init__, but if that's not needed I'm happy to remove this.

Copy link
Contributor

Choose a reason for hiding this comment

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

It may have been put in as a safeguard since Task takes config as a positional argument, and this ensures that happens correctly.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK. I think I will leave it in, then, but I'll add a comment.


def run(self, butler):
"""Construct a set of subfilters for chromatic modeling.

Parameters
----------
butler : `lsst.daf.butler.Butler`
Butler repository to add the subfilter definitions to.
"""
with butler.registry.transaction():
try:
self.register(butler.registry)
except IntegrityError as err:
raise RuntimeError(f"Subfilters for at least one filter of {self.config.filterNames} "
"are already defined.") from err

def register(self, registry):
"""Add Subfilters to the given registry.

Parameters
----------
registry : `lsst.daf.butler.Registry`
The registry to add to.
"""
record = []
for filterName in self.config.filterNames:
self.log.info(f"Initializing filter {filterName} with "
f"{self.config.numSubfilters} subfilters")
for sub in range(self.config.numSubfilters):
subfilterName = f"{filterName}-{sub + 1}"
record.append({
"abstract_filter": filterName,
"subfilter": subfilterName
})
registry.insertDimensionData("subfilter", *record)