# "ValueError: cannot specify integer \`bins\` when input data contains infinity" in multi sample data

**URL:** <https://discourse.scverse.org/t/valueerror-cannot-specify-integer-bins-when-input-data-contains-infinity-in-multi-sample-data/1229>\
**Category:** scanpy\
**Tags:** integration, multivi\
**Created:** [March 15, 2023, 8:37am UTC](https://discourse.scverse.org/t/valueerror-cannot-specify-integer-bins-when-input-data-contains-infinity-in-multi-sample-data/1229 "2023-03-15T08:37:42Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Behzad](https://avatars.discourse-cdn.com/v4/letter/b/c4cdca/32.png) [@Behzad](https://discourse.scverse.org/u/Behzad)\
**Post date:** [March 15, 2023, 8:37am UTC](https://discourse.scverse.org/t/valueerror-cannot-specify-integer-bins-when-input-data-contains-infinity-in-multi-sample-data/1229/1 "2023-03-15T08:37:42Z")

</div>

I have few samples and merged them all (so the adata has 6 samples in it) and followed the scanpy tutorial without any problem until I reached to the point where I had to extract highly variable genes using this command:

**sc.pp.highly\_variable\_genes(adata, min\_mean=0.0125, max\_mean=3, min\_disp=0.5)**

but keep getting this error:

**extracting highly variable genes**  
/home/.conda/envs/single\_cell\_scanpy/lib/python3.10/site-packages/scanpy/preprocessing/\_highly\_variable\_genes.py:200: RuntimeWarning: overflow encountered in expm1  
\*\* X = np.expm1(X)\*\*  
**/home/.conda/envs/single\_cell\_scanpy/lib/python3.10/site-packages/scanpy/preprocessing/\_utils.py:11: RuntimeWarning: overflow encountered in multiply**  
\*\* mean\_sq = np.multiply(X, X).mean(axis=axis, dtype=np.float64)\*\*  
**/home/.conda/envs/single\_cell\_scanpy/lib/python3.10/site-packages/scanpy/preprocessing/\_utils.py:12: RuntimeWarning: invalid value encountered in subtract**  
\*\* var = mean\_sq - mean **2**  
**Traceback (most recent call last):**  
\*\* File “”, line 1, in \*\*  
\*\* File “/home/.conda/envs/single\_cell\_scanpy/lib/python3.10/site-packages/scanpy/preprocessing/\_highly\_variable\_genes.py”, line 440, in highly\_variable\_genes\*\*  
\*\* df = \_highly\_variable\_genes\_single\_batch(\*\*  
\*\* File “/home/.conda/envs/single\_cell\_scanpy/lib/python3.10/site-packages/scanpy/preprocessing/\_highly\_variable\_genes.py”, line 215, in \_highly\_variable\_genes\_single\_batch\*\*  
\*\* df[‘mean\_bin’] = pd.cut(df[‘means’], bins=n\_bins)\*\*  
\*\* File “/home/.conda/envs/single\_cell\_scanpy/lib/python3.10/site-packages/pandas/core/reshape/tile.py”, line 263, in cut\*\*  
\*\* raise ValueError(\*\*  
**ValueError: cannot specify integer `bins` when input data contains infinity**

I looked for the possible solutions but have not found yet. Would you please let me know how I can fix the error?

---

<div class="post-metadata">

**Author:** ![yotamcons](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.scverse.org/yotamcons/32/300_2.png) [@yotamcons](https://discourse.scverse.org/u/yotamcons)\
**Post date:** [March 16, 2023, 8:52am UTC](https://discourse.scverse.org/t/valueerror-cannot-specify-integer-bins-when-input-data-contains-infinity-in-multi-sample-data/1229/2 "2023-03-16T08:52:16Z")

</div>

Hello Behzad,  
The warning you are getting (before the value error) indicate that you have some very very large numbers in your dataset (which causes an overflow to the `np.expm1` function.

[The scanpy documentation](https://scanpy.readthedocs.io/en/stable/generated/scanpy.pp.highly_variable_genes.html) for `sc.pp.highly_variable_genes` states that the function _“Expects **logarithmized data** , except when `flavor='seurat_v3'` , in which count data is expected.”_

My suggestion is that you first verify that you have first ran standard normalizations (`scanpy.pp.normalize_total` and `scanpy.pp.log1p`) and try re-running the code and see if the error repeats.  
If it does, I suggest checking the maximal value of your `adata.X`, and make sure no infinity `np.infty` or humongous values appear there.

Good luck!

---

<div class="post-metadata">

**Author:** ![Behzad](https://avatars.discourse-cdn.com/v4/letter/b/c4cdca/32.png) [@Behzad](https://discourse.scverse.org/u/Behzad)\
**Post date:** [March 16, 2023, 5:16pm UTC](https://discourse.scverse.org/t/valueerror-cannot-specify-integer-bins-when-input-data-contains-infinity-in-multi-sample-data/1229/3 "2023-03-16T17:16:55Z")

</div>

Hello Yotam,

1- I tried `scanpy.pp.normalize_total` and `scanpy.pp.log1p` but got the same error. I tried `normalize_per_cell` as well.  
2- I also checked the max value of your `adata.X`, which is “175.02573” and min value is “-9.317178”. but I do not know how could be used to find a solution for this issue.  
3- I tried `np.infty` however constantly got this error:

> TypeError: ‘float’ object is not callable

but I used to\_list function which made a matrix (list (len is 32100) of lists (len is 24062) in python ). I looked for infinity values there and did not find any.

So, I still have the problem. as more info, I have 6 samples (3 controls and 3 conditions) and concatenated them into one variable (adata).

---

<div class="post-metadata">

**Author:** ![Behzad](https://avatars.discourse-cdn.com/v4/letter/b/c4cdca/32.png) [@Behzad](https://discourse.scverse.org/u/Behzad)\
**Post date:** [March 17, 2023, 10:46am UTC](https://discourse.scverse.org/t/valueerror-cannot-specify-integer-bins-when-input-data-contains-infinity-in-multi-sample-data/1229/4 "2023-03-17T10:46:15Z")

</div>

I tried the same code on every single sample separately and worked perfectly but when I combine the samples into adata (concatenate them), even if I use 2 samples, I will get the same error.  
the only difference that I will see in adata, is that adata.obs will have an extra item which is batch, compare to when I do single sample analysis.

---

<div class="post-metadata">

**Author:** ![yotamcons](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.scverse.org/yotamcons/32/300_2.png) [@yotamcons](https://discourse.scverse.org/u/yotamcons)\
**Post date:** [March 30, 2023, 10:37am UTC](https://discourse.scverse.org/t/valueerror-cannot-specify-integer-bins-when-input-data-contains-infinity-in-multi-sample-data/1229/5 "2023-03-30T10:37:45Z")

</div>

Very strange.  
The fact that you have a minimal value below 0 in your data means you are not working with counts data, but that you’ve performed some normalization.  
I suggest you retry things with the raw data counts, and make sure to concatenate the datasets _before_ you perform any normalizations.

---

<div class="post-metadata">

**Author:** ![Behzad](https://avatars.discourse-cdn.com/v4/letter/b/c4cdca/32.png) [@Behzad](https://discourse.scverse.org/u/Behzad)\
**Post date:** [April 5, 2023, 10:11pm UTC](https://discourse.scverse.org/t/valueerror-cannot-specify-integer-bins-when-input-data-contains-infinity-in-multi-sample-data/1229/6 "2023-04-05T22:11:02Z")

</div>

it is solved. I just wrote the adata to a file and read it. everything worked as it should.

---

<div class="post-metadata">

**Author:** ![GangLi](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.scverse.org/gangli/32/725_2.png) [@GangLi](https://discourse.scverse.org/u/GangLi)\
**Post date:** [June 27, 2023, 10:36pm UTC](https://discourse.scverse.org/t/valueerror-cannot-specify-integer-bins-when-input-data-contains-infinity-in-multi-sample-data/1229/7 "2023-06-27T22:36:44Z")

</div>

Hi,

I also met the same problem. I read the concatenated adata and read it but sill got the error.
