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-25786: Add YAML support for Regions and Pixelization #24
Conversation
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.
Looks good, minor comments only.
python/lsst/sphgeom/_yaml.py
Outdated
# Register all the regions classes with the same constructor and representer | ||
for region_class in (Region, ConvexPolygon, Ellipse, Circle, Box): | ||
yaml.add_representer(region_class, region_representer) | ||
|
||
for loader in YamlLoaders: | ||
yaml.add_constructor("lsst.sphgeom.Region", region_constructor, Loader=loader) |
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.
Consider adding a comment explaining that you don't need the other classes because Region.decode
handles getting the right subclass. This was not obvious to me.
b = yaml.safe_load(yaml.dump(a)) | ||
self.assertEqual(a, b) |
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.
This repetition makes me wonder if it's worth having a general "test yaml" utility somewhere. That would also make it easier to test a wider range of states, for classes that need them (I realize Region
does not, because the underlying persistence code is already tested elsewhere).
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.
There is a lot of repetition in the tests. I was trying to keep this ticket small to get you something quickly and not let it blow up into a full on test refactoring.
python/lsst/sphgeom/_yaml.py
Outdated
for region_class in (Region, ConvexPolygon, Ellipse, Circle, Box): | ||
yaml.add_representer(region_class, region_representer) |
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.
Region
is an abstract class (at least in C++? 😨). Why does it need to have a representer registered?
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.
Region
is an abstract class in Python, too, in the sense that it cannot be directly instantiated, but I'd be quite surprised if it used the ABCMeta
metaclass that pure-Python ABCs use. I don't know anything about yaml representers, but it is possible for Python code to get what appears to be a base a Region
instance there is a concrete concrete C++ subclass was not exposed to pybind11. I don't know if there are any such subclasses in the wild in this hierarchy.
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.
I'll drop it. It shouldn't make a difference.
python/lsst/sphgeom/_yaml.py
Outdated
def pixel_representer(dumper, data, pixelScheme): | ||
"""Represent a pixelization in YAML | ||
|
||
Stored as the pixelization level. Use a mapping for more clarity. |
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.
Please reword to "Uses a mapping". "Use" sounds like an instruction to either the user or a future developer.
More clarity than what?
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.
Since these are custom functions, and not PyYAML hooks, please provide a full description of the parameters.
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 "more clarity" was a note to myself that scalar would be sufficient but a mapping with a level keyword is more explicit. I'll update. I could make the same change for the region serialization as well and use a mapping with a key "encoded" or something and value the hex string rather than a scalar with the hex string directly.
python/lsst/sphgeom/_yaml.py
Outdated
|
||
yaml.add_representer(pixelSchemeCls, representer) | ||
for loader in YamlLoaders: | ||
yaml.add_constructor(f"lsst.sphgeom.{pixelSchemeCls.__name__}", constructor, Loader=loader) |
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.
Will this be a problem if there's a pixelization class that's not at the sphgeom
top level? It might be better to get the fully qualified name, then use that here and for pixel_representer
.
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.
It works for what we have now and it doesn't look like healpix should be implemented differently. If it does we can change it later.
Uses the encode/decode method and stores the encoded bytes as a hex string scalar in the YAML.
No description provided.