2
2
3
3
from collections import Iterable
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
@@ -127,7 +127,7 @@ def __init__(self, coords, data=None, shape=None, has_duplicates=True):
127
127
self .coords = self .coords [None , :]
128
128
129
129
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 )
131
131
132
132
if shape is None :
133
133
if self .coords .nbytes :
@@ -146,9 +146,13 @@ def __init__(self, coords, data=None, shape=None, has_duplicates=True):
146
146
147
147
@classmethod
148
148
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
152
156
return cls (coords , data , shape = x .shape )
153
157
154
158
def todense (self ):
@@ -189,7 +193,7 @@ def __sizeof__(self):
189
193
def __getitem__ (self , index ):
190
194
if not isinstance (index , tuple ):
191
195
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
193
197
for i , ind in enumerate (index ))
194
198
mask = np .ones (self .nnz , dtype = bool )
195
199
for i , ind in enumerate ([i for i in index if i is not None ]):
@@ -202,7 +206,7 @@ def __getitem__(self, index):
202
206
shape = []
203
207
i = 0
204
208
for ind in index :
205
- if isinstance (ind , int ):
209
+ if isinstance (ind , numbers . Integral ):
206
210
i += 1
207
211
continue
208
212
elif isinstance (ind , slice ):
@@ -247,7 +251,7 @@ def reduction(self, method, axis=None, keepdims=False, dtype=None):
247
251
if dtype :
248
252
kwargs ['dtype' ] = dtype
249
253
250
- if isinstance (axis , int ):
254
+ if isinstance (axis , numbers . Integral ):
251
255
axis = (axis ,)
252
256
253
257
if set (axis ) == set (range (self .ndim )):
@@ -328,8 +332,8 @@ def reshape(self, shape):
328
332
if self .shape == shape :
329
333
return self
330
334
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 ]))
333
337
shape = tuple ([d if d != - 1 else extra for d in shape ])
334
338
if self .shape == shape :
335
339
return self
@@ -413,7 +417,7 @@ def sum_duplicates(self):
413
417
return self
414
418
415
419
def __add__ (self , other ):
416
- if isinstance (other , Number ) and other == 0 :
420
+ if isinstance (other , numbers . Number ) and other == 0 :
417
421
return self
418
422
if not isinstance (other , COO ):
419
423
return self .maybe_densify () + other
@@ -578,15 +582,15 @@ def astype(self, dtype, out=None):
578
582
return self .elemwise (np .ndarray .astype , dtype , check = False )
579
583
580
584
def __gt__ (self , other ):
581
- if not isinstance (other , Number ):
585
+ if not isinstance (other , numbers . Number ):
582
586
raise NotImplementedError ("Only scalars supported" )
583
587
if other < 0 :
584
588
raise ValueError ("Comparison with negative number would produce "
585
589
"dense result" )
586
590
return self .elemwise (operator .gt , other )
587
591
588
592
def __ge__ (self , other ):
589
- if not isinstance (other , Number ):
593
+ if not isinstance (other , numbers . Number ):
590
594
raise NotImplementedError ("Only scalars supported" )
591
595
if other <= 0 :
592
596
raise ValueError ("Comparison with negative number would produce "
@@ -702,7 +706,7 @@ def _keepdims(original, new, axis):
702
706
703
707
704
708
def _mask (coords , idx ):
705
- if isinstance (idx , int ):
709
+ if isinstance (idx , numbers . Integral ):
706
710
return coords == idx
707
711
elif isinstance (idx , slice ):
708
712
if idx .step not in (1 , None ):
0 commit comments