Skip to content

Commit

Permalink
Merge pull request #768 from ANTsX/no_hist_match_reg
Browse files Browse the repository at this point in the history
Turn off hard-coded histogram matching in registration
  • Loading branch information
cookpa authored Jan 28, 2025
2 parents 816461c + 38398b0 commit 6d7bd92
Showing 1 changed file with 28 additions and 22 deletions.
50 changes: 28 additions & 22 deletions ants/registration/registration.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ def registration(
restrict_transformation=None,
smoothing_in_mm=False,
singleprecision=True,
use_legacy_histogram_matching=False,
**kwargs
):
"""
Expand Down Expand Up @@ -158,6 +159,11 @@ def registration(
if True, use float32 for computations. This is useful for reducing memory
usage for large datasets, at the cost of precision.
use_legacy_histogram_matching : boolean
if True, use the original histogram matching in ANTs. This is not recommended, but is available for backwards
compatibilty with earlier versions, where it was always turned on. The default is False. A better implementation of
histogram matching is available in the ants.histogram_match_image2 function.
kwargs : keyword args
extra arguments
Expand Down Expand Up @@ -477,7 +483,7 @@ def registration(
"--convergence", "[%s,1e-7,8]" % synits,
"--smoothing-sigmas", smoothingsigmas,
"--shrink-factors", shrinkfactors,
"-u", "1",
"-u", str(int(use_legacy_histogram_matching)),
"-z", "1",
"--output", "[%s,%s,%s]" % (outprefix, warpedmovout_str, warpedfixout_str),
"-x", maskopt
Expand All @@ -504,7 +510,7 @@ def registration(
"--convergence", "[%s,1e-7,8]" % (synits),
"--smoothing-sigmas", smoothingsigmas,
"--shrink-factors", shrinkfactors,
"-u", "1",
"-u", str(int(use_legacy_histogram_matching)),
"-z", "1",
"--output", "[%s,%s,%s]" % (outprefix, warpedmovout_str, warpedfixout_str),
"-x", maskopt
Expand All @@ -525,7 +531,7 @@ def registration(
"--convergence", "[%s,1e-7,8]" % (synits),
"--smoothing-sigmas", smoothingsigmas,
"--shrink-factors", shrinkfactors,
"-u", "1",
"-u", str(int(use_legacy_histogram_matching)),
"-z", "1",
"--output", "[%s,%s,%s]" % (outprefix, warpedmovout_str, warpedfixout_str),
"-x", maskopt
Expand All @@ -546,7 +552,7 @@ def registration(
"--convergence", "[%s,1e-7,8]" % synits,
"--smoothing-sigmas", smoothingsigmas,
"--shrink-factors", shrinkfactors,
"-u", "1",
"-u", str(int(use_legacy_histogram_matching)),
"-z", "1",
"--output", "[%s,%s,%s]" % (outprefix, warpedmovout_str, warpedfixout_str),
"-x", maskopt
Expand All @@ -573,7 +579,7 @@ def registration(
"--convergence", "[%s,1e-7,8]" % synits,
"--smoothing-sigmas", smoothingsigmas,
"--shrink-factors", shrinkfactors,
"-u", "1",
"-u", str(int(use_legacy_histogram_matching)),
"-z", "1",
"--output", "[%s,%s,%s]" % (outprefix, warpedmovout_str, warpedfixout_str),
"-x", maskopt
Expand All @@ -589,7 +595,7 @@ def registration(
"--convergence", "[%s,1e-7,8]" % synits,
"--smoothing-sigmas", smoothingsigmas,
"--shrink-factors", shrinkfactors,
"-u", "1",
"-u", str(int(use_legacy_histogram_matching)),
"-z", "1",
"--output", "[%s,%s,%s]" % (outprefix, warpedmovout_str, warpedfixout_str),
]
Expand Down Expand Up @@ -624,7 +630,7 @@ def registration(
"--convergence", "[%s,1e-7,8]" % synits,
"--smoothing-sigmas", smoothingsigmas,
"--shrink-factors", shrinkfactors,
"-u", "1",
"-u", str(int(use_legacy_histogram_matching)),
"-z", "1",
"--output", "[%s,%s,%s]" % (outprefix, warpedmovout_str, warpedfixout_str),
]
Expand All @@ -647,7 +653,7 @@ def registration(
"--convergence", "[%s,1e-7,8]" % synits,
"--smoothing-sigmas", smoothingsigmas,
"--shrink-factors", shrinkfactors,
"-u", "1",
"-u", str(int(use_legacy_histogram_matching)),
"-z", "1",
"--output", "[%s,%s,%s]" % (outprefix, warpedmovout_str, warpedfixout_str),
"-x", maskopt
Expand Down Expand Up @@ -680,7 +686,7 @@ def registration(
"--convergence", "[2100x1200x1200x20,1e-7,8]",
"--smoothing-sigmas", "3x2x1x0",
"--shrink-factors", "4x3x2x1",
"-u", "1",
"-u", str(int(use_legacy_histogram_matching)),
"-z", "1",
"--output", "[%s,%s,%s]" % (outprefix, warpedmovout_str, warpedfixout_str),
"-x", maskopt
Expand Down Expand Up @@ -726,7 +732,7 @@ def registration(
"--convergence", myconvhi,
"--smoothing-sigmas", smoothingsigmas,
"--shrink-factors", shrinkfactors,
"-u", "1",
"-u", str(int(use_legacy_histogram_matching)),
"-z", "1",
"--output", "[%s,%s,%s]" % (outprefix, warpedmovout_str, warpedfixout_str),
"-x", maskopt
Expand All @@ -753,7 +759,7 @@ def registration(
"--convergence", "50x10x0",
"--smoothing-sigmas", "2x1x0",
"--shrink-factors", "4x2x1",
"-u", "1",
"-u", str(int(use_legacy_histogram_matching)),
"-z", "1",
"--output", "[%s,%s,%s]" % (outprefix, warpedmovout_str, warpedfixout_str),
"-x", maskopt
Expand All @@ -774,7 +780,7 @@ def registration(
"--convergence", "[%s,1e-7,8]" % synits,
"--smoothing-sigmas", smoothingsigmas,
"--shrink-factors", shrinkfactors,
"-u", "1",
"-u", str(int(use_legacy_histogram_matching)),
"-z", "1",
"--output", "[%s,%s,%s]" % (outprefix, warpedmovout_str, warpedfixout_str),
"-x", maskopt
Expand Down Expand Up @@ -803,7 +809,7 @@ def registration(
"--convergence", "[%s,1e-7,8]" % synits,
"--smoothing-sigmas", smoothingsigmas,
"--shrink-factors", shrinkfactors,
"-u", "1",
"-u", str(int(use_legacy_histogram_matching)),
"-z", "0",
"--output", "[%s,%s,%s]" % (outprefix, warpedmovout_str, warpedfixout_str),
"-x", maskopt
Expand All @@ -825,7 +831,7 @@ def registration(
"--convergence", "[%s,1e-7,8]" % synits,
"--smoothing-sigmas", smoothingsigmas,
"--shrink-factors", shrinkfactors,
"-u", "1",
"-u", str(int(use_legacy_histogram_matching)),
"-z", "0",
"--output", "[%s,%s,%s]" % (outprefix, warpedmovout_str, warpedfixout_str),
"-x", maskopt
Expand All @@ -847,7 +853,7 @@ def registration(
"--convergence", "[1200x1200x100x20x0,0,5]",
"--smoothing-sigmas", "8x6x4x2x1vox",
"--shrink-factors", "8x6x4x2x1",
"-u", "1",
"-u", str(int(use_legacy_histogram_matching)),
"-z", "0",
"--output", "[%s,%s,%s]" % (outprefix, warpedmovout_str, warpedfixout_str),
"-x", maskopt
Expand All @@ -862,7 +868,7 @@ def registration(
"--convergence", myiterations,
"--smoothing-sigmas", smoothingsigmas_affine,
"--shrink-factors", shrinkfactors_affine,
"-u", "1",
"-u", str(int(use_legacy_histogram_matching)),
"-z", "1",
"--output", "[%s,%s,%s]" % (outprefix, warpedmovout_str, warpedfixout_str),
"-x", maskopt
Expand Down Expand Up @@ -1282,7 +1288,7 @@ def label_image_registration(fixed_label_images,
of the moving image.
type_of_linear_transform : string
Use label images with the centers of mass to a calculate linear
Use label images with the centers of mass to a calculate linear
transform of type 'identity', 'rigid', 'similarity', or 'affine'.
type_of_deformable_transform : string
Expand Down Expand Up @@ -1368,7 +1374,7 @@ def label_image_registration(fixed_label_images,
if output_prefix == "" or output_prefix is None or len(output_prefix) == 0:
output_prefix = mktemp()

allowable_linear_transforms = ['rigid', 'similarity', 'affine', 'identity']
allowable_linear_transforms = ['rigid', 'similarity', 'affine', 'identity']
if not type_of_linear_transform in allowable_linear_transforms:
raise ValueError("Unrecognized linear transform.")

Expand Down Expand Up @@ -1450,8 +1456,8 @@ def label_image_registration(fixed_label_images,
label = common_label_ids[i][j]
fixed_single_label_image = ants.threshold_image(fixed_label_images[i], label, label, 1, 0)
moving_single_label_image = ants.threshold_image(moving_label_images[i], label, label, 1, 0)
deformable_multivariate_extras.append(["MSQ", fixed_single_label_image,
moving_single_label_image,
deformable_multivariate_extras.append(["MSQ", fixed_single_label_image,
moving_single_label_image,
label_image_weights[i], 0])

if verbose:
Expand Down Expand Up @@ -1611,7 +1617,7 @@ def label_image_registration(fixed_label_images,

find_inverse_warps = np.where([re.search("[0-9]InverseWarp.nii.gz", ff) for ff in all_xfrms])[0]
find_forward_warps = np.where([re.search("[0-9]Warp.nii.gz", ff) for ff in all_xfrms])[0]

fwdtransforms = []
invtransforms = []
if linear_xfrm is not None:
Expand All @@ -1620,7 +1626,7 @@ def label_image_registration(fixed_label_images,
invtransforms = [linear_xfrm_file, all_xfrms[find_inverse_warps[0]]]
else:
fwdtransforms = [linear_xfrm_file]
invtransforms = [linear_xfrm_file]
invtransforms = [linear_xfrm_file]
else:
if len(find_inverse_warps) > 0:
fwdtransforms = [all_xfrms[find_forward_warps[0]]]
Expand Down

0 comments on commit 6d7bd92

Please sign in to comment.