Skip to content

Loading…

BUG+TST: setdiag implementation for dia_matrix (Close #2931) + test_setdiag (was untested) #2932

Closed
wants to merge 6 commits into from

5 participants

@yarikoptic

Revealed similar lack of setdiag imlementation for COO and BSR matrices (TODO)

@yarikoptic yarikoptic BUG+TST: setdiag implementation for dia_matrix (Close #2931) + test_s…
…etdiag (was untested)

Revealed similar lack of setdiag imlementation for COO and BSR matrices (TODO)
e10d618
@pv
SciPy member
pv commented

Before this PR can be merged, either the BSR/COO setdiags need to be implemented, or the tests stubbed out to knownfailures (see the bottom of TestDOK class definition on how to do that)

@yarikoptic

here you go (marked as known failure)

@coveralls

Coverage Status

Coverage remained the same when pulling 856b7c1 on yarikoptic:master into c026262 on scipy:master.

@pv pv commented on an outdated diff
scipy/sparse/dia.py
@@ -181,6 +181,16 @@ def _mul_vector(self, other):
def _mul_multimatrix(self, other):
return np.hstack([self._mul_vector(col).reshape(-1,1) for col in other.T])
+ def setdiag(self, values, k=0):
+ M, N = self.shape
+ if (not k in self.offsets):
+ raise ValueError(
+ "dia matrix does not support assignment for unknown diagonals. "
@pv SciPy member
pv added a note

It might be nice to handle also this case, as it seems it could be done with a couple of lines of code.

pull requests are welcome!

@pv SciPy member
pv added a note

Sure, I'm not saying here that you should tackle it, but that it should be addressed before merging this.

and meanwhile scipy should stay instead with a completely broken version instead of a partially implemented with a consistent error message (may be actually NotImplemented instead of ValueError would be better)?

oh well

@pv SciPy member
pv added a note

Yes. The next release will be in next spring. It's better to do it right in the first round.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@pv pv commented on an outdated diff
scipy/sparse/tests/test_base.py
@@ -552,6 +552,19 @@ def test_diagonal(self):
for m in mats:
assert_equal(self.spmatrix(m).diagonal(),diag(m))
+ def test_setdiag(self):
+ #yoh: could not find a better way to deduce 'format' for the
+ #given matrix
+ format_ = self.spmatrix.__module__.split('.')[-1]
+ m = scipy.sparse.identity(3, format=format_)
+ values = [3, 2, 1]
+ # it is out of limits
+ assert_raises(ValueError, m.setdiag, values, k=4)
+ m.setdiag(values)
+ assert_array_equal(m.diagonal(), values)
+
+ # TODO: test setting offdiagonals (k!=0)
@pv SciPy member
pv added a note

And this should be tested, too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@perimosocordiae perimosocordiae referenced this pull request in yarikoptic/scipy
Merged

enable setdiag for non pre-existing offsets #2

@perimosocordiae
SciPy member

Bump. I've fixed the outstanding TODOs for this PR here, but @yarikoptic seems to be busy with other things. I can roll all the changes into a new PR on this repo, if that would help.

@yarikoptic yarikoptic Merge pull request #2 from perimosocordiae/patch-3
enable setdiag for non pre-existing offsets
f0ebf74
@yarikoptic

Thank you @perimosocordiae and sorry -- I have missed your PR in time. Merged/updated this PR now

@yarikoptic

Anyone from scipy team could possibly have a look and direct us further to get this PR finally accepted?

@rgommers
SciPy member

@pv your comments seem to have been addressed. Do you have time to review this, or should I have a closer look?

@pv
SciPy member
pv commented

Thanks, rebased and merged.
Also fixed a remaining bug with unintended self.offsets dtype promotion (which would cause e.g. matrix multiplication to fail later on).

@pv pv closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Sep 25, 2013
  1. @yarikoptic

    BUG+TST: setdiag implementation for dia_matrix (Close #2931) + test_s…

    yarikoptic committed
    …etdiag (was untested)
    
    Revealed similar lack of setdiag imlementation for COO and BSR matrices (TODO)
Commits on Sep 26, 2013
  1. @yarikoptic
Commits on Nov 18, 2013
  1. @perimosocordiae
  2. @perimosocordiae
  3. @perimosocordiae

    Workaround format hack.

    perimosocordiae committed
Commits on Dec 2, 2013
  1. @yarikoptic

    Merge pull request #2 from perimosocordiae/patch-3

    yarikoptic committed
    enable setdiag for non pre-existing offsets
This page is out of date. Refresh to see the latest.
Showing with 35 additions and 0 deletions.
  1. +13 −0 scipy/sparse/dia.py
  2. +22 −0 scipy/sparse/tests/test_base.py
View
13 scipy/sparse/dia.py
@@ -181,6 +181,19 @@ def _mul_vector(self, other):
def _mul_multimatrix(self, other):
return np.hstack([self._mul_vector(col).reshape(-1,1) for col in other.T])
+ def setdiag(self, values, k=0):
+ M, N = self.shape
+ if k <= -M or k >= N:
+ raise ValueError('k exceeds matrix dimensions')
+ if k in self.offsets:
+ self.data[self.offsets == k, :] = values
+ else:
+ self.offsets = np.append(self.offsets, (k,))
+ self.data = np.vstack((self.data, np.empty((1,N))))
+ self.data[-1, :] = values
+
+ setdiag.__doc__ = _data_matrix.setdiag.__doc__
+
def todia(self,copy=False):
if copy:
return self.copy()
View
22 scipy/sparse/tests/test_base.py
@@ -552,6 +552,20 @@ def test_diagonal(self):
for m in mats:
assert_equal(self.spmatrix(m).diagonal(),diag(m))
+ def test_setdiag(self):
+ m = self.spmatrix(np.eye(3))
+ values = [3, 2, 1]
+ # it is out of limits
+ assert_raises(ValueError, m.setdiag, values, k=4)
+ m.setdiag(values)
+ assert_array_equal(m.diagonal(), values)
+
+ # test setting offdiagonals (k!=0)
+ m.setdiag((9,), k=2)
+ assert_array_equal(m.A[0,2], 9)
+ m.setdiag((9,), k=-2)
+ assert_array_equal(m.A[2,0], 9)
+
def test_nonzero(self):
A = array([[1, 0, 1],[0, 1, 1],[0, 0, 1]])
Asp = self.spmatrix(A)
@@ -3007,6 +3021,10 @@ def test_todia_all_zeros(self):
dia = coo_matrix(zeros).todia()
assert_array_equal(dia.A, zeros)
+ @dec.knownfailureif(True, "known deficiency in COO")
+ def test_setdiag(self):
+ pass
+
class TestDIA(sparse_test_class(getset=False, slicing=False, slicing_assign=False,
fancy_indexing=False, fancy_assign=False,
@@ -3106,5 +3124,9 @@ def test_bsr_matvecs(self):
def test_iterator(self):
pass
+ @dec.knownfailureif(True, "known deficiency in BSR")
+ def test_setdiag(self):
+ pass
+
if __name__ == "__main__":
run_module_suite()
Something went wrong with that request. Please try again.