Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MAINT] remove obsolete version comparison #3883

Merged
merged 9 commits into from
Aug 10, 2023
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 2 additions & 12 deletions nilearn/_utils/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@
from pathlib import Path

import pytest
import sklearn

from nilearn._utils import _compare_version

# we use memory_profiler library for memory consumption checks
try:
Expand Down Expand Up @@ -56,15 +53,8 @@

@functools.wraps(func)
def wrapped(*args, **kwargs):
# --- CAN BE REMOVED --- #
if _compare_version(sklearn.__version__, "<", "0.22"):
with pytest.deprecated_call():
result = func(*args, **kwargs)
# --- CAN BE REMOVED --- #

else:
with pytest.warns(FutureWarning, match=match):
result = func(*args, **kwargs)
with pytest.warns(FutureWarning, match=match):
result = func(*args, **kwargs)

Check warning on line 57 in nilearn/_utils/testing.py

View check run for this annotation

Codecov / codecov/patch

nilearn/_utils/testing.py#L57

Added line #L57 was not covered by tests
return result

return wrapped
Expand Down
13 changes: 6 additions & 7 deletions nilearn/image/resampling.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,9 @@
import warnings

import numpy as np
import scipy
from scipy import linalg
from scipy.ndimage import affine_transform, find_objects

from nilearn._utils import _compare_version

