-
Notifications
You must be signed in to change notification settings - Fork 0
/
spleendatads.py
247 lines (202 loc) · 7.52 KB
/
spleendatads.py
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#!/usr/bin/env python
from pathlib import Path
import shutil
from argparse import ArgumentParser, Namespace, ArgumentDefaultsHelpFormatter
from typing import BinaryIO
from chris_plugin import chris_plugin
from tqdm import tqdm
import requests
import tarfile
import shutil
import hashlib
from requests.exceptions import RequestException
__version__ = "2.0.6"
DISPLAY_TITLE = r"""
_ _ _ _ _
| | | | | | | | | |
_ __ | |______ ___ _ __ | | ___ ___ _ __ __| | __ _| |_ __ _ __| |___
| '_ \| |______/ __| '_ \| |/ _ \/ _ \ '_ \ / _` |/ _` | __/ _` |/ _` / __|
| |_) | | \__ \ |_) | | __/ __/ | | | (_| | (_| | || (_| | (_| \__ \
| .__/|_| |___/ .__/|_|\___|\___|_| |_|\__,_|\__,_|\__\__,_|\__,_|___/
| | | |
|_| |_|
"""
parser = ArgumentParser(
description="""
A ChRIS DS plugin that downloads a spleen data set for training
and inference. Based off a MONAI exemplar:
""",
formatter_class=ArgumentDefaultsHelpFormatter,
)
parser.add_argument(
"--url",
default="https://msd-for-monai.s3-us-west-2.amazonaws.com/Task09_Spleen.tar",
help="url of remote tar archive file",
)
parser.add_argument(
"--md5",
default="410d4a301da4e5b2f6f86ec3ddba524e",
help="md5 sum of remote resource once downloaded",
)
parser.add_argument(
"--skipDownload",
default=False,
action="store_true",
help="""If specified, skip the download. Only useful for debugging/testing really.""",
)
parser.add_argument(
"--trainingOnly",
default=False,
action="store_true",
help="""If specified, only preserve the training data (saving about 462Mb)""",
)
parser.add_argument(
"--testingOnly",
default=False,
action="store_true",
help="""If specified, only preserve the testing data (saving about 1.2Gb)""",
)
parser.add_argument(
"--copyInputDir",
default=False,
action="store_true",
help="""If specified, copy the inputDir to outputDir""",
)
parser.add_argument(
"--man",
default=False,
action="store_true",
help="""If specified, show simple manual page""",
)
parser.add_argument(
"-V", "--version", action="version", version=f"%(prog)s {__version__}"
)
def man():
man: str = """
NAME
spleendatads
SYNOPSIS
spleendatads [--url <url>] \\
[--md5 <sum>] \\
[--skipDownload] \\
[--trainingOnly] \\
[--testingOnly] \\
[--man] \\
[--copyInputDir] \\
<inputDir> <outputDir>
DESCRIPTION
`spleendatads` pulls a specific resource from the internet (a tar/gz) file
and extracts its contents, optionally also checking the md5sum.
ARGS:
[--url <url>]
The url of the resource (file) to download.
[--md5 <sum>]
The md5 sum of this file. Set to empty string to ignore.
[--skipDownload]
If specified, skip the download. Mostly for debugging.
[--trainingOnly]
If specified, keep only the training images.
[--testingOnly]
If specified, keep only the testing images.
[--man]
If specified, show this manual page.
[--copyInputDir]
If specified, copy the input directory to the output.
"""
return man
def file_downloadAndExtract(url: str, toFile: Path) -> bool:
status: bool = True
try:
resp: requests.Response = requests.get(url, stream=True)
resp.raise_for_status()
totalSize: int = int(resp.headers.get("Content-Length", 0))
print(f"Download size {totalSize}")
f: BinaryIO
chunk: bytes
blockSize: int = 8192
with tqdm(total=totalSize, unit="iB", unit_scale=True) as progress_bar:
with open(toFile, "wb") as f:
for chunk in resp.iter_content(chunk_size=blockSize):
if chunk:
progress_bar.update(len(chunk))
f.write(chunk)
tar: tarfile.TarFile
with tarfile.open(toFile, "r") as tar:
totalMembers = sum(member.size for member in tar.getmembers())
progress_bar = tqdm(
total=totalMembers, unit="iB", unit_scale=True, desc="Extracting"
)
for member in tar:
tar.extract(member, toFile.parent)
progress_bar.update(member.size)
progress_bar.close()
except RequestException as e:
print(f"Error downloading the file {e}")
except tarfile.TarError as e:
print(f"Error extracting the archive {e}")
except Exception as e:
print(f"An unexpected error occurred {e}")
return status
def delete_dotunders(path: Path):
for item in path.iterdir():
if item.name.startswith("._"):
if item.is_dir():
delete_dotunders(item)
item.rmdir()
else:
item.unlink()
elif item.is_dir():
delete_dotunders(item)
def dir_findAndDelete(startdir: Path, target: str):
for item in startdir.iterdir():
if item.is_dir():
if item.name == target:
shutil.rmtree(str(item))
# item.rmdir()
print(f"Deleted directory: {target}")
break
else:
dir_findAndDelete(item, target)
# The main function of this *ChRIS* plugin is denoted by this ``@chris_plugin`` "decorator."
# Some metadata about the plugin is specified here. There is more metadata specified in setup.py.
#
# documentation: https://fnndsc.github.io/chris_plugin/chris_plugin.html#chris_plugin
@chris_plugin(
parser=parser,
title="Spleen data downloader",
category="", # ref. https://chrisstore.co/plugins
min_memory_limit="8Gi", # supported units: Mi, Gi
min_cpu_limit="1000m", # millicores, e.g. "1000m" = 1 CPU core
min_gpu_limit=0, # set min_gpu_limit=1 to enable GPU
)
def main(options: Namespace, inputdir: Path, outputdir: Path):
"""
*ChRIS* plugins usually have two positional arguments: an **input directory** containing
input files and an **output directory** where to write output files. Command-line arguments
are passed to this main method implicitly when ``main()`` is called below without parameters.
:param options: non-positional arguments parsed by the parser given to @chris_plugin
:param inputdir: directory containing (read-only) input files
:param outputdir: directory where to write output files
"""
print(DISPLAY_TITLE)
resource: str = options.url
md5: str = options.md5
if options.man:
print(man())
return
compressed_file: Path = outputdir / "Task09_Spleen.tar"
data_dir: Path = outputdir / "Task09_Spleen"
if not data_dir.exists() or options.skipDownload:
file_downloadAndExtract(resource, compressed_file)
delete_dotunders(outputdir)
if options.copyInputDir:
shutil.copytree(str(inputdir), str(outputdir))
if compressed_file.exists():
compressed_file.unlink()
if options.trainingOnly:
dir_findAndDelete(outputdir, "imagesTs")
if options.testingOnly:
dir_findAndDelete(outputdir, "imagesTr")
dir_findAndDelete(outputdir, "labelsTr")
if __name__ == "__main__":
main()