scverse / scverse/spatialdata

Improvement to aggregation (`by_key`)

Open
#304 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
394
Forks
95
Avg merge
4d 3h
Merged PRs (30d)
7

Description

In the Xenium + Visium notebook 01

This block of code, that performs an aggregation of cell types (cells) by visium circles (with fractions=True) could be simplified. Here we aggregate not into individual Visium circles, but into the area given by all Visium circles that have a categorical variable corresponding to a value (e.g. clone 1).

cell_types_categories = xe_rep1_roi_sdata.table.obs["celltype_major"].cat.categories.tolist()

rois_fractions = {}
for row in landmarks_sdata["rois"].iterrows():
    name = row[1][-1]
    cells_inside = cells_in_rois_sdata_rep1[f"Shapes in ROI '{name}'"]
    indices_rep1 = cells_inside.index.tolist()
    corresponding_rows_mask = xe_rep1_roi_sdata.table.obs["cell_id"].isin(indices_rep1)
    corresponding_rows = xe_rep1_roi_sdata.table[corresponding_rows_mask]
    cell_types = corresponding_rows.obs["celltype_major"]
    empty = pd.Series(index=cell_types_categories, data=np.zeros(len(cell_types_categories), dtype=float))
    counts = cell_types.value_counts()
    empty.loc[counts.index] = counts
    rois_fractions[name] = empty
df1_rois = pd.DataFrame(rois_fractions).transpose()

Now the aggregation APIs are robusts enough to considering adding a by_key parameter that groups shapes in by by the value of a categorical column of the by spatial element, and then performs the aggregation.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the aggregation code and the Xenium + Visium notebook at notebooks/paper_reproducibility/01_xenium_and_visium.ipynb. Read the current aggregation API entry points and determine how a by_key value from a categorical column should group the shapes. Done means the API supports this grouping and produces the aggregated cell-type results represented by the notebook example.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
data
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.