7
7
import warnings
8
8
from abc import ABCMeta , abstractmethod
9
9
from collections import namedtuple
10
+ from collections .abc import Sequence
10
11
11
12
import awkward
12
13
import cupy
13
14
import numpy as np
14
15
15
- # Python 2 and 3 compatibility
16
- _regex_pattern = re .compile ("dummy" ).__class__
17
- try :
18
- basestring # noqa: B018
19
- except NameError :
20
- basestring = str
21
-
22
- try :
23
- from collections .abc import Sequence
24
- except ImportError :
25
- from collections .abc import Sequence
26
-
27
16
MaybeSumSlice = namedtuple ("MaybeSumSlice" , ["start" , "stop" , "sum" ])
28
17
29
18
_replace_nans = cupy .ElementwiseKernel ("T v" , "T x" , "x = isnan(x)?v:x" , "replace_nans" )
@@ -115,9 +104,7 @@ def __str__(self):
115
104
return self ._label
116
105
if self .nan ():
117
106
return "(nanflow)"
118
- # string representation of floats is apparently a touchy subject.. further reading:
119
- # https://stackoverflow.com/questions/25898733/why-does-strfloat-return-more-digits-in-python-3-than-python-2
120
- return "{}{:.12g}, {:.12g})" .format (
107
+ return "{}{}, {})" .format (
121
108
"(" if self ._lo == - np .inf else "[" ,
122
109
self ._lo ,
123
110
self ._hi ,
@@ -193,7 +180,7 @@ class StringBin:
193
180
"""
194
181
195
182
def __init__ (self , name , label = None ):
196
- if not isinstance (name , basestring ):
183
+ if not isinstance (name , str ):
197
184
raise TypeError (
198
185
f"StringBin only supports string categories, received a { name !r} "
199
186
)
@@ -273,7 +260,7 @@ def __eq__(self, other):
273
260
return False
274
261
# label doesn't matter
275
262
return True
276
- elif isinstance (other , basestring ):
263
+ elif isinstance (other , str ):
277
264
# Convenient for testing axis in list by name
278
265
return not self ._name != other
279
266
raise TypeError (f"Cannot compare an Axis with a { other !r} " )
@@ -364,9 +351,9 @@ def _ireduce(self, the_slice):
364
351
out = None
365
352
if isinstance (the_slice , StringBin ):
366
353
out = [the_slice .name ]
367
- elif isinstance (the_slice , _regex_pattern ):
354
+ elif isinstance (the_slice , re . Pattern ):
368
355
out = [k for k in self ._sorted if the_slice .match (k )]
369
- elif isinstance (the_slice , basestring ):
356
+ elif isinstance (the_slice , str ):
370
357
pattern = "^" + re .escape (the_slice ).replace (r"\*" , ".*" ) + "$"
371
358
m = re .compile (pattern )
372
359
out = [k for k in self ._sorted if m .match (k )]
@@ -380,11 +367,11 @@ def _ireduce(self, the_slice):
380
367
if the_slice .step is not None :
381
368
raise IndexError ("Not sure how to use slice step for categories..." )
382
369
start , stop = 0 , len (self ._sorted )
383
- if isinstance (the_slice .start , basestring ):
370
+ if isinstance (the_slice .start , str ):
384
371
start = self ._sorted .index (the_slice .start )
385
372
else :
386
373
start = the_slice .start
387
- if isinstance (the_slice .stop , basestring ):
374
+ if isinstance (the_slice .stop , str ):
388
375
stop = self ._sorted .index (the_slice .stop )
389
376
else :
390
377
stop = the_slice .stop
@@ -775,7 +762,7 @@ class AccumulatorABC(metaclass=ABCMeta):
775
762
After defining an accumulator::
776
763
777
764
from coffea.processor import dict_accumulator, column_accumulator, defaultdict_accumulator
778
- from coffea.hist import Hist, Bin
765
+ from cuda_histogram import Hist, Bin
779
766
import numpy as np
780
767
781
768
adef = dict_accumulator({
@@ -861,26 +848,28 @@ class Hist(AccumulatorABC):
861
848
862
849
Creating a histogram with a sparse axis, and two dense axes::
863
850
864
- h = coffea.hist.Hist("Observed bird count",
865
- coffea.hist.Cat("species", "Bird species"),
866
- coffea.hist.Bin("x", "x coordinate [m]", 20, -5, 5),
867
- coffea.hist.Bin("y", "y coordinate [m]", 20, -5, 5),
851
+ import cuda_histogram as chist
852
+
853
+ h = chist.Hist("Observed bird count",
854
+ chist.Cat("species", "Bird species"),
855
+ chist.Bin("x", "x coordinate [m]", 20, -5, 5),
856
+ chist.Bin("y", "y coordinate [m]", 20, -5, 5),
868
857
)
869
858
870
859
# or
871
860
872
- h = coffea.hist .Hist(label="Observed bird count",
873
- axes=(coffea.hist .Cat("species", "Bird species"),
874
- coffea.hist .Bin("x", "x coordinate [m]", 20, -5, 5),
875
- coffea.hist .Bin("y", "y coordinate [m]", 20, -5, 5),
861
+ h = chist .Hist(label="Observed bird count",
862
+ axes=(chist .Cat("species", "Bird species"),
863
+ chist .Bin("x", "x coordinate [m]", 20, -5, 5),
864
+ chist .Bin("y", "y coordinate [m]", 20, -5, 5),
876
865
)
877
866
)
878
867
879
868
# or
880
869
881
- h = coffea.hist. Hist(axes=[coffea.hist .Cat("species", "Bird species"),
882
- coffea.hist .Bin("x", "x coordinate [m]", 20, -5, 5),
883
- coffea.hist .Bin("y", "y coordinate [m]", 20, -5, 5),
870
+ h = chist. Hist(axes=[chist .Cat("species", "Bird species"),
871
+ chist .Bin("x", "x coordinate [m]", 20, -5, 5),
872
+ chist .Bin("y", "y coordinate [m]", 20, -5, 5),
884
873
],
885
874
label="Observed bird count",
886
875
)
@@ -896,12 +885,10 @@ class Hist(AccumulatorABC):
896
885
DEFAULT_DTYPE = "d"
897
886
898
887
def __init__ (self , label , * axes , ** kwargs ):
899
- if not isinstance (label , basestring ):
888
+ if not isinstance (label , str ):
900
889
raise TypeError ("label must be a string" )
901
890
self ._label = label
902
- self ._dtype = kwargs .pop (
903
- "dtype" , Hist .DEFAULT_DTYPE
904
- ) # Much nicer in python3 :(
891
+ self ._dtype = kwargs .get ("dtype" , Hist .DEFAULT_DTYPE )
905
892
self ._axes = axes
906
893
if len (axes ) == 0 and "axes" in kwargs :
907
894
if not isinstance (kwargs ["axes" ], Sequence ):
@@ -1612,7 +1599,7 @@ def expandkey(key):
1612
1599
return out
1613
1600
1614
1601
def to_hist (self ):
1615
- """Convert this coffea.hist histogram to a hist object"""
1602
+ """Convert the cuda_histogram histogram to a hist object"""
1616
1603
import hist
1617
1604
1618
1605
return hist .Hist (self .to_boost ())
0 commit comments