Skip to content

Add function for calculating niches#831

Merged
LLehner merged 108 commits intomainfrom
niche_definitions
Mar 15, 2025
Merged

Add function for calculating niches#831
LLehner merged 108 commits intomainfrom
niche_definitions

Conversation

@LLehner
Copy link
Member

@LLehner LLehner commented May 27, 2024

Description

Adds a function that calculates niches using different strategies. The initial function calculates niches based on neighborhood profiles similar to here.

This PR will get updated with methods discussed in #789.

@LLehner LLehner marked this pull request as draft May 27, 2024 16:24
@LLehner LLehner requested a review from timtreis May 27, 2024 16:43
@timtreis timtreis added the squidpy2.0 Everything releated to a Squidpy 2.0 release label May 29, 2024
@LLehner LLehner requested a review from giovp December 9, 2024 17:46
inplace,
)

if resolutions is None:
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not set this as default right away?

if resolutions is None:
resolutions = [0.5]

if distance is None:
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this too

elif libraries is not None and len(libraries) > 0:
return [f"{base_column}_{lib}" for lib in libraries]

# For neighborhood and utag, we need to handle resolutions
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would do this with if-else block maybe

groups: str | None,
n_neighbors: int | None,
resolutions: float | list[float],
subset_groups: list[str] | None,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why remove this completely? It serves a different purpose than masking. This was also described in the original tutorial. E.g. a mask allows to restrict niche calculation to specific cell types but they can have neighbors outside the masked region. Subsetting means only observations within that subset are considered as neighbors.


def _get_utag_niches(
adata: AnnData,
subset_groups: list[str] | None,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here subsetting serves the same purpose as before.

if library_key not in data.tables[table_key].obs.columns:
raise ValueError(f"'library_key' must be a column in 'adata.obs', got {library_key}")

if n_neighbors is not None and not isinstance(n_neighbors, int):
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why check these separately?

for param_name in param_specs["unused"]:
param_value = param_dict.get(param_name)

# Special handling for boolean parameters with default values
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this necessary?

@LLehner LLehner merged commit 9129acb into main Mar 15, 2025
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

graph 🕸️ release-added squidpy2.0 Everything releated to a Squidpy 2.0 release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants