2
2
3
3
from collections import Iterable , deque , defaultdict
4
4
from functools import reduce
5
- from numbers import Number
5
+ import numbers
6
6
import operator
7
7
8
8
import numpy as np
@@ -131,7 +131,7 @@ def __init__(self, coords, data=None, shape=None, has_duplicates=True,
131
131
self .coords = self .coords [None , :]
132
132
133
133
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 )
135
135
136
136
if shape is None :
137
137
if self .coords .nbytes :
@@ -152,11 +152,14 @@ def __init__(self, coords, data=None, shape=None, has_duplicates=True,
152
152
153
153
@classmethod
154
154
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 )
160
163
161
164
def todense (self ):
162
165
self = self .sum_duplicates ()
@@ -198,7 +201,7 @@ def __sizeof__(self):
198
201
def __getitem__ (self , index ):
199
202
if not isinstance (index , tuple ):
200
203
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
202
205
for i , ind in enumerate (index ))
203
206
if (all (ind == slice (None ) or ind == slice (0 , d )
204
207
for ind , d in zip (index , self .shape ))):
@@ -214,7 +217,7 @@ def __getitem__(self, index):
214
217
shape = []
215
218
i = 0
216
219
for ind in index :
217
- if isinstance (ind , int ):
220
+ if isinstance (ind , numbers . Integral ):
218
221
i += 1
219
222
continue
220
223
elif isinstance (ind , slice ):
@@ -262,7 +265,7 @@ def reduction(self, method, axis=None, keepdims=False, dtype=None):
262
265
if dtype :
263
266
kwargs ['dtype' ] = dtype
264
267
265
- if isinstance (axis , int ):
268
+ if isinstance (axis , numbers . Integral ):
266
269
axis = (axis ,)
267
270
268
271
if set (axis ) == set (range (self .ndim )):
@@ -361,8 +364,8 @@ def reshape(self, shape):
361
364
if self .shape == shape :
362
365
return self
363
366
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 ]))
366
369
shape = tuple ([d if d != - 1 else extra for d in shape ])
367
370
368
371
if self .shape == shape :
@@ -487,7 +490,7 @@ def sum_duplicates(self):
487
490
return self
488
491
489
492
def __add__ (self , other ):
490
- if isinstance (other , Number ) and other == 0 :
493
+ if isinstance (other , numbers . Number ) and other == 0 :
491
494
return self
492
495
if not isinstance (other , COO ):
493
496
return self .maybe_densify () + other
@@ -655,15 +658,15 @@ def astype(self, dtype, out=None):
655
658
return self .elemwise (np .ndarray .astype , dtype , check = False )
656
659
657
660
def __gt__ (self , other ):
658
- if not isinstance (other , Number ):
661
+ if not isinstance (other , numbers . Number ):
659
662
raise NotImplementedError ("Only scalars supported" )
660
663
if other < 0 :
661
664
raise ValueError ("Comparison with negative number would produce "
662
665
"dense result" )
663
666
return self .elemwise (operator .gt , other )
664
667
665
668
def __ge__ (self , other ):
666
- if not isinstance (other , Number ):
669
+ if not isinstance (other , numbers . Number ):
667
670
raise NotImplementedError ("Only scalars supported" )
668
671
if other <= 0 :
669
672
raise ValueError ("Comparison with negative number would produce "
@@ -780,7 +783,7 @@ def _keepdims(original, new, axis):
780
783
781
784
782
785
def _mask (coords , idx ):
783
- if isinstance (idx , int ):
786
+ if isinstance (idx , numbers . Integral ):
784
787
return coords == idx
785
788
elif isinstance (idx , slice ):
786
789
if idx .step not in (1 , None ):
0 commit comments