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