Repository: nilearn/nilearn

Issue:
`MNI152TEMPLATE` (default background) is always plotted with x axis flipped
Reported in [this neurostars discussion](https://neurostars.org/t/is-the-nilearn-mni-brain-flipped-l-r/16336) (thanks!)
the problem is that:
- the default background is not an actual nifti image but a `nilearn.image.img_plotting._MNI152Template` defined [here](https://github.com/nilearn/nilearn/blob/4c1c7337b38506e6ef60873cfe3dd316ce869cf0/nilearn/plotting/img_plotting.py#L339) that mimicks its interface
- `BaseSlicer._map_show` calls `reorder_img` on the input image [here](https://github.com/nilearn/nilearn/blob/4c1c7337b38506e6ef60873cfe3dd316ce869cf0/nilearn/plotting/displays.py#L753) and then expects to be working with an image that has a diagonal and positive affine
- `reorder_img` calls `new_img_like` to build an image with the positive affine and reordered data, `new_img_like` uses the original image's class to build the new image, [here](https://github.com/nilearn/nilearn/blob/4c1c7337b38506e6ef60873cfe3dd316ce869cf0/nilearn/image/image.py#L766), so in this case a `_MNI152Template`
- but this class's `__init__` [ignores its parameters](https://github.com/nilearn/nilearn/blob/4c1c7337b38506e6ef60873cfe3dd316ce869cf0/nilearn/plotting/img_plotting.py#L351), as it is a fake image that always loads the mni template. so the reordering has no effect and `_map_show` proceeds with a freshly loaded, but identical, mni template, and
- the element 0,0 of the mni template is `-2`, so x axis is flipped.


I guess a quick fix would be to have the `_MNI152Template` reorder the image in its `load` function


Discussion:

Resolving patch:
diff --git a/nilearn/plotting/img_plotting.py b/nilearn/plotting/img_plotting.py
--- a/nilearn/plotting/img_plotting.py
+++ b/nilearn/plotting/img_plotting.py
@@ -35,6 +35,7 @@
 from .._utils.ndimage import get_border_data
 from ..datasets import load_mni152_template
 from ..image import new_img_like, iter_img, get_data
+from nilearn.image.resampling import reorder_img
 from ..masking import compute_epi_mask, apply_mask
 from .displays import get_slicer, get_projector
 from . import cm
@@ -355,6 +356,7 @@ def __init__(self, data=None, affine=None, header=None):
     def load(self):
         if self.data is None:
             anat_img = load_mni152_template()
+            anat_img = reorder_img(anat_img)
             data = get_data(anat_img)
             data = data.astype(np.float)
             anat_mask = ndimage.morphology.binary_fill_holes(data > 0)


Test patch:
diff --git a/nilearn/plotting/tests/test_img_plotting.py b/nilearn/plotting/tests/test_img_plotting.py
--- a/nilearn/plotting/tests/test_img_plotting.py
+++ b/nilearn/plotting/tests/test_img_plotting.py
@@ -14,7 +14,7 @@
 
 from scipy import sparse
 
-from nilearn.image.resampling import coord_transform
+from nilearn.image.resampling import coord_transform, reorder_img
 from nilearn.image import get_data
 from nilearn.datasets import load_mni152_template
 from nilearn.plotting.find_cuts import find_cut_slices
@@ -63,6 +63,14 @@ def testdata_4d():
     return data
 
 
+def test_mni152template_is_reordered():
+    """See issue #2550"""
+    reordered_mni = reorder_img(load_mni152_template())
+    assert np.allclose(get_data(reordered_mni), get_data(MNI152TEMPLATE))
+    assert np.allclose(reordered_mni.affine, MNI152TEMPLATE.affine)
+    assert np.allclose(reordered_mni.shape, MNI152TEMPLATE.shape)
+
+
 def demo_plot_roi(**kwargs):
     """ Demo plotting an ROI
     """