Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
281 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import numpy as np | ||
|
||
from .data.image import Image, LabelMap | ||
from .data.subject import Subject | ||
from .transforms.preprocessing.spatial.to_canonical import ToCanonical | ||
|
||
|
||
def import_pyplot(): | ||
try: | ||
import matplotlib.pyplot as plt | ||
except ImportError as e: | ||
raise ImportError('Install matplotlib for plotting support') from e | ||
return plt | ||
|
||
|
||
def rotate(image): | ||
return np.rot90(image) | ||
|
||
|
||
def plot_image( | ||
image: Image, | ||
channel=0, | ||
axes=None, | ||
show=True, | ||
cmap=None, | ||
): | ||
plt = import_pyplot() | ||
if axes is None: | ||
_, axes = plt.subplots(1, 3) | ||
image = ToCanonical()(image) | ||
data = image.data[channel] | ||
indices = np.array(data.shape) // 2 | ||
i, j, k = indices | ||
slice_x = rotate(data[i, :, :]) | ||
slice_y = rotate(data[:, j, :]) | ||
slice_z = rotate(data[:, :, k]) | ||
kwargs = {} | ||
is_label = isinstance(image, LabelMap) | ||
if isinstance(cmap, dict): | ||
slices = slice_x, slice_y, slice_z | ||
slice_x, slice_y, slice_z = color_labels(slices, cmap) | ||
else: | ||
if cmap is None: | ||
cmap = 'inferno' if is_label else 'gray' | ||
kwargs['cmap'] = cmap | ||
if is_label: | ||
kwargs['interpolation'] = 'none' | ||
x_extent, y_extent, z_extent = [tuple(b) for b in image.bounds.T] | ||
axes[0].imshow(slice_x, extent=y_extent + z_extent, **kwargs) | ||
axes[1].imshow(slice_y, extent=x_extent + z_extent, **kwargs) | ||
axes[2].imshow(slice_z, extent=x_extent + y_extent, **kwargs) | ||
plt.tight_layout() | ||
if show: | ||
plt.show() | ||
|
||
|
||
def plot_subject( | ||
subject: Subject, | ||
cmap_dict=None, | ||
): | ||
plt = import_pyplot() | ||
_, axes = plt.subplots(len(subject), 3) | ||
iterable = enumerate(subject.get_images_dict(intensity_only=False).items()) | ||
axes_names = 'sagittal', 'coronal', 'axial' | ||
for row, (name, image) in iterable: | ||
row_axes = axes[row] | ||
cmap = None | ||
if cmap_dict is not None and name in cmap_dict: | ||
cmap = cmap_dict[name] | ||
plot_image(image, axes=row_axes, show=False, cmap=cmap) | ||
for axis, axis_name in zip(row_axes, axes_names): | ||
axis.set_title(f'{name} ({axis_name})') | ||
plt.tight_layout() | ||
plt.show() | ||
|
||
|
||
def color_labels(arrays, cmap_dict): | ||
results = [] | ||
for array in arrays: | ||
si, sj = array.shape | ||
rgb = np.zeros((si, sj, 3), dtype=np.uint8) | ||
for label, value in cmap_dict.items(): | ||
rgb[array == label] = value | ||
results.append(rgb) | ||
return results |