Skip to content

Commit

Permalink
update ValueError msgs and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dschult committed Dec 26, 2023
1 parent 120fa26 commit 1866fed
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
7 changes: 3 additions & 4 deletions scipy/sparse/_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,8 +237,7 @@ def _min_or_max(self, axis, out, min_or_max):

def _arg_min_or_max_axis(self, axis, argmin_or_argmax, compare):
if self.shape[axis] == 0:
raise ValueError("Can't apply the operation along a zero-sized "
"dimension.")
raise ValueError("Cannot apply the operation along a zero-sized dimension.")

if axis < 0:
axis += 2
Expand Down Expand Up @@ -287,7 +286,7 @@ def _arg_min_or_max(self, axis, out, argmin_or_argmax, compare):
return self._arg_min_or_max_axis(axis, argmin_or_argmax, compare)

if 0 in self.shape:
raise ValueError("Can't apply the operation to an empty matrix.")
raise ValueError("Cannot apply the operation to an empty matrix.")

if self.nnz == 0:
return 0
Expand All @@ -301,7 +300,7 @@ def _arg_min_or_max(self, axis, out, argmin_or_argmax, compare):
num_col = mat.shape[-1]

# If the min value is less than zero, or max is greater than zero,
# then we don't need to worry about implicit zeros.
# then we do not need to worry about implicit zeros.
if compare(extreme_value, zero):
# cast to Python int to avoid overflow and RuntimeError
return int(mat.row[extreme_index]) * num_col + int(mat.col[extreme_index])
Expand Down
12 changes: 8 additions & 4 deletions scipy/sparse/tests/test_minmax1d.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,10 @@ def test_minmax_axis(self, spcreator):
toarray(X.min(axis=axis)), D.min(axis=axis, keepdims=True)
)
for axis in [-2, 1]:
pytest.raises(ValueError, X.min, axis=axis)
pytest.raises(ValueError, X.max, axis=axis)
with pytest.raises(ValueError, match="axis out of range"):
X.min(axis=axis)
with pytest.raises(ValueError, match="axis out of range"):
X.max(axis=axis)


def test_numpy_minmax(self, spcreator):
Expand Down Expand Up @@ -74,5 +76,7 @@ def test_argmax(self, spcreator):

for axis in [None, 0]:
mat = spcreator(D6)
pytest.raises(ValueError, mat.argmax, axis=axis)
pytest.raises(ValueError, mat.argmin, axis=axis)
with pytest.raises(ValueError, match="to an empty matrix"):
mat.argmin(axis=axis)
with pytest.raises(ValueError, match="to an empty matrix"):
mat.argmax(axis=axis)

0 comments on commit 1866fed

Please sign in to comment.