Skip to content

Commit 328dd16

Browse files
committed
RF: Simplify create_cfm logic
1 parent e7e3024 commit 328dd16

File tree

1 file changed

+8
-11
lines changed

1 file changed

+8
-11
lines changed

niworkflows/interfaces/mni.py

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -434,28 +434,25 @@ def create_cfm(in_file, lesion_mask=None, global_mask=True, out_path=None):
434434

435435
# Load the input image
436436
in_img = nb.load(in_file)
437-
in_data = in_img.get_data()
438437

439438
# If we want a global mask, create one based on the input image.
440439
if global_mask is True:
441440
# Create a mask of ones with the shape of the input image.
442-
in_data = np.ones_like(in_data, dtype=np.uint8)
441+
data = np.ones(in_img.shape, dtype=np.uint8)
442+
else:
443+
data = in_img.get_data()
443444

444445
# If a lesion mask was provided, combine it with the secondary mask.
445446
if lesion_mask is not None:
446447
# Reorient the lesion mask and get the data.
447448
lm_img = nb.as_closest_canonical(nb.load(lesion_mask))
448-
lm_data = lm_img.get_data()
449449

450-
# Subtract the lesion mask from the secondary mask.
451-
cfm_data = in_data - lm_data
452-
cfm_data[cfm_data < 0] = 0
450+
# Subtract lesion mask from secondary mask, set negatives to 0
451+
data = np.fmax(data - lm_img.get_data(), 0)
452+
# Cost function mask will be created from subtraction
453+
# Otherwise, CFM will be created from global mask
453454

454-
# Create the cost function mask image from the subtraction.
455-
cfm_img = nb.Nifti1Image(cfm_data, in_img.affine, in_img.header)
456-
else:
457-
# Create the cost function mask from the global mask.
458-
cfm_img = nb.Nifti1Image(in_data, in_img.affine, in_img.header)
455+
cfm_img = nb.Nifti1Image(data, in_img.affine, in_img.header)
459456

460457
# Save the cost function mask.
461458
cfm_img.set_data_dtype(np.uint8)

0 commit comments

Comments
 (0)