-
Notifications
You must be signed in to change notification settings - Fork 89
/
content.py
1389 lines (1197 loc) · 47.2 KB
/
content.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# BSD 3-Clause License; see https://github.com/scikit-hep/awkward-1.0/blob/main/LICENSE
import copy
from collections.abc import Iterable
import awkward as ak
import awkward._v2._reducers
from awkward._v2.tmp_for_testing import v1_to_v2
np = ak.nplike.NumpyMetadata.instance()
numpy = ak.nplike.Numpy.instance()
class Content:
is_NumpyType = False
is_UnknownType = False
is_ListType = False
is_RegularType = False
is_OptionType = False
is_IndexedType = False
is_RecordType = False
is_UnionType = False
def _init(self, identifier, parameters, nplike):
if identifier is not None and not isinstance(
identifier, ak._v2.identifier.Identifier
):
raise ak._v2._util.error(
TypeError(
"{} 'identifier' must be an Identifier or None, not {}".format(
type(self).__name__, repr(identifier)
)
)
)
if parameters is not None and not isinstance(parameters, dict):
raise ak._v2._util.error(
TypeError(
"{} 'parameters' must be a dict or None, not {}".format(
type(self).__name__, repr(parameters)
)
)
)
if nplike is not None and not isinstance(nplike, ak.nplike.NumpyLike):
raise ak._v2._util.error(
TypeError(
"{} 'nplike' must be an ak.nplike.NumpyLike or None, not {}".format(
type(self).__name__, repr(nplike)
)
)
)
self._identifier = identifier
self._parameters = parameters
self._nplike = nplike
@property
def identifier(self):
return self._identifier
@property
def parameters(self):
if self._parameters is None:
self._parameters = {}
return self._parameters
def parameter(self, key):
if self._parameters is None:
return None
else:
return self._parameters.get(key)
@property
def nplike(self):
return self._nplike
@property
def form(self):
return self.form_with_key(None)
def form_with_key(self, form_key="node{id}", id_start=0):
hold_id = [id_start]
if form_key is None:
def getkey(layout):
return None
elif ak._v2._util.isstr(form_key):
def getkey(layout):
out = form_key.format(id=hold_id[0])
hold_id[0] += 1
return out
elif callable(form_key):
def getkey(layout):
out = form_key(id=hold_id[0], layout=layout)
hold_id[0] += 1
return out
else:
raise ak._v2._util.error(
TypeError(
"form_key must be None, a string, or a callable, not {}".format(
type(form_key)
)
)
)
return self._form_with_key(getkey)
def to_buffers(
self,
container=None,
buffer_key="{form_key}-{attribute}",
form_key="node{id}",
id_start=0,
nplike=None,
):
if container is None:
container = {}
if nplike is None:
nplike = self._nplike
if not nplike.known_data:
raise ak._v2._util.error(
TypeError("cannot call 'to_buffers' on an array without concrete data")
)
if ak._v2._util.isstr(buffer_key):
def getkey(layout, form, attribute):
return buffer_key.format(form_key=form.form_key, attribute=attribute)
elif callable(buffer_key):
def getkey(layout, form, attribute):
return buffer_key(
form_key=form.form_key,
attribute=attribute,
layout=layout,
form=form,
)
else:
raise ak._v2._util.error(
TypeError(
"buffer_key must be a string or a callable, not {}".format(
type(buffer_key)
)
)
)
if form_key is None:
raise ak._v2._util.error(
TypeError(
"a 'form_key' must be supplied, to match Form elements to buffers in the 'container'"
)
)
form = self.form_with_key(form_key=form_key, id_start=id_start)
self._to_buffers(form, getkey, container, nplike)
return form, len(self), container
def __len__(self):
return self.length
def _repr_extra(self, indent):
out = []
if self._parameters is not None:
for k, v in self._parameters.items():
out.append(
"\n{}<parameter name={}>{}</parameter>".format(
indent, repr(k), repr(v)
)
)
if self._identifier is not None:
out.append(self._identifier._repr("\n" + indent, "", ""))
return out
def maybe_to_array(self, nplike):
return None
def _handle_error(self, error, slicer=None):
if error.str is not None:
if error.filename is None:
filename = ""
else:
filename = " (in compiled code: " + error.filename.decode(
errors="surrogateescape"
).lstrip("\n").lstrip("(")
message = error.str.decode(errors="surrogateescape")
if error.pass_through:
raise ak._v2._util.error(ValueError(message + filename))
else:
if error.id != ak._util.kSliceNone and self._identifier is not None:
# FIXME https://github.com/scikit-hep/awkward-1.0/blob/45d59ef4ae45eebb02995b8e1acaac0d46fb9573/src/libawkward/util.cpp#L443-L450
pass
if error.attempt != ak._util.kSliceNone:
message += f" while attempting to get index {error.attempt}"
message += filename
if slicer is None:
raise ak._v2._util.error(ValueError(message))
else:
raise ak._v2._util.indexerror(self, slicer, message)
@staticmethod
def _selfless_handle_error(error):
if error.str is not None:
if error.filename is None:
filename = ""
else:
filename = " (in compiled code: " + error.filename.decode(
errors="surrogateescape"
).lstrip("\n").lstrip("(")
message = error.str.decode(errors="surrogateescape")
if error.pass_through:
raise ak._v2._util.error(ValueError(message + filename))
else:
if error.attempt != ak._util.kSliceNone:
message += f" while attempting to get index {error.attempt}"
message += filename
raise ak._v2._util.error(ValueError(message))
def __array_ufunc__(self, ufunc, method, *inputs, **kwargs):
raise ak._v2._util.error(
TypeError(
"do not apply NumPy functions to low-level layouts (Content subclasses); put them in ak._v2.highlevel.Array"
)
)
def __array_function__(self, func, types, args, kwargs):
raise ak._v2._util.error(
TypeError(
"do not apply NumPy functions to low-level layouts (Content subclasses); put them in ak._v2.highlevel.Array"
)
)
def __array__(self, **kwargs):
raise ak._v2._util.error(
TypeError(
"do not try to convert low-level layouts (Content subclasses) into NumPy arrays; put them in ak._v2.highlevel.Array"
)
)
def __iter__(self):
if not self._nplike.known_data:
raise ak._v2._util.error(
TypeError("cannot iterate on an array without concrete data")
)
for i in range(len(self)):
yield self._getitem_at(i)
def _getitem_next_field(self, head, tail, advanced):
nexthead, nexttail = ak._v2._slicing.headtail(tail)
return self._getitem_field(head)._getitem_next(nexthead, nexttail, advanced)
def _getitem_next_fields(self, head, tail, advanced):
only_fields, not_fields = [], []
for x in tail:
if ak._util.isstr(x) or isinstance(x, list):
only_fields.append(x)
else:
not_fields.append(x)
nexthead, nexttail = ak._v2._slicing.headtail(tuple(not_fields))
return self._getitem_fields(head, tuple(only_fields))._getitem_next(
nexthead, nexttail, advanced
)
def _getitem_next_newaxis(self, tail, advanced):
nexthead, nexttail = ak._v2._slicing.headtail(tail)
return ak._v2.contents.RegularArray(
self._getitem_next(nexthead, nexttail, advanced),
1, # size
0, # zeros_length is irrelevant when the size is 1 (!= 0)
None,
None,
self._nplike,
)
def _getitem_next_ellipsis(self, tail, advanced):
mindepth, maxdepth = self.minmax_depth
dimlength = sum(
1 if isinstance(x, (int, slice, ak._v2.index.Index64)) else 0 for x in tail
)
if len(tail) == 0 or mindepth - 1 == maxdepth - 1 == dimlength:
nexthead, nexttail = ak._v2._slicing.headtail(tail)
return self._getitem_next(nexthead, nexttail, advanced)
elif dimlength in {mindepth - 1, maxdepth - 1}:
raise ak._v2._util.indexerror(
self,
Ellipsis,
"ellipsis (`...`) can't be used on data with different numbers of dimensions",
)
else:
return self._getitem_next(slice(None), (Ellipsis,) + tail, advanced)
def _getitem_next_regular_missing(self, head, tail, advanced, raw, length):
# if this is in a tuple-slice and really should be 0, it will be trimmed later
length = 1 if length == 0 else length
index = ak._v2.index.Index64(head.index)
indexlength = index.length
index = index._to_nplike(self.nplike)
outindex = ak._v2.index.Index64.empty(index.length * length, self._nplike)
assert outindex.nplike is self._nplike and index.nplike is self._nplike
self._handle_error(
self._nplike[
"awkward_missing_repeat", outindex.dtype.type, index.dtype.type
](
outindex.data,
index.data,
index.length,
length,
raw._size,
)
)
out = ak._v2.contents.indexedoptionarray.IndexedOptionArray(
outindex, raw.content, None, self._parameters, self._nplike
)
return ak._v2.contents.regulararray.RegularArray(
out.simplify_optiontype(),
indexlength,
1,
None,
self._parameters,
self._nplike,
)
def _getitem_next_missing_jagged(self, head, tail, advanced, that):
head = head._to_nplike(self._nplike)
jagged = head.content.toListOffsetArray64()
index = ak._v2.index.Index64(head._index)
content = that._getitem_at(0)
if content.length < index.length and self._nplike.known_shape:
raise ak._v2._util.indexerror(
self,
head,
"cannot fit masked jagged slice with length {} into {} of size {}".format(
index.length, type(that).__name__, content.length
),
)
outputmask = ak._v2.index.Index64.empty(index.length, self._nplike)
starts = ak._v2.index.Index64.empty(index.length, self._nplike)
stops = ak._v2.index.Index64.empty(index.length, self._nplike)
assert (
index.nplike is self._nplike
and jagged._offsets.nplike is self._nplike
and outputmask.nplike is self._nplike
and starts.nplike is self._nplike
and stops.nplike is self._nplike
)
self._handle_error(
self._nplike[
"awkward_Content_getitem_next_missing_jagged_getmaskstartstop",
index.dtype.type,
jagged._offsets.dtype.type,
outputmask.dtype.type,
starts.dtype.type,
stops.dtype.type,
](
index.data,
jagged._offsets.data,
outputmask.data,
starts.data,
stops.data,
index.length,
)
)
tmp = content._getitem_next_jagged(starts, stops, jagged.content, tail)
out = ak._v2.contents.indexedoptionarray.IndexedOptionArray(
outputmask, tmp, None, self._parameters, self._nplike
)
return ak._v2.contents.regulararray.RegularArray(
out.simplify_optiontype(),
index.length,
1,
None,
self._parameters,
self._nplike,
)
def _getitem_next_missing(self, head, tail, advanced):
assert isinstance(head, ak._v2.contents.IndexedOptionArray)
if advanced is not None:
raise ak._v2._util.indexerror(
self,
head,
"cannot mix missing values in slice with NumPy-style advanced indexing",
)
if isinstance(head.content, ak._v2.contents.listoffsetarray.ListOffsetArray):
if self.nplike.known_shape and self.length != 1:
raise ak._v2._util.error(
NotImplementedError("reached a not-well-considered code path")
)
return self._getitem_next_missing_jagged(head, tail, advanced, self)
if isinstance(head.content, ak._v2.contents.numpyarray.NumpyArray):
headcontent = ak._v2.index.Index64(head.content.data)
nextcontent = self._getitem_next(headcontent, tail, advanced)
else:
nextcontent = self._getitem_next(head.content, tail, advanced)
if isinstance(nextcontent, ak._v2.contents.regulararray.RegularArray):
return self._getitem_next_regular_missing(
head, tail, advanced, nextcontent, nextcontent.length
)
elif isinstance(nextcontent, ak._v2.contents.recordarray.RecordArray):
if len(nextcontent._fields) == 0:
return nextcontent
contents = []
for content in nextcontent.contents:
if isinstance(content, ak._v2.contents.regulararray.RegularArray):
contents.append(
self._getitem_next_regular_missing(
head, tail, advanced, content, content.length
)
)
else:
raise ak._v2._util.error(
NotImplementedError(
"FIXME: unhandled case of SliceMissing with RecordArray containing {}".format(
content
)
)
)
return ak._v2.contents.recordarray.RecordArray(
contents,
nextcontent._fields,
None,
None,
self._parameters,
self._nplike,
)
else:
raise ak._v2._util.error(
NotImplementedError(
f"FIXME: unhandled case of SliceMissing with {nextcontent}"
)
)
def __getitem__(self, where):
with ak._v2._util.SlicingErrorContext(self, where):
return self._getitem(where)
def _getitem(self, where):
if ak._util.isint(where):
return self._getitem_at(where)
elif isinstance(where, slice) and where.step is None:
return self._getitem_range(where)
elif isinstance(where, slice):
return self._getitem((where,))
elif ak._util.isstr(where):
return self._getitem_field(where)
elif where is np.newaxis:
return self._getitem((where,))
elif where is Ellipsis:
return self._getitem((where,))
elif isinstance(where, tuple):
if len(where) == 0:
return self
items = [ak._v2._slicing.prepare_tuple_item(x) for x in where]
nextwhere = ak._v2._slicing.getitem_broadcast(items)
next = ak._v2.contents.RegularArray(
self,
self.length if self._nplike.known_shape else 1,
1,
None,
None,
self._nplike,
)
out = next._getitem_next(nextwhere[0], nextwhere[1:], None)
if out.length == 0:
return out._getitem_nothing()
else:
return out._getitem_at(0)
elif isinstance(where, ak.highlevel.Array):
return self._getitem(where.layout)
elif isinstance(where, ak.layout.Content):
return self._getitem(v1_to_v2(where))
elif isinstance(where, ak._v2.highlevel.Array):
return self._getitem(where.layout)
elif (
isinstance(where, Content)
and where._parameters is not None
and (where._parameters.get("__array__") in ("string", "bytestring"))
):
return self._getitem_fields(ak._v2.operations.convert.to_list(where))
elif isinstance(where, ak._v2.contents.emptyarray.EmptyArray):
return where.toNumpyArray(np.int64)
elif isinstance(where, ak._v2.contents.numpyarray.NumpyArray):
if issubclass(where.dtype.type, np.int64):
carry = ak._v2.index.Index64(where.data.reshape(-1))
allow_lazy = True
elif issubclass(where.dtype.type, np.integer):
carry = ak._v2.index.Index64(where.data.astype(np.int64).reshape(-1))
allow_lazy = "copied" # True, but also can be modified in-place
elif issubclass(where.dtype.type, (np.bool_, bool)):
if len(where.data.shape) == 1:
where = self._nplike.nonzero(where.data)[0]
carry = ak._v2.index.Index64(where)
allow_lazy = "copied" # True, but also can be modified in-place
else:
wheres = self._nplike.nonzero(where.data)
return self._getitem(wheres)
else:
raise ak._v2._util.error(
TypeError(
"array slice must be an array of integers or booleans, not\n\n {}".format(
repr(where.data).replace("\n", "\n ")
)
)
)
out = ak._v2._slicing.getitem_next_array_wrap(
self._carry(carry, allow_lazy), where.shape
)
if out.length == 0:
return out._getitem_nothing()
else:
return out._getitem_at(0)
elif isinstance(where, Content):
return self._getitem((where,))
elif isinstance(where, Iterable) and all(ak._util.isstr(x) for x in where):
return self._getitem_fields(where)
elif isinstance(where, Iterable):
layout = ak._v2.operations.convert.to_layout(where)
as_array = layout.maybe_to_array(layout.nplike)
if as_array is None:
return self._getitem(layout)
else:
return self._getitem(
ak._v2.contents.NumpyArray(as_array, None, None, layout.nplike)
)
else:
raise ak._v2._util.error(
TypeError(
"only integers, slices (`:`), ellipsis (`...`), np.newaxis (`None`), "
"integer/boolean arrays (possibly with variable-length nested "
"lists or missing values), field name (str) or names (non-tuple "
"iterable of str) are valid indices for slicing, not\n\n "
+ repr(where).replace("\n", "\n ")
)
)
def _carry_asrange(self, carry):
assert isinstance(carry, ak._v2.index.Index)
result = self._nplike.empty(1, dtype=np.bool_)
assert carry.nplike is self._nplike
self._handle_error(
self._nplike[
"awkward_Index_iscontiguous", # badly named
np.bool_,
carry.dtype.type,
](
result,
carry.data,
carry.length,
)
)
if result[0]:
if carry.length == self.length:
return self
elif carry.length < self.length:
return self._getitem_range(slice(0, carry.length))
else:
raise ak._v2._util.error(IndexError)
else:
return None
def _typetracer_identifier(self):
if self._identifier is None:
return None
else:
raise ak._v2._util.error(NotImplementedError)
def _range_identifier(self, start, stop):
if self._identifier is None:
return None
else:
raise ak._v2._util.error(NotImplementedError)
def _field_identifier(self, field):
if self._identifier is None:
return None
else:
raise ak._v2._util.error(NotImplementedError)
def _fields_identifier(self, fields):
if self._identifier is None:
return None
else:
raise ak._v2._util.error(NotImplementedError)
def _carry_identifier(self, carry):
if self._identifier is None:
return None
else:
raise ak._v2._util.error(NotImplementedError)
def axis_wrap_if_negative(self, axis):
if axis is None or axis >= 0:
return axis
mindepth, maxdepth = self.minmax_depth
depth = self.purelist_depth
if mindepth == depth and maxdepth == depth:
posaxis = depth + axis
if posaxis < 0:
raise ak._v2._util.error(
np.AxisError(
f"axis={axis} exceeds the depth ({depth}) of this array"
)
)
return posaxis
elif mindepth + axis == 0:
raise ak._v2._util.error(
np.AxisError(
"axis={} exceeds the depth ({}) of at least one record field (or union possibility) of this array".format(
axis, depth
)
)
)
return axis
def _localindex_axis0(self):
localindex = ak._v2.index.Index64.empty(self.length, self._nplike)
self._handle_error(
self._nplike["awkward_localindex", np.int64](
localindex.data,
localindex.length,
)
)
return ak._v2.contents.NumpyArray(localindex, None, None, self._nplike)
def merge(self, other):
others = [other]
return self.mergemany(others)
def merge_as_union(self, other):
mylength = self.length
theirlength = other.length
tags = ak._v2.index.Index8.empty((mylength + theirlength), self._nplike)
index = ak._v2.index.Index64.empty((mylength + theirlength), self._nplike)
contents = [self, other]
assert tags.nplike is self._nplike
self._handle_error(
self._nplike["awkward_UnionArray_filltags_const", tags.dtype.type](
tags.data, 0, mylength, 0
)
)
assert index.nplike is self._nplike
self._handle_error(
self._nplike["awkward_UnionArray_fillindex_count", index.dtype.type](
index.data, 0, mylength
)
)
self._handle_error(
self._nplike["awkward_UnionArray_filltags_const", tags.dtype.type](
tags.data, mylength, theirlength, 1
)
)
self._handle_error(
self._nplike["awkward_UnionArray_fillindex_count", index.dtype.type](
index.data, mylength, theirlength
)
)
return ak._v2.contents.unionarray.UnionArray(
tags, index, contents, None, None, self._nplike
)
def _merging_strategy(self, others):
if len(others) == 0:
raise ak._v2._util.error(
ValueError(
"to merge this array with 'others', at least one other must be provided"
)
)
head = [self]
tail = []
i = 0
while i < len(others):
other = others[i]
if isinstance(
other,
(
ak._v2.contents.indexedarray.IndexedArray,
ak._v2.contents.indexedoptionarray.IndexedOptionArray,
ak._v2.contents.bytemaskedarray.ByteMaskedArray,
ak._v2.contents.bitmaskedarray.BitMaskedArray,
ak._v2.contents.unmaskedarray.UnmaskedArray,
ak._v2.contents.unionarray.UnionArray,
),
):
break
else:
head.append(other)
i = i + 1
while i < len(others):
tail.append(others[i])
i = i + 1
if any(
isinstance(x.nplike, ak._v2._typetracer.TypeTracer) for x in head + tail
):
head = [
x
if isinstance(x.nplike, ak._v2._typetracer.TypeTracer)
else x.typetracer
for x in head
]
tail = [
x
if isinstance(x.nplike, ak._v2._typetracer.TypeTracer)
else x.typetracer
for x in tail
]
return (head, tail)
def localindex(self, axis):
return self._localindex(axis, 0)
def _reduce(self, reducer, axis=-1, mask=True, keepdims=False):
if axis is None:
raise ak._v2._util.error(NotImplementedError)
negaxis = -axis
branch, depth = self.branch_depth
if branch:
if negaxis <= 0:
raise ak._v2._util.error(
ValueError(
"cannot use non-negative axis on a nested list structure "
"of variable depth (negative axis counts from the leaves of "
"the tree; non-negative from the root)"
)
)
if negaxis > depth:
raise ak._v2._util.error(
ValueError(
"cannot use axis={} on a nested list structure that splits into "
"different depths, the minimum of which is depth={} "
"from the leaves".format(axis, depth)
)
)
else:
if negaxis <= 0:
negaxis += depth
if not (0 < negaxis and negaxis <= depth):
raise ak._v2._util.error(
ValueError(
"axis={} exceeds the depth of the nested list structure "
"(which is {})".format(axis, depth)
)
)
starts = ak._v2.index.Index64.zeros(1, self._nplike)
parents = ak._v2.index.Index64.zeros(self.length, self._nplike)
shifts = None
next = self._reduce_next(
reducer,
negaxis,
starts,
shifts,
parents,
1,
mask,
keepdims,
)
return next[0]
def argmin(self, axis=-1, mask=True, keepdims=False):
return self._reduce(awkward._v2._reducers.ArgMin, axis, mask, keepdims)
def argmax(self, axis=-1, mask=True, keepdims=False):
return self._reduce(awkward._v2._reducers.ArgMax, axis, mask, keepdims)
def count(self, axis=-1, mask=False, keepdims=False):
return self._reduce(awkward._v2._reducers.Count, axis, mask, keepdims)
def count_nonzero(self, axis=-1, mask=False, keepdims=False):
return self._reduce(awkward._v2._reducers.CountNonzero, axis, mask, keepdims)
def sum(self, axis=-1, mask=False, keepdims=False):
return self._reduce(awkward._v2._reducers.Sum, axis, mask, keepdims)
def prod(self, axis=-1, mask=False, keepdims=False):
return self._reduce(awkward._v2._reducers.Prod, axis, mask, keepdims)
def any(self, axis=-1, mask=False, keepdims=False):
return self._reduce(awkward._v2._reducers.Any, axis, mask, keepdims)
def all(self, axis=-1, mask=False, keepdims=False):
return self._reduce(awkward._v2._reducers.All, axis, mask, keepdims)
def min(self, axis=-1, mask=True, keepdims=False, initial=None):
return self._reduce(awkward._v2._reducers.Min(initial), axis, mask, keepdims)
def max(self, axis=-1, mask=True, keepdims=False, initial=None):
return self._reduce(awkward._v2._reducers.Max(initial), axis, mask, keepdims)
def argsort(self, axis=-1, ascending=True, stable=False, kind=None, order=None):
negaxis = -axis
branch, depth = self.branch_depth
if branch:
if negaxis <= 0:
raise ak._v2._util.error(
ValueError(
"cannot use non-negative axis on a nested list structure "
"of variable depth (negative axis counts from the leaves "
"of the tree; non-negative from the root)"
)
)
if negaxis > depth:
raise ak._v2._util.error(
ValueError(
"cannot use axis={} on a nested list structure that splits into "
"different depths, the minimum of which is depth={} from the leaves".format(
axis, depth
)
)
)
else:
if negaxis <= 0:
negaxis = negaxis + depth
if not (0 < negaxis and negaxis <= depth):
raise ak._v2._util.error(
ValueError(
"axis={} exceeds the depth of the nested list structure "
"(which is {})".format(axis, depth)
)
)
starts = ak._v2.index.Index64.zeros(1, self._nplike)
parents = ak._v2.index.Index64.zeros(self.length, self._nplike)
return self._argsort_next(
negaxis,
starts,
None,
parents,
1,
ascending,
stable,
kind,
order,
)
def sort(self, axis=-1, ascending=True, stable=False, kind=None, order=None):
negaxis = -axis
branch, depth = self.branch_depth
if branch:
if negaxis <= 0:
raise ak._v2._util.error(
ValueError(
"cannot use non-negative axis on a nested list structure "
"of variable depth (negative axis counts from the leaves "
"of the tree; non-negative from the root)"
)
)
if negaxis > depth:
raise ak._v2._util.error(
ValueError(
"cannot use axis={} on a nested list structure that splits into "
"different depths, the minimum of which is depth={} from the leaves".format(
axis, depth
)
)
)
else:
if negaxis <= 0:
negaxis = negaxis + depth
if not (0 < negaxis and negaxis <= depth):
raise ak._v2._util.error(
ValueError(
"axis={} exceeds the depth of the nested list structure "
"(which is {})".format(axis, depth)
)
)
starts = ak._v2.index.Index64.zeros(1, self._nplike)
parents = ak._v2.index.Index64.zeros(self.length, self._nplike)
return self._sort_next(
negaxis,
starts,
parents,
1,
ascending,
stable,
kind,
order,
)
def _combinations_axis0(self, n, replacement, recordlookup, parameters):
size = self.length
if replacement:
size = size + (n - 1)
thisn = n
combinationslen = 0
if thisn > size:
combinationslen = 0
elif thisn == size:
combinationslen = 1
else:
if thisn * 2 > size:
thisn = size - thisn
combinationslen = size
for j in range(2, thisn + 1):
combinationslen = combinationslen * (size - j + 1)
combinationslen = combinationslen // j
tocarryraw = self._nplike.empty(n, dtype=np.intp)
tocarry = []
for i in range(n):
ptr = ak._v2.index.Index64.empty(
combinationslen, self._nplike, dtype=np.int64
)
tocarry.append(ptr)
if self._nplike.known_data:
tocarryraw[i] = ptr.ptr
toindex = ak._v2.index.Index64.empty(n, self._nplike, dtype=np.int64)
fromindex = ak._v2.index.Index64.empty(n, self._nplike, dtype=np.int64)
assert toindex.nplike is self._nplike and fromindex.nplike is self._nplike
self._handle_error(
self._nplike[
"awkward_RegularArray_combinations_64",
np.int64,
toindex.data.dtype.type,
fromindex.data.dtype.type,
](
tocarryraw,
toindex.data,
fromindex.data,
n,
replacement,
self.length,