Merge pull request #20 from jpdefrutos/master
Browse files- livermask/livermask.py +11 -7
- livermask/utils/process.py +5 -5
livermask/livermask.py
CHANGED
|
@@ -29,7 +29,7 @@ os.environ['TF_FORCE_GPU_ALLOW_GROWTH'] = 'true' # due to this: https://github.
|
|
| 29 |
warnings.filterwarnings('ignore', '.*output shape of zoom.*') # mute some warnings
|
| 30 |
|
| 31 |
|
| 32 |
-
def func(path, output, cpu, verbose, vessels):
|
| 33 |
# enable verbose or not
|
| 34 |
log = verboseHandler(verbose)
|
| 35 |
|
|
@@ -54,15 +54,15 @@ def func(path, output, cpu, verbose, vessels):
|
|
| 54 |
|
| 55 |
for curr in tqdm(paths, "CT:"):
|
| 56 |
# check if current file is a nifti file, if not, skip
|
| 57 |
-
if not curr.endswith(".nii"):
|
| 58 |
continue
|
| 59 |
|
| 60 |
# perform liver parenchyma segmentation, launch it in separate process to properly clear memory
|
| 61 |
-
pred = liver_segmenter_wrapper(curr, output, cpu, verbose, multiple_flag, name)
|
| 62 |
|
| 63 |
if vessels:
|
| 64 |
# perform liver vessel segmentation
|
| 65 |
-
vessel_segmenter(curr, output, cpu, verbose, multiple_flag, pred, name_vessel)
|
| 66 |
|
| 67 |
|
| 68 |
def main():
|
|
@@ -77,6 +77,8 @@ def main():
|
|
| 77 |
help="enable verbose.")
|
| 78 |
parser.add_argument('--vessels', action='store_true',
|
| 79 |
help="segment vessels.")
|
|
|
|
|
|
|
| 80 |
ret = parser.parse_args(sys.argv[1:]); print(ret)
|
| 81 |
|
| 82 |
if ret.cpu:
|
|
@@ -105,10 +107,12 @@ def main():
|
|
| 105 |
ret.input = ret.input.replace("\\", "/")
|
| 106 |
ret.output = ret.output.replace("\\", "/")
|
| 107 |
|
| 108 |
-
if not os.path.isdir(ret.input) and not ret.input.endswith(".nii"):
|
| 109 |
-
raise ValueError("Input path provided is not in the supported '.nii'
|
| 110 |
-
if ret.output.endswith(".nii") or "." in ret.output.split("/")[-1]:
|
| 111 |
raise ValueError("Output path provided is not a directory or a name (remove *.nii format from name).")
|
|
|
|
|
|
|
| 112 |
|
| 113 |
func(*vars(ret).values())
|
| 114 |
|
|
|
|
| 29 |
warnings.filterwarnings('ignore', '.*output shape of zoom.*') # mute some warnings
|
| 30 |
|
| 31 |
|
| 32 |
+
def func(path, output, cpu, verbose, vessels, extension):
|
| 33 |
# enable verbose or not
|
| 34 |
log = verboseHandler(verbose)
|
| 35 |
|
|
|
|
| 54 |
|
| 55 |
for curr in tqdm(paths, "CT:"):
|
| 56 |
# check if current file is a nifti file, if not, skip
|
| 57 |
+
if not curr.endswith(".nii") or not curr.endswith(".nii.gz"):
|
| 58 |
continue
|
| 59 |
|
| 60 |
# perform liver parenchyma segmentation, launch it in separate process to properly clear memory
|
| 61 |
+
pred = liver_segmenter_wrapper(curr, output, cpu, verbose, multiple_flag, name, extension)
|
| 62 |
|
| 63 |
if vessels:
|
| 64 |
# perform liver vessel segmentation
|
| 65 |
+
vessel_segmenter(curr, output, cpu, verbose, multiple_flag, pred, name_vessel, extension)
|
| 66 |
|
| 67 |
|
| 68 |
def main():
|
|
|
|
| 77 |
help="enable verbose.")
|
| 78 |
parser.add_argument('--vessels', action='store_true',
|
| 79 |
help="segment vessels.")
|
| 80 |
+
parser.add_argument('--extension', metavar='--e', type=str, default=".nii",
|
| 81 |
+
help="define the output extension. (default: .nii)")
|
| 82 |
ret = parser.parse_args(sys.argv[1:]); print(ret)
|
| 83 |
|
| 84 |
if ret.cpu:
|
|
|
|
| 107 |
ret.input = ret.input.replace("\\", "/")
|
| 108 |
ret.output = ret.output.replace("\\", "/")
|
| 109 |
|
| 110 |
+
if not os.path.isdir(ret.input) and not ret.input.endswith(".nii") and not ret.input.endswith(".nii.gz"):
|
| 111 |
+
raise ValueError("Input path provided is not in the supported '.nii' or '.nii.gz' formats or a directory.")
|
| 112 |
+
if ret.output.endswith(".nii") or ret.output.endswith(".nii.gz") or "." in ret.output.split("/")[-1]:
|
| 113 |
raise ValueError("Output path provided is not a directory or a name (remove *.nii format from name).")
|
| 114 |
+
if ret.extension not in [".nii", ".nii.gz"]:
|
| 115 |
+
raise ValueError("Extension not supported. Expected: .nii or .nii.gz")
|
| 116 |
|
| 117 |
func(*vars(ret).values())
|
| 118 |
|
livermask/utils/process.py
CHANGED
|
@@ -37,11 +37,11 @@ def intensity_normalization(volume, intensity_clipping_range):
|
|
| 37 |
return result
|
| 38 |
|
| 39 |
|
| 40 |
-
def liver_segmenter_wrapper(curr, output, cpu, verbose, multiple_flag, name):
|
| 41 |
# run inference in a different process
|
| 42 |
mp.set_start_method('spawn', force=True)
|
| 43 |
with mp.Pool(processes=1, maxtasksperchild=1) as p: # , initializer=initializer)
|
| 44 |
-
result = p.map_async(liver_segmenter, ((curr, output, cpu, verbose, multiple_flag, name),))
|
| 45 |
log.info("getting result from process...")
|
| 46 |
ret = result.get()[0]
|
| 47 |
return ret
|
|
@@ -49,7 +49,7 @@ def liver_segmenter_wrapper(curr, output, cpu, verbose, multiple_flag, name):
|
|
| 49 |
|
| 50 |
def liver_segmenter(params):
|
| 51 |
try:
|
| 52 |
-
curr, output, cpu, verbose, multiple_flag, name = params
|
| 53 |
|
| 54 |
# load model
|
| 55 |
model = load_model(name, compile=False)
|
|
@@ -123,9 +123,9 @@ def liver_segmenter(params):
|
|
| 123 |
img = nib.Nifti1Image(pred, affine=resampled_volume.affine)
|
| 124 |
resampled_lab = resample_from_to(img, nib_volume, order=0)
|
| 125 |
if multiple_flag:
|
| 126 |
-
nib.save(resampled_lab, output + "/" + curr.split("/")[-1].split(".")[0] + "-livermask
|
| 127 |
else:
|
| 128 |
-
nib.save(resampled_lab, output + "-livermask
|
| 129 |
|
| 130 |
return pred
|
| 131 |
except KeyboardInterrupt:
|
|
|
|
| 37 |
return result
|
| 38 |
|
| 39 |
|
| 40 |
+
def liver_segmenter_wrapper(curr, output, cpu, verbose, multiple_flag, name, extension):
|
| 41 |
# run inference in a different process
|
| 42 |
mp.set_start_method('spawn', force=True)
|
| 43 |
with mp.Pool(processes=1, maxtasksperchild=1) as p: # , initializer=initializer)
|
| 44 |
+
result = p.map_async(liver_segmenter, ((curr, output, cpu, verbose, multiple_flag, name, extension),))
|
| 45 |
log.info("getting result from process...")
|
| 46 |
ret = result.get()[0]
|
| 47 |
return ret
|
|
|
|
| 49 |
|
| 50 |
def liver_segmenter(params):
|
| 51 |
try:
|
| 52 |
+
curr, output, cpu, verbose, multiple_flag, name, extension = params
|
| 53 |
|
| 54 |
# load model
|
| 55 |
model = load_model(name, compile=False)
|
|
|
|
| 123 |
img = nib.Nifti1Image(pred, affine=resampled_volume.affine)
|
| 124 |
resampled_lab = resample_from_to(img, nib_volume, order=0)
|
| 125 |
if multiple_flag:
|
| 126 |
+
nib.save(resampled_lab, output + "/" + curr.split("/")[-1].split(".")[0] + "-livermask" + extension)
|
| 127 |
else:
|
| 128 |
+
nib.save(resampled_lab, output + "-livermask" + extension)
|
| 129 |
|
| 130 |
return pred
|
| 131 |
except KeyboardInterrupt:
|