ANTsX / ANTsPy

A fast medical imaging analysis library in Python with algorithms for registration, segmentation, and more.
https://antspyx.readthedocs.io
Apache License 2.0
592 stars 160 forks source link

[question/feature quest] how to translate ANTs CLI call to Python call? #553

Closed neuronflow closed 1 month ago

neuronflow commented 4 months ago

I currently have a Python script which assembles an ANTs call that is then called by subprocess:

    # ants call parameters
    dimensionality = "-d 3"
    initial_moving_transform = "-r [" + fixed_image + ", " + moving_image + ", 0]"

    # transformations
    if transformationalgorithm == "rigid":
        # rigid ants_transformation
        transform_rigid = "-t rigid[0.1]"
        metric_rigid = (
            "-m Mattes[" + fixed_image + "," + moving_image + ", 1, 32, Regular, 0.5]"
        )
        convergence_rigid = "-c [1000x500x250, 1e-6, 10]"
        smoothing_sigmas_rigid = "-s 3x2x1vox"
        shrink_factors_rigid = "-f 8x4x2"

            # other parameters
    use_estimate_learning_rate_once = "-l 1"
    collapse_output_transforms = "-z 1"
    interpolation = "-n BSpline[3]"
    precision = "--float 1"
    output = "-o " + "[" + outputmat + "]"

I now want to migrate this to ANTsPy. For some parameters, it is unclear to me how to supply them to the ants.registration method?

For instance, the registration method has an aff_metric parameter. However, it only accepts strings as input. What about the , 1, 32, Regular, 0.5] part of my original call?

Similar issue for `convergence_rigid = "-c [1000x500x250, 1e-6, 10]"

Is there a tool to convert ANTs CLI calls to Python calls automatically? This would be a nice feature to help users migrate :)

Thanks for your help!

ncullen93 commented 4 months ago

There's no tool like that unfortunately, although it would be nice. Actually, I think ANTsPy registration is almost too true to ANTs at the cost of being a bit less pythonic in its arguments. For those other args to the aff_metric parameter, I guess you'd have to dig a bit in the code to see but I believe it exists. Here are how the args are built up:

        args = [
            "-d",
            str(fixed.dimension),
            "-r"
        ] + initx + [
            "-m",
            "%s[%s,%s,1,%s,regular,%s]"
            % (aff_metric, f, m, aff_sampling, aff_random_sampling_rate),
            "-t",
            "%s[0.25]" % type_of_transform,
            "-c",
            myiterations,
            "-s",
            mys_aff,
            "-f",
            myf_aff,
            "-u",
            "1",
            "-z",
            "1",
            "-o",
            "[%s,%s,%s]" % (outprefix, wmo, wfo),
            "-x",
            maskopt
        ]

So long-story short, the "32" is the aff_sampling arg to ants.registration and the "0.5" is the aff_random_sampling_rate arg. I personally think it's nice that those two arguments are split out to separate args because it helps document them. The alternative would be having just one "m" argument to the ants.registration function in ANTsPy.