You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
invalid value encountered in divide when running pp.normalize_pearson_residuals #2496
I have checked that this issue has not already been reported.
I have confirmed this bug exists on the latest version of scanpy.
(optional) I have confirmed this bug exists on the master branch of scanpy.
Note: Please read this guide detailing how to provide the necessary information for us to reproduce your bug.
Hi, I'm trying to follow Analytic Pearson residuals
but after getting first 2 nomalizations, I'm stucked with Pearson residuals normalization. It seems like a devided by 0 issue? I don't know. What else I can do to check if the matrix is the problem. already did gene (min_cells=3) and cell filtering(min_genes=200).
Minimal code sample (that we can copy&paste without having any data)
I've run into the same issue. In my case adata.layers["analytic_pearson_residuals"].sum(1) gives an array of nans because there are nans in analytic_pearson["X"], as indicated by RuntimeWarning.
I am still only investigating this, but if treating nans as 0 is OK there is numpy.nansum function that could be used instead of sum.
It seems that some genes did not express in all cells. I tried to used sc.pp.filter_genes(adata, min_cells=1) to filter out those genes, and addressed this issue.
in the recipe, after hvg, we should filter cells again, because with a subset of genes, the previous cell filter retaining cells might not have count:
# After subsetting to HVG, before normalize_pearson_residualscell_sums=adata_pca.X.sum(axis=1)
ifisinstance(cell_sums, DaskArray):
cell_sums=cell_sums.compute()
cell_sums=np.asarray(cell_sums).ravel()
valid_cells=cell_sums>1ifnotvalid_cells.all():
warnings.warn(f"Removing {(~valid_cells).sum()} cells with zero counts in HVG genes.", UserWarning)
adata_pca=adata_pca[valid_cells].copy()
Actually, I am not sure if hvg subset before normalization is a good idea.
and I see _pearson_residuals in _normalization.py and _calculate_res_sparse in _highly_variable_genes.py
intrinsically the same. in one way we might not need to write twice, in another, in the recipe we might need to calculate twice.
Activity
mcjmigdal commentedon Oct 25, 2023
I've run into the same issue. In my case
adata.layers["analytic_pearson_residuals"].sum(1)gives an array of nans because there are nans inanalytic_pearson["X"], as indicated by RuntimeWarning.I am still only investigating this, but if treating nans as 0 is OK there is numpy.nansum function that could be used instead of sum.
flying-sheep commentedon Nov 7, 2023
@LuckyMD any idea why that could be?
WeipengMO commentedon Nov 24, 2023
It seems that some genes did not express in all cells. I tried to used
sc.pp.filter_genes(adata, min_cells=1)to filter out those genes, and addressed this issue.brainfo commentedon Jan 27, 2026
in the recipe, after hvg, we should filter cells again, because with a subset of genes, the previous cell filter retaining cells might not have count:
scverse#2496
brainfo commentedon Jan 29, 2026
Actually, I am not sure if hvg subset before normalization is a good idea.
and I see
_pearson_residuals in _normalization.pyand_calculate_res_sparse in _highly_variable_genes.pyintrinsically the same. in one way we might not need to write twice, in another, in the recipe we might need to calculate twice.
MLubetzki commentedon Sep 10, 2026
Imo we should check whether variance is zero in a gene and if that is the case just set the normalized value to 0 far all cells.