|
a |
|
b/ants/plotting/plot_directory.py |
|
|
1 |
""" |
|
|
2 |
Functions for plotting ants images |
|
|
3 |
""" |
|
|
4 |
|
|
|
5 |
|
|
|
6 |
__all__ = [ |
|
|
7 |
"plot_directory" |
|
|
8 |
] |
|
|
9 |
|
|
|
10 |
import fnmatch |
|
|
11 |
import math |
|
|
12 |
import os |
|
|
13 |
import warnings |
|
|
14 |
|
|
|
15 |
from matplotlib import gridspec |
|
|
16 |
import matplotlib.pyplot as plt |
|
|
17 |
import matplotlib.patheffects as path_effects |
|
|
18 |
import matplotlib.lines as mlines |
|
|
19 |
import matplotlib.patches as patches |
|
|
20 |
import matplotlib.mlab as mlab |
|
|
21 |
import matplotlib.animation as animation |
|
|
22 |
from mpl_toolkits.axes_grid1.inset_locator import inset_axes |
|
|
23 |
|
|
|
24 |
|
|
|
25 |
import numpy as np |
|
|
26 |
|
|
|
27 |
import ants |
|
|
28 |
|
|
|
29 |
|
|
|
30 |
def plot_directory( |
|
|
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) |