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-25786: Add YAML support for Regions and Pixelization #24

Merged
merged 2 commits into from Jul 8, 2020

Conversation

timj
Copy link
Member

@timj timj commented Jul 2, 2020

No description provided.

@timj timj requested a review from kfindeisen July 2, 2020 23:03
kfindeisen
kfindeisen previously approved these changes Jul 6, 2020
Copy link
Member

@kfindeisen kfindeisen left a 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.

Comment on lines 58 to 63
# 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)
Copy link
Member

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.

Comment on lines +144 to +145
b = yaml.safe_load(yaml.dump(a))
self.assertEqual(a, b)
Copy link
Member

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).

Copy link
Member Author

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.

Comment on lines 54 to 63
for region_class in (Region, ConvexPolygon, Ellipse, Circle, Box):
yaml.add_representer(region_class, region_representer)
Copy link
Member

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?

Copy link
Member

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.

Copy link
Member Author

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.

def pixel_representer(dumper, data, pixelScheme):
"""Represent a pixelization in YAML

Stored as the pixelization level. Use a mapping for more clarity.
Copy link
Member

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?

Copy link
Member

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.

Copy link
Member Author

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.


yaml.add_representer(pixelSchemeCls, representer)
for loader in YamlLoaders:
yaml.add_constructor(f"lsst.sphgeom.{pixelSchemeCls.__name__}", constructor, Loader=loader)
Copy link
Member

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.

Copy link
Member Author

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.

timj added 2 commits July 7, 2020 17:24
Uses the encode/decode method and stores the encoded bytes
as a hex string scalar in the YAML.
@timj timj merged commit 009f3de into master Jul 8, 2020
@timj timj deleted the tickets/DM-25786 branch July 8, 2020 04:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants