• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

ANTsX / ANTsPy / 11463837481

22 Oct 2024 03:49PM CUT coverage: 85.341%. Remained the same
11463837481

Pull #722

github

web-flow
Merge 37286119b into 6074d8529
Pull Request #722: COMP: version bump

4512 of 5287 relevant lines covered (85.34%)

0.85 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

38.1
ants/plotting/plot_directory.py
1
"""
2
Functions for plotting ants images
3
"""
4

5

6
__all__ = [
1✔
7
    "plot_directory"
8
]
9

10
import fnmatch
1✔
11
import math
1✔
12
import os
1✔
13
import warnings
1✔
14

15
from matplotlib import gridspec
1✔
16
import matplotlib.pyplot as plt
1✔
17
import matplotlib.patheffects as path_effects
1✔
18
import matplotlib.lines as mlines
1✔
19
import matplotlib.patches as patches
1✔
20
import matplotlib.mlab as mlab
1✔
21
import matplotlib.animation as animation
1✔
22
from mpl_toolkits.axes_grid1.inset_locator import inset_axes
1✔
23

24

25
import numpy as np
1✔
26

27
import ants
1✔
28

29

30
def plot_directory(
1✔
31
    directory,
32
    recursive=False,
33
    regex="*",
34
    save_prefix="",
35
    save_suffix="",
36
    axis=None,
37
    **kwargs
38
):
39
    """
40
    Create and save an ANTsPy plot for every image matching a given regular
41
    expression in a directory, optionally recursively. This is a good function
42
    for quick visualize exploration of all of images in a directory
43

44
    ANTsR function: N/A
45

46
    Arguments
47
    ---------
48
    directory : string
49
        directory in which to search for images and plot them
50

51
    recursive : boolean
52
        If true, this function will search through all directories under
53
        the given directory recursively to make plots.
54
        If false, this function will only create plots for images in the
55
        given directory
56

57
    regex : string
58
        regular expression used to filter out certain filenames or suffixes
59

60
    save_prefix : string
61
        sub-string that will be appended to the beginning of all saved plot filenames.
62
        Default is to add nothing.
63

64
    save_suffix : string
65
        sub-string that will be appended to the end of all saved plot filenames.
66
        Default is add nothing.
67

68
    kwargs : keyword arguments
69
        any additional arguments to pass onto the `ants.plot` function.
70
        e.g. overlay, alpha, cmap, etc. See `ants.plot` for more options.
71

72
    Example
73
    -------
74
    >>> import ants
75
    >>> ants.plot_directory(directory='~/desktop/testdir',
76
                            recursive=False, regex='*')
77
    """
78

79
    def has_acceptable_suffix(fname):
×
80
        suffixes = {".nii.gz"}
×
81
        return sum([fname.endswith(sx) for sx in suffixes]) > 0
×
82

83
    if directory.startswith("~"):
×
84
        directory = os.path.expanduser(directory)
×
85

86
    if not os.path.isdir(directory):
×
87
        raise ValueError("directory %s does not exist!" % directory)
×
88

89
    for root, dirnames, fnames in os.walk(directory):
×
90
        for fname in fnames:
×
91
            if fnmatch.fnmatch(fname, regex) and has_acceptable_suffix(fname):
×
92
                load_fname = os.path.join(root, fname)
×
93
                fname = fname.replace(".".join(fname.split(".")[1:]), "png")
×
94
                fname = fname.replace(".png", "%s.png" % save_suffix)
×
95
                fname = "%s%s" % (save_prefix, fname)
×
96
                save_fname = os.path.join(root, fname)
×
97
                img = ants.image_read(load_fname)
×
98

99
                if axis is None:
×
100
                    axis_range = [i for i in range(img.dimension)]
×
101
                else:
102
                    axis_range = axis if isinstance(axis, (list, tuple)) else [axis]
×
103

104
                if img.dimension > 2:
×
105
                    for axis_idx in axis_range:
×
106
                        filename = save_fname.replace(".png", "_axis%i.png" % axis_idx)
×
107
                        ncol = int(math.sqrt(img.shape[axis_idx]))
×
108
                        ants.plot(
×
109
                            img,
110
                            axis=axis_idx,
111
                            nslices=img.shape[axis_idx],
112
                            ncol=ncol,
113
                            filename=filename,
114
                            **kwargs
115
                        )
116
                else:
117
                    filename = save_fname
×
118
                    ants.plot(img, filename=filename, **kwargs)
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc