-
Notifications
You must be signed in to change notification settings - Fork 45
/
n4correction.py
36 lines (30 loc) · 1.37 KB
/
n4correction.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
from __future__ import division
import os, sys, glob
import numpy as np
import nibabel as nib
from skimage.transform import resize
from multiprocessing import Pool, cpu_count
def n4_correction(im_input):
command = 'N4BiasFieldCorrection -d 3 -i ' + im_input + ' ' + ' -s 3 -c [50x50x30x20] -b [300] -o ' + im_input.replace('.nii.gz', '_corrected.nii.gz')
os.system(command)
def batch_works(k):
if k == n_processes - 1:
paths = all_paths[k * int(len(all_paths) / n_processes) : ]
else:
paths = all_paths[k * int(len(all_paths) / n_processes) : (k + 1) * int(len(all_paths) / n_processes)]
for path in paths:
n4_correction(glob.glob(os.path.join(path, '*_t1.nii.gz'))[0])
n4_correction(glob.glob(os.path.join(path, '*_t1ce.nii.gz'))[0])
n4_correction(glob.glob(os.path.join(path, '*_t2.nii.gz'))[0])
n4_correction(glob.glob(os.path.join(path, '*_flair.nii.gz'))[0])
if __name__ == '__main__':
if len(sys.argv) < 2:
raise Exception("Need at least the input data directory")
input_path = sys.argv[1]
all_paths = []
for dirpath, dirnames, files in os.walk(input_path):
if os.path.basename(dirpath)[0:7] == 'Brats17':
all_paths.append(dirpath)
n_processes = cpu_count()
pool = Pool(processes=n_processes)
pool.map(batch_works, range(n_processes))