Skip to content

Commit

Permalink
formatting + minor display fix in tutorials
Browse files Browse the repository at this point in the history
  • Loading branch information
quentinblampey committed Aug 18, 2024
1 parent fd504bf commit cd12b47
Show file tree
Hide file tree
Showing 9 changed files with 125 additions and 80 deletions.
155 changes: 92 additions & 63 deletions docs/tutorials/api_usage.ipynb

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/tutorials/comseg.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"\n",
"First, follow the original [CLI tutorial](https://gustaveroussy.github.io/sopa/tutorials/cli_usage/) until you finished the \"Cellpose segmentation\" section, and then, continue below.\n",
"\n",
"#### 1. Save a ComSeg config file as a `config.json` file\n",
"#### 1. Save a ComSeg config file as a config.json file\n",
"\n",
"We display below a minimal example of a ComSeg `config.json` file\n",
"\n",
Expand Down
6 changes: 2 additions & 4 deletions docs/tutorials/xenium_explorer/explorer.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
"outputs": [],
"source": [
"import sopa\n",
"import sopa.io\n",
"import sopa.segmentation\n",
"import spatialdata"
]
},
Expand Down Expand Up @@ -175,7 +173,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"## Use the coordinates of a lasso selection in `SpatialData`\n",
"## Use the coordinates of a lasso selection in SpatialData\n",
"\n",
"On the Xenium Explorer, you can use the Lasso or Rectangular selection tools to select some regions of interest. Then, you'll be able to analyze back this region of interest using `spatialdata`.\n",
"\n",
Expand Down Expand Up @@ -252,7 +250,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"### Cropping a `SpatialData` object from a selection\n",
"### Cropping a SpatialData object from a selection\n",
"\n",
"You can also export the whole selection as a polygon and use it to crop the `spatialdata` object. For that, click on \"Download Selection Coordinates as CSV\", as below. It will create a file called `\"Selection_1_coordinates.csv\"`.\n",
"\n",
Expand Down
2 changes: 1 addition & 1 deletion sopa/cli/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ def aggregate(
):
"""Create an `anndata` table containing the transcript count and/or the channel intensities per cell"""
from sopa.io.standardize import read_zarr_standardized
from sopa.segmentation.aggregate import Aggregator
from sopa.segmentation import Aggregator

sdata = read_zarr_standardized(sdata_path, warn=True)

Expand Down
4 changes: 2 additions & 2 deletions sopa/segmentation/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from . import shapes, aggregate, methods, stainings
from . import aggregation, shapes, methods, stainings
from ..patches import Patches2D
from .aggregate import Aggregator, overlay_segmentation
from .aggregation import Aggregator, overlay_segmentation
from .stainings import StainingSegmentation
from .tissue import tissue_segmentation
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,24 @@ def overlay_segmentation(
)


def aggregate(
sdata: SpatialData,
average_intensities: bool = True,
expand_radius_ratio: float = 0,
min_transcripts: int = 0,
min_intensity_ratio: float = 0,
**kwargs: int,
):
aggr = Aggregator(sdata, **kwargs)

aggr.compute_table(
average_intensities=average_intensities,
expand_radius_ratio=expand_radius_ratio,
min_transcripts=min_transcripts,
min_intensity_ratio=min_intensity_ratio,
)


class Aggregator:
"""Perform transcript count and channel averaging over a `SpatialData` object"""

Expand Down Expand Up @@ -151,9 +169,9 @@ def overlay_segmentation(
self.geo_df = pd.concat([geo_df_cropped, geo_df], join="outer", axis=0)
self.geo_df.attrs = old_geo_df.attrs

self.standardized_table(save_table=save_table)
self.add_standardized_table(save_table=save_table)

def standardized_table(self, save_table: bool = True):
def add_standardized_table(self, save_table: bool = True):
self.table.obs_names = list(map(str_cell_id, range(self.table.n_obs)))

self.geo_df.index = list(self.table.obs_names)
Expand Down Expand Up @@ -269,7 +287,7 @@ def compute_table(
SopaKeys.UNS_HAS_INTENSITIES: average_intensities,
}

self.standardized_table(save_table=save_table)
self.add_standardized_table(save_table=save_table)


def _overlap_area_ratio(row) -> float:
Expand Down
4 changes: 2 additions & 2 deletions sopa/segmentation/transcripts.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

from .._constants import SopaKeys
from .._sdata import get_spatial_element, get_spatial_image
from . import aggregate, shapes
from . import aggregation, shapes

log = logging.getLogger(__name__)

Expand Down Expand Up @@ -58,7 +58,7 @@ def resolve(
geo_df_new = ShapesModel.parse(geo_df_new, transformations=transformations)

log.info("Aggregating transcripts on merged cells")
table_conflicts = aggregate.count_transcripts(sdata, gene_column, geo_df=geo_df_new)
table_conflicts = aggregation.count_transcripts(sdata, gene_column, geo_df=geo_df_new)
table_conflicts.obs_names = new_ids
table_conflicts = [table_conflicts]

Expand Down
2 changes: 1 addition & 1 deletion sopa/utils/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ def uniform(


def _add_table(sdata: SpatialData):
from ..segmentation.aggregate import Aggregator
from ..segmentation import Aggregator

aggregator = Aggregator(sdata, shapes_key=SopaKeys.CELLPOSE_BOUNDARIES)

Expand Down
6 changes: 3 additions & 3 deletions tests/test_aggregation.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import xarray as xr
from shapely.geometry import Polygon, box

from sopa.segmentation import aggregate
from sopa.segmentation import aggregation

dask.config.set({"dataframe.query-planning": False})
import dask.dataframe as dd # noqa
Expand All @@ -23,7 +23,7 @@ def test_average_channels_aligned():
# One cell is on the first block, one is overlapping on both blocks, and one is on the last block
cells = [box(x, y, x + cell_size - 1, y + cell_size - 1) for x, y in cell_start]

means = aggregate._average_channels_aligned(xarr, cells)
means = aggregation._average_channels_aligned(xarr, cells)

true_means = np.stack([image[:, y : y + cell_size, x : x + cell_size].mean(axis=(1, 2)) for x, y in cell_start])

Expand All @@ -50,7 +50,7 @@ def test_count_transcripts():

gdf = gpd.GeoDataFrame(geometry=polygons)

adata = aggregate._count_transcripts_aligned(gdf, points, "gene")
adata = aggregation._count_transcripts_aligned(gdf, points, "gene")
expected = np.array([[0, 3, 1], [1, 0, 1], [1, 3, 1]])

assert (adata.X.toarray() == expected).all()

0 comments on commit cd12b47

Please sign in to comment.