-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprepare_OASIS.py
More file actions
58 lines (44 loc) · 1.7 KB
/
Copy pathprepare_OASIS.py
File metadata and controls
58 lines (44 loc) · 1.7 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
import argparse
import os
import tarfile
import nibabel as nib
import torch
import wget
def wget_data(url, path):
if not os.path.exists(path):
wget.download(url, path)
return path
def main(input_path, output_path):
# Search subfolders for files ending with 'slice_norm.nii.gt'
fpaths = []
for root, dirs, files in os.walk(input_path):
for file in files:
if file.endswith('slice_norm.nii.gz'):
fpaths.append(os.path.join(root, file))
fpaths = sorted(fpaths)
imgs = []
for fpath in fpaths:
img = nib.load(fpath).get_fdata()
imgs.append(torch.tensor(img).permute(2,1,0).squeeze().squeeze().float())
imgs = torch.stack(imgs)
# Determine output path
if output_path is None:
output_path = input_path
if not os.path.exists(output_path):
os.makedirs(output_path)
# Save as OASIS_imgs.pth
torch.save(imgs, os.path.join(output_path, 'OASIS_imgs.pth'))
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Process files ending with 'slice_norm.nii.gt' in a directory.")
parser.add_argument("-o", "--output_path", type=str, help="Output directory path (default: input_path)")
wget_data('http://surfer.nmr.mgh.harvard.edu/ftp/data/neurite/data/neurite-oasis.2d.v1.0.tar', 'data.tar')
# The data.tar contains many folders, we want to extract them to a folder called 'OASIS'
with tarfile.open('data.tar') as tar:
tar.extractall('OASIS')
args = parser.parse_args()
main('OASIS', args.output_path)
# force remove the OASIS folder
# remove the data.tar file
os.remove('data.tar')
import shutil
shutil.rmtree('OASIS')