Repository: gammapy/gammapy
Pull request #5279 (2024-05-31 11:48:21)

Add copy method on FluxMaps
Add copy method on FluxMaps

codecov[bot]: ## [Codecov](https://app.codecov.io/gh/gammapy/gammapy/pull/5279?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=gammapy) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Project coverage is 94.46%. Comparing base [(`f85fb7b`)](https://app.codecov.io/gh/gammapy/gammapy/commit/f85fb7b40717542f697d1707ce18d0aec211faaf?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=gammapy) to head [(`52ef74c`)](https://app.codecov.io/gh/gammapy/gammapy/commit/52ef74cd40b4e93c2691261fee0ea0473c912b02?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=gammapy).
> Report is 1 commits behind head on main.


<details><summary>Additional details and impacted files</summary>


```diff
@@           Coverage Diff           @@
##             main    #5279   +/-   ##
=======================================
  Coverage   94.46%   94.46%           
=======================================
  Files         232      232           
  Lines       35636    35653   +17     
=======================================
+ Hits        33664    33681   +17     
  Misses       1972     1972           
```



</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/gammapy/gammapy/pull/5279?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=gammapy).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=gammapy).


Patch:
diff --git a/gammapy/estimators/map/core.py b/gammapy/estimators/map/core.py
index 2bf3033fe..cee19ce84 100644
--- a/gammapy/estimators/map/core.py
+++ b/gammapy/estimators/map/core.py
@@ -1,5 +1,6 @@
 # Licensed under a 3-clause BSD style license - see LICENSE.rst
 import logging
+from copy import deepcopy
 import numpy as np
 from astropy import units as u
 from astropy.io import fits
@@ -1093,6 +1094,31 @@ class FluxMaps:
         ) as hdulist:
             return cls.from_hdulist(hdulist, checksum=checksum)
 
+    def copy(self, reference_model=None):
+        """Deep copy
+
+        Parameters
+        ----------
+
+        reference_model : `~gammapy.modeling.models.SkyModel`, optional
+            The reference model to use for conversions. If None, the originial model is copied.
+            Flux maps have been obtained for a specific reference model.
+            Changing it will change the fluxes. Handle with care.
+        Returns
+        -------
+        flux_maps : `~gammapy.estimators.FluxMaps`
+            Copied flux maps object.
+
+        """
+
+        new = deepcopy(self)
+        if reference_model is not None:
+            new._reference_model = reference_model.copy()
+            log.warning(
+                "Changing the reference model will change the fluxes. Handle with care."
+            )
+        return new
+
     def slice_by_idx(self, slices):
         """Slice flux maps by index.
 


Test patch:
diff --git a/gammapy/estimators/map/tests/test_core.py b/gammapy/estimators/map/tests/test_core.py
index 57ea51745..b02ba2a6a 100644
--- a/gammapy/estimators/map/tests/test_core.py
+++ b/gammapy/estimators/map/tests/test_core.py
@@ -571,3 +571,16 @@ def test_slice_by_coord():
 
     sliced_map4 = ref_map.slice_by_coord({"time": slice(1 * u.d, 3 * u.d)})
     assert sliced_map4.geom.axes["time"].nbin == 1
+
+
+def test_copy(map_flux_estimate):
+
+    model = SkyModel(PowerLawSpectralModel(amplitude="1e-10 cm-2s-1TeV-1", index=3))
+
+    fe = FluxMaps(data=map_flux_estimate, reference_model=model)
+    fe_new = fe.copy(reference_model=None)
+    assert fe_new.reference_model.spectral_model.index.value == 3
+
+    model = SkyModel(PowerLawSpectralModel(amplitude="1e-10 cm-2s-1TeV-1", index=4))
+    fe_new = fe.copy(reference_model=model)
+    assert fe_new.reference_model.spectral_model.index.value == 4