Skip to content

Commit 973cac3

Browse files
committed
Fix windows errors around int/long
1 parent d7adfb7 commit 973cac3

File tree

2 files changed

+19
-14
lines changed

2 files changed

+19
-14
lines changed

sparse/core.py

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

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

88
import numpy as np
@@ -127,7 +127,7 @@ def __init__(self, coords, data=None, shape=None, has_duplicates=True):
127127
self.coords = self.coords[None, :]
128128

129129
if shape and not np.prod(self.coords.shape):
130-
self.coords = np.zeros((len(shape), 0), dtype=int)
130+
self.coords = np.zeros((len(shape), 0), dtype=np.uint64)
131131

132132
if shape is None:
133133
if self.coords.nbytes:
@@ -146,9 +146,13 @@ def __init__(self, coords, data=None, shape=None, has_duplicates=True):
146146

147147
@classmethod
148148
def from_numpy(cls, x):
149-
coords = np.where(x)
150-
data = x[coords]
151-
coords = np.vstack(coords)
149+
if x.shape:
150+
coords = np.where(x)
151+
data = x[coords]
152+
coords = np.vstack(coords)
153+
else:
154+
coords = []
155+
data = x
152156
return cls(coords, data, shape=x.shape)
153157

154158
def todense(self):
@@ -189,7 +193,7 @@ def __sizeof__(self):
189193
def __getitem__(self, index):
190194
if not isinstance(index, tuple):
191195
index = (index,)
192-
index = tuple(ind + self.shape[i] if isinstance(ind, int) and ind < 0 else ind
196+
index = tuple(ind + self.shape[i] if isinstance(ind, numbers.Integral) and ind < 0 else ind
193197
for i, ind in enumerate(index))
194198
mask = np.ones(self.nnz, dtype=bool)
195199
for i, ind in enumerate([i for i in index if i is not None]):
@@ -202,7 +206,7 @@ def __getitem__(self, index):
202206
shape = []
203207
i = 0
204208
for ind in index:
205-
if isinstance(ind, int):
209+
if isinstance(ind, numbers.Integral):
206210
i += 1
207211
continue
208212
elif isinstance(ind, slice):
@@ -247,7 +251,7 @@ def reduction(self, method, axis=None, keepdims=False, dtype=None):
247251
if dtype:
248252
kwargs['dtype'] = dtype
249253

250-
if isinstance(axis, int):
254+
if isinstance(axis, numbers.Integral):
251255
axis = (axis,)
252256

253257
if set(axis) == set(range(self.ndim)):
@@ -328,8 +332,8 @@ def reshape(self, shape):
328332
if self.shape == shape:
329333
return self
330334
if any(d == -1 for d in shape):
331-
extra = int(np.prod(self.shape) /
332-
np.prod([d for d in shape if d != -1]))
335+
extra = np.uint64(np.prod(self.shape) /
336+
np.prod([d for d in shape if d != -1]))
333337
shape = tuple([d if d != -1 else extra for d in shape])
334338
if self.shape == shape:
335339
return self
@@ -413,7 +417,7 @@ def sum_duplicates(self):
413417
return self
414418

415419
def __add__(self, other):
416-
if isinstance(other, Number) and other == 0:
420+
if isinstance(other, numbers.Number) and other == 0:
417421
return self
418422
if not isinstance(other, COO):
419423
return self.maybe_densify() + other
@@ -578,15 +582,15 @@ def astype(self, dtype, out=None):
578582
return self.elemwise(np.ndarray.astype, dtype, check=False)
579583

580584
def __gt__(self, other):
581-
if not isinstance(other, Number):
585+
if not isinstance(other, numbers.Number):
582586
raise NotImplementedError("Only scalars supported")
583587
if other < 0:
584588
raise ValueError("Comparison with negative number would produce "
585589
"dense result")
586590
return self.elemwise(operator.gt, other)
587591

588592
def __ge__(self, other):
589-
if not isinstance(other, Number):
593+
if not isinstance(other, numbers.Number):
590594
raise NotImplementedError("Only scalars supported")
591595
if other <= 0:
592596
raise ValueError("Comparison with negative number would produce "
@@ -702,7 +706,7 @@ def _keepdims(original, new, axis):
702706

703707

704708
def _mask(coords, idx):
705-
if isinstance(idx, int):
709+
if isinstance(idx, numbers.Integral):
706710
return coords == idx
707711
elif isinstance(idx, slice):
708712
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)