Skip to content

Commit

Permalink
Replace deprecated param 'cachedir' with 'location' in joblib's Memor…
Browse files Browse the repository at this point in the history
…y object (nilearn#1946)

* Replaced deprecated param 'cachedir' with 'location' in joblib's Memory object

* Bumped up minimum Joblib  version to 0.12

* Updated obsolete Agent (VM version) to fix failure

* Remove use of SKL-vendored Joblib within Nilearn

* Removed commented out section

* Updated whats_new with warning & version number to 0.7.0a

* Replaced former nistats' use of sklearn vendored joblib with installed joblib
  • Loading branch information
kchawla-pi authored Mar 26, 2020
1 parent 8f8b63b commit 44a6765
Show file tree
Hide file tree
Showing 26 changed files with 64 additions and 62 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ matrix:
python: "3.5"
env: DISTRIB="travisci" PYTHON_VERSION="3.5" NIBABEL_VERSION="2.0.2"
NUMPY_VERSION="1.11" SCIPY_VERSION="0.19" PANDAS_VERSION="0.23.0"
SCIKIT_LEARN_VERSION="0.19" COVERAGE="true" JOBLIB_VERSION="0.11"
SCIKIT_LEARN_VERSION="0.19" COVERAGE="true" JOBLIB_VERSION="0.12"
LXML_VERSION="*"
- name: "Python 3.5 latest package versions"
python: "3.5"
env: DISTRIB="travisci" PYTHON_VERSION="3.5"
NUMPY_VERSION="*" SCIPY_VERSION="*" PANDAS_VERSION="*"
SCIKIT_LEARN_VERSION="*" MATPLOTLIB_VERSION="*" COVERAGE="true"
JOBLIB_VERSION="0.11"
JOBLIB_VERSION="0.12"
LXML_VERSION="*"
- name: "Python 3.6 latest package versions"
python: "3.6"
Expand Down
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ The required dependencies to use the software are:
* Numpy >= 1.11
* SciPy >= 0.19
* Scikit-learn >= 0.19
* Joblib >= 0.11
* Joblib >= 0.12
* Nibabel >= 2.0.2

If you are using nilearn plotting functionalities or running the
Expand Down
5 changes: 5 additions & 0 deletions doc/whats_new.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
0.7.0a
======

.. warning::

Minimum required version of Joblib is now 0.12.


NEW
---

Expand Down
36 changes: 18 additions & 18 deletions nilearn/_utils/cache_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def _check_memory(memory, verbose=0):
instance of joblib.Memory.
"""
if memory is None:
memory = Memory(cachedir=None, verbose=verbose)
memory = Memory(location=None, verbose=verbose)
if isinstance(memory, str):
cache_dir = memory
if nilearn.EXPAND_PATH_WILDCARDS:
Expand Down Expand Up @@ -74,7 +74,7 @@ def _check_memory(memory, verbose=0):
.format(split_cache_dir[0]))
raise ValueError(error_msg)

memory = Memory(cachedir=cache_dir, verbose=verbose)
memory = Memory(location=cache_dir, verbose=verbose)
return memory


Expand All @@ -89,16 +89,16 @@ def _safe_cache(memory, func, **kwargs):
if that fails, ensuring th warning is not generated in any case.
'''
try:
cachedir = os.path.join(memory.location, 'joblib')
location = os.path.join(memory.location, 'joblib')
except AttributeError:
cachedir = memory.cachedir
location = memory.location
except TypeError:
cachedir = None
location = None

if cachedir is None or cachedir in __CACHE_CHECKED:
if location is None or location in __CACHE_CHECKED:
return memory.cache(func, **kwargs)

version_file = os.path.join(cachedir, 'module_versions.json')
version_file = os.path.join(location, 'module_versions.json')

versions = dict()
if os.path.exists(version_file):
Expand All @@ -119,34 +119,34 @@ def _safe_cache(memory, func, **kwargs):
"different version of nibabel. Deleting "
"the cache. Put nilearn.CHECK_CACHE_VERSION "
"to false to avoid this behavior."
% cachedir)
% location)
try:
tmp_dir = (os.path.split(cachedir)[:-1]
tmp_dir = (os.path.split(location)[:-1]
+ ('old_%i' % os.getpid(), ))
tmp_dir = os.path.join(*tmp_dir)
# We use rename + unlink to be more robust to race
# conditions
os.rename(cachedir, tmp_dir)
os.rename(location, tmp_dir)
shutil.rmtree(tmp_dir)
except OSError:
# Another process could have removed this dir
pass

try:
os.makedirs(cachedir)
os.makedirs(location)
except OSError:
# File exists?
pass
else:
warnings.warn("Incompatible cache in %s: "
"old version of nibabel." % cachedir)
"old version of nibabel." % location)

# Write json files if configuration is different
if versions != my_versions:
with open(version_file, 'w') as _version_file:
json.dump(my_versions, _version_file)

__CACHE_CHECKED[cachedir] = True
__CACHE_CHECKED[location] = True

return memory.cache(func, **kwargs)

Expand Down Expand Up @@ -217,12 +217,12 @@ def cache(func, memory, func_memory_level=None, memory_level=None,
if memory is not None and (func_memory_level is None or
memory_level >= func_memory_level):
if isinstance(memory, str):
memory = Memory(cachedir=memory, verbose=verbose)
memory = Memory(location=memory, verbose=verbose)
if not isinstance(memory, MEMORY_CLASSES):
raise TypeError("'memory' argument must be a string or a "
"joblib.Memory object. "
"%s %s was given." % (memory, type(memory)))
if (memory.cachedir is None and memory_level is not None
if (memory.location is None and memory_level is not None
and memory_level > 1):
warnings.warn("Caching has been enabled (memory_level = %d) "
"but no Memory object or path has been provided"
Expand All @@ -231,7 +231,7 @@ def cache(func, memory, func_memory_level=None, memory_level=None,
(memory_level, func.__name__),
stacklevel=2)
else:
memory = Memory(cachedir=None, verbose=verbose)
memory = Memory(location=None, verbose=verbose)
cached_func = _safe_cache(memory, func, **kwargs)
if shelve:
cached_func = _ShelvedFunc(cached_func)
Expand Down Expand Up @@ -286,12 +286,12 @@ def _cache(self, func, func_memory_level=1, shelve=False, **kwargs):
if not hasattr(self, "memory_level"):
self.memory_level = 0
if not hasattr(self, "memory"):
self.memory = Memory(cachedir=None, verbose=verbose)
self.memory = Memory(location=None, verbose=verbose)
self.memory = _check_memory(self.memory, verbose=verbose)

# If cache level is 0 but a memory object has been provided, set
# memory_level to 1 with a warning.
if self.memory_level == 0 and self.memory.cachedir is not None:
if self.memory_level == 0 and self.memory.location is not None:
warnings.warn("memory_level is currently set to 0 but "
"a Memory object has been provided. "
"Setting memory_level to 1.")
Expand Down
5 changes: 1 addition & 4 deletions nilearn/_utils/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,7 @@ def md5_hash(string):
return m.hexdigest()


if sklearn.__version__ < '0.21':
from sklearn.externals import joblib
else:
import joblib
import joblib

Memory = joblib.Memory
Parallel = joblib.Parallel
Expand Down
4 changes: 2 additions & 2 deletions nilearn/_utils/niimg_conversions.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def _index_img(img, index):

def _iter_check_niimg(niimgs, ensure_ndim=None, atleast_4d=False,
target_fov=None, dtype=None,
memory=Memory(cachedir=None),
memory=Memory(location=None),
memory_level=0, verbose=0):
"""Iterate over a list of niimgs and do sanity checks and resampling
Expand Down Expand Up @@ -357,7 +357,7 @@ def check_niimg_4d(niimg, return_iterator=False, dtype=None):


def concat_niimgs(niimgs, dtype=np.float32, ensure_ndim=None,
memory=Memory(cachedir=None), memory_level=0,
memory=Memory(location=None), memory_level=0,
auto_resample=False, verbose=0):
"""Concatenate a list of 3D/4D niimgs of varying lengths.
Expand Down
2 changes: 1 addition & 1 deletion nilearn/connectome/group_sparse_cov.py
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,7 @@ class GroupSparseCovariance(BaseEstimator, CacheMixin):
"""

def __init__(self, alpha=0.1, tol=1e-3, max_iter=10, verbose=0,
memory=Memory(cachedir=None), memory_level=0):
memory=Memory(location=None), memory_level=0):
self.alpha = alpha
self.tol = tol
self.max_iter = max_iter
Expand Down
4 changes: 2 additions & 2 deletions nilearn/decomposition/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def mask_and_reduce(masker, imgs,
reduction_ratio='auto',
n_components=None, random_state=None,
memory_level=0,
memory=Memory(cachedir=None),
memory=Memory(location=None),
n_jobs=1):
"""Mask and reduce provided 4D images with given masker.
Expand Down Expand Up @@ -326,7 +326,7 @@ def __init__(self, n_components=20,
low_pass=None, high_pass=None, t_r=None,
target_affine=None, target_shape=None,
mask_strategy='epi', mask_args=None,
memory=Memory(cachedir=None), memory_level=0,
memory=Memory(location=None), memory_level=0,
n_jobs=1,
verbose=0):
self.n_components = n_components
Expand Down
2 changes: 1 addition & 1 deletion nilearn/decomposition/canica.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ def __init__(self, mask=None, n_components=20, smoothing_fwhm=6,
low_pass=None, high_pass=None, t_r=None,
target_affine=None, target_shape=None,
mask_strategy='epi', mask_args=None,
memory=Memory(cachedir=None), memory_level=0,
memory=Memory(location=None), memory_level=0,
n_jobs=1, verbose=0
):

Expand Down
2 changes: 1 addition & 1 deletion nilearn/decomposition/dict_learning.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ def __init__(self, n_components=20,
smoothing_fwhm=4, standardize=True, detrend=True,
low_pass=None, high_pass=None, t_r=None, target_affine=None,
target_shape=None, mask_strategy='epi', mask_args=None,
n_jobs=1, verbose=0, memory=Memory(cachedir=None),
n_jobs=1, verbose=0, memory=Memory(location=None),
memory_level=0):
BaseDecomposition.__init__(self, n_components=n_components,
random_state=random_state, mask=mask,
Expand Down
2 changes: 1 addition & 1 deletion nilearn/decomposition/multi_pca.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ def __init__(self, n_components=20,
low_pass=None, high_pass=None, t_r=None,
target_affine=None, target_shape=None,
mask_strategy='epi', mask_args=None,
memory=Memory(cachedir=None), memory_level=0,
memory=Memory(location=None), memory_level=0,
n_jobs=1,
verbose=0
):
Expand Down
2 changes: 1 addition & 1 deletion nilearn/input_data/base_masker.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

def filter_and_extract(imgs, extraction_function,
parameters,
memory_level=0, memory=Memory(cachedir=None),
memory_level=0, memory=Memory(location=None),
verbose=0,
confounds=None,
copy=True,
Expand Down
2 changes: 1 addition & 1 deletion nilearn/input_data/multi_nifti_masker.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ def __init__(self, mask_img=None, smoothing_fwhm=None,
standardize=False, detrend=False, low_pass=None,
high_pass=None, t_r=None, target_affine=None,
target_shape=None, mask_strategy='background',
mask_args=None, dtype=None, memory=Memory(cachedir=None),
mask_args=None, dtype=None, memory=Memory(location=None),
memory_level=0, n_jobs=1, verbose=0):
# Mask is provided or computed
self.mask_img = mask_img
Expand Down
2 changes: 1 addition & 1 deletion nilearn/input_data/nifti_labels_masker.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ def __init__(self, labels_img, background_label=0, mask_img=None,
smoothing_fwhm=None, standardize=False, detrend=False,
low_pass=None, high_pass=None, t_r=None, dtype=None,
resampling_target="data",
memory=Memory(cachedir=None, verbose=0), memory_level=1,
memory=Memory(location=None, verbose=0), memory_level=1,
verbose=0, strategy="mean"):
self.labels_img = labels_img
self.background_label = background_label
Expand Down
2 changes: 1 addition & 1 deletion nilearn/input_data/nifti_maps_masker.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ def __init__(self, maps_img, mask_img=None,
allow_overlap=True, smoothing_fwhm=None, standardize=False,
detrend=False, low_pass=None, high_pass=None, t_r=None,
dtype=None, resampling_target="data",
memory=Memory(cachedir=None, verbose=0), memory_level=0,
memory=Memory(location=None, verbose=0), memory_level=0,
verbose=0):
self.maps_img = maps_img
self.mask_img = mask_img
Expand Down
4 changes: 2 additions & 2 deletions nilearn/input_data/nifti_masker.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def __call__(self, imgs):


def filter_and_mask(imgs, mask_img_, parameters,
memory_level=0, memory=Memory(cachedir=None),
memory_level=0, memory=Memory(location=None),
verbose=0,
confounds=None,
copy=True,
Expand Down Expand Up @@ -189,7 +189,7 @@ def __init__(self, mask_img=None, sessions=None, smoothing_fwhm=None,
target_affine=None, target_shape=None,
mask_strategy='background',
mask_args=None, sample_mask=None, dtype=None,
memory_level=1, memory=Memory(cachedir=None),
memory_level=1, memory=Memory(location=None),
verbose=0, reports=True,
):
# Mask is provided or computed
Expand Down
2 changes: 1 addition & 1 deletion nilearn/input_data/nifti_spheres_masker.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ class NiftiSpheresMasker(BaseMasker, CacheMixin):
def __init__(self, seeds, radius=None, mask_img=None, allow_overlap=False,
smoothing_fwhm=None, standardize=False, detrend=False,
low_pass=None, high_pass=None, t_r=None, dtype=None,
memory=Memory(cachedir=None, verbose=0), memory_level=1,
memory=Memory(location=None, verbose=0), memory_level=1,
verbose=0):
self.seeds = seeds
self.mask_img = mask_img
Expand Down
2 changes: 1 addition & 1 deletion nilearn/input_data/tests/test_masker_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def __init__(self, mask=None, smoothing_fwhm=None,
low_pass=None, high_pass=None, t_r=None,
target_affine=None, target_shape=None,
mask_strategy='background', mask_args=None,
memory=Memory(cachedir=None), memory_level=0,
memory=Memory(location=None), memory_level=0,
n_jobs=1, verbose=0,
dummy=None):
self.mask = mask
Expand Down
2 changes: 1 addition & 1 deletion nilearn/input_data/tests/test_multi_nifti_masker.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ def test_shelving():
cachedir = mkdtemp()
try:
masker_shelved = MultiNiftiMasker(mask_img=mask_img,
memory=Memory(cachedir=cachedir,
memory=Memory(location=cachedir,
mmap_mode='r',
verbose=0))
masker_shelved._shelving = True
Expand Down
2 changes: 1 addition & 1 deletion nilearn/input_data/tests/test_nifti_masker.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ def test_joblib_cache():
# imgs return by inverse_transform impossible to save
cachedir = mkdtemp()
try:
masker.memory = Memory(cachedir=cachedir, mmap_mode='r',
masker.memory = Memory(location=cachedir, mmap_mode='r',
verbose=0)
X = masker.transform(mask_img)
# inverse_transform a first time, so that the result is cached
Expand Down
2 changes: 1 addition & 1 deletion nilearn/regions/parcellations.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ def __init__(self, method, n_parcels=50,
target_affine=None, target_shape=None,
mask_strategy='epi', mask_args=None,
scaling=False, n_iter=10,
memory=Memory(cachedir=None),
memory=Memory(location=None),
memory_level=0, n_jobs=1, verbose=1):

self.method = method
Expand Down
2 changes: 1 addition & 1 deletion nilearn/regions/region_extractor.py
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ def __init__(self, maps_img, mask_img=None, min_region_size=1350,
extractor='local_regions', smoothing_fwhm=6,
standardize=False, detrend=False,
low_pass=None, high_pass=None, t_r=None,
memory=Memory(cachedir=None), memory_level=0, verbose=0):
memory=Memory(location=None), memory_level=0, verbose=0):
super(RegionExtractor, self).__init__(
maps_img=maps_img, mask_img=mask_img,
smoothing_fwhm=smoothing_fwhm,
Expand Down
8 changes: 4 additions & 4 deletions nilearn/stats/first_level_model/first_level_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@
import numpy as np
import pandas as pd

from joblib import (delayed,
Memory,
Parallel,
)
from nibabel import Nifti1Image
from nibabel.onetime import setattr_on_read
from sklearn.base import (BaseEstimator,
clone,
TransformerMixin,
)
from sklearn.externals.joblib import (delayed,
Memory,
Parallel,
)

from nilearn._utils import CacheMixin
from nilearn._utils.niimg_conversions import check_niimg
Expand Down
2 changes: 1 addition & 1 deletion nilearn/stats/second_level_model/second_level_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
import pandas as pd
import numpy as np

from joblib import Memory
from nibabel import Nifti1Image
from sklearn.base import BaseEstimator, TransformerMixin, clone
from sklearn.externals.joblib import Memory

from nilearn._utils import CacheMixin
from nilearn._utils.niimg_conversions import check_niimg
Expand Down
Loading

0 comments on commit 44a6765

Please sign in to comment.