PrimeIntellect/SWE-rebench-V2-Filtered-Easy-Verified huggingface dataset:
Repository: gammapy/gammapy (python)
Issue: LabelMapAxis re-orders labels
**Gammapy version**
v1.1

**Bug description**
When instancing a `LabelMapAxis` the labels are re-ordered / sorted. This is problematic because it should keep the order, that corresponds to the  data.

**Expected behavior**
Do not re-order / sort the labels.

**To Reproduce**
```python
import numpy as np
from gammapy.maps import LabelMapAxis

labels = np.array([3, 2, 1, 4, 5])
print(labels)
print(LabelMapAxis(labels).center)
```
Which prints:
```
[3 2 1 4 5]
[1 2 3 4 5]
```

Pull request description: Fix `LabelMapAxis` so it doesn't reorder the labels
Resolves #5384


Note: had to ensure that labels was an array type, as this is what np.unique would do

Patch:
diff --git a/gammapy/maps/axes.py b/gammapy/maps/axes.py
index c12ef6d72..3a0ee0c4f 100644
--- a/gammapy/maps/axes.py
+++ b/gammapy/maps/axes.py
@@ -3129,7 +3129,7 @@ class LabelMapAxis:
         if not len(unique_labels) == len(labels):
             raise ValueError("Node labels must be unique")
 
-        self._labels = unique_labels
+        self._labels = np.array(labels)
         self._name = name
 
     @property


Test patch:
diff --git a/gammapy/maps/tests/test_axes.py b/gammapy/maps/tests/test_axes.py
index f99e88b9a..bd086e4e1 100644
--- a/gammapy/maps/tests/test_axes.py
+++ b/gammapy/maps/tests/test_axes.py
@@ -793,6 +793,21 @@ def test_label_map_axis_basics():
     axis_copy = axis.copy()
     assert axis_copy.name == "label-axis"
 
+    # Ensure order is correct
+    labels = np.array([1, 4, 5])
+    assert_allclose(labels, [1, 4, 5])
+    assert_allclose(LabelMapAxis(labels).center, labels)
+    assert_allclose(LabelMapAxis(labels)._labels, labels)
+
+    # Test case with non unique labels
+    with pytest.raises(ValueError) as exc_info:
+        LabelMapAxis([1, 1, 1])
+    assert str(exc_info.value) == "Node labels must be unique"
+
+    # Ensure non alphabetical ordering
+    labels = np.array(["b", "a", "d", "c"])
+    assert_equal(LabelMapAxis(labels).center, labels)
+
 
 def test_label_map_axis_coord_to_idx():
     axis = LabelMapAxis(labels=["label-1", "label-2", "label-3"], name="label-axis")