{"action":"created","issue":{"url":"https://api.github.com/repos/gammapy/gammapy/issues/1547","repository_url":"https://api.github.com/repos/gammapy/gammapy","labels_url":"https://api.github.com/repos/gammapy/gammapy/issues/1547/labels{/name}","comments_url":"https://api.github.com/repos/gammapy/gammapy/issues/1547/comments","events_url":"https://api.github.com/repos/gammapy/gammapy/issues/1547/events","html_url":"https://github.com/gammapy/gammapy/issues/1547","id":342739039,"node_id":"MDU6SXNzdWUzNDI3MzkwMzk=","number":1547,"title":"Cloning / copy pattern for Gammapy maps","user":{"login":"adonath","id":3715928,"node_id":"MDQ6VXNlcjM3MTU5Mjg=","avatar_url":"https://avatars1.githubusercontent.com/u/3715928?v=4","gravatar_id":"","url":"https://api.github.com/users/adonath","html_url":"https://github.com/adonath","followers_url":"https://api.github.com/users/adonath/followers","following_url":"https://api.github.com/users/adonath/following{/other_user}","gists_url":"https://api.github.com/users/adonath/gists{/gist_id}","starred_url":"https://api.github.com/users/adonath/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/adonath/subscriptions","organizations_url":"https://api.github.com/users/adonath/orgs","repos_url":"https://api.github.com/users/adonath/repos","events_url":"https://api.github.com/users/adonath/events{/privacy}","received_events_url":"https://api.github.com/users/adonath/received_events","type":"User","site_admin":false},"labels":[{"id":39740771,"node_id":"MDU6TGFiZWwzOTc0MDc3MQ==","url":"https://api.github.com/repos/gammapy/gammapy/labels/feature","name":"feature","color":"84b6eb","default":false},{"id":39740773,"node_id":"MDU6TGFiZWwzOTc0MDc3Mw==","url":"https://api.github.com/repos/gammapy/gammapy/labels/question","name":"question","color":"cc317c","default":true}],"state":"open","locked":false,"assignee":{"login":"adonath","id":3715928,"node_id":"MDQ6VXNlcjM3MTU5Mjg=","avatar_url":"https://avatars1.githubusercontent.com/u/3715928?v=4","gravatar_id":"","url":"https://api.github.com/users/adonath","html_url":"https://github.com/adonath","followers_url":"https://api.github.com/users/adonath/followers","following_url":"https://api.github.com/users/adonath/following{/other_user}","gists_url":"https://api.github.com/users/adonath/gists{/gist_id}","starred_url":"https://api.github.com/users/adonath/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/adonath/subscriptions","organizations_url":"https://api.github.com/users/adonath/orgs","repos_url":"https://api.github.com/users/adonath/repos","events_url":"https://api.github.com/users/adonath/events{/privacy}","received_events_url":"https://api.github.com/users/adonath/received_events","type":"User","site_admin":false},"assignees":[{"login":"adonath","id":3715928,"node_id":"MDQ6VXNlcjM3MTU5Mjg=","avatar_url":"https://avatars1.githubusercontent.com/u/3715928?v=4","gravatar_id":"","url":"https://api.github.com/users/adonath","html_url":"https://github.com/adonath","followers_url":"https://api.github.com/users/adonath/followers","following_url":"https://api.github.com/users/adonath/following{/other_user}","gists_url":"https://api.github.com/users/adonath/gists{/gist_id}","starred_url":"https://api.github.com/users/adonath/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/adonath/subscriptions","organizations_url":"https://api.github.com/users/adonath/orgs","repos_url":"https://api.github.com/users/adonath/repos","events_url":"https://api.github.com/users/adonath/events{/privacy}","received_events_url":"https://api.github.com/users/adonath/received_events","type":"User","site_admin":false}],"milestone":{"url":"https://api.github.com/repos/gammapy/gammapy/milestones/11","html_url":"https://github.com/gammapy/gammapy/milestone/11","labels_url":"https://api.github.com/repos/gammapy/gammapy/milestones/11/labels","id":2487784,"node_id":"MDk6TWlsZXN0b25lMjQ4Nzc4NA==","number":11,"title":"0.8","description":"","creator":{"login":"cdeil","id":852409,"node_id":"MDQ6VXNlcjg1MjQwOQ==","avatar_url":"https://avatars3.githubusercontent.com/u/852409?v=4","gravatar_id":"","url":"https://api.github.com/users/cdeil","html_url":"https://github.com/cdeil","followers_url":"https://api.github.com/users/cdeil/followers","following_url":"https://api.github.com/users/cdeil/following{/other_user}","gists_url":"https://api.github.com/users/cdeil/gists{/gist_id}","starred_url":"https://api.github.com/users/cdeil/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/cdeil/subscriptions","organizations_url":"https://api.github.com/users/cdeil/orgs","repos_url":"https://api.github.com/users/cdeil/repos","events_url":"https://api.github.com/users/cdeil/events{/privacy}","received_events_url":"https://api.github.com/users/cdeil/received_events","type":"User","site_admin":false},"open_issues":37,"closed_issues":227,"state":"open","created_at":"2017-04-28T09:17:03Z","updated_at":"2018-07-20T09:26:17Z","due_on":null,"closed_at":null},"comments":2,"created_at":"2018-07-19T13:56:06Z","updated_at":"2018-07-20T09:36:49Z","closed_at":null,"author_association":"MEMBER","body":"In the Gammapy code we have many use cases, especially with `Map` objects, where we want to basically clone an object and modify a selection of its attributes. E.g. cloning a `Map` object and then set its data, meta or unit attribute. Currently this is typically done as follows:\r\n\r\n```python\r\n# Current pattern\r\nflux = copy.deepcopy(counts)  \r\nflux.data = data\r\nflux.unit = unit\r\nflux.meta = meta\r\n```\r\nThis patter is inefficient, because data is copied and thrown away again. It is also error-prone because, people might forget to set attributes and it is also a lot of code lines.  I've discussed with @cdeil in the past that we should introduce a coding pattern that makes this simpler to achieve, ideally with only one line of code. And we suggest to introduce a `.copy()` method, that works as following:\r\n\r\n```python\r\n# pattern using a copy function\r\nflux = counts.copy(data=data, meta=meta, unit=unit)\r\n\r\n# or more explicit function name\r\nflux = counts.copy_and_setattr(data=data, meta=meta, unit=unit)\r\n\r\n# alternative pattern with factory function, which might need an additional import\r\nflux = Map.from_map(counts, data=data, meta=meta, unit=unit)\r\n```\r\nWhile the structure of the `.copy()` function is clear, I do not like the name, because it does more. It copies the map object and overwrites given attributes. So I suggested the altenative name `.copy_and_setattr()`. Another alternative name could be `.clone()`.\r\n\r\nWhat do you think @cdeil. @registerrier @woodmd? \r\n\r\n\r\n\r\n\r\n"},"comment":{"url":"https://api.github.com/repos/gammapy/gammapy/issues/comments/406546397","html_url":"https://github.com/gammapy/gammapy/issues/1547#issuecomment-406546397","issue_url":"https://api.github.com/repos/gammapy/gammapy/issues/1547","id":406546397,"node_id":"MDEyOklzc3VlQ29tbWVudDQwNjU0NjM5Nw==","user":{"login":"cdeil","id":852409,"node_id":"MDQ6VXNlcjg1MjQwOQ==","avatar_url":"https://avatars3.githubusercontent.com/u/852409?v=4","gravatar_id":"","url":"https://api.github.com/users/cdeil","html_url":"https://github.com/cdeil","followers_url":"https://api.github.com/users/cdeil/followers","following_url":"https://api.github.com/users/cdeil/following{/other_user}","gists_url":"https://api.github.com/users/cdeil/gists{/gist_id}","starred_url":"https://api.github.com/users/cdeil/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/cdeil/subscriptions","organizations_url":"https://api.github.com/users/cdeil/orgs","repos_url":"https://api.github.com/users/cdeil/repos","events_url":"https://api.github.com/users/cdeil/events{/privacy}","received_events_url":"https://api.github.com/users/cdeil/received_events","type":"User","site_admin":false},"created_at":"2018-07-20T09:36:49Z","updated_at":"2018-07-20T09:36:49Z","author_association":"MEMBER","body":"Decision in Gammapy dev call today: `clone`.\r\nAxel will add this very soon."}}
{"actor":{"display_login":"cdeil"}}