Repository: nilearn/nilearn

Issue:
plotting icbm152 gray matter with plot_img does not work
``` Python
from nilearn import datasets, plotting
icbm152 = datasets.fetch_icbm152_2009()
plotting.plot_img(icbm152.gm)
```

gives me the following image
![icbm152_gm](https://cloud.githubusercontent.com/assets/7080143/17331821/daeadb10-58cc-11e6-8110-cde4ccd95236.png)

I also failed in displaying the image with `plotting.plot_anat`. Am I doing something wrong ?



Discussion:
Isn't that a known bug?

I am not sure if I should consider as bug or images should be in 4D space as (x, y, z, 1) ? It works like this.

``` python
from nilearn import datasets
from nilearn._utils import check_niimg
from nilearn import plotting

icbm = datasets.fetch_icbm152_2009()
gm = check_niimg(icbm.gm, atleast_4d=True)
print(gm.shape)
plotting.plot_roi(gm)
```

Ok thanks

Here for this issue, if I reorder_img before plotting. It is good to visualize.

On the other side, if I plot as it is w/o reorder_img. I see variations deep inside the code. It breaks at line 171 in displays.py
_Breaking is due to this: `y_map` has more length calculated 250 which is more than actual data length 233 in 'y' direction._
What's going wrong here ? How does `self.coord` is given ?

Some information related to that:

``` python
self.coord = 116.5 (may be half of affine in 'y' direction)
data.shape
(197, 233, 189)
 affine
array([[   1.,    0.,    0.,  -98.],
       [   0.,    1.,    0., -134.],
       [   0.,    0.,    1.,  -72.],
       [   0.,    0.,    0.,    1.]])
```

data type at some point is converted to `int32` with `check_niimg_3d(img, dtype='auto')` leading `find_xyz_cut_coords` to find wrong coordinates. I guess.

May be it is bad idea. Can we give parameter option `dtype` to plotting functions to control ? Any other ideas/hints ? Given that we have `dtype` plotting issues with some images.
For your record, plot_prob_atlas is broken particularly with icbm templates.

FYI: This is been reported in nibabel https://github.com/nipy/nibabel/issues/490


Resolving patch:
diff --git a/nilearn/_utils/niimg.py b/nilearn/_utils/niimg.py
--- a/nilearn/_utils/niimg.py
+++ b/nilearn/_utils/niimg.py
@@ -28,17 +28,6 @@ def _safe_get_data(img):
     return img.get_data()
 
 
-def _get_data_dtype(img):
-    """Returns the dtype of an image.
-    If the image is non standard (no get_data_dtype member), this function
-    relies on the data itself.
-    """
-    try:
-        return img.get_data_dtype()
-    except AttributeError:
-        return img.get_data().dtype
-
-
 def _get_target_dtype(dtype, target_dtype):
     """Returns a new dtype if conversion is needed
 
@@ -103,7 +92,7 @@ def load_niimg(niimg, dtype=None):
                         " not compatible with nibabel format:\n"
                         + short_repr(niimg))
 
-    dtype = _get_target_dtype(_get_data_dtype(niimg), dtype)
+    dtype = _get_target_dtype(niimg.get_data().dtype, dtype)
 
     if dtype is not None:
         niimg = new_img_like(niimg, niimg.get_data().astype(dtype),
diff --git a/nilearn/_utils/niimg_conversions.py b/nilearn/_utils/niimg_conversions.py
--- a/nilearn/_utils/niimg_conversions.py
+++ b/nilearn/_utils/niimg_conversions.py
@@ -455,7 +455,7 @@ def concat_niimgs(niimgs, dtype=np.float32, ensure_ndim=None,
 
     target_shape = first_niimg.shape[:3]
     if dtype == None:
-        dtype = first_niimg.get_data_dtype()
+        dtype = first_niimg.get_data().dtype
     data = np.ndarray(target_shape + (sum(lengths), ),
                       order="F", dtype=dtype)
     cur_4d_index = 0
diff --git a/nilearn/input_data/nifti_maps_masker.py b/nilearn/input_data/nifti_maps_masker.py
--- a/nilearn/input_data/nifti_maps_masker.py
+++ b/nilearn/input_data/nifti_maps_masker.py
@@ -7,7 +7,6 @@
 
 from .. import _utils
 from .._utils import logger, CacheMixin
-from .._utils.niimg import _get_data_dtype
 from .._utils.class_inspect import get_params
 from .._utils.niimg_conversions import _check_same_fov
 from .._utils.compat import get_affine
@@ -276,7 +275,7 @@ def transform_single_imgs(self, imgs, confounds=None):
             # Check if there is an overlap.
 
             # If float, we set low values to 0
-            dtype = _get_data_dtype(self._resampled_maps_img_)
+            dtype = self._resampled_maps_img_.get_data().dtype
             data = self._resampled_maps_img_.get_data()
             if dtype.kind == 'f':
                 data[data < np.finfo(dtype).eps] = 0.


Test patch:
diff --git a/nilearn/tests/test_niimg.py b/nilearn/tests/test_niimg.py
--- a/nilearn/tests/test_niimg.py
+++ b/nilearn/tests/test_niimg.py
@@ -35,3 +35,17 @@ def test_new_img_like_side_effect():
                  copy_header=True)
     hash2 = joblib.hash(img1)
     assert_equal(hash1, hash2)
+
+
+def test_get_target_dtype():
+    img = Nifti1Image(np.ones((2, 2, 2), dtype=np.float64), affine=np.eye(4))
+    assert_equal(img.get_data().dtype.kind, 'f')
+    dtype_kind_float = niimg._get_target_dtype(img.get_data().dtype,
+                                               target_dtype='auto')
+    assert_equal(dtype_kind_float, np.float32)
+
+    img2 = Nifti1Image(np.ones((2, 2, 2), dtype=np.int64), affine=np.eye(4))
+    assert_equal(img2.get_data().dtype.kind, 'i')
+    dtype_kind_int = niimg._get_target_dtype(img2.get_data().dtype,
+                                             target_dtype='auto')
+    assert_equal(dtype_kind_int, np.int32)
diff --git a/nilearn/tests/test_niimg_conversions.py b/nilearn/tests/test_niimg_conversions.py
--- a/nilearn/tests/test_niimg_conversions.py
+++ b/nilearn/tests/test_niimg_conversions.py
@@ -115,6 +115,10 @@ def test_check_niimg_3d():
     with testing.write_tmp_imgs(data_img, create_files=True) as filename:
         _utils.check_niimg_3d(filename)
 
+    # check data dtype equal with dtype='auto'
+    img_check = _utils.check_niimg_3d(img, dtype='auto')
+    assert_equal(img.get_data().dtype.kind, img_check.get_data().dtype.kind)
+
 
 def test_check_niimg_4d():
     assert_raises_regex(TypeError, 'nibabel format',
@@ -205,6 +209,13 @@ def test_check_niimg():
         "a list of list of 4D images \(6D\)",
         _utils.check_niimg, img_2_4d, ensure_ndim=4)
 
+    # check data dtype equal with dtype='auto'
+    img_3d_check = _utils.check_niimg(img_3d, dtype='auto')
+    assert_equal(img_3d.get_data().dtype.kind, img_3d_check.get_data().dtype.kind)
+
+    img_4d_check = _utils.check_niimg(img_4d, dtype='auto')
+    assert_equal(img_4d.get_data().dtype.kind, img_4d_check.get_data().dtype.kind)
+
 
 def test_check_niimg_wildcards():
     tmp_dir = tempfile.tempdir + os.sep
@@ -464,9 +475,9 @@ def test_concat_niimg_dtype():
         np.zeros(shape + [n_scans]).astype(np.int16), np.eye(4))
             for n_scans in [1, 5]]
     nimg = _utils.concat_niimgs(vols)
-    assert_equal(nimg.get_data_dtype(), np.float32)
+    assert_equal(nimg.get_data().dtype, np.float32)
     nimg = _utils.concat_niimgs(vols, dtype=None)
-    assert_equal(nimg.get_data_dtype(), np.int16)
+    assert_equal(nimg.get_data().dtype, np.int16)
 
 
 def nifti_generator(buffer):