11import os
2- import os .path as op
32import requests
43import shutil
54
@@ -51,16 +50,17 @@ def download_crcns(datafile, username, password, destination,
5150 raise RuntimeError (response .text )
5251
5352 # remove the dataset name
54- filename = op .join (* login_data ['fn' ].split ('/' )[1 :])
55- local_filename = op .join (destination , filename )
53+ filename = os . path .join (* login_data ['fn' ].split ('/' )[1 :])
54+ local_filename = os . path .join (destination , filename )
5655
5756 # create subdirectory if necessary
58- local_directory = op .dirname (local_filename )
59- if not op .exists (local_directory ) or not op .isdir (local_directory ):
57+ local_directory = os .path .dirname (local_filename )
58+ if not os .path .exists (local_directory ) or not os .path .isdir (
59+ local_directory ):
6060 os .makedirs (local_directory )
6161
6262 # download the file if it does not already exist
63- if op .exists (local_filename ):
63+ if os . path .exists (local_filename ):
6464 print ("%s already exists." % local_filename )
6565 else :
6666 bar = ProgressBar (title = filename , max_value = content_length )
@@ -71,7 +71,7 @@ def download_crcns(datafile, username, password, destination,
7171 f .write (chunk )
7272
7373 # uncompress archives
74- if unpack and op .splitext (local_filename )[1 ] in [".zip" , ".gz" ]:
74+ if unpack and os . path .splitext (local_filename )[1 ] in [".zip" , ".gz" ]:
7575 unpack_archive (local_filename )
7676
7777 return local_filename
@@ -86,7 +86,7 @@ def unpack_archive(archive_name):
8686 Local name of the archive.
8787 """
8888 print ('\t Unpacking' )
89- extract_dir = op .dirname (archive_name )
89+ extract_dir = os . path .dirname (archive_name )
9090 shutil .unpack_archive (archive_name , extract_dir = extract_dir )
9191
9292
@@ -169,3 +169,43 @@ def save_hdf5_dataset(file_name, dataset, mode='w'):
169169 hf .create_dataset (name , data = array , compression = 'gzip' )
170170
171171 print ("Saved %s" % file_name )
172+
173+
174+ def get_data_home (data_home = None ) -> str :
175+ """Return the path of the voxelwise tutorials data dir.
176+
177+ This folder is used by some large dataset loaders to avoid downloading the
178+ data several times. By default the data dir is set to a folder named
179+ 'voxelwise_tutorials' in the user home folder. Alternatively, it can be set
180+ by the 'VOXELWISE_TUTORIALS_DATA' environment variable or programmatically
181+ by giving an explicit folder path. The '~' symbol is expanded to the user
182+ home folder. If the folder does not already exist, it is automatically
183+ created.
184+
185+ Parameters
186+ ----------
187+ data_home : str | None
188+ The path to voxelwise tutorials data dir.
189+ """
190+ if data_home is None :
191+ data_home = os .environ .get (
192+ 'VOXELWISE_TUTORIALS_DATA' ,
193+ os .path .join ('~' , 'voxelwise_tutorials_data' ))
194+
195+ data_home = os .path .expanduser (data_home )
196+ if not os .path .exists (data_home ):
197+ os .makedirs (data_home )
198+
199+ return data_home
200+
201+
202+ def clear_data_home (data_home = None ):
203+ """Delete all the content of the data home cache.
204+
205+ Parameters
206+ ----------
207+ data_home : str | None
208+ The path to voxelwise tutorials data dir.
209+ """
210+ data_home = get_data_home (data_home )
211+ shutil .rmtree (data_home )
0 commit comments