Skip to content

Commit ce26829

Browse files
authored
Merge branch 'master' into tocsr
2 parents d4801f4 + 973cac3 commit ce26829

File tree

2 files changed

+20
-16
lines changed

2 files changed

+20
-16
lines changed

sparse/core.py

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from collections import Iterable, deque, defaultdict
44
from functools import reduce
5-
from numbers import Number
5+
import numbers
66
import operator
77

88
import numpy as np
@@ -131,7 +131,7 @@ def __init__(self, coords, data=None, shape=None, has_duplicates=True,
131131
self.coords = self.coords[None, :]
132132

133133
if shape and not np.prod(self.coords.shape):
134-
self.coords = np.zeros((len(shape), 0), dtype=int)
134+
self.coords = np.zeros((len(shape), 0), dtype=np.uint64)
135135

136136
if shape is None:
137137
if self.coords.nbytes:
@@ -152,11 +152,14 @@ def __init__(self, coords, data=None, shape=None, has_duplicates=True,
152152

153153
@classmethod
154154
def from_numpy(cls, x):
155-
coords = np.where(x)
156-
data = x[coords]
157-
coords = np.vstack(coords)
158-
return cls(coords, data, shape=x.shape, has_duplicates=False,
159-
sorted=True)
155+
if x.shape:
156+
coords = np.where(x)
157+
data = x[coords]
158+
coords = np.vstack(coords)
159+
else:
160+
coords = []
161+
data = x
162+
return cls(coords, data, shape=x.shape, has_duplicates=False, sorted=True)
160163

161164
def todense(self):
162165
self = self.sum_duplicates()
@@ -198,7 +201,7 @@ def __sizeof__(self):
198201
def __getitem__(self, index):
199202
if not isinstance(index, tuple):
200203
index = (index,)
201-
index = tuple(ind + self.shape[i] if isinstance(ind, int) and ind < 0 else ind
204+
index = tuple(ind + self.shape[i] if isinstance(ind, numbers.Integral) and ind < 0 else ind
202205
for i, ind in enumerate(index))
203206
if (all(ind == slice(None) or ind == slice(0, d)
204207
for ind, d in zip(index, self.shape))):
@@ -214,7 +217,7 @@ def __getitem__(self, index):
214217
shape = []
215218
i = 0
216219
for ind in index:
217-
if isinstance(ind, int):
220+
if isinstance(ind, numbers.Integral):
218221
i += 1
219222
continue
220223
elif isinstance(ind, slice):
@@ -262,7 +265,7 @@ def reduction(self, method, axis=None, keepdims=False, dtype=None):
262265
if dtype:
263266
kwargs['dtype'] = dtype
264267

265-
if isinstance(axis, int):
268+
if isinstance(axis, numbers.Integral):
266269
axis = (axis,)
267270

268271
if set(axis) == set(range(self.ndim)):
@@ -361,8 +364,8 @@ def reshape(self, shape):
361364
if self.shape == shape:
362365
return self
363366
if any(d == -1 for d in shape):
364-
extra = int(np.prod(self.shape) /
365-
np.prod([d for d in shape if d != -1]))
367+
extra = np.uint64(np.prod(self.shape) /
368+
np.prod([d for d in shape if d != -1]))
366369
shape = tuple([d if d != -1 else extra for d in shape])
367370

368371
if self.shape == shape:
@@ -487,7 +490,7 @@ def sum_duplicates(self):
487490
return self
488491

489492
def __add__(self, other):
490-
if isinstance(other, Number) and other == 0:
493+
if isinstance(other, numbers.Number) and other == 0:
491494
return self
492495
if not isinstance(other, COO):
493496
return self.maybe_densify() + other
@@ -655,15 +658,15 @@ def astype(self, dtype, out=None):
655658
return self.elemwise(np.ndarray.astype, dtype, check=False)
656659

657660
def __gt__(self, other):
658-
if not isinstance(other, Number):
661+
if not isinstance(other, numbers.Number):
659662
raise NotImplementedError("Only scalars supported")
660663
if other < 0:
661664
raise ValueError("Comparison with negative number would produce "
662665
"dense result")
663666
return self.elemwise(operator.gt, other)
664667

665668
def __ge__(self, other):
666-
if not isinstance(other, Number):
669+
if not isinstance(other, numbers.Number):
667670
raise NotImplementedError("Only scalars supported")
668671
if other <= 0:
669672
raise ValueError("Comparison with negative number would produce "
@@ -780,7 +783,7 @@ def _keepdims(original, new, axis):
780783

781784

782785
def _mask(coords, idx):
783-
if isinstance(idx, int):
786+
if isinstance(idx, numbers.Integral):
784787
return coords == idx
785788
elif isinstance(idx, slice):
786789
if idx.step not in (1, None):

sparse/tests/test_core.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ def test_transpose(axis):
5555
[(2, 3, 4, 5), (8, 15)],
5656
[(2, 3, 4, 5), (24, 5)],
5757
[(2, 3, 4, 5), (20, 6)],
58+
[(), ()],
5859
])
5960
def test_reshape(a, b):
6061
x = random_x(a)

0 commit comments

Comments
 (0)