Skip to content

elastix

Get elastix version.

Source code in navis/transforms/elastix.py
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
@requires_elastix
def elastix_version(as_string=False):
    """Get elastix version."""
    p = subprocess.run([_elastixbin / "elastix", "--version"], capture_output=True)
    if p.stderr:
        raise BaseException(f"Error running elastix:\n{p.stderr.decode()}")

    version = p.stdout.decode("utf-8").rstrip()

    # Extract version from "elastix version: 5.0.1"
    version = version.split(":")[-1]

    if as_string:
        return version
    else:
        return tuple(int(v) for v in version.split("."))

Find directory with elastix binaries.

Source code in navis/transforms/elastix.py
34
35
36
37
38
39
40
41
42
43
44
45
46
def find_elastixbin(tool: str = "transformix") -> str:
    """Find directory with elastix binaries."""
    for path in _search_path:
        path = pathlib.Path(path)
        if not path.is_dir():
            continue

        try:
            return next(path.glob(tool)).resolve().parent
        except StopIteration:
            continue
        except BaseException:
            raise

Check if elastix is available.

Source code in navis/transforms/elastix.py
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
def requires_elastix(func):
    """Check if elastix is available."""

    @functools.wraps(func)
    def wrapper(*args, **kwargs):
        if not _elastixbin:
            raise ValueError(
                "Could not find elastix binaries. Please download "
                "the releases page at https://github.com/SuperElastix/elastix, "
                "unzip at a convenient location and add that "
                "location to your PATH variable. Note that you "
                "will also have to set a LD_LIBRARY_PATH (Linux) "
                "or DYLD_LIBRARY_PATH (OSX) variable. See the "
                "elastic manual (release page) for details."
            )
        return func(*args, **kwargs)

    return wrapper

Set up to make elastix work from inside a Python session.

Briefly: elastix requires the LD_LIBRARY_PATH (Linux) or LDY_LIBRARY_PATH (OSX) environment variables to (also) point to the directory with the elastix lib directory. For reasons unknown to me, these variables do not make it into the Python session. Hence, we have to set them here explicitly.

Above info is based on: https://github.com/jasper-tms/pytransformix

Source code in navis/transforms/elastix.py
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
def setup_elastix():
    """Set up to make elastix work from inside a Python session.

    Briefly: elastix requires the `LD_LIBRARY_PATH` (Linux) or `LDY_LIBRARY_PATH`
    (OSX) environment variables to (also) point to the directory with the
    elastix `lib` directory. For reasons unknown to me, these variables do not
    make it into the Python session. Hence, we have to set them here explicitly.

    Above info is based on: https://github.com/jasper-tms/pytransformix

    """
    # Don't do anything if no elastixbin
    if not _elastixbin:
        return

    # Check if this variable already exists
    var = os.environ.get("LD_LIBRARY_PATH", os.environ.get("LDY_LIBRARY_PATH", ""))

    # Get the actual path
    path = (_elastixbin.parent / "lib").absolute()

    if str(path) not in var:
        var = f"{path}{os.pathsep}{var}" if var else str(path)

    # Note that `LD_LIBRARY_PATH` works for both Linux and OSX
    os.environ["LD_LIBRARY_PATH"] = var
    # As per navis/issues/112
    os.environ["DYLD_LIBRARY_PATH"] = var