<s> """<STR_LIT>""" <EOL> import os <EOL> import shutil <EOL> import csv <EOL> import numpy as np <EOL> import nibabel <EOL> from nose import with_setup <EOL> from nose . tools import assert_true , assert_equal , assert_not_equal <EOL> from nilearn . _utils . testing import assert_raises_regex <EOL> from . import test_utils as tst <EOL> from nilearn . _utils . compat import _basestring <EOL> from nilearn . datasets import utils , atlas , struct <EOL> def setup_mock ( ) : <EOL> return tst . setup_mock ( utils , atlas ) <EOL> def teardown_mock ( ) : <EOL> return tst . teardown_mock ( utils , atlas ) <EOL> @ with_setup ( tst . setup_tmpdata , tst . teardown_tmpdata ) <EOL> def test_get_dataset_dir ( ) : <EOL> os . environ . pop ( '<STR_LIT>' , None ) <EOL> os . environ . pop ( '<STR_LIT>' , None ) <EOL> expected_base_dir = os . path . expanduser ( '<STR_LIT>' ) <EOL> data_dir = utils . _get_dataset_dir ( '<STR_LIT:test>' , verbose = <NUM_LIT:0> ) <EOL> assert_equal ( data_dir , os . path . join ( expected_base_dir , '<STR_LIT:test>' ) ) <EOL> assert os . path . exists ( data_dir ) <EOL> shutil . rmtree ( data_dir ) <EOL> expected_base_dir = os . path . join ( tst . tmpdir , '<STR_LIT>' ) <EOL> os . environ [ '<STR_LIT>' ] = expected_base_dir <EOL> data_dir = utils . _get_dataset_dir ( '<STR_LIT:test>' , verbose = <NUM_LIT:0> ) <EOL> assert_equal ( data_dir , os . path . join ( expected_base_dir , '<STR_LIT:test>' ) ) <EOL> assert os . path . exists ( data_dir ) <EOL> shutil . rmtree ( data_dir ) <EOL> expected_base_dir = os . path . join ( tst . tmpdir , '<STR_LIT>' ) <EOL> os . environ [ '<STR_LIT>' ] = expected_base_dir <EOL> data_dir = utils . _get_dataset_dir ( '<STR_LIT:test>' , verbose = <NUM_LIT:0> ) <EOL> assert_equal ( data_dir , os . path . join ( expected_base_dir , '<STR_LIT:test>' ) ) <EOL> assert os . path . exists ( data_dir ) <EOL> shutil . rmtree ( data_dir ) <EOL> expected_base_dir = os . path . join ( tst . tmpdir , '<STR_LIT>' ) <EOL> expected_dataset_dir = os . path . join ( expected_base_dir , '<STR_LIT:test>' ) <EOL> data_dir = utils . _get_dataset_dir ( <EOL> '<STR_LIT:test>' , default_paths = [ expected_dataset_dir ] , verbose = <NUM_LIT:0> ) <EOL> assert_equal ( data_dir , os . path . join ( expected_base_dir , '<STR_LIT:test>' ) ) <EOL> assert os . path . exists ( data_dir ) <EOL> shutil . rmtree ( data_dir ) <EOL> no_write = os . path . join ( tst . tmpdir , '<STR_LIT>' ) <EOL> os . makedirs ( no_write ) <EOL> os . chmod ( no_write , <NUM_LIT> ) <EOL> expected_base_dir = os . path . join ( tst . tmpdir , '<STR_LIT>' ) <EOL> os . environ [ '<STR_LIT>' ] = expected_base_dir <EOL> data_dir = utils . _get_dataset_dir ( '<STR_LIT:test>' , <EOL> default_paths = [ no_write ] , <EOL> verbose = <NUM_LIT:0> ) <EOL> assert_equal ( data_dir , no_write ) <EOL> assert os . path . exists ( data_dir ) <EOL> os . chmod ( no_write , <NUM_LIT> ) <EOL> shutil . rmtree ( data_dir ) <EOL> test_file = os . path . join ( tst . tmpdir , '<STR_LIT>' ) <EOL> with open ( test_file , '<STR_LIT:w>' ) as out : <EOL> out . write ( '<STR_LIT>' ) <EOL> assert_raises_regex ( OSError , <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> utils . _get_dataset_dir , <EOL> '<STR_LIT:test>' , test_file , verbose = <NUM_LIT:0> ) <EOL> @ with_setup ( tst . setup_tmpdata , tst . teardown_tmpdata ) <EOL> def test_fail_fetch_atlas_harvard_oxford ( ) : <EOL> assert_raises_regex ( ValueError , '<STR_LIT>' , <EOL> atlas . fetch_atlas_harvard_oxford , <EOL> '<STR_LIT>' ) <EOL> target_atlas = '<STR_LIT>' <EOL> target_atlas_fname = '<STR_LIT>' + target_atlas + '<STR_LIT>' <EOL> ho_dir = os . path . join ( tst . tmpdir , '<STR_LIT>' , '<STR_LIT:data>' , '<STR_LIT>' ) <EOL> os . makedirs ( ho_dir ) <EOL> nifti_dir = os . path . join ( ho_dir , '<STR_LIT>' ) <EOL> os . makedirs ( nifti_dir ) <EOL> target_atlas_nii = os . path . join ( nifti_dir , target_atlas_fname ) <EOL> atlas_data = np . zeros ( ( <NUM_LIT:10> , <NUM_LIT:10> , <NUM_LIT:10> ) , dtype = int ) <EOL> atlas_data [ : , : <NUM_LIT:2> , : ] = <NUM_LIT:1> <EOL> atlas_data [ : <NUM_LIT:5> , <NUM_LIT:3> : <NUM_LIT:5> , : ] = <NUM_LIT:2> <EOL> atlas_data [ <NUM_LIT:5> : , <NUM_LIT:7> : <NUM_LIT:9> , : ] = <NUM_LIT:3> <EOL> atlas_data [ <NUM_LIT:4> , <NUM_LIT:7> , <NUM_LIT:0> ] = <NUM_LIT:3> <EOL> nibabel . Nifti1Image ( atlas_data , np . eye ( <NUM_LIT:4> ) * <NUM_LIT:3> ) . to_filename ( <EOL> target_atlas_nii ) <EOL> dummy = open ( os . path . join ( ho_dir , '<STR_LIT>' ) , '<STR_LIT:w>' ) <EOL> dummy . write ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> "<STR_LIT>" ) <EOL> dummy . close ( ) <EOL> ho = atlas . fetch_atlas_harvard_oxford ( target_atlas , <EOL> data_dir = tst . tmpdir , <EOL> symmetric_split = True ) <EOL> assert_true ( isinstance ( ho . maps , nibabel . Nifti1Image ) ) <EOL> assert_true ( isinstance ( ho . labels , list ) ) <EOL> assert_equal ( len ( ho . labels ) , <NUM_LIT:5> ) <EOL> assert_equal ( ho . labels [ <NUM_LIT:0> ] , "<STR_LIT>" ) <EOL> assert_equal ( ho . labels [ <NUM_LIT:1> ] , "<STR_LIT>" ) <EOL> assert_equal ( ho . labels [ <NUM_LIT:2> ] , "<STR_LIT>" ) <EOL> assert_equal ( ho . labels [ <NUM_LIT:3> ] , "<STR_LIT>" ) <EOL> assert_equal ( ho . labels [ <NUM_LIT:4> ] , "<STR_LIT>" ) <EOL> @ with_setup ( setup_mock , teardown_mock ) <EOL> @ with_setup ( tst . setup_tmpdata , tst . teardown_tmpdata ) <EOL> def test_fetch_atlas_craddock_2012 ( ) : <EOL> bunch = atlas . fetch_atlas_craddock_2012 ( data_dir = tst . tmpdir , <EOL> verbose = <NUM_LIT:0> ) <EOL> keys = ( "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" ) <EOL> filenames = [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ] <EOL> assert_equal ( len ( tst . mock_url_request . urls ) , <NUM_LIT:1> ) <EOL> for key , fn in zip ( keys , filenames ) : <EOL> assert_equal ( bunch [ key ] , os . path . join ( tst . tmpdir , '<STR_LIT>' , fn ) ) <EOL> assert_not_equal ( bunch . description , '<STR_LIT>' ) <EOL> @ with_setup ( setup_mock , teardown_mock ) <EOL> @ with_setup ( tst . setup_tmpdata , tst . teardown_tmpdata ) <EOL> def test_fetch_atlas_smith_2009 ( ) : <EOL> bunch = atlas . fetch_atlas_smith_2009 ( data_dir = tst . tmpdir , verbose = <NUM_LIT:0> ) <EOL> keys = ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) <EOL> filenames = [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ] <EOL> assert_equal ( len ( tst . mock_url_request . urls ) , <NUM_LIT:6> ) <EOL> for key , fn in zip ( keys , filenames ) : <EOL> assert_equal ( bunch [ key ] , os . path . join ( tst . tmpdir , '<STR_LIT>' , fn ) ) <EOL> assert_not_equal ( bunch . description , '<STR_LIT>' ) <EOL> def test_fetch_coords_power_2011 ( ) : <EOL> bunch = atlas . fetch_coords_power_2011 ( ) <EOL> assert_equal ( len ( bunch . rois ) , <NUM_LIT> ) <EOL> assert_not_equal ( bunch . description , '<STR_LIT>' ) <EOL> @ with_setup ( setup_mock , teardown_mock ) <EOL> @ with_setup ( tst . setup_tmpdata , tst . teardown_tmpdata ) <EOL> def test_fetch_atlas_destrieux_2009 ( ) : <EOL> datadir = os . path . join ( tst . tmpdir , '<STR_LIT>' ) <EOL> os . mkdir ( datadir ) <EOL> dummy = open ( os . path . join ( <EOL> datadir , '<STR_LIT>' ) , '<STR_LIT:w>' ) <EOL> dummy . write ( "<STR_LIT>" ) <EOL> dummy . close ( ) <EOL> bunch = atlas . fetch_atlas_destrieux_2009 ( data_dir = tst . tmpdir , <EOL> verbose = <NUM_LIT:0> ) <EOL> assert_equal ( len ( tst . mock_url_request . urls ) , <NUM_LIT:1> ) <EOL> assert_equal ( bunch [ '<STR_LIT>' ] , os . path . join ( <EOL> tst . tmpdir , '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> dummy = open ( os . path . join ( <EOL> datadir , '<STR_LIT>' ) , '<STR_LIT:w>' ) <EOL> dummy . write ( "<STR_LIT>" ) <EOL> dummy . close ( ) <EOL> bunch = atlas . fetch_atlas_destrieux_2009 ( <EOL> lateralized = False , data_dir = tst . tmpdir , verbose = <NUM_LIT:0> ) <EOL> assert_equal ( len ( tst . mock_url_request . urls ) , <NUM_LIT:1> ) <EOL> assert_equal ( bunch [ '<STR_LIT>' ] , os . path . join ( <EOL> tst . tmpdir , '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> @ with_setup ( setup_mock , teardown_mock ) <EOL> @ with_setup ( tst . setup_tmpdata , tst . teardown_tmpdata ) <EOL> def test_fetch_atlas_msdl ( ) : <EOL> datadir = os . path . join ( tst . tmpdir , '<STR_LIT>' ) <EOL> os . mkdir ( datadir ) <EOL> os . mkdir ( os . path . join ( datadir , '<STR_LIT>' ) ) <EOL> data_dir = os . path . join ( datadir , '<STR_LIT>' , '<STR_LIT>' ) <EOL> csv = np . rec . array ( [ ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , '<STR_LIT>' , '<STR_LIT>' ) ] , <EOL> dtype = [ ( '<STR_LIT:x>' , '<STR_LIT>' ) , ( '<STR_LIT:y>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT:z>' , '<STR_LIT>' ) , ( '<STR_LIT:name>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) ] ) <EOL> with open ( data_dir , '<STR_LIT:wb>' ) as csv_file : <EOL> header = '<STR_LIT>' . format ( '<STR_LIT:U+002C>' . join ( csv . dtype . names ) ) <EOL> csv_file . write ( header . encode ( ) ) <EOL> np . savetxt ( csv_file , csv , delimiter = '<STR_LIT:U+002C>' , fmt = '<STR_LIT:%s>' ) <EOL> dataset = atlas . fetch_atlas_msdl ( data_dir = tst . tmpdir , verbose = <NUM_LIT:0> ) <EOL> assert_true ( isinstance ( dataset . labels , list ) ) <EOL> assert_true ( isinstance ( dataset . region_coords , list ) ) <EOL> assert_true ( isinstance ( dataset . networks , list ) ) <EOL> assert_true ( isinstance ( dataset . maps , _basestring ) ) <EOL> assert_equal ( len ( tst . mock_url_request . urls ) , <NUM_LIT:1> ) <EOL> assert_not_equal ( dataset . description , '<STR_LIT>' ) <EOL> @ with_setup ( setup_mock , teardown_mock ) <EOL> @ with_setup ( tst . setup_tmpdata , tst . teardown_tmpdata ) <EOL> def test_fetch_atlas_yeo_2011 ( ) : <EOL> dataset = atlas . fetch_atlas_yeo_2011 ( data_dir = tst . tmpdir , verbose = <NUM_LIT:0> ) <EOL> assert_true ( isinstance ( dataset . anat , _basestring ) ) <EOL> assert_true ( isinstance ( dataset . colors_17 , _basestring ) ) <EOL> assert_true ( isinstance ( dataset . colors_7 , _basestring ) ) <EOL> assert_true ( isinstance ( dataset . thick_17 , _basestring ) ) <EOL> assert_true ( isinstance ( dataset . thick_7 , _basestring ) ) <EOL> assert_true ( isinstance ( dataset . thin_17 , _basestring ) ) <EOL> assert_true ( isinstance ( dataset . thin_7 , _basestring ) ) <EOL> assert_equal ( len ( tst . mock_url_request . urls ) , <NUM_LIT:1> ) <EOL> assert_not_equal ( dataset . description , '<STR_LIT>' ) <EOL> @ with_setup ( setup_mock , teardown_mock ) <EOL> @ with_setup ( tst . setup_tmpdata , tst . teardown_tmpdata ) <EOL> def test_fetch_atlas_aal ( ) : <EOL> ho_dir = os . path . join ( tst . tmpdir , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> os . makedirs ( ho_dir ) <EOL> with open ( os . path . join ( ho_dir , '<STR_LIT>' ) , '<STR_LIT:w>' ) as xml_file : <EOL> xml_file . write ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> dataset = atlas . fetch_atlas_aal ( data_dir = tst . tmpdir , verbose = <NUM_LIT:0> ) <EOL> assert_true ( isinstance ( dataset . maps , _basestring ) ) <EOL> assert_true ( isinstance ( dataset . labels , list ) ) <EOL> assert_true ( isinstance ( dataset . indices , list ) ) <EOL> assert_equal ( len ( tst . mock_url_request . urls ) , <NUM_LIT:1> ) <EOL> assert_raises_regex ( ValueError , '<STR_LIT>' , <EOL> atlas . fetch_atlas_aal , version = "<STR_LIT>" , <EOL> data_dir = tst . tmpdir , verbose = <NUM_LIT:0> ) <EOL> assert_not_equal ( dataset . description , '<STR_LIT>' ) <EOL> @ with_setup ( setup_mock , teardown_mock ) <EOL> @ with_setup ( tst . setup_tmpdata , tst . teardown_tmpdata ) <EOL> def test_fetch_atlas_basc_multiscale_2015 ( ) : <EOL> data_sym = atlas . fetch_atlas_basc_multiscale_2015 ( data_dir = tst . tmpdir , <EOL> verbose = <NUM_LIT:0> ) <EOL> data_asym = atlas . fetch_atlas_basc_multiscale_2015 ( version = '<STR_LIT>' , <EOL> verbose = <NUM_LIT:0> , <EOL> data_dir = tst . tmpdir ) <EOL> keys = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> dataset_name = '<STR_LIT>' <EOL> name_sym = '<STR_LIT>' <EOL> basenames_sym = [ '<STR_LIT>' + <EOL> key + '<STR_LIT>' for key in keys ] <EOL> for key , basename_sym in zip ( keys , basenames_sym ) : <EOL> assert_equal ( data_sym [ key ] , os . path . join ( tst . tmpdir , dataset_name , <EOL> name_sym , basename_sym ) ) <EOL> name_asym = '<STR_LIT>' <EOL> basenames_asym = [ '<STR_LIT>' + <EOL> key + '<STR_LIT>' for key in keys ] <EOL> for key , basename_asym in zip ( keys , basenames_asym ) : <EOL> assert_equal ( data_asym [ key ] , os . path . join ( tst . tmpdir , dataset_name , <EOL> name_asym , basename_asym ) ) <EOL> assert_equal ( len ( data_sym ) , <NUM_LIT:10> ) <EOL> assert_raises_regex ( ValueError , <EOL> '<STR_LIT>' , <EOL> atlas . fetch_atlas_basc_multiscale_2015 , version = "<STR_LIT>" , <EOL> data_dir = tst . tmpdir , verbose = <NUM_LIT:0> ) <EOL> assert_equal ( len ( tst . mock_url_request . urls ) , <NUM_LIT:2> ) <EOL> assert_not_equal ( data_sym . description , '<STR_LIT>' ) <EOL> assert_not_equal ( data_asym . description , '<STR_LIT>' ) <EOL> def test_fetch_coords_dosenbach_2010 ( ) : <EOL> bunch = atlas . fetch_coords_dosenbach_2010 ( ) <EOL> assert_equal ( len ( bunch . rois ) , <NUM_LIT> ) <EOL> assert_equal ( len ( bunch . labels ) , <NUM_LIT> ) <EOL> assert_equal ( len ( np . unique ( bunch . networks ) ) , <NUM_LIT:6> ) <EOL> assert_not_equal ( bunch . description , '<STR_LIT>' ) </s>
