|
a |
|
b/src/skull_stripping.py |
|
|
1 |
from __future__ import print_function |
|
|
2 |
|
|
|
3 |
import os |
|
|
4 |
import subprocess |
|
|
5 |
from multiprocessing import Pool, cpu_count |
|
|
6 |
|
|
|
7 |
|
|
|
8 |
def create_dir(path): |
|
|
9 |
if not os.path.isdir(path): |
|
|
10 |
os.makedirs(path) |
|
|
11 |
|
|
|
12 |
|
|
|
13 |
def bet(src_path, dst_path, frac="0.5"): |
|
|
14 |
command = ["bet", src_path, dst_path, "-R", "-f", frac, "-g", "0"] |
|
|
15 |
subprocess.call(command) |
|
|
16 |
return |
|
|
17 |
|
|
|
18 |
|
|
|
19 |
def unwarp_strip_skull(arg, **kwarg): |
|
|
20 |
return strip_skull(*arg, **kwarg) |
|
|
21 |
|
|
|
22 |
|
|
|
23 |
def strip_skull(src_path, dst_path, frac="0.4"): |
|
|
24 |
print("Working on :", src_path) |
|
|
25 |
try: |
|
|
26 |
bet(src_path, dst_path, frac) |
|
|
27 |
except RuntimeError: |
|
|
28 |
print("\tFailed on: ", src_path) |
|
|
29 |
|
|
|
30 |
return |
|
|
31 |
|
|
|
32 |
|
|
|
33 |
parent_dir = os.path.dirname(os.getcwd()) |
|
|
34 |
data_dir = os.path.join(parent_dir, "data") |
|
|
35 |
data_src_dir = os.path.join(data_dir, "ADNIReg") |
|
|
36 |
data_dst_dir = os.path.join(data_dir, "ADNIBrain") |
|
|
37 |
data_labels = ["AD", "NC"] |
|
|
38 |
create_dir(data_dst_dir) |
|
|
39 |
|
|
|
40 |
data_src_paths, data_dst_paths = [], [] |
|
|
41 |
for label in data_labels: |
|
|
42 |
src_label_dir = os.path.join(data_src_dir, label) |
|
|
43 |
dst_label_dir = os.path.join(data_dst_dir, label) |
|
|
44 |
create_dir(dst_label_dir) |
|
|
45 |
for subject in os.listdir(src_label_dir): |
|
|
46 |
data_src_paths.append(os.path.join(src_label_dir, subject)) |
|
|
47 |
data_dst_paths.append(os.path.join(dst_label_dir, subject)) |
|
|
48 |
|
|
|
49 |
# Test |
|
|
50 |
# strip_skull(data_src_paths[0], data_dst_paths[0]) |
|
|
51 |
|
|
|
52 |
# Multi-processing |
|
|
53 |
paras = zip(data_src_paths, data_dst_paths) |
|
|
54 |
pool = Pool(processes=cpu_count()) |
|
|
55 |
pool.map(unwarp_strip_skull, paras) |