.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples\01_data_in\plot_02_simpleitk.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_01_data_in_plot_02_simpleitk.py: Load from SimpleITK =================== **Background.** Many imaging scripts already hold images as SimpleITK objects. HABIT can wrap those objects directly, keeping their spacing, origin and direction, so no files have to be rearranged. **Purpose.** You get a :class:`~habit.contracts.Cohort` built from SimpleITK images, plus a quick one-step habitat map on one patient to show the cohort is usable. **When to use.** Use this when your code already calls ``sitk.ReadImage`` or produces SimpleITK images; if your files follow the HABIT folder layout, :doc:`/auto_examples/01_data_in/plot_01_directory` is shorter. **Key terms.** * **cohort / subject / ROI** -- see :doc:`/auto_examples/01_data_in/plot_01_directory`. * **one-step habitats** -- habitats clustered inside one subject only; see :doc:`/auto_examples/04_designs/plot_02_inside_each_subject`. Read an image file and a mask file with SimpleITK, then put the volumes on a :class:`~habit.contracts.Subject`. ``modality=`` is the series or ROI name. One person and several people are each a :class:`~habit.contracts.Cohort`. .. GENERATED FROM PYTHON SOURCE LINES 32-33 One person. Change the two paths to your files. .. GENERATED FROM PYTHON SOURCE LINES 33-58 .. code-block:: Python from pathlib import Path import matplotlib.pyplot as plt import SimpleITK as sitk from habit.contracts import Cohort, ImageVolume, MaskVolume, Subject from habit.datasets import fetch_demo from habit.recipes import one_step_habitat from habit.viz import plot_habitat_overlay, plot_simpleitk_ingest DATA = fetch_demo() IMAGE = DATA / "images" / "subj001" / "LAP" / "WATER__WATER__Ax_Dyn_LAVA_Flex+C_Series0009.nrrd" MASK = DATA / "masks" / "subj001" / "LAP" / "WATER__BH_Ax_LAVA_Flex_10min_Series0017_mask.nrrd" # from_sitk keeps spacing / origin / direction, so voxel sizes stay physical. volume = ImageVolume.from_sitk(sitk.ReadImage(str(IMAGE)), modality="LAP") roi = MaskVolume.from_sitk(sitk.ReadImage(str(MASK)), modality="LAP") # Dictionary keys are the names later stages use (modalities=..., roi=...). subject = Subject( subject_id="subj001", images={"LAP": volume}, masks={"LAP": roi}, ) one = Cohort([subject], name="one") print(one) .. rst-class:: sphx-glr-script-out .. code-block:: none HABIT demo data (cached) DATA (preprocessed root): C:\Users\dongm\.habit_data\demo-data-v1\preprocessed On-disk inventory of this folder: subjects (5): subj001, subj002, subj003, subj004, subj005 image series: LAP, PVP, delay_3min, pre_contrast mask keys: LAP, PVP, delay_3min, pre_contrast example image: images/subj001/delay_3min/WATER__BH_Ax_LAVA_Flex_3min_Series0012.nrrd example mask: masks/subj001/delay_3min/WATER__BH_Ax_LAVA_Flex_10min_Series0017_mask.nrrd Your own data must use the same folder tree (change IDs / series names): DATA/ images/// masks/// Then load it with the same call the demos use: cohort = cohort_from_directory(DATA, modalities=("LAP",), roi="LAP") Swap DATA / modalities / roi to match your tree. Mask key is often the same as one image series (here LAP). Cohort(1 subjects [subj001], name='one') .. GENERATED FROM PYTHON SOURCE LINES 59-61 One-step habitats on this one person, then overlay. This only proves the cohort works end to end; habitat stages are explained in section 2. .. GENERATED FROM PYTHON SOURCE LINES 61-80 .. code-block:: Python sitk_result = one_step_habitat( modalities=("LAP",), n_habitats=3, random_seed=0, roi="LAP" ).fit_predict(one) Path("out").mkdir(exist_ok=True) # Visual summary of this route: SimpleITK image objects in, habitats out # (right panel zooms to the habitat bounding box). fig_ingest = plot_simpleitk_ingest(volume, sitk_result.habitat_maps[0]) fig_ingest.savefig("out/data_from_sitk_ingest.png", dpi=150, bbox_inches="tight") plt.show() fig_sitk = plot_habitat_overlay( volume, sitk_result.habitat_maps[0], title="Habitats from SimpleITK", ) fig_sitk.savefig("out/data_from_sitk_overlay.png", dpi=150, bbox_inches="tight") plt.show() .. rst-class:: sphx-glr-horizontal * .. image-sg:: /auto_examples/01_data_in/images/sphx_glr_plot_02_simpleitk_001.png :alt: From SimpleITK to habitats, Habitats :srcset: /auto_examples/01_data_in/images/sphx_glr_plot_02_simpleitk_001.png :class: sphx-glr-multi-img * .. image-sg:: /auto_examples/01_data_in/images/sphx_glr_plot_02_simpleitk_002.png :alt: Habitats from SimpleITK, Axis 0 (axial-like) @ 96, Axis 1 (coronal-like) @ 165, Axis 2 (sagittal-like) @ 71 :srcset: /auto_examples/01_data_in/images/sphx_glr_plot_02_simpleitk_002.png :class: sphx-glr-multi-img .. rst-class:: sphx-glr-script-out .. code-block:: none Cohort.map[_DefineAndLabelWithinSubject]: 0%| | 0/1 [00:00` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_02_simpleitk.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_02_simpleitk.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_