Add replicate and replicate_without_data methods to frames#6182
Add replicate and replicate_without_data methods to frames#6182mhvk merged 12 commits intoastropy:masterfrom
Conversation
|
Cool, sounds reasonable to me. I don't love the name though - just brainstorming:
|
|
+1 to |
mhvk
left a comment
There was a problem hiding this comment.
How about just a new copy method? It could have **kwargs like here, but by default also include representation. Then, if you do not want a representation included, you put representation=None? (I think this would "just work"!).
astropy/coordinates/baseframe.py
Outdated
| A new object with the same frame attributes as this one, but | ||
| with no data and possibly new frame attributes. | ||
| """ | ||
| attrs_to_set = {nm:getattr(self, nm) for nm in self.frame_attributes} |
There was a problem hiding this comment.
This should exclude defaults (i.e., just copy frattrs definition from realize_frame)
astropy/coordinates/baseframe.py
Outdated
| with no data and possibly new frame attributes. | ||
| """ | ||
| attrs_to_set = {nm:getattr(self, nm) for nm in self.frame_attributes} | ||
| for nm, val in kwargs.items(): |
There was a problem hiding this comment.
Why not just frattrs.update(kwargs)?
|
p.s. Note that currently |
|
@mhvk - #6182 (comment) is exactly why I didn't go with |
|
although... now that I think about it, I kind of like the |
|
By analogy with p.s. It is perhaps slightly unfortunate that our |
|
Hmm, a |
|
How about |
|
OK, I went the route @mhvk suggested here, with the name choice of |
mhvk
left a comment
There was a problem hiding this comment.
This looks very good, I'm happy it worked so well. Apart from the minor comments in-line, one question is whether one should just expose the possibility of setting the data, i.e., use representation explicitly instead of _framedata -- but obviously we can also add this later, so fine to keep as is too.
astropy/coordinates/baseframe.py
Outdated
| def apply_method(value): | ||
| if isinstance(value, ShapedLikeNDArray): | ||
| return value._apply(method, *args, **kwargs) | ||
| if method=='replicate' and not hasattr(value, method): |
There was a problem hiding this comment.
Actually, it wasn't just future-proofing (although that is indeed an important side-benefit!). It turns out this is necessary just to get the tests to pass because Quantity sub-classes like Angle/Latitude/Longitude don't have replicate.
But in the end it's good anyway this way, I think!
astropy/coordinates/baseframe.py
Outdated
| else: | ||
| return getattr(value, method)(*args, **kwargs) | ||
|
|
||
| if method=='replicate' and not hasattr(value, method): |
astropy/coordinates/baseframe.py
Outdated
|
|
||
| def replicate(self, copy=False, **kwargs): | ||
| """ | ||
| Generates a new frame object that is a copy of this one, but possibly |
There was a problem hiding this comment.
It is mostly a matter of taste, but I prefer single-line "titles" of the docstring. E.g., inspired by Times:
Return a replicate of the frame, optionally overriding some attributes.
astropy/coordinates/baseframe.py
Outdated
|
|
||
| def replicate_without_data(self, copy=False, **kwargs): | ||
| """ | ||
| Generates a new frame object that is a copy of this one, but without |
There was a problem hiding this comment.
Similarly, maybe
Return a replicate without data, optionally overriding some attributes.
(and then still some text like you have here, including the analogy with realize_frame)
|
p.s. Maybe edit the title of this PR. |
|
(Approved changes, since really only small comments remain.) |
|
Alright, I addressed @mhvk's comments, and I think this is ready to merge after the tests pass (or maybe even before, given that these are very minor changes). |
|
Agreed that running the full tests is not necessary, especially since appveyor and circle already passed. So, I cancelled travis and will merge. Nice addition! |
|
This seems to have broken SunPy master. It seems that our use of https://travis-ci.org/sunpy/sunpy/jobs/242845641#L1123 Any bright ideas before I jump all the way into this? |
This is a straightforward PR that adds a
clone_framemethod toBaseCoordinateFrame. This method copies the frame and its attributes, without the data, optionally overriding frame attributes.I'm not a huge fan of the specific name
clone_frame, as it doesn't clearly convey that the data get removed. But I feltunrealize_framedoesn't convey the fact that this is also a good way to copy attributes (overwriting specific ones if desired). So this seemed like a compromise... But I'm willing to be convinced if someone has strong opinions.This idiom is used in a variety of places, and this method can probably be used to clean things up. But for now just getting it in for 2.0 is a good starting point. (Note that it also makes some of the velocity work @adrn and I are finishing up easier).
cc @astrofrog or @Cadair (one of you I think originally suggested this in some form or another... I think?)