-
Notifications
You must be signed in to change notification settings - Fork 0
Adding functions to drop a percentage of counts and plot FMS #24
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
Open
nbedanova
wants to merge
2
commits into
main
Choose a base branch
from
Deviance-comparison
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,6 +5,7 @@ | |
| import scanpy as sc | ||
| import scipy.sparse as sps | ||
| from pacmap import PaCMAP | ||
| from parafac2.normalize import prepare_dataset | ||
| from parafac2.parafac2 import parafac2_nd, store_pf2 | ||
| from scipy.stats import gmean | ||
| from sklearn.decomposition import PCA | ||
|
|
@@ -190,3 +191,188 @@ def fms_diff_ranks( | |
| ) | ||
|
|
||
| return df | ||
|
|
||
|
|
||
| def downsample_counts_multinomial( | ||
| X: anndata.AnnData, | ||
| percent_drop: float, | ||
| random_state: int = 0, | ||
| ) -> anndata.AnnData: | ||
| """ | ||
| Create a downsampled counts copy of AnnData using multinomial sampling. | ||
|
|
||
| Parameters: | ||
| ----------- | ||
| X : anndata.AnnData | ||
| Input dataset | ||
| percent_drop : float | ||
| Percentage of counts to drop (0-100) | ||
| random_state : int | ||
| Random seed for reproducibility | ||
|
|
||
| Returns: | ||
| -------- | ||
| anndata.AnnData | ||
| Downsampled copy of the input data | ||
| """ | ||
| import scipy.sparse as sp | ||
|
|
||
| # Handle 0% drop case | ||
| if percent_drop == 0: | ||
| return X.copy() | ||
|
|
||
| # Set random seed | ||
| np.random.seed(random_state) | ||
|
|
||
| # Convert to CSR and extract structure | ||
| original_csr = X.X.tocsr() | ||
| data = original_csr.data.copy() | ||
| indices = original_csr.indices | ||
| indptr = original_csr.indptr | ||
|
|
||
| # Process each cell | ||
| for cell_idx in range(X.n_obs): | ||
| start_idx = indptr[cell_idx] | ||
| end_idx = indptr[cell_idx + 1] | ||
|
|
||
| if start_idx == end_idx: | ||
| continue | ||
|
|
||
| cell_data = data[start_idx:end_idx] | ||
| total_counts = int(np.sum(cell_data)) | ||
|
|
||
| if total_counts == 0: | ||
| continue | ||
|
|
||
| new_total = int(total_counts * (1 - percent_drop / 100)) | ||
| if new_total == 0: | ||
| data[start_idx:end_idx] = 0 | ||
| continue | ||
|
|
||
| # Convert to probabilities and normalize | ||
| probs = cell_data / total_counts | ||
| probs = probs / np.sum(probs) # Ensure sum = 1.0 | ||
|
|
||
| # Multinomial sampling | ||
| new_counts = np.random.multinomial(new_total, probs) | ||
| data[start_idx:end_idx] = new_counts.astype(cell_data.dtype) | ||
|
|
||
| # Create new sparse matrix | ||
| sampled_csr = sp.csr_matrix((data, indices, indptr), shape=original_csr.shape) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ohh preserving the sparsity is clever. |
||
|
|
||
| # Create new AnnData object | ||
| sampled_data = X.copy() | ||
| sampled_data.X = sampled_csr | ||
|
|
||
| return sampled_data | ||
|
|
||
|
|
||
| def calculate_fms_downsample( | ||
| X: anndata.AnnData, | ||
| X_pf2: anndata.AnnData, | ||
| percent_drop: float, | ||
| rank: int = 30, | ||
| deviance: bool = False, | ||
| condition: str = "Condition", | ||
| random_state: int = 0, | ||
| ) -> float: | ||
| """ | ||
| Calculate FMS for a single downsampling scenario. | ||
|
|
||
| Parameters: | ||
| ----------- | ||
| X : anndata.AnnData | ||
| Original dataset for reference | ||
| X_pf2 : anndata.AnnData | ||
| Full factorized dataset | ||
| percent_drop : float | ||
| Percentage of counts to drop (0-100) | ||
| rank : int | ||
| Factorization rank | ||
| deviance : bool | ||
| Whether to use deviance normalization | ||
| condition : str | ||
| Condition column name | ||
| random_state : int | ||
| Random seed | ||
|
|
||
| Returns: | ||
| -------- | ||
| float | ||
| FMS score | ||
| """ | ||
|
|
||
| # Handle 0% drop case | ||
| if percent_drop == 0: | ||
| return 1.0 | ||
|
|
||
| # Create downsampled data | ||
| sampled_data = downsample_counts_multinomial( | ||
| X, percent_drop, random_state=random_state | ||
| ) | ||
|
|
||
| # Apply same processing as reference | ||
| sampled_data = prepare_dataset( | ||
| sampled_data, condition, geneThreshold=0.0, deviance=deviance | ||
| ) | ||
|
|
||
| # Factorization | ||
| sampledX = pf2(sampled_data, rank, random_state=random_state + 2, doEmbedding=False) | ||
|
|
||
| return calculateFMS(X_pf2, sampledX) | ||
|
|
||
|
|
||
| def fms_percent_drop_counts( | ||
| X: anndata.AnnData, | ||
| percentList: np.ndarray, | ||
| rank: int = 30, | ||
| deviance: bool = False, | ||
| condition: str = "Condition", | ||
| geneThreshold: float = 0.0, | ||
nbedanova marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| random_state: int = 0, | ||
| ) -> pd.DataFrame: | ||
| """ | ||
| Calculate FMS for multiple downsampling percentages (single run). | ||
|
|
||
| Parameters: | ||
| ----------- | ||
| X : anndata.AnnData | ||
| Input dataset | ||
| percentList : np.ndarray | ||
| Array of dropout percentages to test | ||
| rank : int | ||
| Factorization rank | ||
| deviance : bool | ||
| Whether to use deviance normalization | ||
| condition : str | ||
| Condition column name | ||
| geneThreshold : float | ||
| Gene threshold for preparation | ||
| random_state : int | ||
| Random seed | ||
|
|
||
| Returns: | ||
| -------- | ||
| pd.DataFrame | ||
| DataFrame with columns: Percentage of Counts Dropped, FMS | ||
| """ | ||
| results = [] | ||
| X_prepared = prepare_dataset( | ||
| X, condition, geneThreshold=geneThreshold, deviance=deviance | ||
| ) | ||
| X_pf2 = pf2(X_prepared, rank, doEmbedding=False) | ||
|
|
||
| for percent_drop in percentList: | ||
| fms_score = calculate_fms_downsample( | ||
| X=X, | ||
| X_pf2=X_pf2, | ||
| percent_drop=percent_drop, | ||
| rank=rank, | ||
| deviance=deviance, | ||
| condition=condition, | ||
| random_state=random_state, | ||
| ) | ||
|
|
||
| results.append({"Percentage of Counts Dropped": percent_drop, "FMS": fms_score}) | ||
|
|
||
| return pd.DataFrame(results) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| """ | ||
| factorization score | ||
|
|
||
| """ | ||
|
|
||
| from anndata import read_h5ad | ||
|
|
||
| from .common import getSetup, subplotLabel | ||
| from .commonFuncs.plotGeneral import plot_fms_percent_drop_counts | ||
|
|
||
|
|
||
| def makeFigure(): | ||
| ax, f = getSetup((6, 3), (1, 1)) | ||
| subplotLabel(ax) | ||
| # Using our cytokine dataset | ||
| X = read_h5ad("/opt/extra-storage/Treg_h5ads/Treg_raw.h5ad") | ||
nbedanova marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| # Remove multiplexing identifiers | ||
| X = X[:, ~X.var_names.str.match("^CMO3[0-9]{2}$")] # type: ignore | ||
| # Remove genes with too few reads now | ||
| X = X[X.X.sum(axis=1) > 10, X.X.mean(axis=0) > 0.1] | ||
| X = X.copy() | ||
| percentList = [0.0, 30.0, 50.0] | ||
| plot_fms_percent_drop_counts( | ||
| X, ax[0], percentList, rank=15, deviance=True, label="Deviance" | ||
| ) | ||
| plot_fms_percent_drop_counts( | ||
| X, ax[0], percentList, rank=15, deviance=False, label="CPM" | ||
| ) | ||
|
|
||
| return f | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.