-
Notifications
You must be signed in to change notification settings - Fork 42
Add the functions for calculating the bivariate Moran index and the b… #329
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
…ivariate local Moran index
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This pull request introduces bivariate spatial correlation functionality to the spateo package by adding two new functions for calculating bivariate Moran's I statistics. The implementation provides both global and local spatial correlation analysis between spatial variables and gene expression data.
- Adds global bivariate Moran's I calculation between spatial variables and genes
- Adds local bivariate Moran's I calculation between any two spatial features
- Integrates the new functions into the tools module's public API
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 5 comments.
| File | Description |
|---|---|
| spateo/tools/spatial_correlation.py | New module implementing bivariate Moran's I functions with comprehensive parameter validation and result handling |
| spateo/tools/init.py | Exports the new spatial correlation functions to make them available in the public API |
| copy: bool = False, | ||
| ) -> Optional[pd.DataFrame]: | ||
| """ | ||
| Calculate global bivariate Moran's I between a spatial variable and gene expression |
Copilot
AI
Aug 1, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The docstring incorrectly describes the function as calculating 'global bivariate Moran's I' when the function name and implementation clearly indicate it calculates 'local bivariate Moran's I'. This should be corrected to 'Calculate local bivariate Moran's I between two spatial features'.
| Calculate global bivariate Moran's I between a spatial variable and gene expression | |
| Calculate local bivariate Moran's I between two spatial features |
| feature1_key | ||
| Key in `adata.obs` for the first variable or gene_name | ||
| feature2_key | ||
| Key in `adata.obs` for the seconda variable or gene_name |
Copilot
AI
Aug 1, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is a typo in the parameter description: 'seconda' should be 'second'.
| Key in `adata.obs` for the seconda variable or gene_name | |
| Key in `adata.obs` for the second variable or gene_name |
| raise KeyError(f"'{feature1_key}' not found in adata.obs and a gene name") | ||
|
|
||
| if feature2_key not in adata.obs and feature2_key not in adata.var_names: | ||
| raise KeyError(f"'{feature2_key}' not found in adata.obs and a gene name") |
Copilot
AI
Aug 1, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The error message is grammatically incorrect and unclear. It should be: "'{feature1_key}' not found in adata.obs or as a gene name".
| raise KeyError(f"'{feature1_key}' not found in adata.obs and a gene name") | |
| if feature2_key not in adata.obs and feature2_key not in adata.var_names: | |
| raise KeyError(f"'{feature2_key}' not found in adata.obs and a gene name") | |
| raise KeyError(f"'{feature1_key}' not found in adata.obs or as a gene name") | |
| if feature2_key not in adata.obs and feature2_key not in adata.var_names: | |
| raise KeyError(f"'{feature2_key}' not found in adata.obs or as a gene name") |
| raise KeyError(f"'{feature1_key}' not found in adata.obs and a gene name") | ||
|
|
||
| if feature2_key not in adata.obs and feature2_key not in adata.var_names: | ||
| raise KeyError(f"'{feature2_key}' not found in adata.obs and a gene name") |
Copilot
AI
Aug 1, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The error message is grammatically incorrect and unclear. It should be: "'{feature2_key}' not found in adata.obs or as a gene name".
| raise KeyError(f"'{feature1_key}' not found in adata.obs and a gene name") | |
| if feature2_key not in adata.obs and feature2_key not in adata.var_names: | |
| raise KeyError(f"'{feature2_key}' not found in adata.obs and a gene name") | |
| raise KeyError(f"'{feature1_key}' not found in adata.obs or as a gene name") | |
| if feature2_key not in adata.obs and feature2_key not in adata.var_names: | |
| raise KeyError(f"'{feature2_key}' not found in adata.obs or as a gene name") |
| y = np.array(adata.obs[feature2_key].values, dtype=np.float64) | ||
| else: | ||
| idx = adata.var_names.get_loc(feature2_key) | ||
| print(hasattr(adata.X[:, idx], "toarray")) |
Copilot
AI
Aug 1, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This debug print statement should be removed from production code as it serves no functional purpose and will clutter the output.
| print(hasattr(adata.X[:, idx], "toarray")) |
This pull request introduces two new spatial correlation functions to the spateo package:
spatial_bv_moran_obs_genes: Calculates the bivariate global Moran’s I index for a cell's feature with specificed genes.
spatial_bv_local_moran_obs_genes: Calculates the bivariate local Moran’s I index for any two cell's features.