internlm/SWE-Fixer-Train-110K huggingface dataset:
Repository: nilearn/nilearn

Issue:
Replace get_data by safe_get_data in plotting.
In plotting, there is no reason to use get_data that may have side effects. We should use safe_get_data.



Discussion:

Resolving patch:
diff --git a/nilearn/plotting/find_cuts.py b/nilearn/plotting/find_cuts.py
--- a/nilearn/plotting/find_cuts.py
+++ b/nilearn/plotting/find_cuts.py
@@ -118,7 +118,7 @@ def find_xyz_cut_coords(img, mask=None, activation_threshold=None):
 def _get_auto_mask_bounds(img):
     """ Compute the bounds of the data with an automaticaly computed mask
     """
-    data = img.get_data().copy()
+    data = _safe_get_data(img)
     affine = img.get_affine()
     if hasattr(data, 'mask'):
         # Masked array
@@ -207,7 +207,7 @@ def find_cut_slices(img, direction='z', n_cuts=7, spacing='auto'):
                 direction))
     axis = 'xyz'.index(direction)
     affine = img.get_affine()
-    orig_data = np.abs(img.get_data())
+    orig_data = np.abs(_safe_get_data(img))
     this_shape = orig_data.shape[axis]
 
     if not isinstance(n_cuts, numbers.Number):
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
@@ -22,6 +22,7 @@
 
 from .._utils.numpy_conversions import as_ndarray
 from .._utils.compat import _basestring
+from .._utils.niimg import _safe_get_data
 
 import matplotlib.pyplot as plt
 
