question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Warnings are not filtered in csr_matrix.sum()

See original GitHub issue

Dear 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:closed
  • Created 3 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
dizczacommented, Jun 30, 2020

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.

0reactions
WarrenWeckessercommented, Jun 30, 2020

@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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found