diff --git a/docs/api.md b/docs/api.md index 74f6bf8..662074c 100644 --- a/docs/api.md +++ b/docs/api.md @@ -167,6 +167,9 @@ Plotting functions. .. autosummary:: :toctree: generated + pl.pixel_clusters + pl.pixel_clusters_heatmap + pl.snr_ratio pl.group_snr_ratio pl.snr_clustermap diff --git a/docs/tutorials/general/FlowSOM_for_pixel_and_cell_clustering.ipynb b/docs/tutorials/general/FlowSOM_for_pixel_and_cell_clustering.ipynb index 0b8a95a..bf87146 100644 --- a/docs/tutorials/general/FlowSOM_for_pixel_and_cell_clustering.ipynb +++ b/docs/tutorials/general/FlowSOM_for_pixel_and_cell_clustering.ipynb @@ -9,26 +9,15 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stderr", - "output_type": "stream", - "text": [ - "/Users/arnedf/miniconda3/envs/harpy/lib/python3.10/site-packages/dask/dataframe/__init__.py:31: FutureWarning: The legacy Dask DataFrame implementation is deprecated and will be removed in a future version. Set the configuration option `dataframe.query-planning` to `True` or None to enable the new Dask Dataframe implementation and silence this warning.\n", - " warnings.warn(\n" - ] - } - ], + "outputs": [], "source": [ "%load_ext autoreload\n", "%autoreload 2\n", "\n", "import harpy as hp\n", "from harpy.datasets import pixie_example\n", - "from harpy.table.cell_clustering._utils import _export_to_ark_format as _export_to_ark_format_cells\n", - "from harpy.table.pixel_clustering._cluster_intensity import _export_to_ark_format as _export_to_ark_format_pixels\n", "from harpy.utils._keys import ClusteringKey" ] }, @@ -41,72 +30,9 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stderr", - "output_type": "stream", - "text": [ - "/Users/arnedf/miniconda3/envs/harpy/lib/python3.10/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n", - " from .autonotebook import tqdm as notebook_tqdm\n", - "2024-12-11 11:14:28,429 - harpy.image._manager - WARNING - No dims parameter specified. Assuming order of dimension of provided array is ((c), (z), y, x)\n", - "2024-12-11 11:14:28,551 - harpy.image._manager - INFO - Writing results to layer 'raw_image_fov0'\n", - "2024-12-11 11:14:28,555 - harpy.image._manager - WARNING - No dims parameter specified. Assuming order of dimension of provided array is ((c), (z), y, x)\n", - "2024-12-11 11:14:28,560 - harpy.image._manager - INFO - Writing results to layer 'label_nuclear_fov0'\n", - "2024-12-11 11:14:28,563 - harpy.image._manager - WARNING - No dims parameter specified. Assuming order of dimension of provided array is ((c), (z), y, x)\n", - "2024-12-11 11:14:28,567 - harpy.image._manager - INFO - Writing results to layer 'label_whole_fov0'\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "/Users/arnedf/.cache/huggingface/datasets/downloads/extracted/ed276a09a07145a5c25cd3c0a3fd99368fc2f3387300f55927c0b600c043de39/post_clustering\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "2024-12-11 11:14:28,623 - harpy.image._manager - WARNING - No dims parameter specified. Assuming order of dimension of provided array is ((c), (z), y, x)\n", - "2024-12-11 11:14:28,717 - harpy.image._manager - INFO - Writing results to layer 'raw_image_fov1'\n", - "2024-12-11 11:14:28,721 - harpy.image._manager - WARNING - No dims parameter specified. Assuming order of dimension of provided array is ((c), (z), y, x)\n", - "2024-12-11 11:14:28,727 - harpy.image._manager - INFO - Writing results to layer 'label_nuclear_fov1'\n", - "2024-12-11 11:14:28,730 - harpy.image._manager - WARNING - No dims parameter specified. Assuming order of dimension of provided array is ((c), (z), y, x)\n", - "2024-12-11 11:14:28,735 - harpy.image._manager - INFO - Writing results to layer 'label_whole_fov1'\n", - "/Users/arnedf/miniconda3/envs/harpy/lib/python3.10/site-packages/anndata/_core/aligned_df.py:67: ImplicitModificationWarning: Transforming to str index.\n", - " warnings.warn(\"Transforming to str index.\", ImplicitModificationWarning)\n", - "/Users/arnedf/miniconda3/envs/harpy/lib/python3.10/site-packages/spatialdata/models/models.py:1046: ImplicitModificationWarning: Trying to modify attribute `._uns` of view, initializing view as actual.\n", - " adata.uns[cls.ATTRS_KEY] = attr\n" - ] - }, - { - "data": { - "text/plain": [ - "SpatialData object\n", - "├── Images\n", - "│ ├── 'raw_image_fov0': DataArray[cyx] (22, 512, 512)\n", - "│ └── 'raw_image_fov1': DataArray[cyx] (22, 1024, 1024)\n", - "├── Labels\n", - "│ ├── 'label_nuclear_fov0': DataArray[yx] (512, 512)\n", - "│ ├── 'label_nuclear_fov1': DataArray[yx] (1024, 1024)\n", - "│ ├── 'label_whole_fov0': DataArray[yx] (512, 512)\n", - "│ └── 'label_whole_fov1': DataArray[yx] (1024, 1024)\n", - "└── Tables\n", - " └── 'table': AnnData (1414, 22)\n", - "with coordinate systems:\n", - " ▸ 'fov0', with elements:\n", - " raw_image_fov0 (Images), label_nuclear_fov0 (Labels), label_whole_fov0 (Labels)\n", - " ▸ 'fov1', with elements:\n", - " raw_image_fov1 (Images), label_nuclear_fov1 (Labels), label_whole_fov1 (Labels)" - ] - }, - "execution_count": 2, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "sdata_ark_analysis = pixie_example([\"fov0\", \"fov1\"])\n", "sdata_ark_analysis" @@ -114,7 +40,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 16, "metadata": {}, "outputs": [], "source": [ @@ -138,49 +64,18 @@ "]" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Preprocess" + ] + }, { "cell_type": "code", - "execution_count": 4, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stderr", - "output_type": "stream", - "text": [ - "2024-12-11 11:14:29,159 - harpy.image._manager - WARNING - No dims parameter specified. Assuming order of dimension of provided array is ((c), (z), y, x)\n", - "2024-12-11 11:14:29,929 - harpy.image._manager - INFO - Writing results to layer 'raw_image_fov0_processed'\n", - "2024-12-11 11:14:29,931 - harpy.image._manager - WARNING - No dims parameter specified. Assuming order of dimension of provided array is ((c), (z), y, x)\n", - "2024-12-11 11:14:30,716 - harpy.image._manager - INFO - Writing results to layer 'raw_image_fov1_processed'\n" - ] - }, - { - "data": { - "text/plain": [ - "SpatialData object\n", - "├── Images\n", - "│ ├── 'raw_image_fov0': DataArray[cyx] (22, 512, 512)\n", - "│ ├── 'raw_image_fov0_processed': DataArray[cyx] (16, 512, 512)\n", - "│ ├── 'raw_image_fov1': DataArray[cyx] (22, 1024, 1024)\n", - "│ └── 'raw_image_fov1_processed': DataArray[cyx] (16, 1024, 1024)\n", - "├── Labels\n", - "│ ├── 'label_nuclear_fov0': DataArray[yx] (512, 512)\n", - "│ ├── 'label_nuclear_fov1': DataArray[yx] (1024, 1024)\n", - "│ ├── 'label_whole_fov0': DataArray[yx] (512, 512)\n", - "│ └── 'label_whole_fov1': DataArray[yx] (1024, 1024)\n", - "└── Tables\n", - " └── 'table': AnnData (1414, 22)\n", - "with coordinate systems:\n", - " ▸ 'fov0', with elements:\n", - " raw_image_fov0 (Images), raw_image_fov0_processed (Images), label_nuclear_fov0 (Labels), label_whole_fov0 (Labels)\n", - " ▸ 'fov1', with elements:\n", - " raw_image_fov1 (Images), raw_image_fov1_processed (Images), label_nuclear_fov1 (Labels), label_whole_fov1 (Labels)" - ] - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "sdata_ark_analysis = hp.im.pixel_clustering_preprocess(\n", " sdata_ark_analysis,\n", @@ -188,66 +83,25 @@ " output_layer=[\"raw_image_fov0_processed\", \"raw_image_fov1_processed\"],\n", " channels=channels,\n", " chunks=2048,\n", + " persist_intermediate=True, # set to False if you have multiple images, and if they are large.\n", " overwrite=True,\n", " sigma=2.0,\n", ")\n", "sdata_ark_analysis" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Pixel clustering" + ] + }, { "cell_type": "code", - "execution_count": 5, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stderr", - "output_type": "stream", - "text": [ - "\u001b[32m2024-12-11 11:14:30.894\u001b[0m | \u001b[34m\u001b[1mDEBUG \u001b[0m | \u001b[36mflowsom.main\u001b[0m:\u001b[36m__init__\u001b[0m:\u001b[36m84\u001b[0m - \u001b[34m\u001b[1mReading input.\u001b[0m\n", - "\u001b[32m2024-12-11 11:14:30.895\u001b[0m | \u001b[34m\u001b[1mDEBUG \u001b[0m | \u001b[36mflowsom.main\u001b[0m:\u001b[36m__init__\u001b[0m:\u001b[36m86\u001b[0m - \u001b[34m\u001b[1mFitting model: clustering and metaclustering.\u001b[0m\n", - "\u001b[32m2024-12-11 11:14:32.906\u001b[0m | \u001b[34m\u001b[1mDEBUG \u001b[0m | \u001b[36mflowsom.main\u001b[0m:\u001b[36m__init__\u001b[0m:\u001b[36m88\u001b[0m - \u001b[34m\u001b[1mUpdating derived values.\u001b[0m\n", - "2024-12-11 11:14:33,626 - harpy.image._manager - WARNING - No dims parameter specified. Assuming order of dimension of provided array is ((c), (z), y, x)\n", - "2024-12-11 11:14:33,629 - harpy.image._manager - INFO - Writing results to layer 'raw_image_fov0_flowsom_clusters'\n", - "2024-12-11 11:14:33,630 - harpy.image._manager - WARNING - No dims parameter specified. Assuming order of dimension of provided array is ((c), (z), y, x)\n", - "2024-12-11 11:14:33,633 - harpy.image._manager - INFO - Writing results to layer 'raw_image_fov0_flowsom_metaclusters'\n", - "2024-12-11 11:14:33,951 - harpy.image._manager - WARNING - No dims parameter specified. Assuming order of dimension of provided array is ((c), (z), y, x)\n", - "2024-12-11 11:14:33,957 - harpy.image._manager - INFO - Writing results to layer 'raw_image_fov1_flowsom_clusters'\n", - "2024-12-11 11:14:33,958 - harpy.image._manager - WARNING - No dims parameter specified. Assuming order of dimension of provided array is ((c), (z), y, x)\n", - "2024-12-11 11:14:33,961 - harpy.image._manager - INFO - Writing results to layer 'raw_image_fov1_flowsom_metaclusters'\n" - ] - }, - { - "data": { - "text/plain": [ - "SpatialData object\n", - "├── Images\n", - "│ ├── 'raw_image_fov0': DataArray[cyx] (22, 512, 512)\n", - "│ ├── 'raw_image_fov0_processed': DataArray[cyx] (16, 512, 512)\n", - "│ ├── 'raw_image_fov1': DataArray[cyx] (22, 1024, 1024)\n", - "│ └── 'raw_image_fov1_processed': DataArray[cyx] (16, 1024, 1024)\n", - "├── Labels\n", - "│ ├── 'label_nuclear_fov0': DataArray[yx] (512, 512)\n", - "│ ├── 'label_nuclear_fov1': DataArray[yx] (1024, 1024)\n", - "│ ├── 'label_whole_fov0': DataArray[yx] (512, 512)\n", - "│ ├── 'label_whole_fov1': DataArray[yx] (1024, 1024)\n", - "│ ├── 'raw_image_fov0_flowsom_clusters': DataArray[yx] (512, 512)\n", - "│ ├── 'raw_image_fov0_flowsom_metaclusters': DataArray[yx] (512, 512)\n", - "│ ├── 'raw_image_fov1_flowsom_clusters': DataArray[yx] (1024, 1024)\n", - "│ └── 'raw_image_fov1_flowsom_metaclusters': DataArray[yx] (1024, 1024)\n", - "└── Tables\n", - " └── 'table': AnnData (1414, 22)\n", - "with coordinate systems:\n", - " ▸ 'fov0', with elements:\n", - " raw_image_fov0 (Images), raw_image_fov0_processed (Images), label_nuclear_fov0 (Labels), label_whole_fov0 (Labels), raw_image_fov0_flowsom_clusters (Labels), raw_image_fov0_flowsom_metaclusters (Labels)\n", - " ▸ 'fov1', with elements:\n", - " raw_image_fov1 (Images), raw_image_fov1_processed (Images), label_nuclear_fov1 (Labels), label_whole_fov1 (Labels), raw_image_fov1_flowsom_clusters (Labels), raw_image_fov1_flowsom_metaclusters (Labels)" - ] - }, - "execution_count": 5, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "import flowsom as fs\n", "from dask.distributed import Client, LocalCluster\n", @@ -286,6 +140,8 @@ " num_batches = 10,\n", " xdim=10,\n", " ydim=10,\n", + " z_score=True,\n", + " z_cap=3,\n", " persist_intermediate=True,\n", " overwrite=True,\n", ")\n", @@ -294,62 +150,9 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stderr", - "output_type": "stream", - "text": [ - "/Users/arnedf/VIB/harpy/src/harpy/table/_allocation_intensity.py:217: ImplicitModificationWarning: Setting element `.obsm['spatial']` of view, initializing view as actual.\n", - " adata.obsm[_SPATIAL] = coordinates\n", - "/Users/arnedf/VIB/harpy/src/harpy/table/_allocation_intensity.py:217: ImplicitModificationWarning: Setting element `.obsm['spatial']` of view, initializing view as actual.\n", - " adata.obsm[_SPATIAL] = coordinates\n", - "/Users/arnedf/miniconda3/envs/harpy/lib/python3.10/site-packages/spatialdata/_core/_elements.py:116: UserWarning: Key `counts_clusters` already exists. Overwriting it in-memory.\n", - " self._check_key(key, self.keys(), self._shared_keys)\n", - "2024-12-11 11:14:35,924 - harpy.table._preprocess - INFO - Calculating cell size from provided labels_layer 'raw_image_fov0_flowsom_clusters'\n", - "2024-12-11 11:14:35,941 - harpy.table._preprocess - INFO - Calculating cell size from provided labels_layer 'raw_image_fov1_flowsom_clusters'\n", - "/Users/arnedf/miniconda3/envs/harpy/lib/python3.10/site-packages/spatialdata/models/models.py:1048: UserWarning: Converting `region_key: fov_labels` to categorical dtype.\n", - " return convert_region_column_to_categorical(adata)\n", - "/Users/arnedf/miniconda3/envs/harpy/lib/python3.10/site-packages/spatialdata/_core/_elements.py:116: UserWarning: Key `counts_clusters` already exists. Overwriting it in-memory.\n", - " self._check_key(key, self.keys(), self._shared_keys)\n", - "/Users/arnedf/miniconda3/envs/harpy/lib/python3.10/site-packages/spatialdata/_core/_elements.py:116: UserWarning: Key `counts_clusters` already exists. Overwriting it in-memory.\n", - " self._check_key(key, self.keys(), self._shared_keys)\n" - ] - }, - { - "data": { - "text/plain": [ - "SpatialData object\n", - "├── Images\n", - "│ ├── 'raw_image_fov0': DataArray[cyx] (22, 512, 512)\n", - "│ ├── 'raw_image_fov0_processed': DataArray[cyx] (16, 512, 512)\n", - "│ ├── 'raw_image_fov1': DataArray[cyx] (22, 1024, 1024)\n", - "│ └── 'raw_image_fov1_processed': DataArray[cyx] (16, 1024, 1024)\n", - "├── Labels\n", - "│ ├── 'label_nuclear_fov0': DataArray[yx] (512, 512)\n", - "│ ├── 'label_nuclear_fov1': DataArray[yx] (1024, 1024)\n", - "│ ├── 'label_whole_fov0': DataArray[yx] (512, 512)\n", - "│ ├── 'label_whole_fov1': DataArray[yx] (1024, 1024)\n", - "│ ├── 'raw_image_fov0_flowsom_clusters': DataArray[yx] (512, 512)\n", - "│ ├── 'raw_image_fov0_flowsom_metaclusters': DataArray[yx] (512, 512)\n", - "│ ├── 'raw_image_fov1_flowsom_clusters': DataArray[yx] (1024, 1024)\n", - "│ └── 'raw_image_fov1_flowsom_metaclusters': DataArray[yx] (1024, 1024)\n", - "└── Tables\n", - " ├── 'counts_clusters': AnnData (100, 16)\n", - " └── 'table': AnnData (1414, 22)\n", - "with coordinate systems:\n", - " ▸ 'fov0', with elements:\n", - " raw_image_fov0 (Images), raw_image_fov0_processed (Images), label_nuclear_fov0 (Labels), label_whole_fov0 (Labels), raw_image_fov0_flowsom_clusters (Labels), raw_image_fov0_flowsom_metaclusters (Labels)\n", - " ▸ 'fov1', with elements:\n", - " raw_image_fov1 (Images), raw_image_fov1_processed (Images), label_nuclear_fov1 (Labels), label_whole_fov1 (Labels), raw_image_fov1_flowsom_clusters (Labels), raw_image_fov1_flowsom_metaclusters (Labels)" - ] - }, - "execution_count": 6, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "sdata_ark_analysis = hp.tb.cluster_intensity(\n", " sdata_ark_analysis,\n", @@ -363,64 +166,65 @@ "sdata_ark_analysis" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Visualization of pixel clusters and metaclusters." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "hp.pl.pixel_clusters(\n", + " sdata_ark_analysis,\n", + " labels_layer=\"raw_image_fov0_flowsom_metaclusters\",\n", + " figsize=(5, 5),\n", + " to_coordinate_system=\"fov0\",\n", + " render_labels_kwargs={ \"alpha\":1 }\n", + ")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Heatmap of channel intensity per cluster and metacluster" + ] + }, { "cell_type": "code", - "execution_count": 7, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stderr", - "output_type": "stream", - "text": [ - "2024-12-11 11:14:36,153 - harpy.table._preprocess - INFO - Calculating cell size from provided labels_layer 'label_whole_fov0'\n", - "2024-12-11 11:14:36,170 - harpy.table._preprocess - INFO - Calculating cell size from provided labels_layer 'label_whole_fov1'\n", - "/Users/arnedf/miniconda3/envs/harpy/lib/python3.10/site-packages/spatialdata/models/models.py:1048: UserWarning: Converting `region_key: fov_labels` to categorical dtype.\n", - " return convert_region_column_to_categorical(adata)\n", - "/Users/arnedf/miniconda3/envs/harpy/lib/python3.10/site-packages/spatialdata/_core/_elements.py:116: UserWarning: Key `table_cell_clustering_flowsom` already exists. Overwriting it in-memory.\n", - " self._check_key(key, self.keys(), self._shared_keys)\n", - "\u001b[32m2024-12-11 11:14:36.219\u001b[0m | \u001b[34m\u001b[1mDEBUG \u001b[0m | \u001b[36mflowsom.main\u001b[0m:\u001b[36m__init__\u001b[0m:\u001b[36m84\u001b[0m - \u001b[34m\u001b[1mReading input.\u001b[0m\n", - "\u001b[32m2024-12-11 11:14:36.220\u001b[0m | \u001b[34m\u001b[1mDEBUG \u001b[0m | \u001b[36mflowsom.main\u001b[0m:\u001b[36m__init__\u001b[0m:\u001b[36m86\u001b[0m - \u001b[34m\u001b[1mFitting model: clustering and metaclustering.\u001b[0m\n", - "\u001b[32m2024-12-11 11:14:36.227\u001b[0m | \u001b[34m\u001b[1mDEBUG \u001b[0m | \u001b[36mflowsom.main\u001b[0m:\u001b[36m__init__\u001b[0m:\u001b[36m88\u001b[0m - \u001b[34m\u001b[1mUpdating derived values.\u001b[0m\n", - "2024-12-11 11:14:36,347 - harpy.table.cell_clustering._clustering - INFO - Adding mean cluster intensity to '.uns['clustering']'\n", - "2024-12-11 11:14:36,362 - harpy.table.cell_clustering._clustering - INFO - Adding mean cluster intensity to '.uns['metaclustering']'\n", - "/Users/arnedf/miniconda3/envs/harpy/lib/python3.10/site-packages/spatialdata/_core/_elements.py:116: UserWarning: Key `table_cell_clustering_flowsom` already exists. Overwriting it in-memory.\n", - " self._check_key(key, self.keys(), self._shared_keys)\n" - ] - }, - { - "data": { - "text/plain": [ - "SpatialData object\n", - "├── Images\n", - "│ ├── 'raw_image_fov0': DataArray[cyx] (22, 512, 512)\n", - "│ ├── 'raw_image_fov0_processed': DataArray[cyx] (16, 512, 512)\n", - "│ ├── 'raw_image_fov1': DataArray[cyx] (22, 1024, 1024)\n", - "│ └── 'raw_image_fov1_processed': DataArray[cyx] (16, 1024, 1024)\n", - "├── Labels\n", - "│ ├── 'label_nuclear_fov0': DataArray[yx] (512, 512)\n", - "│ ├── 'label_nuclear_fov1': DataArray[yx] (1024, 1024)\n", - "│ ├── 'label_whole_fov0': DataArray[yx] (512, 512)\n", - "│ ├── 'label_whole_fov1': DataArray[yx] (1024, 1024)\n", - "│ ├── 'raw_image_fov0_flowsom_clusters': DataArray[yx] (512, 512)\n", - "│ ├── 'raw_image_fov0_flowsom_metaclusters': DataArray[yx] (512, 512)\n", - "│ ├── 'raw_image_fov1_flowsom_clusters': DataArray[yx] (1024, 1024)\n", - "│ └── 'raw_image_fov1_flowsom_metaclusters': DataArray[yx] (1024, 1024)\n", - "└── Tables\n", - " ├── 'counts_clusters': AnnData (100, 16)\n", - " ├── 'table': AnnData (1414, 22)\n", - " └── 'table_cell_clustering_flowsom': AnnData (1409, 20)\n", - "with coordinate systems:\n", - " ▸ 'fov0', with elements:\n", - " raw_image_fov0 (Images), raw_image_fov0_processed (Images), label_nuclear_fov0 (Labels), label_whole_fov0 (Labels), raw_image_fov0_flowsom_clusters (Labels), raw_image_fov0_flowsom_metaclusters (Labels)\n", - " ▸ 'fov1', with elements:\n", - " raw_image_fov1 (Images), raw_image_fov1_processed (Images), label_nuclear_fov1 (Labels), label_whole_fov1 (Labels), raw_image_fov1_flowsom_clusters (Labels), raw_image_fov1_flowsom_metaclusters (Labels)" - ] - }, - "execution_count": 7, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], + "source": [ + "for _metaclusters in [True, False]:\n", + " hp.pl.pixel_clusters_heatmap(\n", + " sdata_ark_analysis,\n", + " table_layer=\"counts_clusters\",\n", + " figsize=(8, 8),\n", + " fig_kwargs={\"dpi\": 100},\n", + " linewidths=0.001,\n", + " metaclusters=_metaclusters,\n", + " z_score=True,\n", + " )" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Cell clustering" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], "source": [ "batch_model=fs.models.BatchFlowSOMEstimator\n", "\n", @@ -441,56 +245,18 @@ "sdata_ark_analysis" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Optional export to a .csv format that can be used for visualization using the ark gui" + ] + }, { "cell_type": "code", - "execution_count": 8, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stderr", - "output_type": "stream", - "text": [ - "2024-12-11 11:14:36,436 - harpy.table.cell_clustering._weighted_channel_expression - INFO - Adding mean over obtained cell clusters '(clustering)' of the average marker expression for each cell weighted by pixel cluster count to '.uns[ 'clustering_channels' ]' of table layer 'table_cell_clustering_flowsom'\n", - "2024-12-11 11:14:36,437 - harpy.table.cell_clustering._weighted_channel_expression - INFO - Adding mean over obtained cell clusters '(metaclustering)' of the average marker expression for each cell weighted by pixel cluster count to '.uns[ 'metaclustering_channels' ]' of table layer 'table_cell_clustering_flowsom'\n", - "2024-12-11 11:14:36,438 - harpy.table.cell_clustering._weighted_channel_expression - INFO - Adding average marker expression for each cell weighted by pixel cluster count to '.obs' of table layer 'table_cell_clustering_flowsom'\n", - "/Users/arnedf/miniconda3/envs/harpy/lib/python3.10/site-packages/spatialdata/_core/_elements.py:116: UserWarning: Key `table_cell_clustering_flowsom` already exists. Overwriting it in-memory.\n", - " self._check_key(key, self.keys(), self._shared_keys)\n" - ] - }, - { - "data": { - "text/plain": [ - "SpatialData object\n", - "├── Images\n", - "│ ├── 'raw_image_fov0': DataArray[cyx] (22, 512, 512)\n", - "│ ├── 'raw_image_fov0_processed': DataArray[cyx] (16, 512, 512)\n", - "│ ├── 'raw_image_fov1': DataArray[cyx] (22, 1024, 1024)\n", - "│ └── 'raw_image_fov1_processed': DataArray[cyx] (16, 1024, 1024)\n", - "├── Labels\n", - "│ ├── 'label_nuclear_fov0': DataArray[yx] (512, 512)\n", - "│ ├── 'label_nuclear_fov1': DataArray[yx] (1024, 1024)\n", - "│ ├── 'label_whole_fov0': DataArray[yx] (512, 512)\n", - "│ ├── 'label_whole_fov1': DataArray[yx] (1024, 1024)\n", - "│ ├── 'raw_image_fov0_flowsom_clusters': DataArray[yx] (512, 512)\n", - "│ ├── 'raw_image_fov0_flowsom_metaclusters': DataArray[yx] (512, 512)\n", - "│ ├── 'raw_image_fov1_flowsom_clusters': DataArray[yx] (1024, 1024)\n", - "│ └── 'raw_image_fov1_flowsom_metaclusters': DataArray[yx] (1024, 1024)\n", - "└── Tables\n", - " ├── 'counts_clusters': AnnData (100, 16)\n", - " ├── 'table': AnnData (1414, 22)\n", - " └── 'table_cell_clustering_flowsom': AnnData (1409, 20)\n", - "with coordinate systems:\n", - " ▸ 'fov0', with elements:\n", - " raw_image_fov0 (Images), raw_image_fov0_processed (Images), label_nuclear_fov0 (Labels), label_whole_fov0 (Labels), raw_image_fov0_flowsom_clusters (Labels), raw_image_fov0_flowsom_metaclusters (Labels)\n", - " ▸ 'fov1', with elements:\n", - " raw_image_fov1 (Images), raw_image_fov1_processed (Images), label_nuclear_fov1 (Labels), label_whole_fov1 (Labels), raw_image_fov1_flowsom_clusters (Labels), raw_image_fov1_flowsom_metaclusters (Labels)" - ] - }, - "execution_count": 8, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "# weighted channel average for visualization -> calculate this on the flowsom clustered matrix\n", "sdata_ark_analysis = hp.tb.weighted_channel_expression(\n", @@ -506,410 +272,13 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stderr", - "output_type": "stream", - "text": [ - "2024-12-11 11:14:36,543 - harpy.table.cell_clustering._utils - WARNING - Increasing cell cluster IDs (SOM cluster and meta cluster IDs) with +1 for visualization. The underlying dataframe in the SpatialData object remains unchanges.\n", - "2024-12-11 11:14:36,547 - harpy.table.cell_clustering._utils - WARNING - Increasing cell cluster IDs (SOM cluster and meta cluster IDs) with +1 for visualization. The underlying dataframe in the SpatialData object remains unchanges.\n", - "2024-12-11 11:14:36,550 - harpy.table.cell_clustering._utils - WARNING - Increasing cell cluster IDs (SOM cluster and meta cluster IDs) with +1 for visualization. The underlying dataframe in the SpatialData object remains unchanges.\n" - ] - }, - { - "data": { - "text/html": [ - "
channels | \n", - "CD3 | \n", - "CD4 | \n", - "CD8 | \n", - "CD14 | \n", - "CD20 | \n", - "CD31 | \n", - "CD45 | \n", - "CD68 | \n", - "CD163 | \n", - "CK17 | \n", - "Collagen1 | \n", - "Fibronectin | \n", - "ECAD | \n", - "HLADR | \n", - "SMA | \n", - "Vim | \n", - "pixel_meta_cluster | \n", - "pixel_som_cluster | \n", - "count | \n", - "
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
cells | \n", - "\n", - " | \n", - " | \n", - " | \n", - " | \n", - " | \n", - " | \n", - " | \n", - " | \n", - " | \n", - " | \n", - " | \n", - " | \n", - " | \n", - " | \n", - " | \n", - " | \n", - " | \n", - " | \n", - " |
1_counts_clusters_a34405d3 | \n", - "79.662205 | \n", - "55.991234 | \n", - "3.339938 | \n", - "4.108338 | \n", - "3.976343 | \n", - "0.578644 | \n", - "40.246395 | \n", - "1.907112 | \n", - "2.754490 | \n", - "1.733413 | \n", - "7.401993 | \n", - "4.566187 | \n", - "2.153363 | \n", - "2.462132 | \n", - "1.229484 | \n", - "5.993419 | \n", - "5 | \n", - "1 | \n", - "11752 | \n", - "
2_counts_clusters_a34405d3 | \n", - "44.357776 | \n", - "78.521997 | \n", - "1.993199 | \n", - "6.019438 | \n", - "2.279956 | \n", - "0.575271 | \n", - "29.573188 | \n", - "2.653170 | \n", - "4.149742 | \n", - "1.820865 | \n", - "9.875389 | \n", - "6.779230 | \n", - "2.466204 | \n", - "3.465180 | \n", - "1.839119 | \n", - "9.828461 | \n", - "5 | \n", - "2 | \n", - "9440 | \n", - "
3_counts_clusters_a34405d3 | \n", - "60.862157 | \n", - "17.323038 | \n", - "7.604455 | \n", - "6.990412 | \n", - "2.871016 | \n", - "1.422096 | \n", - "21.476851 | \n", - "3.547263 | \n", - "6.289704 | \n", - "3.325128 | \n", - "19.664543 | \n", - "12.088449 | \n", - "7.148716 | \n", - "3.925060 | \n", - "5.011668 | \n", - "12.711450 | \n", - "4 | \n", - "3 | \n", - "7020 | \n", - "
4_counts_clusters_a34405d3 | \n", - "13.080275 | \n", - "11.272689 | \n", - "5.681579 | \n", - "7.350961 | \n", - "7.338359 | \n", - "2.766095 | \n", - "79.544518 | \n", - "3.125768 | \n", - "6.229959 | \n", - "3.226597 | \n", - "10.637938 | \n", - "11.225132 | \n", - "3.957128 | \n", - "6.422467 | \n", - "2.363766 | \n", - "12.759784 | \n", - "17 | \n", - "4 | \n", - "7375 | \n", - "
5_counts_clusters_a34405d3 | \n", - "49.938381 | \n", - "10.921948 | \n", - "49.406746 | \n", - "4.959634 | \n", - "5.208744 | \n", - "1.200178 | \n", - "53.361240 | \n", - "1.872839 | \n", - "3.809860 | \n", - "2.536913 | \n", - "5.482853 | \n", - "5.099018 | \n", - "2.056258 | \n", - "2.575106 | \n", - "1.056300 | \n", - "8.767088 | \n", - "2 | \n", - "5 | \n", - "11025 | \n", - "
... | \n", - "... | \n", - "... | \n", - "... | \n", - "... | \n", - "... | \n", - "... | \n", - "... | \n", - "... | \n", - "... | \n", - "... | \n", - "... | \n", - "... | \n", - "... | \n", - "... | \n", - "... | \n", - "... | \n", - "... | \n", - "... | \n", - "... | \n", - "
96_counts_clusters_a34405d3 | \n", - "3.981039 | \n", - "13.323010 | \n", - "3.211220 | \n", - "41.065997 | \n", - "1.524707 | \n", - "1.301879 | \n", - "12.672745 | \n", - "6.005695 | \n", - "9.931868 | \n", - "1.654690 | \n", - "34.965053 | \n", - "12.188016 | \n", - "2.678179 | \n", - "11.858054 | \n", - "6.403222 | \n", - "9.091599 | \n", - "9 | \n", - "96 | \n", - "15057 | \n", - "
97_counts_clusters_a34405d3 | \n", - "2.245529 | \n", - "9.532662 | \n", - "2.118796 | \n", - "62.286725 | \n", - "0.782066 | \n", - "0.652548 | \n", - "9.386354 | \n", - "3.824479 | \n", - "8.670170 | \n", - "1.040886 | \n", - "41.990669 | \n", - "11.365051 | \n", - "1.014963 | \n", - "7.734525 | \n", - "3.382813 | \n", - "4.569495 | \n", - "9 | \n", - "97 | \n", - "11901 | \n", - "
98_counts_clusters_a34405d3 | \n", - "2.191547 | \n", - "5.792474 | \n", - "1.799250 | \n", - "20.217224 | \n", - "0.784272 | \n", - "0.465956 | \n", - "4.838669 | \n", - "3.831804 | \n", - "6.003469 | \n", - "1.157570 | \n", - "79.623646 | \n", - "9.886305 | \n", - "1.222871 | \n", - "4.735837 | \n", - "3.083184 | \n", - "4.082930 | \n", - "11 | \n", - "98 | \n", - "15650 | \n", - "
99_counts_clusters_a34405d3 | \n", - "2.008014 | \n", - "2.885256 | \n", - "2.435322 | \n", - "4.162790 | \n", - "0.742080 | \n", - "0.314810 | \n", - "2.069228 | \n", - "1.886825 | \n", - "2.113793 | \n", - "1.124537 | \n", - "101.731324 | \n", - "8.516621 | \n", - "1.073964 | \n", - "1.264790 | \n", - "2.645304 | \n", - "2.994190 | \n", - "11 | \n", - "99 | \n", - "25505 | \n", - "
100_counts_clusters_a34405d3 | \n", - "0.770209 | \n", - "1.002478 | \n", - "1.119347 | \n", - "1.118850 | \n", - "0.302310 | \n", - "0.087209 | \n", - "0.579103 | \n", - "0.772100 | \n", - "0.737610 | \n", - "0.875762 | \n", - "118.201814 | \n", - "3.522398 | \n", - "0.357518 | \n", - "0.378539 | \n", - "0.837177 | \n", - "1.117873 | \n", - "11 | \n", - "100 | \n", - "21010 | \n", - "
100 rows × 19 columns
\n", - "channels | \n", - "cell_meta_cluster | \n", - "CD3 | \n", - "CD4 | \n", - "CD8 | \n", - "CD14 | \n", - "CD20 | \n", - "CD31 | \n", - "CD45 | \n", - "CD68 | \n", - "CD163 | \n", - "CK17 | \n", - "Collagen1 | \n", - "Fibronectin | \n", - "ECAD | \n", - "HLADR | \n", - "SMA | \n", - "Vim | \n", - "cell_meta_cluster_rename | \n", - "
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | \n", - "1 | \n", - "9.920224 | \n", - "10.636219 | \n", - "9.059409 | \n", - "11.995957 | \n", - "6.404475 | \n", - "7.600641 | \n", - "19.981869 | \n", - "5.353948 | \n", - "7.567123 | \n", - "5.640777 | \n", - "17.392973 | \n", - "13.367731 | \n", - "5.794182 | \n", - "6.907827 | \n", - "9.516907 | \n", - "19.260004 | \n", - "1 | \n", - "
1 | \n", - "2 | \n", - "9.960207 | \n", - "15.345797 | \n", - "6.050494 | \n", - "21.287334 | \n", - "5.233185 | \n", - "2.117434 | \n", - "17.084238 | \n", - "6.750041 | \n", - "13.714162 | \n", - "2.039449 | \n", - "20.468669 | \n", - "16.143988 | \n", - "4.065160 | \n", - "9.322342 | \n", - "6.876040 | \n", - "14.503086 | \n", - "2 | \n", - "
2 | \n", - "3 | \n", - "20.953209 | \n", - "26.071361 | \n", - "8.986690 | \n", - "11.771607 | \n", - "8.442361 | \n", - "1.953716 | \n", - "27.605551 | \n", - "7.085774 | \n", - "8.561212 | \n", - "3.263422 | \n", - "14.460568 | \n", - "10.752086 | \n", - "4.982453 | \n", - "8.778626 | \n", - "5.584443 | \n", - "14.549717 | \n", - "3 | \n", - "
3 | \n", - "4 | \n", - "14.599806 | \n", - "19.162653 | \n", - "6.738744 | \n", - "10.326302 | \n", - "31.657597 | \n", - "2.134133 | \n", - "36.219244 | \n", - "4.099054 | \n", - "9.283807 | \n", - "2.693036 | \n", - "10.250166 | \n", - "8.842964 | \n", - "4.352090 | \n", - "11.591215 | \n", - "2.726349 | \n", - "11.210159 | \n", - "4 | \n", - "
4 | \n", - "5 | \n", - "2.607173 | \n", - "3.224016 | \n", - "1.587835 | \n", - "4.004721 | \n", - "1.092902 | \n", - "0.628333 | \n", - "3.484541 | \n", - "2.121840 | \n", - "2.685696 | \n", - "0.959712 | \n", - "8.164292 | \n", - "5.457338 | \n", - "3.288760 | \n", - "2.259438 | \n", - "3.133704 | \n", - "3.889296 | \n", - "5 | \n", - "
5 | \n", - "6 | \n", - "3.951987 | \n", - "13.003755 | \n", - "3.170035 | \n", - "43.084493 | \n", - "1.789685 | \n", - "1.112180 | \n", - "10.437545 | \n", - "24.989896 | \n", - "19.458082 | \n", - "1.397150 | \n", - "15.664330 | \n", - "9.878738 | \n", - "2.170529 | \n", - "11.558954 | \n", - "5.503335 | \n", - "12.998675 | \n", - "6 | \n", - "
6 | \n", - "7 | \n", - "5.573563 | \n", - "10.668688 | \n", - "3.377258 | \n", - "18.102451 | \n", - "2.561174 | \n", - "4.944619 | \n", - "9.995903 | \n", - "5.517863 | \n", - "9.383994 | \n", - "1.853947 | \n", - "18.188148 | \n", - "14.227881 | \n", - "3.399089 | \n", - "7.011984 | \n", - "9.899051 | \n", - "38.214906 | \n", - "7 | \n", - "
7 | \n", - "8 | \n", - "27.069442 | \n", - "35.805355 | \n", - "5.529667 | \n", - "9.248749 | \n", - "16.994136 | \n", - "1.639795 | \n", - "36.993515 | \n", - "3.775317 | \n", - "7.440898 | \n", - "2.550704 | \n", - "10.308884 | \n", - "8.615445 | \n", - "4.747283 | \n", - "8.732910 | \n", - "2.593586 | \n", - "10.743067 | \n", - "8 | \n", - "
8 | \n", - "9 | \n", - "15.020223 | \n", - "19.070327 | \n", - "6.534041 | \n", - "10.172453 | \n", - "22.049004 | \n", - "2.585827 | \n", - "30.746578 | \n", - "4.145985 | \n", - "8.132600 | \n", - "2.868514 | \n", - "13.845175 | \n", - "11.285477 | \n", - "6.531780 | \n", - "11.375026 | \n", - "3.932905 | \n", - "12.996394 | \n", - "9 | \n", - "
9 | \n", - "10 | \n", - "6.325782 | \n", - "10.142026 | \n", - "4.140835 | \n", - "12.977858 | \n", - "2.843029 | \n", - "3.477757 | \n", - "10.121100 | \n", - "5.695717 | \n", - "7.105341 | \n", - "2.085989 | \n", - "21.844039 | \n", - "16.293455 | \n", - "4.120571 | \n", - "6.143693 | \n", - "13.951618 | \n", - "33.232461 | \n", - "10 | \n", - "
10 | \n", - "11 | \n", - "13.092599 | \n", - "17.965436 | \n", - "4.825496 | \n", - "8.515950 | \n", - "42.571354 | \n", - "1.532716 | \n", - "40.957757 | \n", - "3.471640 | \n", - "8.142602 | \n", - "2.668636 | \n", - "8.247525 | \n", - "7.364200 | \n", - "3.827464 | \n", - "11.626355 | \n", - "1.792867 | \n", - "9.971521 | \n", - "11 | \n", - "
11 | \n", - "12 | \n", - "35.788462 | \n", - "47.732614 | \n", - "5.096323 | \n", - "9.389730 | \n", - "6.580950 | \n", - "1.574196 | \n", - "37.113912 | \n", - "3.773668 | \n", - "6.861717 | \n", - "2.523721 | \n", - "9.791365 | \n", - "8.184188 | \n", - "4.291852 | \n", - "6.876105 | \n", - "2.593271 | \n", - "10.422270 | \n", - "12 | \n", - "
12 | \n", - "13 | \n", - "11.267333 | \n", - "22.112405 | \n", - "4.436330 | \n", - "11.221689 | \n", - "8.517499 | \n", - "1.823718 | \n", - "26.488645 | \n", - "5.377660 | \n", - "7.821649 | \n", - "2.314485 | \n", - "11.743102 | \n", - "10.017828 | \n", - "5.973943 | \n", - "32.964157 | \n", - "4.123915 | \n", - "15.393191 | \n", - "13 | \n", - "
13 | \n", - "14 | \n", - "5.599900 | \n", - "8.217478 | \n", - "4.093117 | \n", - "11.615315 | \n", - "2.393532 | \n", - "2.072741 | \n", - "8.319907 | \n", - "4.740646 | \n", - "5.862981 | \n", - "1.771940 | \n", - "45.296469 | \n", - "17.510412 | \n", - "3.273068 | \n", - "4.844663 | \n", - "9.434585 | \n", - "14.237374 | \n", - "14 | \n", - "
14 | \n", - "15 | \n", - "4.070490 | \n", - "10.468684 | \n", - "2.958566 | \n", - "20.728478 | \n", - "3.051426 | \n", - "1.548027 | \n", - "8.166279 | \n", - "65.663976 | \n", - "15.892551 | \n", - "1.361709 | \n", - "12.513647 | \n", - "7.178638 | \n", - "2.129428 | \n", - "10.472426 | \n", - "4.074070 | \n", - "14.436947 | \n", - "15 | \n", - "
15 | \n", - "16 | \n", - "25.989399 | \n", - "10.389395 | \n", - "43.361902 | \n", - "11.320441 | \n", - "5.502868 | \n", - "1.596107 | \n", - "31.901745 | \n", - "4.047915 | \n", - "6.649028 | \n", - "2.593001 | \n", - "12.630810 | \n", - "9.429332 | \n", - "3.332031 | \n", - "5.850626 | \n", - "4.441145 | \n", - "12.780872 | \n", - "16 | \n", - "
16 | \n", - "17 | \n", - "6.171975 | \n", - "14.129711 | \n", - "4.450432 | \n", - "32.999771 | \n", - "2.991384 | \n", - "1.449651 | \n", - "14.678769 | \n", - "10.301502 | \n", - "16.227985 | \n", - "1.979589 | \n", - "19.928467 | \n", - "13.745564 | \n", - "3.307375 | \n", - "13.041965 | \n", - "7.408849 | \n", - "14.392972 | \n", - "17 | \n", - "
17 | \n", - "18 | \n", - "4.160652 | \n", - "5.233203 | \n", - "3.001677 | \n", - "3.694203 | \n", - "1.580467 | \n", - "0.939031 | \n", - "6.375645 | \n", - "1.274844 | \n", - "2.753888 | \n", - "96.253502 | \n", - "3.903337 | \n", - "3.271621 | \n", - "16.286033 | \n", - "3.430607 | \n", - "59.106409 | \n", - "5.572448 | \n", - "18 | \n", - "
18 | \n", - "19 | \n", - "5.443319 | \n", - "5.737314 | \n", - "4.578300 | \n", - "6.719422 | \n", - "1.976835 | \n", - "2.241670 | \n", - "6.249517 | \n", - "2.944613 | \n", - "6.888751 | \n", - "17.290541 | \n", - "7.797342 | \n", - "5.843747 | \n", - "63.039098 | \n", - "4.542249 | \n", - "11.757854 | \n", - "4.433470 | \n", - "19 | \n", - "
19 | \n", - "20 | \n", - "12.661802 | \n", - "14.215822 | \n", - "6.632497 | \n", - "10.445187 | \n", - "5.404565 | \n", - "2.035558 | \n", - "17.039902 | \n", - "4.757361 | \n", - "7.733497 | \n", - "3.587793 | \n", - "18.233667 | \n", - "12.162264 | \n", - "9.456957 | \n", - "7.759186 | \n", - "5.301065 | \n", - "10.924089 | \n", - "20 | \n", - "