Skip to content

Instantly share code, notes, and snippets.

@kchawla-pi
Created November 8, 2018 18:48
Show Gist options
  • Save kchawla-pi/6d42eb67abbdfce19baee65e97cbbf14 to your computer and use it in GitHub Desktop.
Save kchawla-pi/6d42eb67abbdfce19baee65e97cbbf14 to your computer and use it in GitHub Desktop.
def fetch_spm_auditory(data_dir=None, data_name='spm_auditory',
subject_id="sub001", verbose=1):
"""Function to fetch SPM auditory single-subject data.
Parameters
----------
data_dir: string
Path of the data directory. Used to force data storage in a specified
location. If the data is already present there, then will simply
glob it.
Returns
-------
data: sklearn.datasets.base.Bunch
Dictionary-like object, the interest attributes are:
- 'func': string list. Paths to functional images
- 'anat': string list. Path to anat image
References
----------
:download:
http://www.fil.ion.ucl.ac.uk/spm/data/auditory/
"""
data_dir = _get_dataset_dir(data_name, data_dir=data_dir,
verbose=verbose)
subject_dir = os.path.join(data_dir, subject_id)
def _glob_spm_auditory_data():
"""glob data from subject_dir.
"""
if not os.path.exists(subject_dir):
return None
subject_data = {}
for file_name in SPM_AUDITORY_DATA_FILES:
file_path = os.path.join(subject_dir, file_name)
if os.path.exists(file_path):
subject_data[file_name] = file_path
else:
print("%s missing from filelist!" % file_name)
return None
_subject_data = {}
_subject_data["func"] = sorted(
[subject_data[x] for x in subject_data.keys()
if re.match("^fM00223_0\d\d\.img$", os.path.basename(x))])
# volumes for this dataset of shape (64, 64, 64, 1); let's fix this
for x in _subject_data["func"]:
vol = nib.load(x)
if len(vol.shape) == 4:
vol = nib.Nifti1Image(vol.get_data()[:, :, :, 0],
vol.affine)
nib.save(vol, x)
_subject_data["anat"] = [subject_data[x] for x in subject_data.keys()
if re.match("^sM00223_002\.img$",
os.path.basename(x))][0]
# ... same thing for anat
vol = nib.load(_subject_data["anat"])
if len(vol.shape) == 4:
vol = nib.Nifti1Image(vol.get_data()[:, :, :, 0],
vol.affine)
nib.save(vol, _subject_data["anat"])
return Bunch(**_subject_data)
# maybe data_dir already contains the data ?
data = _glob_spm_auditory_data()
if data is not None:
return data
# No. Download the data
print("Data absent, downloading...")
url = ("http://www.fil.ion.ucl.ac.uk/spm/download/data/MoAEpilot/"
"MoAEpilot.zip")
archive_path = os.path.join(subject_dir, os.path.basename(url))
_fetch_file(url, subject_dir)
try:
_uncompress_file(archive_path)
except:
print("Archive corrupted, trying to download it again.")
return fetch_spm_auditory(data_dir=data_dir, data_name="",
subject_id=subject_id)
return _glob_spm_auditory_data()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment