[5d12a0]: / ants / utils / get_ants_data.py

Download this file

109 lines (91 with data), 3.2 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
"""
Get local ANTsPy data
"""
__all__ = ['get_ants_data',
'get_data']
import os
import requests
import tempfile
def get_data(file_id=None, target_file_name=None, antsx_cache_directory=None):
"""
Get ANTsPy test data file
ANTsR function: `getANTsRData`
Arguments
---------
name : string
name of test image tag to retrieve
Options:
- 'r16'
- 'r27'
- 'r30'
- 'r62'
- 'r64'
- 'r85'
- 'ch2'
- 'mni'
- 'surf'
- 'pcasl'
Returns
-------
string
filepath of test image
Example
-------
>>> import ants
>>> mnipath = ants.get_ants_data('mni')
"""
def switch_data(argument):
switcher = {
"r16": "https://ndownloader.figshare.com/files/28726512",
"r27": "https://ndownloader.figshare.com/files/28726515",
"r30": "https://ndownloader.figshare.com/files/28726518",
"r62": "https://ndownloader.figshare.com/files/28726521",
"r64": "https://ndownloader.figshare.com/files/28726524",
"r85": "https://ndownloader.figshare.com/files/28726527",
"ch2": "https://ndownloader.figshare.com/files/28726494",
"mni": "https://ndownloader.figshare.com/files/28726500",
"surf": "https://ndownloader.figshare.com/files/28726530",
"pcasl": "http://files.figshare.com/1862041/101_pcasl.nii.gz",
}
return(switcher.get(argument, "Invalid argument."))
if antsx_cache_directory is None:
antsx_cache_directory = os.path.expanduser('~/.antspy/')
os.makedirs(antsx_cache_directory, exist_ok=True)
if os.path.isdir(antsx_cache_directory) == False:
antsx_cache_directory = tempfile.TemporaryDirectory()
valid_list = ("r16",
"r27",
"r30",
"r62",
"r64",
"r85",
"ch2",
"mni",
"surf",
"pcasl",
"show")
if file_id == "show" or file_id is None:
return(valid_list)
url = switch_data(file_id)
if target_file_name == None:
if file_id == "pcasl":
target_file_name = antsx_cache_directory + "pcasl.nii.gz"
else:
extension = ".jpg"
if file_id == "ch2" or file_id == "mni" or file_id == "surf":
extension = ".nii.gz"
if extension == ".jpg":
target_file_name = antsx_cache_directory + file_id + "slice" + extension
else:
target_file_name = antsx_cache_directory + file_id + extension
target_file_name_path = target_file_name
if target_file_name == None:
target_file = tempfile.NamedTemporaryFile(prefix=target_file_name, dir=antsx_cache_directory)
target_file_name_path = target_file.name
target_file.close()
if not os.path.exists(target_file_name_path):
r = requests.get(url)
with open(target_file_name_path, 'wb') as f:
f.write(r.content)
return(target_file_name_path)
get_ants_data = get_data