From 8a6cba668498065fa8f214d4dc496fadcecd533c Mon Sep 17 00:00:00 2001 From: ArneDefauw Date: Tue, 10 Dec 2024 09:53:37 +0100 Subject: [PATCH] SC_40 example with multiple coordinate systems for flowsom clustering --- ...lowSOM_for_pixel_and_cell_clustering.ipynb | 270 ++++++++++-------- src/harpy/datasets/pixie_example.py | 12 +- .../table/cell_clustering/_preprocess.py | 2 +- 3 files changed, 158 insertions(+), 126 deletions(-) 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 3c1c1bd..1325a24 100644 --- a/docs/tutorials/general/FlowSOM_for_pixel_and_cell_clustering.ipynb +++ b/docs/tutorials/general/FlowSOM_for_pixel_and_cell_clustering.ipynb @@ -50,13 +50,13 @@ "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-10 09:11:01,189 - harpy.image._manager - WARNING - No dims parameter specified. Assuming order of dimension of provided array is ((c), (z), y, x)\n", - "2024-12-10 09:11:01,266 - harpy.image._manager - INFO - Writing results to layer 'raw_image_fov0'\n", - "2024-12-10 09:11:01,268 - harpy.image._manager - WARNING - No dims parameter specified. Assuming order of dimension of provided array is ((c), (z), y, x)\n", - "2024-12-10 09:11:01,273 - harpy.image._manager - INFO - Writing results to layer 'label_nuclear_fov0'\n", - "2024-12-10 09:11:01,275 - harpy.image._manager - WARNING - No dims parameter specified. Assuming order of dimension of provided array is ((c), (z), y, x)\n", - "2024-12-10 09:11:01,280 - harpy.image._manager - INFO - Writing results to layer 'label_whole_fov0'\n", - "2024-12-10 09:11:01,300 - harpy.image._manager - WARNING - No dims parameter specified. Assuming order of dimension of provided array is ((c), (z), y, x)\n" + "2024-12-10 09:46:33,318 - harpy.image._manager - WARNING - No dims parameter specified. Assuming order of dimension of provided array is ((c), (z), y, x)\n", + "2024-12-10 09:46:33,401 - harpy.image._manager - INFO - Writing results to layer 'raw_image_fov0'\n", + "2024-12-10 09:46:33,403 - harpy.image._manager - WARNING - No dims parameter specified. Assuming order of dimension of provided array is ((c), (z), y, x)\n", + "2024-12-10 09:46:33,408 - harpy.image._manager - INFO - Writing results to layer 'label_nuclear_fov0'\n", + "2024-12-10 09:46:33,410 - harpy.image._manager - WARNING - No dims parameter specified. Assuming order of dimension of provided array is ((c), (z), y, x)\n", + "2024-12-10 09:46:33,414 - harpy.image._manager - INFO - Writing results to layer 'label_whole_fov0'\n", + "2024-12-10 09:46:33,436 - harpy.image._manager - WARNING - No dims parameter specified. Assuming order of dimension of provided array is ((c), (z), y, x)\n" ] }, { @@ -70,11 +70,11 @@ "name": "stderr", "output_type": "stream", "text": [ - "2024-12-10 09:11:01,404 - harpy.image._manager - INFO - Writing results to layer 'raw_image_fov1'\n", - "2024-12-10 09:11:01,406 - harpy.image._manager - WARNING - No dims parameter specified. Assuming order of dimension of provided array is ((c), (z), y, x)\n", - "2024-12-10 09:11:01,412 - harpy.image._manager - INFO - Writing results to layer 'label_nuclear_fov1'\n", - "2024-12-10 09:11:01,413 - harpy.image._manager - WARNING - No dims parameter specified. Assuming order of dimension of provided array is ((c), (z), y, x)\n", - "2024-12-10 09:11:01,419 - harpy.image._manager - INFO - Writing results to layer 'label_whole_fov1'\n", + "2024-12-10 09:46:33,539 - harpy.image._manager - INFO - Writing results to layer 'raw_image_fov1'\n", + "2024-12-10 09:46:33,541 - harpy.image._manager - WARNING - No dims parameter specified. Assuming order of dimension of provided array is ((c), (z), y, x)\n", + "2024-12-10 09:46:33,546 - harpy.image._manager - INFO - Writing results to layer 'label_nuclear_fov1'\n", + "2024-12-10 09:46:33,548 - harpy.image._manager - WARNING - No dims parameter specified. Assuming order of dimension of provided array is ((c), (z), y, x)\n", + "2024-12-10 09:46:33,553 - 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", @@ -96,8 +96,10 @@ "└── Tables\n", " └── 'table': AnnData (1414, 22)\n", "with coordinate systems:\n", - " ▸ 'global', with elements:\n", - " raw_image_fov0 (Images), raw_image_fov1 (Images), label_nuclear_fov0 (Labels), label_nuclear_fov1 (Labels), label_whole_fov0 (Labels), label_whole_fov1 (Labels)" + " ▸ '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, @@ -145,10 +147,10 @@ "name": "stderr", "output_type": "stream", "text": [ - "2024-12-10 09:11:01,857 - harpy.image._manager - WARNING - No dims parameter specified. Assuming order of dimension of provided array is ((c), (z), y, x)\n", - "2024-12-10 09:11:02,713 - harpy.image._manager - INFO - Writing results to layer 'raw_image_fov0_processed'\n", - "2024-12-10 09:11:02,715 - harpy.image._manager - WARNING - No dims parameter specified. Assuming order of dimension of provided array is ((c), (z), y, x)\n", - "2024-12-10 09:11:03,545 - harpy.image._manager - INFO - Writing results to layer 'raw_image_fov1_processed'\n" + "2024-12-10 09:46:33,984 - harpy.image._manager - WARNING - No dims parameter specified. Assuming order of dimension of provided array is ((c), (z), y, x)\n", + "2024-12-10 09:46:34,839 - harpy.image._manager - INFO - Writing results to layer 'raw_image_fov0_processed'\n", + "2024-12-10 09:46:34,841 - harpy.image._manager - WARNING - No dims parameter specified. Assuming order of dimension of provided array is ((c), (z), y, x)\n", + "2024-12-10 09:46:35,665 - harpy.image._manager - INFO - Writing results to layer 'raw_image_fov1_processed'\n" ] }, { @@ -168,8 +170,10 @@ "└── Tables\n", " └── 'table': AnnData (1414, 22)\n", "with coordinate systems:\n", - " ▸ 'global', with elements:\n", - " raw_image_fov0 (Images), raw_image_fov0_processed (Images), raw_image_fov1 (Images), raw_image_fov1_processed (Images), label_nuclear_fov0 (Labels), label_nuclear_fov1 (Labels), label_whole_fov0 (Labels), label_whole_fov1 (Labels)" + " ▸ '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, @@ -199,17 +203,17 @@ "name": "stderr", "output_type": "stream", "text": [ - "\u001b[32m2024-12-10 09:11:03.706\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-10 09:11:03.707\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-10 09:11:05.745\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-10 09:11:06,083 - harpy.image._manager - WARNING - No dims parameter specified. Assuming order of dimension of provided array is ((c), (z), y, x)\n", - "2024-12-10 09:11:06,582 - harpy.image._manager - INFO - Writing results to layer 'raw_image_fov0_flowsom_clusters'\n", - "2024-12-10 09:11:06,583 - harpy.image._manager - WARNING - No dims parameter specified. Assuming order of dimension of provided array is ((c), (z), y, x)\n", - "2024-12-10 09:11:06,880 - harpy.image._manager - INFO - Writing results to layer 'raw_image_fov0_flowsom_metaclusters'\n", - "2024-12-10 09:11:06,933 - harpy.image._manager - WARNING - No dims parameter specified. Assuming order of dimension of provided array is ((c), (z), y, x)\n", - "2024-12-10 09:11:07,613 - harpy.image._manager - INFO - Writing results to layer 'raw_image_fov1_flowsom_clusters'\n", - "2024-12-10 09:11:07,613 - harpy.image._manager - WARNING - No dims parameter specified. Assuming order of dimension of provided array is ((c), (z), y, x)\n", - "2024-12-10 09:11:08,238 - harpy.image._manager - INFO - Writing results to layer 'raw_image_fov1_flowsom_metaclusters'\n" + "\u001b[32m2024-12-10 09:46:35.835\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-10 09:46:35.836\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-10 09:46:37.821\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-10 09:46:38,138 - harpy.image._manager - WARNING - No dims parameter specified. Assuming order of dimension of provided array is ((c), (z), y, x)\n", + "2024-12-10 09:46:38,618 - harpy.image._manager - INFO - Writing results to layer 'raw_image_fov0_flowsom_clusters'\n", + "2024-12-10 09:46:38,618 - harpy.image._manager - WARNING - No dims parameter specified. Assuming order of dimension of provided array is ((c), (z), y, x)\n", + "2024-12-10 09:46:38,904 - harpy.image._manager - INFO - Writing results to layer 'raw_image_fov0_flowsom_metaclusters'\n", + "2024-12-10 09:46:38,956 - harpy.image._manager - WARNING - No dims parameter specified. Assuming order of dimension of provided array is ((c), (z), y, x)\n", + "2024-12-10 09:46:39,650 - harpy.image._manager - INFO - Writing results to layer 'raw_image_fov1_flowsom_clusters'\n", + "2024-12-10 09:46:39,650 - harpy.image._manager - WARNING - No dims parameter specified. Assuming order of dimension of provided array is ((c), (z), y, x)\n", + "2024-12-10 09:46:40,267 - harpy.image._manager - INFO - Writing results to layer 'raw_image_fov1_flowsom_metaclusters'\n" ] }, { @@ -233,8 +237,10 @@ "└── Tables\n", " └── 'table': AnnData (1414, 22)\n", "with coordinate systems:\n", - " ▸ 'global', with elements:\n", - " raw_image_fov0 (Images), raw_image_fov0_processed (Images), raw_image_fov1 (Images), raw_image_fov1_processed (Images), label_nuclear_fov0 (Labels), label_nuclear_fov1 (Labels), label_whole_fov0 (Labels), label_whole_fov1 (Labels), raw_image_fov0_flowsom_clusters (Labels), raw_image_fov0_flowsom_metaclusters (Labels), raw_image_fov1_flowsom_clusters (Labels), raw_image_fov1_flowsom_metaclusters (Labels)" + " ▸ '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, @@ -274,8 +280,8 @@ " 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-10 09:11:10,356 - harpy.table._preprocess - INFO - Calculating cell size from provided labels_layer 'raw_image_fov0_flowsom_clusters'\n", - "2024-12-10 09:11:10,372 - harpy.table._preprocess - INFO - Calculating cell size from provided labels_layer 'raw_image_fov1_flowsom_clusters'\n", + "2024-12-10 09:46:42,395 - harpy.table._preprocess - INFO - Calculating cell size from provided labels_layer 'raw_image_fov0_flowsom_clusters'\n", + "2024-12-10 09:46:42,412 - 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", @@ -306,8 +312,10 @@ " ├── 'counts_clusters': AnnData (100, 16)\n", " └── 'table': AnnData (1414, 22)\n", "with coordinate systems:\n", - " ▸ 'global', with elements:\n", - " raw_image_fov0 (Images), raw_image_fov0_processed (Images), raw_image_fov1 (Images), raw_image_fov1_processed (Images), label_nuclear_fov0 (Labels), label_nuclear_fov1 (Labels), label_whole_fov0 (Labels), label_whole_fov1 (Labels), raw_image_fov0_flowsom_clusters (Labels), raw_image_fov0_flowsom_metaclusters (Labels), raw_image_fov1_flowsom_clusters (Labels), raw_image_fov1_flowsom_metaclusters (Labels)" + " ▸ '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, @@ -321,11 +329,10 @@ " mapping=mapping,\n", " img_layer=[\"raw_image_fov0_processed\", \"raw_image_fov1_processed\"],\n", " labels_layer=[\"raw_image_fov0_flowsom_clusters\", \"raw_image_fov1_flowsom_clusters\"],\n", - " to_coordinate_system=[ \"global\", \"global\" ],\n", + " to_coordinate_system=[ \"fov0\", \"fov1\" ],\n", " output_layer=\"counts_clusters\",\n", " overwrite=True,\n", ")\n", - "# Note: to visualize in napari-spatialdata, this counts_clusters table layer needs to be removed for now. See https://github.com/scverse/napari-spatialdata/issues/244\n", "sdata_ark_analysis" ] }, @@ -338,17 +345,17 @@ "name": "stderr", "output_type": "stream", "text": [ - "2024-12-10 09:11:10,618 - harpy.table._preprocess - INFO - Calculating cell size from provided labels_layer 'label_whole_fov0'\n", - "2024-12-10 09:11:10,634 - harpy.table._preprocess - INFO - Calculating cell size from provided labels_layer 'label_whole_fov1'\n", + "2024-12-10 09:46:42,627 - harpy.table._preprocess - INFO - Calculating cell size from provided labels_layer 'label_whole_fov0'\n", + "2024-12-10 09:46:42,643 - 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-10 09:11:10.681\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-10 09:11:10.682\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-10 09:11:10.705\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-10 09:11:10,839 - harpy.table.cell_clustering._clustering - INFO - Adding mean cluster intensity to '.uns['clustering']'\n", - "2024-12-10 09:11:10,854 - harpy.table.cell_clustering._clustering - INFO - Adding mean cluster intensity to '.uns['metaclustering']'\n", + "\u001b[32m2024-12-10 09:46:42.688\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-10 09:46:42.689\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-10 09:46:42.711\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-10 09:46:42,840 - harpy.table.cell_clustering._clustering - INFO - Adding mean cluster intensity to '.uns['clustering']'\n", + "2024-12-10 09:46:42,853 - 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" ] @@ -376,8 +383,10 @@ " ├── 'table': AnnData (1414, 22)\n", " └── 'table_cell_clustering_flowsom': AnnData (1409, 20)\n", "with coordinate systems:\n", - " ▸ 'global', with elements:\n", - " raw_image_fov0 (Images), raw_image_fov0_processed (Images), raw_image_fov1 (Images), raw_image_fov1_processed (Images), label_nuclear_fov0 (Labels), label_nuclear_fov1 (Labels), label_whole_fov0 (Labels), label_whole_fov1 (Labels), raw_image_fov0_flowsom_clusters (Labels), raw_image_fov0_flowsom_metaclusters (Labels), raw_image_fov1_flowsom_clusters (Labels), raw_image_fov1_flowsom_metaclusters (Labels)" + " ▸ '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, @@ -410,9 +419,9 @@ "name": "stderr", "output_type": "stream", "text": [ - "2024-12-10 09:11:10,899 - 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-10 09:11:10,904 - 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-10 09:11:10,913 - 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", + "2024-12-10 09:46:42,910 - 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-10 09:46:42,931 - 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-10 09:46:42,933 - 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" ] @@ -440,8 +449,10 @@ " ├── 'table': AnnData (1414, 22)\n", " └── 'table_cell_clustering_flowsom': AnnData (1409, 20)\n", "with coordinate systems:\n", - " ▸ 'global', with elements:\n", - " raw_image_fov0 (Images), raw_image_fov0_processed (Images), raw_image_fov1 (Images), raw_image_fov1_processed (Images), label_nuclear_fov0 (Labels), label_nuclear_fov1 (Labels), label_whole_fov0 (Labels), label_whole_fov1 (Labels), raw_image_fov0_flowsom_clusters (Labels), raw_image_fov0_flowsom_metaclusters (Labels), raw_image_fov1_flowsom_clusters (Labels), raw_image_fov1_flowsom_metaclusters (Labels)" + " ▸ '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, @@ -471,9 +482,9 @@ "name": "stderr", "output_type": "stream", "text": [ - "2024-12-10 09:11:10,968 - 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-10 09:11:10,978 - 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-10 09:11:10,981 - 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-10 09:46:42,978 - 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-10 09:46:42,981 - 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-10 09:46:42,986 - 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" ] }, { @@ -542,7 +553,7 @@ " \n", " \n", " \n", - " 1_counts_clusters_55e09093\n", + " 1_counts_clusters_fe90513d\n", " 37.843970\n", " 2.887842\n", " 97.043740\n", @@ -564,7 +575,7 @@ " 9930\n", " \n", " \n", - " 2_counts_clusters_55e09093\n", + " 2_counts_clusters_fe90513d\n", " 38.690112\n", " 9.945175\n", " 49.737972\n", @@ -586,7 +597,7 @@ " 12134\n", " \n", " \n", - " 3_counts_clusters_55e09093\n", + " 3_counts_clusters_fe90513d\n", " 73.091492\n", " 17.922896\n", " 18.220559\n", @@ -608,7 +619,7 @@ " 5392\n", " \n", " \n", - " 4_counts_clusters_55e09093\n", + " 4_counts_clusters_fe90513d\n", " 76.282287\n", " 61.344410\n", " 2.660174\n", @@ -630,7 +641,7 @@ " 12617\n", " \n", " \n", - " 5_counts_clusters_55e09093\n", + " 5_counts_clusters_fe90513d\n", " 47.305260\n", " 59.382376\n", " 3.534356\n", @@ -674,7 +685,7 @@ " ...\n", " \n", " \n", - " 96_counts_clusters_55e09093\n", + " 96_counts_clusters_fe90513d\n", " 3.381922\n", " 10.202954\n", " 2.722567\n", @@ -696,7 +707,7 @@ " 11251\n", " \n", " \n", - " 97_counts_clusters_55e09093\n", + " 97_counts_clusters_fe90513d\n", " 2.470278\n", " 8.337522\n", " 2.250204\n", @@ -718,7 +729,7 @@ " 13194\n", " \n", " \n", - " 98_counts_clusters_55e09093\n", + " 98_counts_clusters_fe90513d\n", " 3.030046\n", " 12.170709\n", " 2.506028\n", @@ -740,7 +751,7 @@ " 15672\n", " \n", " \n", - " 99_counts_clusters_55e09093\n", + " 99_counts_clusters_fe90513d\n", " 3.106052\n", " 14.744514\n", " 2.634652\n", @@ -762,7 +773,7 @@ " 16393\n", " \n", " \n", - " 100_counts_clusters_55e09093\n", + " 100_counts_clusters_fe90513d\n", " 1.480684\n", " 13.220733\n", " 1.721745\n", @@ -791,73 +802,73 @@ "text/plain": [ "channels CD3 CD4 CD8 CD14 \\\n", "cells \n", - "1_counts_clusters_55e09093 37.843970 2.887842 97.043740 5.178658 \n", - "2_counts_clusters_55e09093 38.690112 9.945175 49.737972 5.014744 \n", - "3_counts_clusters_55e09093 73.091492 17.922896 18.220559 4.631251 \n", - "4_counts_clusters_55e09093 76.282287 61.344410 2.660174 3.988683 \n", - "5_counts_clusters_55e09093 47.305260 59.382376 3.534356 5.237534 \n", + "1_counts_clusters_fe90513d 37.843970 2.887842 97.043740 5.178658 \n", + "2_counts_clusters_fe90513d 38.690112 9.945175 49.737972 5.014744 \n", + "3_counts_clusters_fe90513d 73.091492 17.922896 18.220559 4.631251 \n", + "4_counts_clusters_fe90513d 76.282287 61.344410 2.660174 3.988683 \n", + "5_counts_clusters_fe90513d 47.305260 59.382376 3.534356 5.237534 \n", "... ... ... ... ... \n", - "96_counts_clusters_55e09093 3.381922 10.202954 2.722567 19.882297 \n", - "97_counts_clusters_55e09093 2.470278 8.337522 2.250204 45.239891 \n", - "98_counts_clusters_55e09093 3.030046 12.170709 2.506028 59.447868 \n", - "99_counts_clusters_55e09093 3.106052 14.744514 2.634652 77.775664 \n", - "100_counts_clusters_55e09093 1.480684 13.220733 1.721745 106.230223 \n", + "96_counts_clusters_fe90513d 3.381922 10.202954 2.722567 19.882297 \n", + "97_counts_clusters_fe90513d 2.470278 8.337522 2.250204 45.239891 \n", + "98_counts_clusters_fe90513d 3.030046 12.170709 2.506028 59.447868 \n", + "99_counts_clusters_fe90513d 3.106052 14.744514 2.634652 77.775664 \n", + "100_counts_clusters_fe90513d 1.480684 13.220733 1.721745 106.230223 \n", "\n", "channels CD20 CD31 CD45 CD68 \\\n", "cells \n", - "1_counts_clusters_55e09093 1.315880 0.589464 30.252512 1.639205 \n", - "2_counts_clusters_55e09093 8.713191 1.219908 56.096997 2.077967 \n", - "3_counts_clusters_55e09093 4.585831 1.025564 52.106125 2.153796 \n", - "4_counts_clusters_55e09093 3.950231 0.583871 39.929262 1.899014 \n", - "5_counts_clusters_55e09093 8.039006 0.985423 55.497515 2.064406 \n", + "1_counts_clusters_fe90513d 1.315880 0.589464 30.252512 1.639205 \n", + "2_counts_clusters_fe90513d 8.713191 1.219908 56.096997 2.077967 \n", + "3_counts_clusters_fe90513d 4.585831 1.025564 52.106125 2.153796 \n", + "4_counts_clusters_fe90513d 3.950231 0.583871 39.929262 1.899014 \n", + "5_counts_clusters_fe90513d 8.039006 0.985423 55.497515 2.064406 \n", "... ... ... ... ... \n", - "96_counts_clusters_55e09093 2.628480 1.435061 12.535290 6.423287 \n", - "97_counts_clusters_55e09093 0.850247 0.568664 8.317586 3.651513 \n", - "98_counts_clusters_55e09093 1.032256 0.860999 11.455320 4.825058 \n", - "99_counts_clusters_55e09093 1.103696 0.984863 14.039679 5.912146 \n", - "100_counts_clusters_55e09093 0.480134 0.498194 10.636129 5.904330 \n", + "96_counts_clusters_fe90513d 2.628480 1.435061 12.535290 6.423287 \n", + "97_counts_clusters_fe90513d 0.850247 0.568664 8.317586 3.651513 \n", + "98_counts_clusters_fe90513d 1.032256 0.860999 11.455320 4.825058 \n", + "99_counts_clusters_fe90513d 1.103696 0.984863 14.039679 5.912146 \n", + "100_counts_clusters_fe90513d 0.480134 0.498194 10.636129 5.904330 \n", "\n", "channels CD163 CK17 Collagen1 Fibronectin \\\n", "cells \n", - "1_counts_clusters_55e09093 2.432772 2.077082 7.781796 4.139287 \n", - "2_counts_clusters_55e09093 4.129621 2.686037 5.455420 5.524063 \n", - "3_counts_clusters_55e09093 4.097220 2.331725 7.267913 6.139904 \n", - "4_counts_clusters_55e09093 2.709320 1.705489 7.024916 4.346849 \n", - "5_counts_clusters_55e09093 4.395945 2.033076 4.772678 4.934692 \n", + "1_counts_clusters_fe90513d 2.432772 2.077082 7.781796 4.139287 \n", + "2_counts_clusters_fe90513d 4.129621 2.686037 5.455420 5.524063 \n", + "3_counts_clusters_fe90513d 4.097220 2.331725 7.267913 6.139904 \n", + "4_counts_clusters_fe90513d 2.709320 1.705489 7.024916 4.346849 \n", + "5_counts_clusters_fe90513d 4.395945 2.033076 4.772678 4.934692 \n", "... ... ... ... ... \n", - "96_counts_clusters_55e09093 7.808438 1.716372 37.751931 11.812116 \n", - "97_counts_clusters_55e09093 7.229076 1.053102 55.057834 11.486410 \n", - "98_counts_clusters_55e09093 9.948482 1.224571 33.234845 12.156787 \n", - "99_counts_clusters_55e09093 12.283513 1.081740 13.259535 10.251130 \n", - "100_counts_clusters_55e09093 14.462784 0.657815 9.449697 6.441781 \n", + "96_counts_clusters_fe90513d 7.808438 1.716372 37.751931 11.812116 \n", + "97_counts_clusters_fe90513d 7.229076 1.053102 55.057834 11.486410 \n", + "98_counts_clusters_fe90513d 9.948482 1.224571 33.234845 12.156787 \n", + "99_counts_clusters_fe90513d 12.283513 1.081740 13.259535 10.251130 \n", + "100_counts_clusters_fe90513d 14.462784 0.657815 9.449697 6.441781 \n", "\n", "channels ECAD HLADR SMA Vim \\\n", "cells \n", - "1_counts_clusters_55e09093 1.638660 2.509107 2.552425 7.210590 \n", - "2_counts_clusters_55e09093 2.271283 3.385231 0.894532 8.693713 \n", - "3_counts_clusters_55e09093 2.960410 2.698596 1.826165 9.665717 \n", - "4_counts_clusters_55e09093 2.103179 2.492270 1.044199 5.976883 \n", - "5_counts_clusters_55e09093 2.381851 4.444559 0.383196 5.731354 \n", + "1_counts_clusters_fe90513d 1.638660 2.509107 2.552425 7.210590 \n", + "2_counts_clusters_fe90513d 2.271283 3.385231 0.894532 8.693713 \n", + "3_counts_clusters_fe90513d 2.960410 2.698596 1.826165 9.665717 \n", + "4_counts_clusters_fe90513d 2.103179 2.492270 1.044199 5.976883 \n", + "5_counts_clusters_fe90513d 2.381851 4.444559 0.383196 5.731354 \n", "... ... ... ... ... \n", - "96_counts_clusters_55e09093 2.634270 31.108982 6.185559 9.853439 \n", - "97_counts_clusters_55e09093 1.136371 6.642796 3.817631 4.767680 \n", - "98_counts_clusters_55e09093 1.475615 9.946127 4.933891 6.348947 \n", - "99_counts_clusters_55e09093 1.735470 12.797073 6.092375 8.267192 \n", - "100_counts_clusters_55e09093 0.566056 12.013471 2.820398 5.216806 \n", + "96_counts_clusters_fe90513d 2.634270 31.108982 6.185559 9.853439 \n", + "97_counts_clusters_fe90513d 1.136371 6.642796 3.817631 4.767680 \n", + "98_counts_clusters_fe90513d 1.475615 9.946127 4.933891 6.348947 \n", + "99_counts_clusters_fe90513d 1.735470 12.797073 6.092375 8.267192 \n", + "100_counts_clusters_fe90513d 0.566056 12.013471 2.820398 5.216806 \n", "\n", "channels pixel_meta_cluster pixel_som_cluster count \n", "cells \n", - "1_counts_clusters_55e09093 2 1 9930 \n", - "2_counts_clusters_55e09093 1 2 12134 \n", - "3_counts_clusters_55e09093 1 3 5392 \n", - "4_counts_clusters_55e09093 9 4 12617 \n", - "5_counts_clusters_55e09093 9 5 16815 \n", + "1_counts_clusters_fe90513d 2 1 9930 \n", + "2_counts_clusters_fe90513d 1 2 12134 \n", + "3_counts_clusters_fe90513d 1 3 5392 \n", + "4_counts_clusters_fe90513d 9 4 12617 \n", + "5_counts_clusters_fe90513d 9 5 16815 \n", "... ... ... ... \n", - "96_counts_clusters_55e09093 5 96 11251 \n", - "97_counts_clusters_55e09093 5 97 13194 \n", - "98_counts_clusters_55e09093 5 98 15672 \n", - "99_counts_clusters_55e09093 5 99 16393 \n", - "100_counts_clusters_55e09093 18 100 10009 \n", + "96_counts_clusters_fe90513d 5 96 11251 \n", + "97_counts_clusters_fe90513d 5 97 13194 \n", + "98_counts_clusters_fe90513d 5 98 15672 \n", + "99_counts_clusters_fe90513d 5 99 16393 \n", + "100_counts_clusters_fe90513d 18 100 10009 \n", "\n", "[100 rows x 19 columns]" ] @@ -1446,6 +1457,33 @@ "source": [ "df_cell_meta_cluster_channel_avg" ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'region': ['label_whole_fov0', 'label_whole_fov1'],\n", + " 'region_key': 'fov_labels',\n", + " 'instance_key': 'cell_ID'}" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# \"table_cell_clustering_flowsom\" is annotated by segmentation masks, so they can be visualised using napari-spatialdata\n", + "sdata_ark_analysis[ \"table_cell_clustering_flowsom\" ].uns[ \"spatialdata_attrs\" ]\n", + "\n", + "#from napari_spatialdata import Interactive\n", + "\n", + "#Interactive(sdata_ark_analysis)" + ] } ], "metadata": { diff --git a/src/harpy/datasets/pixie_example.py b/src/harpy/datasets/pixie_example.py index 6e53a76..d90b3ae 100644 --- a/src/harpy/datasets/pixie_example.py +++ b/src/harpy/datasets/pixie_example.py @@ -96,9 +96,7 @@ def pixie_example(fovs: list | None = None, with_pixel_output=True, with_cells_o output_layer=f"raw_image_{fov}", c_coords=channels, overwrite=True, - # transformation={ - # fov: sd.transformations.Identity() - # } + transformations={fov: sd.transformations.Identity()}, ) sdata = harpy.im.add_labels_layer( @@ -106,9 +104,7 @@ def pixie_example(fovs: list | None = None, with_pixel_output=True, with_cells_o arr=imread.imread(os.path.join(path_segment_data, f"{fov}_nuclear.tiff")).squeeze(), output_layer=f"label_nuclear_{fov}", overwrite=True, - # transformation={ - # fov: sd.transformations.Identity() - # } + transformations={fov: sd.transformations.Identity()}, ) sdata = harpy.im.add_labels_layer( @@ -116,9 +112,7 @@ def pixie_example(fovs: list | None = None, with_pixel_output=True, with_cells_o arr=imread.imread(os.path.join(path_segment_data, f"{fov}_whole_cell.tiff")).squeeze(), output_layer=f"label_whole_{fov}", overwrite=True, - # transformation={ - # fov: sd.transformations.Identity() - # } + transformations={fov: sd.transformations.Identity()}, ) if with_cells_output: prefix = "label_whole_" diff --git a/src/harpy/table/cell_clustering/_preprocess.py b/src/harpy/table/cell_clustering/_preprocess.py index bcaeb35..c47880f 100644 --- a/src/harpy/table/cell_clustering/_preprocess.py +++ b/src/harpy/table/cell_clustering/_preprocess.py @@ -86,7 +86,7 @@ def cell_clustering_preprocess( ), f"Provided labels layers '{_labels_layer_cells}' and '{_labels_layer_clusters}' do not have the same shape." assert ( - get_transformation(se_labels) == get_transformation(se_clusters) + get_transformation(se_labels, get_all=True) == get_transformation(se_clusters, get_all=True) ), f"Transformation on provided labels layers '{_labels_layer_cells}' and '{_labels_layer_clusters}' are not equal. This is currently not supported." if i == 0: