.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/plot_3d_to_2d.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_plot_3d_to_2d.py: Sample slices from volumes ========================== In this example, volumes are padded, scaled, rotated and sometimes flipped. Then, 2D slices are extracted. .. GENERATED FROM PYTHON SOURCE LINES 8-45 .. code-block:: Python import matplotlib.pyplot as plt import torch import torchio as tio torch.manual_seed(0) max_queue_length = 16 patches_per_volume = 2 subject = tio.datasets.Colin27() subject.remove_image('head') subjects = 50 * [subject] max_side = max(subject.shape) transform = tio.Compose( ( tio.CropOrPad(max_side), tio.RandomFlip(), tio.RandomAffine(degrees=360), ) ) dataset = tio.SubjectsDataset(subjects, transform=transform) patch_size = (max_side, max_side, 1) # 2D slices def plot_batch(sampler): queue = tio.Queue(dataset, max_queue_length, patches_per_volume, sampler) loader = torch.utils.data.DataLoader(queue, batch_size=16) batch = tio.utils.get_first_item(loader) fig, axes = plt.subplots(4, 4, figsize=(12, 10)) for ax, im in zip(axes.flatten(), batch['t1']['data']): ax.imshow(im.squeeze(), cmap='gray') plt.suptitle(sampler.__class__.__name__) plt.tight_layout() .. GENERATED FROM PYTHON SOURCE LINES 46-50 Uniform sampler --------------- When a :class:`torchio.UniformSampler` is used, some of the patches don't contain much useful information: .. GENERATED FROM PYTHON SOURCE LINES 50-54 .. code-block:: Python sampler = tio.UniformSampler(patch_size) plot_batch(sampler) .. image-sg:: /auto_examples/images/sphx_glr_plot_3d_to_2d_001.png :alt: UniformSampler :srcset: /auto_examples/images/sphx_glr_plot_3d_to_2d_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 55-60 Weighted sampler ---------------- We can use the ``brain`` image contained in the subject as a probability map for a :class:`torchio.WeightedSampler`. That way, we ensure that the center of all patches correspond to brain tissue. .. GENERATED FROM PYTHON SOURCE LINES 60-65 .. code-block:: Python sampler = tio.WeightedSampler(patch_size, probability_map='brain') plot_batch(sampler) plt.show() .. image-sg:: /auto_examples/images/sphx_glr_plot_3d_to_2d_002.png :alt: WeightedSampler :srcset: /auto_examples/images/sphx_glr_plot_3d_to_2d_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 23.530 seconds) .. _sphx_glr_download_auto_examples_plot_3d_to_2d.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_3d_to_2d.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_3d_to_2d.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_