@@ -145,7 +146,7 @@ def _plot_img_with_bg(img, bg_img=None, cut_coords=None,
 
     if img is not False and img is not None:
         img = _utils.check_niimg_3d(img, dtype='auto')
-        data = img.get_data()
+        data = _safe_get_data(img)
         affine = img.get_affine()
 
         if np.isnan(np.sum(data)):
@@ -900,7 +901,7 @@ def plot_stat_map(stat_map_img, bg_img=MNI152TEMPLATE, cut_coords=None,
     stat_map_img = _utils.check_niimg_3d(stat_map_img, dtype='auto')
 
     cbar_vmin, cbar_vmax, vmin, vmax = _get_colorbar_and_data_ranges(
-        stat_map_img.get_data(),
+        _safe_get_data(stat_map_img),
         vmax,
         symmetric_cbar,
         kwargs)
@@ -1007,14 +1008,14 @@ def plot_glass_brain(stat_map_img,
         stat_map_img = _utils.check_niimg_3d(stat_map_img, dtype='auto')
         if plot_abs:
             cbar_vmin, cbar_vmax, vmin, vmax = _get_colorbar_and_data_ranges(
-                stat_map_img.get_data(),
+                _safe_get_data(stat_map_img),
                 vmax,
                 symmetric_cbar,
                 kwargs,
                 0)
         else:
             cbar_vmin, cbar_vmax, vmin, vmax = _get_colorbar_and_data_ranges(
-                stat_map_img.get_data(),
+                _safe_get_data(stat_map_img),
                 vmax,
                 symmetric_cbar,
                 kwargs)


Test patch:
Repository: nilearn/nilearn

Issue:
plot_mixed_gambles_space_net if broken on Python 3
Simple Python 3 glitch:

```
Traceback (most recent call last):
  File "/home/arthur/work/repos/nilearn/examples/02_decoding/plot_mixed_gambles_space_net.py", line 16, in <module>
    data = fetch_mixed_gambles(n_subjects=16)
  File "/home/arthur/work/repos/nilearn/nilearn/datasets/func.py", line 1285, in fetch_mixed_gambles
    X, y, mask_img = _load_mixed_gambles(map(nibabel.load, data.zmaps))
  File "/home/arthur/work/repos/nilearn/nilearn/datasets/func.py", line 1209, in _load_mixed_gambles
    mask = np.sum(mask, axis=0) > .5 * len(zmap_imgs)
TypeError: object of type 'map' has no len()
```



Discussion:
This is surprising. These examples are run routinely in CircleCI without problems.

With Python 3 ? No chance given the error...

OK, CircleCI runs on Python 2. Oooooooold!


Resolving patch:
diff --git a/nilearn/datasets/func.py b/nilearn/datasets/func.py
--- a/nilearn/datasets/func.py
+++ b/nilearn/datasets/func.py
@@ -11,7 +11,7 @@
 
 from .utils import (_get_dataset_dir, _fetch_files, _get_dataset_descr,
                     _read_md5_sum_file, _tree, _filter_columns)
-
+from .._utils import check_niimg
 from .._utils.compat import BytesIO, _basestring, _urllib
 from .._utils.numpy_conversions import csv_to_array
 
@@ -1215,7 +1215,7 @@ def _load_mixed_gambles(zmap_imgs):
         mask.append(this_mask)
     y = np.array(y)
     X = np.concatenate(X, axis=-1)
-    mask = np.sum(mask, axis=0) > .5 * len(zmap_imgs)
+    mask = np.sum(mask, axis=0) > .5 * len(mask)
     mask = np.logical_and(mask, np.all(np.isfinite(X), axis=-1))
     X = X[mask, :].T
     tmp = np.zeros(list(mask.shape) + [len(X)])
@@ -1291,7 +1291,8 @@ def fetch_mixed_gambles(n_subjects=1, data_dir=None, url=None, resume=True,
     zmap_fnames = _fetch_files(data_dir, files, resume=resume, verbose=verbose)
     data = Bunch(zmaps=zmap_fnames)
     if not return_raw_data:
-        X, y, mask_img = _load_mixed_gambles(map(nibabel.load, data.zmaps))
+        X, y, mask_img = _load_mixed_gambles(check_niimg(data.zmaps,
+                                                         return_iterator=True))
         data.zmaps, data.gain, data.mask_img = X, y, mask_img
     return data
 


Test patch:
Repository: nilearn/nilearn

Issue:
Automatically loading colormaps when importing nilearn (or cm?)
The colormaps that are created in [cm.py](https://github.com/KirstieJane/nilearn/blob/40069cb14b43333a73137797eac895cfb054db29/nilearn/plotting/cm.py) are really nice!

Currently, if I want to use them in one of my wrapper scripts I have the following hack:

```
from nilearn.plotting import cm

cmap = 'cold_white_hot'

if hasattr(cm, cmap):
    cmap = getatt(cm, cmap)
```

Would it be possible to have these colormaps already loaded so they're available by name without this code?



Discussion:
> Would it be possible to have these colormaps already loaded so they're
> available by name without this code?

Is there a reason that the way it is done in:
http://nilearn.github.io/auto_examples/01_plotting/plot_overlay.html#sphx-glr-auto-examples-01-plotting-plot-overlay-py
is not satisfactory?

When proposing new colormaps, it is a good practice to register them in matplotlib so that it is automatically available using string identifiers.

> When proposing new colormaps, it is a good practice to register them in
> matplotlib so that it is automatically available using string identifiers.

OK, sounds like a good idea. PR welcomed!

Thank you @AlexandreAbraham for explaining the problem more clearly! And to @GaelVaroquaux for all the support :grinning:


Resolving patch:
diff --git a/nilearn/plotting/cm.py b/nilearn/plotting/cm.py
--- a/nilearn/plotting/cm.py
+++ b/nilearn/plotting/cm.py
@@ -175,7 +175,11 @@ def alpha_cmap(color, name='', alpha_min=0.5, alpha_max=1.):
         alpha_max=1, name=name)
 
 
+# Save colormaps in the scope of the module
 locals().update(_cmap_d)
+# Register cmaps in matplotlib too
+for k, v in _cmap_d.items():
+    _cm.register_cmap(name=k, cmap=v)
 
 
 ################################################################################


Test patch:
diff --git a/nilearn/plotting/tests/test_cm.py b/nilearn/plotting/tests/test_cm.py
--- a/nilearn/plotting/tests/test_cm.py
+++ b/nilearn/plotting/tests/test_cm.py
@@ -19,3 +19,7 @@ def test_replace_inside():
     if hasattr(plt.cm, 'gnuplot'):
         # gnuplot is only in recent version of MPL
         replace_inside(plt.cm.gnuplot, plt.cm.gnuplot2, .2, .8)
+
+
+def test_cm_preload():
+    plt.imshow([list(range(10))], cmap="cold_hot")
Repository: nilearn/nilearn

Issue:
Error for unspecfied TR and filtering in signal.clean
may be this error should be more explicit

``` Python
from nilearn import datasets, input_data
adhd = datasets.fetch_adhd(n_subjects=1)
masker = input_data.NiftiSpheresMasker(seeds=[(42, 11, 23)], radius=6,
                                       low_pass=.1)
signal = masker.fit_transform(adhd.func[0])
```

gives the following error

> unsupported operand type(s) for /: 'float' and 'NoneType'



Discussion:

Resolving patch:
diff --git a/nilearn/signal.py b/nilearn/signal.py
--- a/nilearn/signal.py
+++ b/nilearn/signal.py
@@ -508,6 +508,10 @@ def clean(signals, sessions=None, detrend=True, standardize=True,
                 signals -= confounds.dot(inv).dot(confounds.T).dot(signals)
 
     if low_pass is not None or high_pass is not None:
+        if t_r is None:
+            raise ValueError("Repetition time (t_r) must be specified for "
+                             "filtering")
+
         signals = butterworth(signals, sampling_rate=1. / t_r,
                               low_pass=low_pass, high_pass=high_pass)
 


Test patch:
diff --git a/nilearn/tests/test_signal.py b/nilearn/tests/test_signal.py
--- a/nilearn/tests/test_signal.py
+++ b/nilearn/tests/test_signal.py
@@ -356,6 +356,8 @@ def test_clean_confounds():
                   confounds=filename1)
     assert_raises(TypeError, nisignal.clean, signals,
                   confounds=[None])
+    assert_raises(ValueError, nisignal.clean, signals, t_r=None,
+                  low_pass=.01)
 
     # Test without standardizing that constant parts of confounds are
     # accounted for
Repository: nilearn/nilearn

Issue:
Dropping scikit-learn dependency < 0.14.1
It makes easier to set base for decoder object in nilearn which actually requires lot of backports for versions. #1148 

I don't have great justifications though. Let me know if I miss something important in nilearn which we need to take into account dropping 0.13.

FYI: https://packages.debian.org/jessie/python-sklearn

Discussions are welcome.



Discussion:
+1 for me!

0.14.1 is very old. This seems like a safe move to me.

+1.

OK, I am assigning myself to address this issue.


Resolving patch:
diff --git a/nilearn/version.py b/nilearn/version.py
--- a/nilearn/version.py
+++ b/nilearn/version.py
@@ -39,7 +39,7 @@
         'required_at_installation': True,
         'install_info': _NILEARN_INSTALL_MSG}),
     ('sklearn', {
-        'min_version': '0.13',
+        'min_version': '0.14.1',
         'required_at_installation': True,
         'install_info': _NILEARN_INSTALL_MSG}),
     ('nibabel', {


Test patch:
Repository: nilearn/nilearn

Issue:
extra spaces in somes labels and networks of MSDL atlas
The csv file has extra-spaces

``` Python
from nilearn import datasets
msdl = datasets.fetch_atlas_msdl()
```

for instance `msdl.networks[-10]` is  `'    Language'` and  `msdl.labels[-5])` is `'  L Ins'`



Discussion:
Good catch. We should probably get that fixed.

I am trying to figure out how to change csv file corrected for white spaces. How can I ?

> I am trying to figure out how to change csv file corrected for white
> spaces. How can I ?

What is the question: do you want to change the file that is hosted on
the website?

In general, How can I solve this issue ?

> What is the question: do you want to change the file that is hosted on the website?

Yes, Can I ?

> ```
> What is the question: do you want to change the file that is hosted
> on the website?
> ```
> 
> Yes, Can I ?

No, you can upload a new one, but not override the present one.

> No, you can upload a new one

Ok. I have managed to upload in nitrc. PR ?


Resolving patch:
diff --git a/nilearn/datasets/atlas.py b/nilearn/datasets/atlas.py
--- a/nilearn/datasets/atlas.py
+++ b/nilearn/datasets/atlas.py
@@ -334,9 +334,9 @@ def fetch_atlas_msdl(data_dir=None, url=None, resume=True, verbose=1):
                                 verbose=verbose)
     files = _fetch_files(data_dir, files, resume=resume, verbose=verbose)
     csv_data = np.recfromcsv(files[0])
-    labels = csv_data['name'].tolist()
+    labels = [name.strip() for name in csv_data['name'].tolist()]
     region_coords = csv_data[['x', 'y', 'z']].tolist()
-    net_names = csv_data['net_name'].tolist()
+    net_names = [net_name.strip() for net_name in csv_data['net_name'].tolist()]
     fdescr = _get_dataset_descr(dataset_name)
 
     return Bunch(maps=files[1], labels=labels, region_coords=region_coords,


Test patch:
Repository: nilearn/nilearn

Issue:
display.add_edeges uncorrect docstring ?
dcostring says inputs are the map and the affine 

``` Python
def add_edges(self, img, color='r'):
    """ Plot the edges of a 3D map in all the views.
    Parameters
    ----------
    map: 3D ndarray
    affine: 4x4 ndarray
```

but function takes only a Niimg-like object :

``` Python
import nibabel
from nilearn import datasets, plotting
icbm152 = datasets.fetch_icbm152_2009()
img = nibabel.load(icbm152.gm)
display = plotting.plot_anat(icbm152.t1)
display.add_edges(img.get_data(), img.affine)
```

gives an error



Discussion:
> dcostring says inputs are the map and the affine

It's indeed incorrect. Do you want to send a PR to fix that?


Resolving patch:
diff --git a/nilearn/plotting/displays.py b/nilearn/plotting/displays.py
--- a/nilearn/plotting/displays.py
+++ b/nilearn/plotting/displays.py
@@ -746,12 +746,10 @@ def add_edges(self, img, color='r'):
 
         Parameters
         ----------
-        map: 3D ndarray
-            The 3D map to be plotted. If it is a masked array, only
-            the non-masked part will be plotted.
-        affine: 4x4 ndarray
-            The affine matrix giving the transformation from voxel
-            indices to world space.
+        img: Niimg-like object
+            See http://nilearn.github.io/manipulating_images/input_output.html.
+            The 3D map to be plotted.
+            If it is a masked array, only the non-masked part will be plotted.
         color: matplotlib color: string or (r, g, b) value
             The color used to display the edge map
         """


Test patch:
Repository: nilearn/nilearn

Issue:
plot_connectome warning for edge_threshold exceeding weakest connection is uncorrect
``` Python
from nilearn import plotting
adjacency_matrix = np.array([[1., .6], [.6, 1.]])
node_coords = [(40, 20, 44), (-40, 20, 44)]
plotting.plot_connectome(adjacency_matrix, node_coords, edge_threshold=.7)
```

throws the following warning

> nilearn/_utils/param_validation.py:61: UserWarning: The given float value must not exceed 0. But, you have given threshold=0.7 
>   threshold))

The minimal float value is rather .6 not 0.



Discussion:

Resolving patch:
diff --git a/nilearn/_utils/param_validation.py b/nilearn/_utils/param_validation.py
--- a/nilearn/_utils/param_validation.py
+++ b/nilearn/_utils/param_validation.py
@@ -56,9 +56,9 @@ def check_threshold(threshold, data, percentile_func, name='threshold'):
         # value of the image data
         value_check = abs(data).max()
         if abs(threshold) > value_check:
-            warnings.warn("The given float value must not exceed %d. "
-                          "But, you have given threshold=%s " % (value_check,
-                                                                 threshold))
+            warnings.warn("The given float value must not exceed {0}. "
+                          "But, you have given threshold={1} ".format(value_check,
+                                                                      threshold))
     else:
         raise TypeError('%s should be either a number '
                         'or a string finishing with a percent sign' % (name, ))


Test patch:
Repository: nilearn/nilearn

Issue:
tangent does not outperform other connectivity measures in plot_connectivity_measures example
as it is said in doc
https://github.com/nilearn/nilearn/blob/master/examples/03_connectivity/plot_connectivity_measures.py#L8



Discussion:

Resolving patch:
diff --git a/examples/03_connectivity/plot_connectivity_measures.py b/examples/03_connectivity/plot_connectivity_measures.py
--- a/examples/03_connectivity/plot_connectivity_measures.py
+++ b/examples/03_connectivity/plot_connectivity_measures.py
@@ -13,7 +13,7 @@
 # Fetch dataset
 import nilearn.datasets
 atlas = nilearn.datasets.fetch_atlas_msdl()
-dataset = nilearn.datasets.fetch_adhd(n_subjects=30)
+dataset = nilearn.datasets.fetch_adhd(n_subjects=20)
 
 
 ######################################################################
@@ -21,18 +21,17 @@
 import nilearn.input_data
 masker = nilearn.input_data.NiftiMapsMasker(
     atlas.maps, resampling_target="maps", detrend=True,
-    low_pass=None, high_pass=None, t_r=2.5, standardize=False,
+    low_pass=.1, high_pass=.01, t_r=2.5, standardize=False,
     memory='nilearn_cache', memory_level=1)
 subjects = []
 sites = []
 adhds = []
-for func_file, phenotypic in zip(dataset.func, dataset.phenotypic):
-    # keep only 3 sites, to save computation time
-    if phenotypic['site'] in [b'"NYU"', b'"OHSU"', b'"NeuroImage"']:
-        time_series = masker.fit_transform(func_file)
-        subjects.append(time_series)
-        sites.append(phenotypic['site'])
-        adhds.append(phenotypic['adhd'])  # ADHD/control label
+for func_file, confound_file, phenotypic in zip(
+        dataset.func, dataset.confounds, dataset.phenotypic):
+    time_series = masker.fit_transform(func_file, confounds=confound_file)
+    subjects.append(time_series)
+    sites.append(phenotypic['site'])
+    adhds.append(phenotypic['adhd'])  # ADHD/control label
 
 
 ######################################################################
@@ -54,7 +53,7 @@
 
 
 ######################################################################
-# Plot the mean connectome
+# Plot the mean connectome with hemispheric saggital cuts
 import numpy as np
 import nilearn.plotting
 labels = atlas.labels
@@ -62,7 +61,7 @@
 for kind in kinds:
     nilearn.plotting.plot_connectome(mean_connectivity_matrix[kind],
                                      region_coords, edge_threshold='98%',
-                                     title=kind)
+                                     title=kind, display_mode='lzry')
 
 
 ######################################################################
@@ -97,12 +96,3 @@
 plt.grid(True)
 plt.tight_layout()
 plt.show()
-
-
-######################################################################
-# Plot the mean connectome with hemispheric saggital cuts
-for kind in kinds:
-    nilearn.plotting.plot_connectome(mean_connectivity_matrix[kind],
-                                     region_coords, edge_threshold='98%',
-                                     title=kind, display_mode='lzry')
-plt.show()


Test patch:
Repository: nilearn/nilearn

Issue:
np.corrcoef or ConnectivityMeasure
Two connectivity examples use `np.corrcoef`to compute correlation matrix
- `plot_signal_extraction`
- `plot_probabilistic_atlas_extraction`

Shall we rely on `ConnectivityMeasure((cov_estimator=EmpiricalCovariance(), kind='correlation')` instead ?



Discussion:
I think so.

+1


Resolving patch:
diff --git a/examples/03_connectivity/plot_probabilistic_atlas_extraction.py b/examples/03_connectivity/plot_probabilistic_atlas_extraction.py
--- a/examples/03_connectivity/plot_probabilistic_atlas_extraction.py
+++ b/examples/03_connectivity/plot_probabilistic_atlas_extraction.py
@@ -50,10 +50,12 @@
 
 ############################################################################
 # Build and display a correlation matrix
-import numpy as np
-correlation_matrix = np.corrcoef(time_series.T)
+from nilearn.connectome import ConnectivityMeasure
+correlation_measure = ConnectivityMeasure(kind='correlation')
+correlation_matrix = correlation_measure.fit_transform([time_series])[0]
 
 # Display the correlation matrix
+import numpy as np
 from matplotlib import pyplot as plt
 plt.figure(figsize=(10, 10))
 # Mask out the major diagonal
diff --git a/examples/03_connectivity/plot_signal_extraction.py b/examples/03_connectivity/plot_signal_extraction.py
--- a/examples/03_connectivity/plot_signal_extraction.py
+++ b/examples/03_connectivity/plot_signal_extraction.py
@@ -50,10 +50,12 @@
 
 ##############################################################################
 # Compute and display a correlation matrix
-import numpy as np
-correlation_matrix = np.corrcoef(time_series.T)
+from nilearn.connectome import ConnectivityMeasure
+correlation_measure = ConnectivityMeasure(kind='correlation')
+correlation_matrix = correlation_measure.fit_transform([time_series])[0]
 
 # Plot the correlation matrix
+import numpy as np
 from matplotlib import pyplot as plt
 plt.figure(figsize=(10, 10))
 # Mask the main diagonal for visualization:
@@ -75,7 +77,7 @@
 time_series = masker.fit_transform(fmri_filenames)
 # Note how we did not specify confounds above. This is bad!
 
-correlation_matrix = np.corrcoef(time_series.T)
+correlation_matrix = correlation_measure.fit_transform([time_series])[0]
 
 # Mask the main diagonal for visualization:
 np.fill_diagonal(correlation_matrix, 0)


Test patch:
Repository: nilearn/nilearn

Issue:
sklearn.covariance.LedoitWolf or ConnectivityMeasure in plot_adhd_spheres
`ConnectivityMeasure` can be used here, and its default covariance estimator is `LedoitWolf`from `sklearn.covariance`.
I also prefer using partial correlations rather than precision, because no need for negating the connections.



Discussion:

Resolving patch:
diff --git a/examples/03_connectivity/plot_adhd_spheres.py b/examples/03_connectivity/plot_adhd_spheres.py
--- a/examples/03_connectivity/plot_adhd_spheres.py
+++ b/examples/03_connectivity/plot_adhd_spheres.py
@@ -60,23 +60,25 @@
 
 
 ##########################################################################
-# Compute precision matrices
-from sklearn.covariance import LedoitWolf
-cve = LedoitWolf()
-cve.fit(time_series)
-
+# Compute partial correlation matrix using object
+# :class:`nilearn.connectome.ConnectivityMeasure`: Its default covariance
+# estimator is Ledoit-Wolf, allowing to obtain accurate partial correlations.
+from nilearn.connectome import ConnectivityMeasure
+connectivity_measure = ConnectivityMeasure(kind='partial correlation')
+partial_correlation_matrix = connectivity_measure.fit_transform(
+    [time_series])[0]
 
 ##########################################################################
 # Display connectome
 from nilearn import plotting
 
-plotting.plot_connectome(cve.precision_, dmn_coords,
+plotting.plot_connectome(partial_correlation_matrix, dmn_coords,
                          title="Default Mode Network Connectivity")
 
 # Display connectome with hemispheric projections.
 # Notice (0, -52, 18) is included in both hemispheres since x == 0.
 title = "Connectivity projected on hemispheres"
-plotting.plot_connectome(cve.precision_, dmn_coords, title=title,
+plotting.plot_connectome(partial_correlation_matrix, dmn_coords, title=title,
                          display_mode='lyrz')
 
 plotting.show()


Test patch: