| 1 |
""" |
| 2 |
.. _anatomical_groups_example: |
| 3 |
|
| 4 |
Visualize Anatomical Groups |
| 5 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 6 |
This example visualizes different anatomical groups using the segmentation |
| 7 |
labels available from the downloadable datasets |
| 8 |
:func:`~pyvista.examples.downloads.download_whole_body_ct_female` and |
| 9 |
:func:`~pyvista.examples.downloads.download_whole_body_ct_male`. |
| 10 |
|
| 11 |
These datasets include labels for 117 anatomical structures. In this example, |
| 12 |
the labels are grouped by filtering the list of labels and coloring the |
| 13 |
labels with the recommended RGB values used by the 3DSlicer |
| 14 |
`TotalSegmentator Extension <https://github.com/lassoan/SlicerTotalSegmentator>`_. |
| 15 |
""" |
| 16 |
|
| 17 |
import pyvista as pv |
| 18 |
from pyvista import examples |
| 19 |
|
| 20 |
# %% |
| 21 |
# Load Dataset |
| 22 |
# ------------ |
| 23 |
# Load a TotalSegmentator dataset. Can be the |
| 24 |
# :func:`male <pyvista.examples.downloads.download_whole_body_ct_male>` or |
| 25 |
# :func:`female <pyvista.examples.downloads.download_whole_body_ct_female>` female |
| 26 |
# subjects included with `PyVista`, or any other subject from the |
| 27 |
# `TotalSegmentator dataset <https://zenodo.org/records/10047292>`_. |
| 28 |
dataset = examples.download_whole_body_ct_female() |
| 29 |
|
| 30 |
# %% |
| 31 |
# Get the label map used for plotting different anatomical groups. |
| 32 |
label_map = dataset['label_map'] |
| 33 |
|
| 34 |
# %% |
| 35 |
# Get metadata associated with the dataset |
| 36 |
|
| 37 |
# Get a list of all label names. This list will be filtered by group. |
| 38 |
label_names = dataset['segmentations'].keys() |
| 39 |
|
| 40 |
# Get color and id mappings included with the dataset. These are used to filter and |
| 41 |
# color the contours. |
| 42 |
names_to_colors = dataset.user_dict['names_to_colors'] |
| 43 |
names_to_ids = dataset.user_dict['names_to_ids'] |
| 44 |
ids_to_colors = dataset.user_dict['ids_to_colors'] |
| 45 |
|
| 46 |
# %% |
| 47 |
# Color Mapping |
| 48 |
# ============= |
| 49 |
# Show the color mapping included with the dataset. Print the dictionary and format |
| 50 |
# it to visually align the RGB values. The formatted dictionary is valid python code. |
| 51 |
# |
| 52 |
# .. note |
| 53 |
# |
| 54 |
# This mapping is specific to the PyVista datasets and is not part of the |
| 55 |
# TotalSegmentator data. |
| 56 |
|
| 57 |
print('{') |
| 58 |
for name, (R, G, B) in names_to_colors.items(): |
| 59 |
print(f'{name!r:<32}: ({R:>3}, {G:>3}, {B:>3}),') |
| 60 |
print('}') |
| 61 |
|
| 62 |
# %% |
| 63 |
# Utility Functions |
| 64 |
# ----------------- |
| 65 |
# Define helper functions to visualize the data. |
| 66 |
|
| 67 |
|
| 68 |
# %% |
| 69 |
# filter_labels |
| 70 |
# ============= |
| 71 |
# To visualize a particular group of anatomic regions, we first define a function |
| 72 |
# to filter the labels by name. Given a list of terms, the function returns |
| 73 |
# any label which contains any of the search terms. |
| 74 |
def filter_labels(label_names: list[str], search_terms: list[str]): |
| 75 |
def include_label(label_name: str): |
| 76 |
return any(target in label_name for target in search_terms) |
| 77 |
|
| 78 |
return [label for label in label_names if include_label(label)] |
| 79 |
|
| 80 |
|
| 81 |
# %% |
| 82 |
# plot_anatomy |
| 83 |
# ============ |
| 84 |
# Define a function which, given a list of terms, will look up labels associated |
| 85 |
# with those terms, generate contours for the labels, and plot the result. The function |
| 86 |
# uses :meth:`~pyvista.ImageDataFilters.contour_labels` for generating contours and |
| 87 |
# :meth:`~pyvista.DataSetFilters.color_labels` for coloring them. |
| 88 |
def plot_anatomy(search_terms: list[str]): |
| 89 |
# Get a list of labels which contain any of the listed terms. |
| 90 |
group_names = filter_labels(label_names, search_terms) |
| 91 |
|
| 92 |
# Get the label ids corresponding to the matched labels. |
| 93 |
group_ids = [names_to_ids[name] for name in group_names] |
| 94 |
|
| 95 |
# Selectively generate surfaces for the specified labels |
| 96 |
group_surface = dataset['label_map'].contour_labels(select_inputs=group_ids) |
| 97 |
|
| 98 |
# Color the labels with the color mapping |
| 99 |
colored_surface = group_surface.color_labels(colors=ids_to_colors) |
| 100 |
|
| 101 |
# Plot the label map. |
| 102 |
pl = pv.Plotter() |
| 103 |
pl.add_mesh(colored_surface) |
| 104 |
pl.view_zx() |
| 105 |
pl.camera.up = (0, 0, 1) |
| 106 |
pl.show() |
| 107 |
|
| 108 |
|
| 109 |
# %% |
| 110 |
# Anatomical Groups |
| 111 |
# ----------------- |
| 112 |
# Group the labels and visualize the result. |
| 113 |
|
| 114 |
# %% |
| 115 |
# Cardiovascular System |
| 116 |
# ===================== |
| 117 |
# Show segments of the cardiovascular system. |
| 118 |
|
| 119 |
# Define terms which describe all relevant segments. |
| 120 |
cardio = [ |
| 121 |
'heart', |
| 122 |
'aorta', |
| 123 |
'artery', |
| 124 |
'brachiocephalic_trunk', |
| 125 |
'vein', |
| 126 |
'atrial_appendage', |
| 127 |
'vena_cava', |
| 128 |
] |
| 129 |
|
| 130 |
# Plot the labels associated with these terms. |
| 131 |
plot_anatomy(cardio) |
| 132 |
|
| 133 |
# %% |
| 134 |
# Gastrointestinal System |
| 135 |
# ======================= |
| 136 |
# Show segments of the gastrointestinal system. |
| 137 |
|
| 138 |
# Define terms which describe all relevant segments. |
| 139 |
gastro = [ |
| 140 |
'esophagus', |
| 141 |
'stomach', |
| 142 |
'duodenum', |
| 143 |
'small_bowel', |
| 144 |
'colon', |
| 145 |
'urinary_bladder', |
| 146 |
] |
| 147 |
|
| 148 |
# Plot the labels associated with these terms. |
| 149 |
plot_anatomy(gastro) |
| 150 |
|
| 151 |
# %% |
| 152 |
# Spine |
| 153 |
# ===== |
| 154 |
# Show segments of the spinal. |
| 155 |
|
| 156 |
# Define terms which describe all relevant segments. |
| 157 |
spine = [ |
| 158 |
'spinal_cord', |
| 159 |
'vertebrae', |
| 160 |
'sacrum', |
| 161 |
] |
| 162 |
|
| 163 |
# Plot the labels associated with these terms. |
| 164 |
plot_anatomy(spine) |
| 165 |
|
| 166 |
# %% |
| 167 |
# Other Organs |
| 168 |
# ============ |
| 169 |
# Show other organs not included in the cardiovascular or gastrointestinal |
| 170 |
# systems. |
| 171 |
|
| 172 |
# Define terms which describe all relevant segments. |
| 173 |
other_organs = [ |
| 174 |
'brain', |
| 175 |
'spinal_cord', |
| 176 |
'thyroid_gland', |
| 177 |
'trachea', |
| 178 |
'lung', |
| 179 |
'adrenal_gland', |
| 180 |
'spleen', |
| 181 |
'liver', |
| 182 |
'gallbladder', |
| 183 |
'kidney', |
| 184 |
'pancreas', |
| 185 |
'prostate', |
| 186 |
] |
| 187 |
|
| 188 |
# Plot the labels associated with these terms. |
| 189 |
plot_anatomy(other_organs) |
| 190 |
|
| 191 |
# %% |
| 192 |
# Muscles |
| 193 |
# ======= |
| 194 |
# Show the muscles. |
| 195 |
|
| 196 |
# Define terms which describe all relevant segments. |
| 197 |
muscles = [ |
| 198 |
'gluteus', |
| 199 |
'autochthon', |
| 200 |
'iliopsoas', |
| 201 |
] |
| 202 |
|
| 203 |
# Plot the labels associated with these terms. |
| 204 |
plot_anatomy(muscles) |
| 205 |
|
| 206 |
# %% |
| 207 |
# Ribs |
| 208 |
# ==== |
| 209 |
# Show the ribs. |
| 210 |
|
| 211 |
# Define terms which describe all relevant segments. |
| 212 |
ribs = [ |
| 213 |
'rib', |
| 214 |
'sternum', |
| 215 |
'costal_cartilages', |
| 216 |
] |
| 217 |
|
| 218 |
# Plot the labels associated with these terms. |
| 219 |
plot_anatomy(ribs) |
| 220 |
|
| 221 |
# %% |
| 222 |
# Skeleton |
| 223 |
# ======== |
| 224 |
# Show the skeleton. |
| 225 |
|
| 226 |
# Define terms which describe all relevant segments. |
| 227 |
skeleton = [ |
| 228 |
'skull', |
| 229 |
'clavicula', |
| 230 |
'scapula', |
| 231 |
'humerus', |
| 232 |
'vertebrae', |
| 233 |
'sternum', |
| 234 |
'rib', |
| 235 |
'costal_cartilages', |
| 236 |
'hip', |
| 237 |
'sacrum', |
| 238 |
'femur', |
| 239 |
] |
| 240 |
|
| 241 |
# Plot the labels associated with these terms. |
| 242 |
plot_anatomy(skeleton) |
| 243 |
|
| 244 |
# %% |
| 245 |
# .. tags:: medical |