From 27d4cd0ff3ed254c3eea9083b0227999b815f8e9 Mon Sep 17 00:00:00 2001 From: tdefa Date: Mon, 3 Jun 2024 16:50:56 +0200 Subject: [PATCH 1/2] comseg_patch update and api_tuto" --- docs/tutorials/other_segmentations.ipynb | 441 +++++++++++++++++++++-- sopa/segmentation/methods.py | 14 +- 2 files changed, 410 insertions(+), 45 deletions(-) diff --git a/docs/tutorials/other_segmentations.ipynb b/docs/tutorials/other_segmentations.ipynb index de9b231e..3dfbcf91 100644 --- a/docs/tutorials/other_segmentations.ipynb +++ b/docs/tutorials/other_segmentations.ipynb @@ -4,13 +4,27 @@ "cell_type": "code", "execution_count": 1, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "import sopa\n", "import sopa.io\n", "import sopa.segmentation\n", "from sopa.segmentation.transcripts import resolve\n", - "from sopa.segmentation.methods import comseg_patch" + "from sopa.segmentation.methods import comseg_patch\n", + "import importlib\n", + "import sopa.segmentation.methods as method \n", + "importlib.reload(method)" ] }, { @@ -19,9 +33,16 @@ "source": [ "# ComSeg segmentation\n", "\n", - "[ComSeg](https://github.com/fish-quant/ComSeg) is a transcript-based segmentation method. It uses a segmentation prior (here, Cellpose) and improves it using the transcripts information.\n", + "[ComSeg](https://github.com/fish-quant/ComSeg) is a transcript-based segmentation method that creates a KNN graph of RNA molecules, weighted by the co-expression of RNA species. Initially, this KNN graph is partitioned into communities of RNAs likely to belong to the same cell. ComSeg then merges these RNA communities to compute the final cell segmentation. The method uses nucleus segmentation as prior to initialize the partitioning of the KNN graph of RNA molecules. \n", + "ComSeg only segments cells with their nuclei segmented and does not take into account cells withou missing nucleus. \n", + "\n", + "If nucleus segmentation is not available, ComSeg can operate using other staining segmentation or solely nucleus centroids obtained from other sources. For more details, please refer to the [documentation](https://comseg.readthedocs.io/en/latest/)\n", + "\n", + "In this tutorial, we first compute the nucleus segmentation prior using Cellpose on the DAPI staining\n", + "\n", + "## 1. Running Cellpose as a prior\n", "\n", - "## 1. Running Cellpose as a prior" + "First, we generate the bounding boxes of the patches on which Cellpose will be run. Here, the patches have a width and height of 1500 pixels and an overlap of 50 pixels. We advise bigger sizes for real datasets (see our default parameters in one of our config files). On the toy dataset, this will generate 4 patches.\n" ] }, { @@ -36,14 +57,14 @@ "\u001b[36;20m[INFO] (sopa.utils.data)\u001b[0m Image of size ((4, 2048, 2048)) with 400 cells and 100 transcripts per cell\n", "\u001b[36;20m[INFO] (sopa.patches.patches)\u001b[0m 4 patches were saved in sdata['sopa_patches']\n", "\u001b[33;20m[WARNING] (sopa.segmentation.stainings)\u001b[0m Running segmentation in a sequential manner. This is not recommended on large images because it can be extremely slow (see https://github.com/gustaveroussy/sopa/discussions/36 for more details)\n", - "Run all patches: 0%| | 0/4 [00:00" + "
" ] }, "metadata": {}, @@ -315,9 +684,9 @@ ], "metadata": { "kernelspec": { - "display_name": "spatial", + "display_name": "sopa", "language": "python", - "name": "python3" + "name": "sopa" }, "language_info": { "codemirror_mode": { diff --git a/sopa/segmentation/methods.py b/sopa/segmentation/methods.py index dab3db93..ffe7dabc 100644 --- a/sopa/segmentation/methods.py +++ b/sopa/segmentation/methods.py @@ -81,7 +81,6 @@ def segmentation_function(image: np.ndarray) -> np.ndarray: def comseg_patch(temp_dir: str, patch_index: int, config: dict): - import importlib import json try: @@ -92,9 +91,6 @@ def comseg_patch(temp_dir: str, patch_index: int, config: dict): "Install the ComSeg package (`pip install comseg`) for this method to work" ) - importlib.reload(ds) - importlib.reload(dictionary) - path_dataset_folder = Path(temp_dir) / str(patch_index) dataset = ds.ComSegDataset( @@ -107,7 +103,7 @@ def comseg_patch(temp_dir: str, patch_index: int, config: dict): path_cell_centroid=path_dataset_folder, ) - dataset.compute_edge_weight() + dataset.compute_edge_weight(config=config) Comsegdict = dictionary.ComSegDict( dataset=dataset, @@ -115,11 +111,11 @@ def comseg_patch(temp_dir: str, patch_index: int, config: dict): prior_name=SopaKeys.DEFAULT_CELL_KEY, ) - Comsegdict.run_all(max_cell_radius=config["max_cell_radius"]) + Comsegdict.run_all(config=config) - anndata_comseg, json_dict = Comsegdict.anndata_from_comseg_result( - return_polygon=True, alpha=config["alpha"], min_rna_per_cell=config["min_rna_per_cell"] - ) + if 'return_polygon' in config: + assert config["return_polygon"] is True, "Only return_polygon=True is supported in sopa" + anndata_comseg, json_dict = Comsegdict.anndata_from_comseg_result(config=config) anndata_comseg.write_h5ad(path_dataset_folder / "segmentation_counts.h5ad") with open(path_dataset_folder / "segmentation_polygons.json", "w") as f: json.dump(json_dict["transcripts"], f) From 088fc14b3982712b37c32467fd02213abc389b44 Mon Sep 17 00:00:00 2001 From: Thomas Defard <40570554+tdefa@users.noreply.github.com> Date: Mon, 3 Jun 2024 19:20:24 +0200 Subject: [PATCH 2/2] Update methods.py --- sopa/segmentation/methods.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sopa/segmentation/methods.py b/sopa/segmentation/methods.py index ffe7dabc..ceb5b4e4 100644 --- a/sopa/segmentation/methods.py +++ b/sopa/segmentation/methods.py @@ -113,7 +113,7 @@ def comseg_patch(temp_dir: str, patch_index: int, config: dict): Comsegdict.run_all(config=config) - if 'return_polygon' in config: + if "return_polygon" in config: assert config["return_polygon"] is True, "Only return_polygon=True is supported in sopa" anndata_comseg, json_dict = Comsegdict.anndata_from_comseg_result(config=config) anndata_comseg.write_h5ad(path_dataset_folder / "segmentation_counts.h5ad")