Repository: nilearn/nilearn

Issue:
Change default dataset download directory 
Copied and pasted from different emails with minor tweaks.
# The problem

People often complain about nilearn data storage policy: by default datasets are stored in a "nilearn_data" folder created in the current directory. People are sometimes puzzled by the fact that, when they change directory, some examples download an already-downloaded dataset.
# Caveats
1. The number one problem is of disk space running out. In the home, it can have horrible consequences, because it tends to freeze many things. For example you may have a quota on your home folder that is easily overwhelmed by the size, roughly 5-10 GB, of the datasets.
2. Warnings, configuration options, ... are mostly useless, because the non-advanced users don't read/use them
# Proposed solution
- Use $HOME/nilearn_data by default
- In all the data downloaders:
  - capture the I/O errors that pop up when writing a file and that
      the disk is full
  - erase a bit of that half-written file (or all of it) to avoid
      leaving the disk full
  - raise an error clearly identifying the problem (disk full or
      quota blown) and suggesting solutions (the NILEARN_DATA
      variable + talk to your sysadmin).



Discussion:
The error message part of this has been tackled in #262.


Resolving patch:
diff --git a/nilearn/datasets.py b/nilearn/datasets.py
--- a/nilearn/datasets.py
+++ b/nilearn/datasets.py
@@ -186,7 +186,7 @@ def _get_dataset_dir(dataset_name, data_dir=None, verbose=0):
     1. the keyword argument data_dir
     2. the global environment variable NILEARN_SHARED_DATA
     3. the user environment variable NILEARN_DATA
-    4. "nilearn_data" directory into the current working directory
+    4. nilearn_data in the user home folder
     """
     # We build an array of successive paths by priority
     paths = []
@@ -203,7 +203,7 @@ def _get_dataset_dir(dataset_name, data_dir=None, verbose=0):
         if local_data is not None:
             paths.extend(local_data.split(':'))
 
-        paths.append(os.path.join(os.getcwd(), 'nilearn_data'))
+        paths.append(os.path.expanduser('~/nilearn_data'))
 
     if verbose > 2:
         print 'Dataset search paths:', paths


Test patch:
diff --git a/nilearn/tests/test_datasets.py b/nilearn/tests/test_datasets.py
--- a/nilearn/tests/test_datasets.py
+++ b/nilearn/tests/test_datasets.py
@@ -57,47 +57,49 @@ def test_md5_sum_file():
     assert_equal(datasets._md5_sum_file(f), '18f32295c556b2a1a3a8e68fe1ad40f7')
     os.remove(f)
 
+
 @with_setup(setup_tmpdata, teardown_tmpdata)
 def test_get_dataset_dir():
-
-    os.chdir(tmpdir)
-    #testing folder creation under different environments, enforcing a custom
-    #clean install
+    # testing folder creation under different environments, enforcing
+    # a custom clean install
     os.environ.pop('NILEARN_DATA', None)
     os.environ.pop('NILEARN_SHARED_DATA', None)
 
+    expected_base_dir = os.path.expanduser('~/nilearn_data')
     data_dir = datasets._get_dataset_dir('test')
-    assert_equal(data_dir, os.path.abspath(os.path.join('nilearn_data', 'test')))
+    assert_equal(data_dir, os.path.join(expected_base_dir, 'test'))
     assert os.path.exists(data_dir)
     shutil.rmtree(data_dir)
 
-    os.environ['NILEARN_DATA'] = 'test_nilearn_data'
+    expected_base_dir = os.path.join(tmpdir, 'test_nilearn_data')
+    os.environ['NILEARN_DATA'] = expected_base_dir
     data_dir = datasets._get_dataset_dir('test')
-    assert_equal(data_dir, os.path.join('test_nilearn_data', 'test'))
+    assert_equal(data_dir, os.path.join(expected_base_dir, 'test'))
     assert os.path.exists(data_dir)
     shutil.rmtree(data_dir)
 
-    os.environ['NILEARN_SHARED_DATA'] = 'nilearn_shared_data'
+    expected_base_dir = os.path.join(tmpdir, 'nilearn_shared_data')
+    os.environ['NILEARN_SHARED_DATA'] = expected_base_dir
     data_dir = datasets._get_dataset_dir('test')
-    assert_equal(data_dir, os.path.join('nilearn_shared_data', 'test'))
+    assert_equal(data_dir, os.path.join(expected_base_dir, 'test'))
     assert os.path.exists(data_dir)
     shutil.rmtree(data_dir)
 
-    os.chdir(currdir)
-
-    #Verify exception is raised on read-only directories
-    no_write = mkdtemp()
+    # Verify exception is raised on read-only directories
+    no_write = os.path.join(tmpdir, 'no_write')
+    os.makedirs(no_write)
     os.chmod(no_write, 0400)
     assert_raises_regexp(OSError, 'Permission denied',
                          datasets._get_dataset_dir, 'test', no_write)
-    #Verify exception for not paths as files
-    test_file = mktemp()
-    out = open(test_file, 'w')
-    out.write('abcfeg')
-    out.close()
+
+    # Verify exception for a path which exists and is a file
+    test_file = os.path.join(tmpdir, 'some_file')
+    with open(test_file, 'w') as out:
+        out.write('abcfeg')
     assert_raises_regexp(OSError, 'Not a directory',
                          datasets._get_dataset_dir, 'test', test_file)
 
+
 def test_read_md5_sum_file():
     # Create dummy temporary file
     f = mktemp()