.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples\03_quantify\plot_02_msi.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_03_quantify_plot_02_msi.py: Multiregional spatial interaction (MSI) ======================================= **Background.** Two tumours can have the same habitat fractions but a different spatial arrangement: habitats mixed together, or kept apart. MSI measures that arrangement by counting which habitats touch which. **Purpose.** You get the MSI matrix as a table and heatmap, plus a dictionary of scalar MSI features (one value per name) ready to use as columns in a statistics or model table. **Key terms.** * **MSI** (multiregional spatial interaction, Wu et al. 2018) -- counts how often voxels of each habitat touch voxels of each other habitat (face neighbours), describing how habitats are arranged in space. * **MSI matrix** -- entry ``[i, j]`` is the number of face-neighbour voxel pairs labelled ``i`` and ``j``; row/column 0 is background, so it records how much of each habitat lies on the tumour border. * **MSI scalars** -- ``firstorder_*`` are the matrix entries (raw and normalised); ``contrast`` / ``homogeneity`` / ``correlation`` / ``energy`` are texture-style summaries of the normalised matrix. Atomic MSI from a habitat label map (Wu et al., *Radiology* 2018): :func:`~habit.kernels.spatial_interaction_matrix` and :func:`~habit.kernels.msi_features_from_matrix`. .. GENERATED FROM PYTHON SOURCE LINES 31-33 Build a habitat map, then compute the MSI matrix and scalar summaries. sphinx_gallery_thumbnail_number = 1 .. GENERATED FROM PYTHON SOURCE LINES 33-57 .. code-block:: Python from pathlib import Path import matplotlib.pyplot as plt import pandas as pd from habit.contracts import cohort_from_directory from habit.datasets import fetch_demo from habit.kernels import msi_features_from_matrix, spatial_interaction_matrix from habit.recipes import one_step_habitat from habit.viz import plot_msi_matrix DATA = fetch_demo() MODALITIES = ("LAP",) ROI = "LAP" cohort = cohort_from_directory(DATA, modalities=MODALITIES, roi=ROI)[:1] result = one_step_habitat( modalities=MODALITIES, n_habitats=3, random_seed=0, roi=ROI ).fit_predict(cohort) habitat_map = result.habitat_maps[0] labels = habitat_map.label_array ids = tuple(sorted({int(v) for v in labels.ravel() if int(v) != 0})) # Matrix size includes background (class 0), hence the + 1. n_classes = int(max(ids)) + 1 .. 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.map[_DefineAndLabelWithinSubject]: 0%| | 0/1 [00:00` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_02_msi.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_02_msi.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_