Warnings are not filtered in csr_matrix.sum()
See original GitHub issueDear scipy community,
suffering for one hour on the application side, I realized that there is a noteworthy bug in scipy.
import scipy.sparse as sps
import warnings
counts = [1]
row_ids = [0]
column_ids = [1]
csr_matrix = sps.csr_matrix((counts, (row_ids, column_ids)),
shape=(1, 2),
dtype=int)
for i in range(5):
s = csr_matrix.sum()
warnings.warn("This message should be printed only once", UserWarning)
Warnings:
/home/ulianych/PycharmProjects/elephant/ignored/scipy_warn_bug.py:17: UserWarning: This message should be printed only once
warnings.warn("This message should be printed only once", UserWarning)
/home/ulianych/PycharmProjects/elephant/ignored/scipy_warn_bug.py:17: UserWarning: This message should be printed only once
warnings.warn("This message should be printed only once", UserWarning)
/home/ulianych/PycharmProjects/elephant/ignored/scipy_warn_bug.py:17: UserWarning: This message should be printed only once
warnings.warn("This message should be printed only once", UserWarning)
/home/ulianych/PycharmProjects/elephant/ignored/scipy_warn_bug.py:17: UserWarning: This message should be printed only once
warnings.warn("This message should be printed only once", UserWarning)
/home/ulianych/PycharmProjects/elephant/ignored/scipy_warn_bug.py:17: UserWarning: This message should be printed only once
warnings.warn("This message should be printed only once", UserWarning)
As the message says, the warning should be printed only once. Citing the official python documentation,
Repetitions of a particular warning for the same source location are typically suppressed.
Adding warnings.simplefilter("module", UserWarning)
or once
doesn’t help either.
Note that if you comment out the line where the sum of a matrix is computed, the warning is printed only once as it should.
Scipy/Numpy/Python version information
Master branch (pip install -e .
):
>>> import sys, scipy, numpy; print(scipy.__version__, numpy.__version__, sys.version_info)
1.4.1 1.18.1 sys.version_info(major=3, minor=7, micro=3, releaselevel='final', serial=0)
I also tried conda v1.5.0 - same behavior.
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (4 by maintainers)
Top Results From Across the Web
Source code for pegasus.tools.preprocessing
DataFrame: """Calculate filtration stats on cell barcodes. ... Among {data.shape[1]} genes, {data.var['robust'].sum()} genes are robust.").
Read more >scipy.sparse.csr_matrix.sum — SciPy v1.9.3 Manual
The default is to compute the sum of all the matrix elements, returning a scalar (i.e., axis = None). The type of the...
Read more >Python scipy.sparse.SparseEfficiencyWarning() Examples
This page shows Python examples of scipy.sparse.SparseEfficiencyWarning.
Read more >https://raw.githubusercontent.com/clara-parabricks...
Returns ------- filtered : scipy.sparse.csr_matrix of shape (n_cells, ... filtered """ thr = np.asarray(sparse_gpu_array.sum(axis=0) >= min_cells).ravel() ...
Read more >What's New — pandas 0.23.0 documentation - PyData |
Please note that the string index is not supported with the round trip format, ... We've added a min_count parameter to .sum() and...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Good to know, thank you! It has been opened for >3 years and does not seem to have any recent updates. Please consider not using this buggy warnings catching, because otherwise on the application side we need either to ignore all the warnings or be annoyed with flooded messages that the user erroneously thinks that he has control over them by filtering the warnings with
once
(he doesn’t). Expect more issues of this sort.I don’t have any further comments, thank you for the quick feedback! You can close the issue as won’t fix or leave it open if you think you may come up with a solution that avoids catching the warnings in the future.
@dizcza, I agree that, given the Python bug, we should avoid using
catch_warnings
. I’m not planning on closing the issue, but I don’t know if other devs have already looked into this and found that this is the best we can do at the moment.