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.

BUG: sparse.dok_matrix.fromkeys method totally nonfunctional

See original GitHub issue

Describe your issue.

The fromkeys method of sparse.dok_matrix returns an error. If it is called in the form scipy.sparse.dok_matrix.fromkeys(keys, values=1) it throws TypeError: dok_matrix.fromkeys() takes no keyword arguments. If called as scipy.sparse.dok_matrix.fromkeys(keys) or scipy.sparse.dok_matrix.fromkeys(keys, 1) it throws TypeError: __init__() missing 1 required positional argument: 'arg1'. The content of keys and values do not seem to matter.

The first error contradicts the documentation for the method, which seems to have been inherited from the docs for Python dicts. The two disagree on whether values is a positional or keyword argument.

Reproducing Code Example

import scipy
edges = [(0,1),(1,2),(2,0)]
scipy.sparse.dok_matrix.fromkeys(edges, values=1)
scipy.sparse.dok_matrix.fromkeys(edges, 1)
scipy.sparse.dok_matrix.fromkeys(edges)

Error message

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: dok_matrix.fromkeys() takes no keyword arguments

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: __init__() missing 1 required positional argument: 'arg1'

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: __init__() missing 1 required positional argument: 'arg1'

SciPy/NumPy/Python version information

1.7.2 1.21.4 sys.version_info(major=3, minor=9, micro=8, releaselevel=‘final’, serial=0)

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
perimosocordiaecommented, Nov 15, 2021

One way to address this issue might be to override the fromkeys classmethod and have it return a NotImplementedError that points users toward the above workaround.

0reactions
perimosocordiaecommented, Nov 17, 2021

Here’s an implementation that should work:

@classmethod
def fromkeys(cls, iterable, value=1):
    d = dict.fromkeys(iterable, value)
    num_rows = 1 + max(k[0] for k in d.keys())
    num_cols = 1 + max(k[1] for k in d.keys())
    result = cls.__init__((num_rows, num_cols))
    result._update(d)
    return result
Read more comments on GitHub >

github_iconTop Results From Across the Web

scipy.sparse.dok_matrix — SciPy v1.9.3 Manual
This is an efficient structure for constructing sparse matrices incrementally. This can be instantiated in several ways: dok_matrix(D). with a dense matrix, ...
Read more >
Scipy Ref | PDF | Integral | Mathematical Optimization - Scribd
3.15 Sparse linear algebra (scipy.sparse.linalg) . ... The functions and classes available in SciPy use this method for on-line documentation.
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