Skip to content

Commit

Permalink
Merge branch 'master' of github.com:nilearn/nilearn into release-060a
Browse files Browse the repository at this point in the history
* 'master' of github.com:nilearn/nilearn:
  Fix cache mixin tests (nilearn#2161)
  Do not fail if metadata cannot be updated for an image (nilearn#2167)
  [MRG] Nans in view connectome (nilearn#2166)
  • Loading branch information
kchawla-pi committed Oct 14, 2019
2 parents 3c470ae + 1905835 commit 4970382
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 56 deletions.
3 changes: 3 additions & 0 deletions doc/whats_new.rst
Expand Up @@ -64,6 +64,9 @@ Changes
colormap. These arguments were already accepted in `kwargs` but not documented
before.

- :func:`nilearn.plotting.view_connectome` now converts NaNs in the adjacency
matrix to 0.

Fixes
-----

Expand Down
24 changes: 15 additions & 9 deletions nilearn/datasets/neurovault.py
Expand Up @@ -1651,19 +1651,25 @@ def _update_image(image_info, download_params):
"""
if not download_params['write_ok']:
return image_info
collection = _fetch_collection_for_image(
image_info, download_params)
image_info, collection = _download_image_terms(
image_info, collection, download_params)
metadata_file_path = os.path.join(
os.path.dirname(image_info['absolute_path']),
'image_{0}_metadata.json'.format(image_info['id']))
_write_metadata(image_info, metadata_file_path)
try:
collection = _fetch_collection_for_image(
image_info, download_params)
image_info, collection = _download_image_terms(
image_info, collection, download_params)
metadata_file_path = os.path.join(
os.path.dirname(image_info['absolute_path']),
'image_{0}_metadata.json'.format(image_info['id']))
_write_metadata(image_info, metadata_file_path)
except OSError:
warnings.warn(
"could not update metadata for image {}, "
"most likely because you do not have write "
"permissions to its metadata file".format(image_info["id"]))
return image_info


def _update(image_info, collection, download_params):
"""Update local metadata for an image and its collection."""
"Update local metadata for an image and its collection."""
image_info = _update_image(image_info, download_params)
return image_info, collection

Expand Down
2 changes: 1 addition & 1 deletion nilearn/plotting/html_connectome.py
Expand Up @@ -31,7 +31,7 @@ def _get_connectome(adjacency_matrix, coords, threshold=None,
marker_size=None, cmap=cm.cold_hot, symmetric_cmap=True):
connectome = {}
coords = np.asarray(coords, dtype='<f4')
adjacency_matrix = adjacency_matrix.copy()
adjacency_matrix = np.nan_to_num(adjacency_matrix, copy=True)
colors = colorscale(
cmap, adjacency_matrix.ravel(), threshold=threshold,
symmetric_cmap=symmetric_cmap)
Expand Down
14 changes: 10 additions & 4 deletions nilearn/plotting/tests/test_html_connectome.py
Expand Up @@ -47,6 +47,11 @@ def test_get_connectome():
assert {'_con_x', '_con_y', '_con_z', '_con_w', 'colorscale'
}.issubset(connectome.keys())
assert (connectome['cmin'], connectome['cmax']) == (-2.5, 2.5)
adj[adj == 0] = np.nan
connectome = html_connectome._get_connectome(adj, coord)
con_x = decode(connectome['_con_x'], '<f4')
assert (con_x == expected_x).all()
assert (connectome['cmin'], connectome['cmax']) == (-2.5, 2.5)


def test_view_connectome():
Expand Down Expand Up @@ -129,10 +134,11 @@ def test_params_deprecation_view_connectome():
)
old_params = ['coords', 'threshold', 'cmap', 'marker_size']

assert len(raised_warnings) == 4
for old_param_, raised_warning_ in zip(old_params, raised_warnings):
assert warning_msgs[old_param_] == str(raised_warning_.message)
assert raised_warning_.category is DeprecationWarning
raised_warning_messages = ''.join(
str(warning.message) for warning in raised_warnings)
print(raised_warning_messages)
for old_param_ in old_params:
assert warning_msgs[old_param_] in raised_warning_messages


def test_get_markers():
Expand Down
68 changes: 26 additions & 42 deletions nilearn/tests/test_cache_mixin.py
@@ -1,14 +1,12 @@
"""
Test the _utils.cache_mixin module
"""
import glob
import json
import os
import shutil
import tempfile
from distutils.version import LooseVersion
from pathlib import Path

import sklearn
from nose.tools import assert_false, assert_true, assert_equal
from nilearn._utils.compat import Memory

Expand All @@ -17,6 +15,11 @@
from nilearn._utils.testing import assert_raises_regex


def _get_subdirs(top_dir):
top_dir = Path(top_dir)
children = list(top_dir.glob("*"))
return [child for child in children if child.is_dir()]


def f(x):
# A simple test function
Expand All @@ -26,8 +29,7 @@ def f(x):
def test_check_memory():
# Test if _check_memory returns a memory object with the cachedir equal to
# input path
try:
temp_dir = tempfile.mkdtemp()
with tempfile.TemporaryDirectory() as temp_dir:

mem_none = Memory(cachedir=None)
mem_temp = Memory(cachedir=temp_dir)
Expand All @@ -42,17 +44,11 @@ def test_check_memory():
assert_equal(memory.cachedir, mem_temp.cachedir)
assert_true(memory, Memory)

finally:
if os.path.exists(temp_dir):
shutil.rmtree(temp_dir)



def test__safe_cache_dir_creation():
# Test the _safe_cache function that is supposed to flush the
# cache if the nibabel version changes
try:
temp_dir = tempfile.mkdtemp()
with tempfile.TemporaryDirectory() as temp_dir:
mem = Memory(cachedir=temp_dir)
version_file = os.path.join(temp_dir, 'joblib', 'module_versions.json')
assert_false(os.path.exists(version_file))
Expand All @@ -63,16 +59,12 @@ def test__safe_cache_dir_creation():
os.unlink(version_file)
cache_mixin._safe_cache(mem, f)
assert_false(os.path.exists(version_file))
finally:
if os.path.exists(temp_dir):
shutil.rmtree(temp_dir)


def test__safe_cache_flush():
# Test the _safe_cache function that is supposed to flush the
# cache if the nibabel version changes
try:
temp_dir = tempfile.mkdtemp()
with tempfile.TemporaryDirectory() as temp_dir:
mem = Memory(cachedir=temp_dir)
version_file = os.path.join(temp_dir, 'joblib', 'module_versions.json')
# Create an mock version_file with old module versions
Expand All @@ -96,25 +88,21 @@ def test__safe_cache_flush():
cache_mixin._safe_cache(mem, f)
assert_true(os.path.exists(version_file))
assert_false(os.path.exists(nibabel_dir))
finally:
pass
# if os.path.exists(temp_dir):
# shutil.rmtree(temp_dir)


def test_cache_memory_level():
temp_dir = tempfile.mkdtemp()
job_glob = os.path.join(temp_dir, 'joblib', 'nilearn', 'tests',
'test_cache_mixin', 'f', '*')
mem = Memory(cachedir=temp_dir, verbose=0)
cache_mixin.cache(f, mem, func_memory_level=2, memory_level=1)(2)
assert_equal(len(glob.glob(job_glob)), 0)
cache_mixin.cache(f, Memory(cachedir=None))(2)
assert_equal(len(glob.glob(job_glob)), 0)
cache_mixin.cache(f, mem, func_memory_level=2, memory_level=3)(2)
assert_equal(len(glob.glob(job_glob)), 2)
cache_mixin.cache(f, mem)(3)
assert_equal(len(glob.glob(job_glob)), 3)
with tempfile.TemporaryDirectory() as temp_dir:
joblib_dir = Path(
temp_dir, 'joblib', 'nilearn', 'tests', 'test_cache_mixin', 'f')
mem = Memory(cachedir=temp_dir, verbose=0)
cache_mixin.cache(f, mem, func_memory_level=2, memory_level=1)(2)
assert_equal(len(_get_subdirs(joblib_dir)), 0)
cache_mixin.cache(f, Memory(cachedir=None))(2)
assert_equal(len(_get_subdirs(joblib_dir)), 0)
cache_mixin.cache(f, mem, func_memory_level=2, memory_level=3)(2)
assert_equal(len(_get_subdirs(joblib_dir)), 1)
cache_mixin.cache(f, mem)(3)
assert_equal(len(_get_subdirs(joblib_dir)), 2)


class CacheMixinTest(CacheMixin):
Expand Down Expand Up @@ -182,17 +170,13 @@ def test_cache_mixin_wrong_dirs():


def test_cache_shelving():
try:
temp_dir = tempfile.mkdtemp()
job_glob = os.path.join(temp_dir, 'joblib', 'nilearn', 'tests',
'test_cache_mixin', 'f', '*')
with tempfile.TemporaryDirectory() as temp_dir:
joblib_dir = Path(
temp_dir, 'joblib', 'nilearn', 'tests', 'test_cache_mixin', 'f')
mem = Memory(cachedir=temp_dir, verbose=0)
res = cache_mixin.cache(f, mem, shelve=True)(2)
assert_equal(res.get(), 2)
assert_equal(len(glob.glob(job_glob)), 1)
assert_equal(len(_get_subdirs(joblib_dir)), 1)
res = cache_mixin.cache(f, mem, shelve=True)(2)
assert_equal(res.get(), 2)
assert_equal(len(glob.glob(job_glob)), 1)
finally:
del mem
shutil.rmtree(temp_dir, ignore_errors=True)
assert_equal(len(_get_subdirs(joblib_dir)), 1)

0 comments on commit 4970382

Please sign in to comment.