-
Notifications
You must be signed in to change notification settings - Fork 18
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
Review for DM-4831: Add bright object masks to pipeline outputs #39
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e866fdf
Added support for ds9 region files to specify object masks
natelust a6db3fb
Set mask plane BRIGHT_OBJECT as per HSC-1342
RobertLuptonTheGood b5b8e26
AssembleCoaddTask: move bright object read into method
PaulPrice ea46580
AssembleCoaddTask: log bright object mask application
PaulPrice df27754
Fix proper reading of coordinate with functor
natelust File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
import re | ||
import lsst.daf.base as dafBase | ||
import lsst.pex.logging as pexLog | ||
import lsst.afw.coord as afwCoord | ||
import lsst.afw.geom as afwGeom | ||
import lsst.afw.table as afwTable | ||
|
||
class ObjectMaskCatalog(object): | ||
"""Class to support bright object masks | ||
|
||
N.b. I/O is done by providing a readFits method which fools the butler. | ||
""" | ||
def __init__(self): | ||
schema = afwTable.SimpleTable.makeMinimalSchema() | ||
schema.addField("radius", "Angle", "radius of mask") | ||
|
||
self._catalog = afwTable.SimpleCatalog(schema) | ||
self._catalog.table.setMetadata(dafBase.PropertyList()) | ||
|
||
self.table = self._catalog.table | ||
self.addNew = self._catalog.addNew | ||
|
||
def __len__(self): | ||
return len(self._catalog) | ||
|
||
def __iter__(self): | ||
return iter(self._catalog) | ||
|
||
def __getitem__(self, i): | ||
return self._catalog.__getitem__(i) | ||
|
||
def __setitem__(self, i, v): | ||
return self._catalog.__setitem__(i, v) | ||
|
||
@staticmethod | ||
def readFits(fileName, hdu=0, flags=0): | ||
"""Read a ds9 region file, returning a ObjectMaskCatalog object | ||
|
||
This method is called "readFits" to fool the butler. The corresponding mapper entry looks like | ||
brightObjectMask: { | ||
template: "deepCoadd/BrightObjectMasks/%(tract)d/BrightObjectMask-%(tract)d-%(patch)s-%(filter)s.reg" | ||
python: "lsst.obs.subaru.objectMasks.ObjectMaskCatalog" | ||
persistable: "PurePythonClass" | ||
storage: "FitsCatalogStorage" | ||
} | ||
and this is the only way I know to get it to read a random file type, in this case a ds9 region file | ||
|
||
This method expects to find files named as BrightObjectMask-%(tract)d-%(patch)s-%(filter)s.reg | ||
The files should be structured as follows: | ||
|
||
# Description of catalogue as a comment | ||
# CATALOG: catalog-id-string | ||
# TRACT: 0 | ||
# PATCH: 5,4 | ||
# FILTER: HSC-I | ||
|
||
wcs; fk5 | ||
|
||
circle(RA, DEC, RADIUS) # ID: 1 | ||
|
||
The commented lines must be present, with the relevant fields such as tract patch and filter filled | ||
in. The coordinate system must be listed as above. Each patch is specified as a circle, with an RA, | ||
DEC, and Radius specified in decimal degrees. Only circles are supported as region definitions | ||
currently. | ||
""" | ||
|
||
log = pexLog.getDefaultLog().createChildLog("ObjectMaskCatalog") | ||
|
||
brightObjects = ObjectMaskCatalog() | ||
checkedWcsIsFk5 = False | ||
|
||
with open(fileName) as fd: | ||
for lineNo, line in enumerate(fd.readlines(), 1): | ||
line = line.rstrip() | ||
|
||
if re.search(r"^\s*#", line): | ||
# | ||
# Parse any line of the form "# key : value" and put them into the metadata. | ||
# | ||
# The medatdata values must be defined as outlined in the above docstring | ||
# | ||
# The value of these three keys will be checked, | ||
# so get them right! | ||
# | ||
mat = re.search(r"^\s*#\s*([a-zA-Z][a-zA-Z0-9_]+)\s*:\s*(.*)", line) | ||
if mat: | ||
key, value = mat.group(1).lower(), mat.group(2) | ||
if key == "tract": | ||
value = int(value) | ||
|
||
brightObjects.table.getMetadata().set(key, value) | ||
|
||
line = re.sub(r"^\s*#.*", "", line) | ||
if not line: | ||
continue | ||
|
||
if re.search(r"^\s*wcs\s*;\s*fk5\s*$", line, re.IGNORECASE): | ||
checkedWcsIsFk5 = True | ||
continue | ||
|
||
# This regular expression parses the regions file for each region to be masked, | ||
# with the format as specified in the above docstring. | ||
mat = re.search(r"^\s*circle(?:\s+|\s*\(\s*)" | ||
"(\d+(?:\.\d*)([d]*))" "(?:\s+|\s*,\s*)" | ||
"([+-]?\d+(?:\.\d*)([d]*))" "(?:\s+|\s*,\s*)" | ||
"(\d+(?:\.\d*))([d'\"]*)" "(?:\s*|\s*\)\s*)" | ||
"\s*#\s*ID:\s*(\d+)" "\s*$" | ||
, line) | ||
if mat: | ||
ra, raUnit, dec, decUnit, radius, radiusUnit, _id = mat.groups() | ||
|
||
_id = int(_id) | ||
ra = convertToAngle(ra, raUnit, "ra", fileName, lineNo) | ||
dec = convertToAngle(dec, decUnit, "dec", fileName, lineNo) | ||
radius = convertToAngle(radius, radiusUnit, "radius", fileName, lineNo) | ||
|
||
rec = brightObjects.addNew() | ||
# N.b. rec["coord"] = Coord is not supported, so we have to use the setter | ||
rec["id"] = _id | ||
rec.setCoord(afwCoord.Fk5Coord(ra, dec)) | ||
rec["radius"] = radius | ||
else: | ||
log.warn("Unexpected line \"%s\" at %s:%d" % (line, fileName, lineNo)) | ||
|
||
if not checkedWcsIsFk5: | ||
raise RuntimeError("Expected to see a line specifying an fk5 wcs") | ||
|
||
# This makes the deep copy contiguous in memory so that a ColumnView can be exposed to Numpy | ||
brightObjects._catalog = brightObjects._catalog.copy(True) | ||
|
||
return brightObjects | ||
|
||
|
||
def convertToAngle(var, varUnit, what, fileName, lineNo): | ||
"""Given a variable and its units, return an afwGeom.Angle | ||
|
||
what, fileName, and lineNo are used to generate helpful error messages | ||
""" | ||
var = float(var) | ||
|
||
if varUnit in ("d", ""): | ||
pass | ||
elif varUnit == "'": | ||
var /= 60.0 | ||
elif varUnit == '"': | ||
var /= 3600.0 | ||
else: | ||
raise RuntimeError("unsupported unit \"%s\" for %s at %s:%d" % | ||
(varUnit, what, fileName, lineNo)) | ||
|
||
return var*afwGeom.degrees |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The whitespace churn is gratuitous (& I'm not even sure it's an improvement).