download_sample.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. # Demo 1-1. Setting an enviroment (download_sample.py)
  2. from os import listdir, mkdir, path, system, getcwd
  3. import warnings; warnings.simplefilter("ignore")
  4. dir_origin = dir_origin = getcwd()+'/' # <- Change this in local machine
  5. dir_dataset= 'dataset/'
  6. print('\n1)============ Start Downloading =================\n')
  7. print('Target directory ... => [%s%s]'%(dir_origin,dir_dataset))
  8. #!rm -rf /content/dataset/
  9. import requests, time
  10. def download_dataset( animal_list = range(1,7), dir_dataset = dir_dataset ):
  11. # Check directory
  12. if not path.isdir('%s%s'%(dir_origin,dir_dataset)):
  13. mkdir('%s%s'%(dir_origin,dir_dataset))
  14. mkdir('%s%s/dataset_1/'%(dir_origin,dir_dataset))
  15. mkdir('%s%s/dataset_2/'%(dir_origin,dir_dataset))
  16. # File names to be downloaded
  17. file_ids = [ 'meta.csv', 'montage.csv' ]
  18. for set_id in animal_list:
  19. file_ids.append( 'dataset_1/epochs_animal%s.set'%set_id )
  20. file_ids.append( 'dataset_1/epochs_animal%s.fdt'%set_id )
  21. file_ids.append( 'dataset_2/epochs_animal%s.set'%set_id )
  22. file_ids.append( 'dataset_2/epochs_animal%s.fdt'%set_id )
  23. # Request & download
  24. repo_url = 'https://gin.g-node.org/hiobeen/Mouse_hdEEG_ASSR_Hwang_et_al/raw/f361198e4444c29969b4b6014cfd3e771eca381d/'
  25. for file_id in file_ids:
  26. fname_dest = "%s%s%s"%(dir_origin, dir_dataset, file_id)
  27. if path.isfile(fname_dest) is False:
  28. print('...copying to [%s]...'%fname_dest)
  29. file_url = '%s%s'%(repo_url, file_id)
  30. r = requests.get(file_url, stream = True)
  31. with open(fname_dest, "wb") as file:
  32. for block in r.iter_content(chunk_size=1024):
  33. if block: file.write(block)
  34. time.sleep(1) # wait a second to prevent possible errors
  35. else:
  36. print('...skipping already existing file [%s]...'%fname_dest)
  37. # Initiate downloading
  38. animal_list = [2] # Partial download to prevent long download time
  39. #animal_list = [1,2,3,4,5,6] # Full download
  40. download_dataset(animal_list)
  41. print('\n============= Download finished ==================\n\n')
  42. # List up 'dataset/' directory
  43. print('\n2)=== List of available files in google drive ====\n')
  44. print(listdir('%sdataset/'%dir_origin))
  45. print('\n============= End of the list ==================\n\n')
  46. # Install mne-python module
  47. system('pip install mne');
  48. # Make figure output directory
  49. dir_fig = 'figures/'
  50. if not path.isdir(dir_fig): mkdir('%s%s'%(dir_origin, dir_fig))