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
Introduce map copy method #1554
Introduce map copy method #1554
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.
Great, thanks!
Add a test that shows that the clone is an independent deep copy by default (using is not
to test that object IDs are different)?
Is the complexity with init
inspect needed? Or is there are different way to write this?
OK to merge if you want to move on, we can still adjust next week. But @woodmd and @registerrier - please review this.
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.
OK I might have missed the point of this clone
method but I would not expect a clone method to be able to change the geometry of a Map
.
Note also the possible issue with multi resolution HpxNDMap
gammapy/maps/base.py
Outdated
|
||
if 'geom' in kwargs and 'data' not in kwargs: | ||
raise ValueError("Can't clone and overwrite geometry if the data is not overwritten too.") | ||
|
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 am not sure I follow the logic here.
If you change the geometry you are not cloning the map. You can reproject, cutout but not clone, right?
I would simply return a ValueError if geom
is in kwargs
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.
OK I see this is actually most of the usage for this clone
method.
I think then this should be a private method. In my opinion, it is very confusing for a user to have a clone that basically can recreate a Map
from scratch e.g. map.clone(geom=geom, data=data, meta=meta, unit=unit)
gammapy/maps/hpxnd.py
Outdated
geom = self.geom.to_image() | ||
axis = tuple(range(self.data.ndim - 1)) | ||
data = np.nansum(self.data, axis=axis) | ||
return self.clone(geom=geom, data=data) |
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.
Why did you remove the case for multi resolution map? Does this implementation work for healpix maps with mutliresolution scales?
Again the logic of cloning with a new geom is a bit strange to me. You just keep unit
and meta
. You don't gain much in terms of complexity with: map_out = self.__class__(geom=geom, meta=meta, unit=unit)
|
||
def downsample(self, factor, preserve_counts=True): | ||
geom = self.geom.downsample(factor) | ||
coords = self.geom.get_coord() | ||
vals = self.get_by_coord(coords) |
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.
Isn't it slower to use get_by_coord
rather than get_by_idx
used in the initial implementation?
I see, the complexity comes from wanting to use I think I agree with the comment by @registerrier :
If possible, I would prefer to remove the complex implementation of clone with argument inspection. Is this possible?
Note that str is immutable in Python, so making a copy is not needed / useful. And then for the cases where geom changes, we keep using the If so, it might be better to rename |
I actually noticed the same issue, that the |
49e23a4
to
263f2ff
Compare
I've changed now to the following pattern. I introduced a hidden |
I don't like the I'll make a follow-up commit here and then merge without further discussion, so that we can move on. That doesn't have to be the final solution, we can re-discuss on Friday or later once we have more experience and callers with this, and then put something better if we find it. But this should go in, IMO it's been a real pain-point and error-prone, to not have a way to make a copy without having to import and hard-code the map class (i.e. WCSNDMap or HPX map). |
This PR introduces the
Map.clone()
method to simplify creating new maps from existing ones. This PR addresses #1547.