from .. import _utils
from .._utils import stringify_path
from .._utils.niimg import _get_data
Expand Down Expand Up @@ -300,8 +297,9 @@ def _resample_one_img(

# Suppresses warnings in https://github.com/nilearn/nilearn/issues/1363
with warnings.catch_warnings():
if _compare_version(scipy.__version__, ">=", "0.18"):
warnings.simplefilter("ignore", UserWarning)
warnings.filterwarnings(
"ignore", message=".*has changed in SciPy 0.18.*"
)
Remi-Gau marked this conversation as resolved.
Show resolved Hide resolved
# The resampling itself
affine_transform(
data,
Expand All @@ -316,8 +314,9 @@ def _resample_one_img(
if has_not_finite:
# Suppresses warnings in https://github.com/nilearn/nilearn/issues/1363
with warnings.catch_warnings():
if _compare_version(scipy.__version__, ">=", "0.18"):
warnings.simplefilter("ignore", UserWarning)
warnings.filterwarnings(
"ignore", message=".*has changed in SciPy 0.18.*"
)
# We need to resample the mask of not_finite values
not_finite = affine_transform(
not_finite,
Expand Down
26 changes: 6 additions & 20 deletions nilearn/plotting/displays/_slicers.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import collections
import numbers

import matplotlib
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.colorbar import ColorbarBase
from matplotlib.colors import LinearSegmentedColormap, ListedColormap
from matplotlib.transforms import Bbox

from nilearn._utils import _compare_version, check_niimg_3d
from nilearn._utils import check_niimg_3d
from nilearn._utils.docs import fill_doc
from nilearn._utils.niimg import _is_binary_niimg, _safe_get_data
from nilearn.image import get_data, new_img_like, reorder_img
Expand Down Expand Up @@ -462,13 +461,9 @@ def _map_show(
ims = []
to_iterate_over = zip(self.axes.values(), data_2d_list)
for display_ax, data_2d in to_iterate_over:
if data_2d is not None and data_2d.min() is not np.ma.masked:
if data_2d is not None:
# If data_2d is completely masked, then there is nothing to
# plot. Hence, no point to do imshow(). Moreover, we see
# problem came up with matplotlib 2.1.0 (issue #9280) when
# data is completely masked or with numpy < 1.14
# (issue #4595). This work around can be removed when bumping
# matplotlib version above 2.1.0
# plot. Hence, no point to do imshow().
im = display_ax.draw_2d(
data_2d, data_bounds, bounding_box, type=type, **kwargs
)
Expand Down Expand Up @@ -523,10 +518,7 @@ def _show_colorbar(
- (self._colorbar_margin["top"] + self._colorbar_margin["bottom"]),
]
self._colorbar_ax = figure.add_axes(lt_wid_top_ht)
if _compare_version(matplotlib.__version__, ">=", "1.6"):
self._colorbar_ax.set_facecolor("w")
else:
self._colorbar_ax.set_axis_bgcolor("w")
self._colorbar_ax.set_facecolor("w")

our_cmap = plt.get_cmap(cmap)
# edge case where the data has a single value
Expand Down Expand Up @@ -902,10 +894,7 @@ def _init_axes(self, **kwargs):
[0.3 * index * (x1 - x0) + x0, y0, 0.3 * (x1 - x0), y1 - y0],
aspect="equal",
)
if _compare_version(matplotlib.__version__, ">=", "1.6"):
ax.set_facecolor(facecolor)
else:
ax.set_axis_bgcolor(facecolor)
ax.set_facecolor(facecolor)

ax.axis("off")
coord = self.cut_coords[
Expand Down Expand Up @@ -1173,10 +1162,7 @@ def _init_axes(self, **kwargs):
axes_coords = self._find_initial_axes_coord(index)
ax = fh.add_axes(axes_coords, aspect="equal")

if _compare_version(matplotlib.__version__, ">=", "1.6"):
ax.set_facecolor(facecolor)
else:
ax.set_axis_bgcolor(facecolor)
ax.set_facecolor(facecolor)

ax.axis("off")
coord = self.cut_coords[
Expand Down
8 changes: 1 addition & 7 deletions nilearn/plotting/glass_brain.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,9 @@
import json
import os

import matplotlib
from matplotlib import colors, patches, transforms
from matplotlib.path import Path

from nilearn._utils import _compare_version


def _codes_bezier(pts):
bezier_num = len(pts)
Expand Down Expand Up @@ -160,10 +157,7 @@ def plot_brain_schematics(ax, direction, **kwargs):
Useful for the caller to be able to set axes limits.

"""
if _compare_version(matplotlib.__version__, '>=', "2.0"):
get_axis_bg_color = ax.get_facecolor()
else:
get_axis_bg_color = ax.get_axis_bgcolor()
get_axis_bg_color = ax.get_facecolor()

black_bg = colors.colorConverter.to_rgba(get_axis_bg_color) \
== colors.colorConverter.to_rgba('k')
Expand Down
1 change: 0 additions & 1 deletion nilearn/plotting/tests/test_displays.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,6 @@ def test_user_given_cmap_with_colorbar(img):
@pytest.mark.parametrize("display", [OrthoSlicer, LYRZProjector])
def test_data_complete_mask(display):
"""Test for a special case due to matplotlib 2.1.0.

When the data is completely masked, then we have plotting issues
See similar issue #9280 reported in matplotlib. This function
tests the patch added for this particular issue.
Expand Down
5 changes: 3 additions & 2 deletions nilearn/surface/surface.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,15 +104,16 @@ def _sample_locations_between_surfaces(
mesh, inner_mesh, affine, n_points=10, depth=None):
outer_vertices, _ = mesh
inner_vertices, _ = inner_mesh
# when we drop support for np 1.5 replace the next 2 lines with
# sample_locations = np.linspace(inner_vertices, outer_vertices, n_points)

if depth is None:
steps = np.linspace(0, 1, n_points)[:, None, None]
else:
steps = np.asarray(depth)[:, None, None]

sample_locations = outer_vertices + steps * (
inner_vertices - outer_vertices)
sample_locations = np.rollaxis(sample_locations, 1)

sample_locations_voxel_space = np.asarray(
resampling.coord_transform(
*np.vstack(sample_locations).T,
Expand Down
14 changes: 10 additions & 4 deletions nilearn/surface/tests/test_surface.py
Original file line number Diff line number Diff line change
Expand Up @@ -562,24 +562,30 @@ def test_sample_locations_depth(depth, n_points):

@pytest.mark.parametrize(
"depth,n_points",
[(None, 1), (None, 7), ([0.], 8), ([-1.], 8),
([1.], 8), ([-1., 0., .5], 8)])
[(None, 1),
(None, 7),
([0.], 8),
([-1.], 8),
([1.], 8),
([-1., 0., .5], 8)])
def test_sample_locations_between_surfaces(depth, n_points):
Remi-Gau marked this conversation as resolved.
Show resolved Hide resolved
inner = flat_mesh(5, 7)
outer = inner[0] + [0., 0., 1.], inner[1]

locations = surface._sample_locations_between_surfaces(
outer, inner, np.eye(4), n_points=n_points, depth=depth)

if depth is None:
# can be simplified when we drop support for np 1.15
# (broadcasting linspace)
expected = np.asarray(
[np.linspace(b, a, n_points)
for (a, b) in zip(inner[0].ravel(), outer[0].ravel())])
expected = np.rollaxis(
expected.reshape((*outer[0].shape, n_points)), 2, 1)

else:
offsets = ([[0., 0., - z] for z in depth])
expected = np.asarray([vertex + offsets for vertex in outer[0]])

assert np.allclose(locations, expected)


Expand Down