carray 3.0.7 → 3.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.yardopts +1 -1
- data/CHANGELOG.md +7 -1940
- data/README.md +38 -32
- data/carray.gemspec +16 -4
- data/docs/changelogs/CHANGELOG-3.0.md +2464 -0
- data/ext/ca_array_pool.c +4 -5
- data/ext/ca_axis_descriptor.h +45 -30
- data/ext/ca_axis_dispatch.c +7 -4
- data/ext/ca_axis_group.c +6 -9
- data/ext/ca_bincmp_dispatch.h +16 -38
- data/ext/ca_binop_dispatch.c +27 -0
- data/ext/ca_binop_dispatch.h +22 -47
- data/ext/ca_categorical_iterator.c +36 -34
- data/ext/ca_compare.c +0 -1
- data/ext/ca_compare.h +8 -1
- data/ext/ca_composite_dispatch.h +6 -0
- data/ext/ca_for_buffer.h +6 -1
- data/ext/ca_group_iter.c +17 -18
- data/ext/ca_iter_state_fields.h +196 -0
- data/ext/ca_iter_substrate.h +84 -103
- data/ext/ca_kernel_iterator.c +125 -89
- data/ext/ca_kernel_iterator.h +229 -1506
- data/ext/ca_kernel_iterator_internal.h +1167 -0
- data/ext/ca_moncmp_dispatch.h +17 -24
- data/ext/ca_monop_dispatch.h +25 -74
- data/ext/ca_obj_array.c +90 -163
- data/ext/ca_obj_bincmp.c +47 -46
- data/ext/ca_obj_binop.c +38 -38
- data/ext/ca_obj_bitarray.c +5 -17
- data/ext/ca_obj_bitfield.c +15 -30
- data/ext/ca_obj_block.c +11 -12
- data/ext/ca_obj_byte_swap.c +32 -30
- data/ext/ca_obj_const_string.c +57 -62
- data/ext/ca_obj_face.c +99 -91
- data/ext/ca_obj_face.h +32 -170
- data/ext/ca_obj_fake.c +15 -21
- data/ext/ca_obj_farray.c +7 -9
- data/ext/ca_obj_field.c +17 -13
- data/ext/ca_obj_fixlen_string.c +8 -19
- data/ext/ca_obj_grid.c +137 -106
- data/ext/ca_obj_meld.c +23 -21
- data/ext/ca_obj_moncmp.c +10 -21
- data/ext/ca_obj_monop.c +24 -35
- data/ext/ca_obj_object.c +51 -37
- data/ext/ca_obj_record.c +74 -22
- data/ext/ca_obj_reduce.c +8 -10
- data/ext/ca_obj_refer.c +42 -29
- data/ext/ca_obj_remap.c +11 -29
- data/ext/ca_obj_repeat.c +41 -23
- data/ext/ca_obj_roll.c +18 -24
- data/ext/ca_obj_select.c +10 -22
- data/ext/ca_obj_select_axis.c +27 -36
- data/ext/ca_obj_shift.c +22 -42
- data/ext/ca_obj_source.c +1 -5
- data/ext/ca_obj_stack.c +28 -24
- data/ext/ca_obj_stride.c +53 -54
- data/ext/ca_obj_string.c +6 -18
- data/ext/ca_obj_tile.c +12 -12
- data/ext/ca_obj_time.c +12 -23
- data/ext/ca_obj_timedelta.c +12 -23
- data/ext/ca_obj_transpose.c +8 -10
- data/ext/ca_obj_triop.c +35 -39
- data/ext/ca_obj_window.c +67 -86
- data/ext/ca_op_cmplx64.h +4 -0
- data/ext/ca_op_ipower.c +34 -12
- data/ext/ca_op_powi.h +4 -0
- data/ext/ca_pow5_table.h +2 -1
- data/ext/ca_rng_normal.h +4 -0
- data/ext/ca_rng_xoshiro256pp.h +3 -0
- data/ext/ca_sort_kernels.h +20 -26
- data/ext/ca_sweep_engine.c +2 -3
- data/ext/ca_sweep_engine.h +17 -9
- data/ext/ca_transform_common.c +3 -8
- data/ext/ca_triop_dispatch.h +20 -32
- data/ext/caframe_csv_reader.c +11 -11
- data/ext/carray.h +300 -1011
- data/ext/carray_access.c +234 -180
- data/ext/carray_address_basis.c +33 -38
- data/ext/carray_attribute.c +81 -104
- data/ext/carray_bincount.c +12 -14
- data/ext/carray_broadcast.c +30 -26
- data/ext/carray_call_cfunc.c +169 -277
- data/ext/carray_call_cfunc.h +36 -28
- data/ext/carray_cast.c +912 -346
- data/ext/carray_cast_func.rb +31 -3
- data/ext/carray_class.c +6 -11
- data/ext/carray_conversion.c +43 -38
- data/ext/carray_copy.c +28 -26
- data/ext/carray_core.c +217 -126
- data/ext/carray_count.c +27 -15
- data/ext/carray_element.c +40 -25
- data/ext/carray_factorize.c +53 -52
- data/ext/carray_generate.c +26 -19
- data/ext/carray_histogram.c +8 -10
- data/ext/carray_hold.c +7 -9
- data/ext/carray_index_classifier.c +123 -228
- data/ext/carray_index_classifier.h +11 -10
- data/ext/carray_integer_total.c +523 -0
- data/ext/carray_internal.h +1274 -160
- data/ext/carray_key_windows.c +3 -3
- data/ext/carray_lazy.c +21 -36
- data/ext/carray_loop.c +19 -22
- data/ext/carray_mask.c +123 -145
- data/ext/carray_math_kernel.h +23 -47
- data/ext/carray_mathfunc.c +10 -3
- data/ext/carray_median_percentile.c +95 -94
- data/ext/carray_memory_view.c +42 -47
- data/ext/carray_operator.c +423 -122
- data/ext/carray_order.c +121 -85
- data/ext/carray_partition.c +13 -15
- data/ext/carray_random.c +47 -41
- data/ext/carray_round.c +9 -4
- data/ext/carray_scatter.c +54 -40
- data/ext/carray_segment.c +41 -45
- data/ext/carray_slab.c +64 -47
- data/ext/carray_slab.h +44 -33
- data/ext/carray_sort.c +32 -29
- data/ext/carray_sort_kernel.c +12 -10
- data/ext/carray_struct.c +38 -43
- data/ext/carray_test.c +47 -43
- data/ext/carray_time_text.c +24 -31
- data/ext/carray_undef.c +50 -7
- data/ext/carray_utils.c +258 -94
- data/ext/extconf.rb +24 -17
- data/ext/mk_call_cfunc.rb +19 -20
- data/ext/mk_pow5_table.rb +2 -1
- data/ext/mkkernel.rb +782 -461
- data/ext/ruby_carray.c +47 -42
- data/ext/version.h +7 -5
- data/lib/carray/attribute.rb +19 -18
- data/lib/carray/autoload_carray.rb +20 -15
- data/lib/carray/autoload_method_extension.rb +64 -57
- data/lib/carray/axis_group.rb +264 -180
- data/lib/carray/basics.rb +272 -48
- data/lib/carray/bincount_nd.rb +33 -16
- data/lib/carray/block_iterator.rb +123 -102
- data/lib/carray/boolean_reduce.rb +7 -4
- data/lib/carray/categorical.rb +63 -51
- data/lib/carray/categorical_iterator.rb +89 -67
- data/lib/carray/complex.rb +61 -41
- data/lib/carray/conditional.rb +49 -22
- data/lib/carray/const_string.rb +54 -62
- data/lib/carray/core_extensions.rb +1 -1
- data/lib/carray/distance_limit.rb +7 -5
- data/lib/carray/factories.rb +269 -0
- data/lib/carray/fixlen_string.rb +25 -24
- data/lib/carray/frame/column_verbs.rb +20 -8
- data/lib/carray/frame/concat.rb +3 -3
- data/lib/carray/frame/convert.rb +2 -1
- data/lib/carray/frame/csv_parser.rb +7 -1
- data/lib/carray/frame/frame.rb +35 -25
- data/lib/carray/frame/group.rb +183 -150
- data/lib/carray/frame/io.rb +26 -7
- data/lib/carray/frame/join.rb +37 -12
- data/lib/carray/frame/records.rb +1 -1
- data/lib/carray/frame/reshape.rb +3 -3
- data/lib/carray/frame/sort.rb +10 -8
- data/lib/carray/frame/verbs.rb +148 -118
- data/lib/carray/frame/windows.rb +232 -21
- data/lib/carray/frame.rb +1 -2
- data/lib/carray/fuse_source.rb +24 -21
- data/lib/carray/fusion.rb +30 -30
- data/lib/carray/histogram.rb +35 -16
- data/lib/carray/inspect.rb +7 -7
- data/lib/carray/iterator.rb +97 -24
- data/lib/carray/key_windows.rb +83 -57
- data/lib/carray/lazy.rb +196 -94
- data/lib/carray/limits.rb +80 -0
- data/lib/carray/mask_gap_fill.rb +21 -16
- data/lib/carray/math.rb +87 -50
- data/lib/carray/meld_reduce.rb +30 -8
- data/lib/carray/methods/align_addr.rb +29 -17
- data/lib/carray/methods/bin.rb +5 -1
- data/lib/carray/methods/bincount.rb +4 -2
- data/lib/carray/methods/bit_string.rb +19 -31
- data/lib/carray/methods/broadcast.rb +8 -3
- data/lib/carray/methods/choose.rb +5 -5
- data/lib/carray/methods/composition.rb +72 -71
- data/lib/carray/methods/{index.rb → coordinate.rb} +13 -13
- data/lib/carray/methods/discovery_along.rb +3 -3
- data/lib/carray/methods/gather_nd.rb +106 -106
- data/lib/carray/methods/insert_block.rb +2 -2
- data/lib/carray/methods/is_between.rb +6 -4
- data/lib/carray/methods/is_in.rb +165 -145
- data/lib/carray/methods/join.rb +2 -1
- data/lib/carray/methods/lag.rb +6 -2
- data/lib/carray/methods/locate_addr.rb +150 -138
- data/lib/carray/methods/mask_duplicates.rb +1 -1
- data/lib/carray/methods/meshgrid.rb +28 -16
- data/lib/carray/methods/mode.rb +67 -67
- data/lib/carray/methods/nunique.rb +9 -7
- data/lib/carray/methods/repeat.rb +12 -12
- data/lib/carray/methods/resize.rb +1 -1
- data/lib/carray/methods/snap.rb +24 -20
- data/lib/carray/methods/string_format.rb +2 -2
- data/lib/carray/methods/unique.rb +4 -2
- data/lib/carray/methods/value_counts.rb +1 -1
- data/lib/carray/mkmf.rb +25 -28
- data/lib/carray/percentile_arguments.rb +82 -0
- data/lib/carray/record_order.rb +18 -17
- data/lib/carray/rng.rb +1 -0
- data/lib/carray/runtime.rb +190 -22
- data/lib/carray/segment_iterator.rb +251 -129
- data/lib/carray/serialize.rb +618 -571
- data/lib/carray/slab_iterator.rb +114 -114
- data/lib/carray/stack.rb +11 -12
- data/lib/carray/string.rb +16 -22
- data/lib/carray/string_operation_extension.rb +76 -52
- data/lib/carray/struct.rb +122 -164
- data/lib/carray/struct_builder.rb +243 -72
- data/lib/carray/time.rb +496 -342
- data/lib/carray/window_iterator.rb +191 -150
- data/lib/carray.rb +6 -11
- data/yard-stubs/ca_obj_array.rb +20 -135
- data/yard-stubs/ca_obj_bitarray.rb +1 -8
- data/yard-stubs/ca_obj_bitfield.rb +4 -3
- data/yard-stubs/ca_obj_byte_swap.rb +21 -7
- data/yard-stubs/ca_obj_fake.rb +3 -1
- data/yard-stubs/ca_obj_farray.rb +1 -1
- data/yard-stubs/ca_obj_field.rb +8 -6
- data/yard-stubs/ca_obj_refer.rb +7 -5
- data/yard-stubs/ca_obj_roll.rb +7 -6
- data/yard-stubs/ca_obj_shift.rb +2 -3
- data/yard-stubs/ca_obj_stride.rb +5 -7
- data/yard-stubs/ca_obj_tile.rb +1 -1
- data/yard-stubs/ca_obj_time.rb +43 -0
- data/yard-stubs/ca_obj_transpose.rb +1 -1
- data/yard-stubs/ca_obj_window.rb +26 -15
- data/yard-stubs/carray_access.rb +51 -4
- data/yard-stubs/carray_attribute.rb +26 -38
- data/yard-stubs/carray_cast.rb +118 -165
- data/yard-stubs/carray_class.rb +3 -4
- data/yard-stubs/carray_conversion.rb +6 -7
- data/yard-stubs/carray_copy.rb +2 -1
- data/yard-stubs/carray_count.rb +5 -3
- data/yard-stubs/carray_element.rb +4 -3
- data/yard-stubs/carray_limits.rb +100 -0
- data/yard-stubs/carray_loop.rb +0 -20
- data/yard-stubs/carray_mask.rb +59 -41
- data/yard-stubs/carray_math.rb +157 -100
- data/yard-stubs/carray_mathfunc.rb +415 -3
- data/yard-stubs/carray_median_percentile.rb +29 -11
- data/yard-stubs/carray_memory_view.rb +2 -2
- data/yard-stubs/carray_order.rb +67 -34
- data/yard-stubs/carray_random.rb +6 -6
- data/yard-stubs/carray_reduce.rb +353 -0
- data/yard-stubs/carray_rng.rb +92 -0
- data/yard-stubs/carray_scatter.rb +10 -2
- data/yard-stubs/carray_slab.rb +7 -4
- data/yard-stubs/carray_sort.rb +7 -6
- data/yard-stubs/carray_undef.rb +44 -5
- data/yard-stubs/ruby_carray.rb +75 -40
- metadata +62 -24
- data/CHANGELOG.v1.md +0 -126
- data/ext/carray_build_flags.h +0 -3
- data/ext/carray_config.h +0 -28
- data/ext/carray_data_type.c +0 -90
- data/ext/carray_kernels_bincmp.c +0 -6457
- data/ext/carray_kernels_binop.c +0 -31534
- data/ext/carray_kernels_init.c +0 -1734
- data/ext/carray_kernels_moncmp.c +0 -2104
- data/ext/carray_kernels_monop.c +0 -25337
- data/ext/carray_kernels_reduce_aggregate.c +0 -27545
- data/ext/carray_kernels_reduce_boolean.c +0 -1924
- data/ext/carray_kernels_reduce_cumulative.c +0 -11646
- data/ext/carray_kernels_reduce_extreme.c +0 -21631
- data/ext/carray_kernels_reduce_variance.c +0 -5981
- data/ext/carray_kernels_scan.c +0 -4302
- data/ext/carray_kernels_search.c +0 -37776
- data/ext/carray_kernels_sort.c +0 -10814
- data/ext/carray_kernels_triop.c +0 -1864
- data/lib/carray/construct.rb +0 -360
- data/lib/carray/data_type_extension.rb +0 -262
- data/lib/carray/data_type_limits.rb +0 -91
|
@@ -37,7 +37,7 @@ class CASegmentIterator < CAIterator
|
|
|
37
37
|
# Takes a copy of the cells the segments cover, so the reductions answer
|
|
38
38
|
# about the values as they were at construction. Cells before offsets[0]
|
|
39
39
|
# or from offsets[-1] on belong to no segment.
|
|
40
|
-
def initialize
|
|
40
|
+
def initialize(value, offsets: nil, lengths: nil)
|
|
41
41
|
kw = {}
|
|
42
42
|
kw[:offsets] = offsets unless offsets.nil?
|
|
43
43
|
kw[:lengths] = lengths unless lengths.nil?
|
|
@@ -48,7 +48,7 @@ class CASegmentIterator < CAIterator
|
|
|
48
48
|
"segments: the segments span #{origin}...#{finish}, " \
|
|
49
49
|
"outside the #{value.elements} elements of the value"
|
|
50
50
|
end
|
|
51
|
-
@
|
|
51
|
+
@source = value
|
|
52
52
|
@src_shape = value.shape
|
|
53
53
|
@k = bounds.elements - 1
|
|
54
54
|
@ndim = 1
|
|
@@ -112,7 +112,7 @@ class CASegmentIterator < CAIterator
|
|
|
112
112
|
# returns {#count_not_masked} (present cells); `count(UNDEF)` returns
|
|
113
113
|
# {#count_masked}; `count(v)` counts cells whose value equals `v`.
|
|
114
114
|
# @return [CArray] length-k int64
|
|
115
|
-
def count
|
|
115
|
+
def count(*args, axis: nil)
|
|
116
116
|
if axis
|
|
117
117
|
return count_not_masked(axis: axis) if args.empty?
|
|
118
118
|
raise NotImplementedError,
|
|
@@ -143,15 +143,23 @@ class CASegmentIterator < CAIterator
|
|
|
143
143
|
end
|
|
144
144
|
|
|
145
145
|
# @overload sum
|
|
146
|
-
# Returns per-segment sums in the data type `CArray#sum`
|
|
147
|
-
#
|
|
148
|
-
#
|
|
149
|
-
#
|
|
150
|
-
#
|
|
146
|
+
# Returns per-segment sums in the data type `CArray#sum` answers in: int64
|
|
147
|
+
# for a signed integer value (uint64 for an unsigned one), float64 when a
|
|
148
|
+
# sum is out of that range, and float64 for a float value. `accumulate`
|
|
149
|
+
# is the same fold kept in the value's own type. An empty or fully-masked
|
|
150
|
+
# segment sums the empty set, which is the additive identity `0`
|
|
151
|
+
# (unmasked) — the same contract as `CArray#sum` on an empty / all-masked
|
|
152
|
+
# array.
|
|
151
153
|
# @return [CArray]
|
|
152
154
|
def sum(axis: nil)
|
|
153
155
|
return axis_sum(axis) if axis
|
|
154
156
|
m = moments
|
|
157
|
+
if integer_payload?
|
|
158
|
+
return per_segment_total(:sum) unless m
|
|
159
|
+
return integer_total(:sum, m[:sum], min: m[:min], max: m[:max], count: m[:count]) {
|
|
160
|
+
per_segment_total(:sum)
|
|
161
|
+
}
|
|
162
|
+
end
|
|
155
163
|
return per_segment(core_reduce_kind(:sum)) { |s| s.sum } unless m
|
|
156
164
|
m[:sum].copy # the moments sum IS the core fold (empty -> 0.0)
|
|
157
165
|
end
|
|
@@ -215,13 +223,21 @@ class CASegmentIterator < CAIterator
|
|
|
215
223
|
# `:linear` interpolation, matching `CArray#percentile`). Empty segments
|
|
216
224
|
# are MASKED. Order statistics need every value of a segment held together —
|
|
217
225
|
# this is the reduceat that only the eager grouped copy can serve.
|
|
226
|
+
# Several p are taken in every form `CArray#percentile` takes, and
|
|
227
|
+
# answered in the form p was written in (ReductionCountAndFill reads
|
|
228
|
+
# them, one p at a time).
|
|
218
229
|
# @param p [Numeric] percentile in 0..100.
|
|
219
|
-
# @return [CArray]
|
|
220
|
-
def percentile
|
|
230
|
+
# @return [CArray, Array<CArray>]
|
|
231
|
+
def percentile(p, axis: nil)
|
|
221
232
|
return axis_order_stat(axis, :percentile, p) if axis
|
|
222
233
|
unless kernel_reads?
|
|
223
234
|
return per_segment(core_reduce_kind(:percentile, p)) { |s| s.percentile(p) }
|
|
224
235
|
end
|
|
236
|
+
# The kernel takes a Float; read p as CArray#percentile does, rather
|
|
237
|
+
# than let to_f turn nil or a String into 0.
|
|
238
|
+
unless p.is_a?(Numeric) or p.is_a?(CScalar)
|
|
239
|
+
raise ArgumentError, "percentile: p must be Numeric in [0,100] (got #{p.inspect})"
|
|
240
|
+
end
|
|
225
241
|
out = CArray.float64(@k)
|
|
226
242
|
grouped.send(:__reduceat_percentile__, @offsets, p.to_f, out)
|
|
227
243
|
out
|
|
@@ -234,7 +250,10 @@ class CASegmentIterator < CAIterator
|
|
|
234
250
|
# MASKED. For a single fraction q in 0..1 use `percentile(q * 100)`.
|
|
235
251
|
# @param axis [Integer, nil] as for {#percentile}.
|
|
236
252
|
# @return [Array<CArray>]
|
|
237
|
-
def quantile
|
|
253
|
+
def quantile(*args, axis: nil, **kw)
|
|
254
|
+
CAIterator.send(:refuse_quantile_arguments, args)
|
|
255
|
+
# min_count: / fill_value: are those of percentile, which takes them.
|
|
256
|
+
return percentile([0, 25, 50, 75, 100], axis: axis, **kw) unless kw.empty?
|
|
238
257
|
return CArray.__name_errors_after_method__(:quantile) { [0, 25, 50, 75, 100].map { |p| percentile(p, axis: axis) } } if axis
|
|
239
258
|
unless kernel_reads?
|
|
240
259
|
return CArray.__name_errors_after_method__(:quantile) { [0, 25, 50, 75, 100].map { |p| percentile(p) } }
|
|
@@ -259,7 +278,7 @@ class CASegmentIterator < CAIterator
|
|
|
259
278
|
# The kernel divides the centred sum of squares by count - ddof once, as
|
|
260
279
|
# the core reduction does; scaling the sample variance by (n-1)/n instead
|
|
261
280
|
# rounds twice and lands an ulp away.
|
|
262
|
-
def centred_variance
|
|
281
|
+
def centred_variance(m, ddof)
|
|
263
282
|
cnt = m[:count]
|
|
264
283
|
means = m[:sum] / cnt.float64 # per-segment mean (garbage where the count
|
|
265
284
|
out = CArray.float64(@k) # is too small; the kernel masks those)
|
|
@@ -281,38 +300,62 @@ class CASegmentIterator < CAIterator
|
|
|
281
300
|
end
|
|
282
301
|
|
|
283
302
|
# @overload prod
|
|
284
|
-
# Returns per-segment products
|
|
285
|
-
#
|
|
303
|
+
# Returns per-segment products in the data type `CArray#prod` answers in:
|
|
304
|
+
# int64 for a signed integer value (uint64 for an unsigned one), float64
|
|
305
|
+
# when a product is out of that range, and float64 for a float value. An
|
|
306
|
+
# empty / fully-masked segment is `1` (the multiplicative identity).
|
|
286
307
|
# Single-pass reduceat for numeric values; per-segment fallback otherwise.
|
|
287
308
|
# @return [CArray]
|
|
288
309
|
def prod(axis: nil)
|
|
289
310
|
return axis_prod(axis) if axis
|
|
311
|
+
if integer_payload?
|
|
312
|
+
return per_segment_total(:prod) unless kernel_reads?
|
|
313
|
+
out = CArray.float64(@k)
|
|
314
|
+
grouped.send(:__reduceat_prod__, @offsets, out)
|
|
315
|
+
return integer_total(:prod, out) { per_segment_total(:prod) }
|
|
316
|
+
end
|
|
290
317
|
return per_segment(core_reduce_kind(:prod)) { |s| s.prod } unless kernel_reads?
|
|
291
318
|
out = CArray.float64(@k)
|
|
292
319
|
grouped.send(:__reduceat_prod__, @offsets, out)
|
|
293
320
|
out
|
|
294
321
|
end
|
|
295
322
|
|
|
296
|
-
# @overload all
|
|
323
|
+
# @overload all(skip_masked: true)
|
|
297
324
|
# Returns the per-segment `all` as boolean (matching `CArray#all`): true
|
|
298
325
|
# iff every present value is truthy (empty segment -> true, vacuously).
|
|
299
|
-
# The value data type must be boolean, as for `CArray#all`.
|
|
326
|
+
# The value data type must be boolean, as for `CArray#all`. With
|
|
327
|
+
# `skip_masked: false` the fold is three-valued, as `CArray#all` folds
|
|
328
|
+
# it: a segment is `UNDEF` when a masked cell could change it.
|
|
329
|
+
# @param skip_masked [Boolean]
|
|
300
330
|
# @return [CArray]
|
|
301
|
-
def all
|
|
302
|
-
|
|
303
|
-
aa = all_any
|
|
304
|
-
aa ? aa[:all] : per_segment(CA_BOOLEAN) { |s| s.all }
|
|
331
|
+
def all(axis: nil, skip_masked: true)
|
|
332
|
+
fold_all_any(:all, axis, skip_masked)
|
|
305
333
|
end
|
|
306
334
|
|
|
307
|
-
# @overload any
|
|
335
|
+
# @overload any(skip_masked: true)
|
|
308
336
|
# Returns the per-segment `any` as boolean (matching `CArray#any`): true
|
|
309
337
|
# iff some present value is truthy (empty segment -> false). The value
|
|
310
|
-
# data type must be boolean, as for `CArray#any`.
|
|
338
|
+
# data type must be boolean, as for `CArray#any`. With
|
|
339
|
+
# `skip_masked: false` the fold is three-valued, as for {#all}.
|
|
340
|
+
# @param skip_masked [Boolean]
|
|
311
341
|
# @return [CArray]
|
|
312
|
-
def any
|
|
313
|
-
|
|
342
|
+
def any(axis: nil, skip_masked: true)
|
|
343
|
+
fold_all_any(:any, axis, skip_masked)
|
|
344
|
+
end
|
|
345
|
+
|
|
346
|
+
private def fold_all_any(op, axis, skip_masked)
|
|
347
|
+
skip_masked = CArray.send(:bool_arg, skip_masked, "skip_masked", op.to_s)
|
|
348
|
+
if axis
|
|
349
|
+
unless skip_masked
|
|
350
|
+
raise NotImplementedError,
|
|
351
|
+
"#{self.class}##{op}(axis:, skip_masked: false) is not implemented " \
|
|
352
|
+
"-- call it without axis:."
|
|
353
|
+
end
|
|
354
|
+
return axis_by_core(axis, op)
|
|
355
|
+
end
|
|
314
356
|
aa = all_any
|
|
315
|
-
aa ? aa[
|
|
357
|
+
r = aa ? aa[op] : per_segment(CA_BOOLEAN) { |s| s.public_send(op) }
|
|
358
|
+
skip_masked ? r : kleene_settle(op, r, count_masked)
|
|
316
359
|
end
|
|
317
360
|
|
|
318
361
|
# @overload first
|
|
@@ -320,7 +363,7 @@ class CASegmentIterator < CAIterator
|
|
|
320
363
|
# type (matching `CArray#first`). A segment with no present value is
|
|
321
364
|
# UNDEF.
|
|
322
365
|
# @return [CArray]
|
|
323
|
-
def first
|
|
366
|
+
def first(axis: nil)
|
|
324
367
|
return axis_by_core(axis, :first) if axis
|
|
325
368
|
edge_values(:first)
|
|
326
369
|
end
|
|
@@ -330,7 +373,7 @@ class CASegmentIterator < CAIterator
|
|
|
330
373
|
# type (matching `CArray#last`). A segment with no present value is
|
|
331
374
|
# UNDEF.
|
|
332
375
|
# @return [CArray]
|
|
333
|
-
def last
|
|
376
|
+
def last(axis: nil)
|
|
334
377
|
return axis_by_core(axis, :last) if axis
|
|
335
378
|
edge_values(:last)
|
|
336
379
|
end
|
|
@@ -345,7 +388,7 @@ class CASegmentIterator < CAIterator
|
|
|
345
388
|
# by one: from an average of about 100 cells (1M cells on an M2 Max).
|
|
346
389
|
NUNIQUE_KERNEL_MAX_LENGTH = 100
|
|
347
390
|
|
|
348
|
-
def nunique
|
|
391
|
+
def nunique(axis: nil)
|
|
349
392
|
return axis_by_core(axis, :nunique) if axis
|
|
350
393
|
if ! CArray.__fast_paths_enabled__(:segment_nunique) ||
|
|
351
394
|
grouped.elements >= NUNIQUE_KERNEL_MAX_LENGTH * @k
|
|
@@ -458,28 +501,41 @@ class CASegmentIterator < CAIterator
|
|
|
458
501
|
# Unlike {#min_index} / {#max_index} (positions within a segment), this
|
|
459
502
|
# indexes back into the original array; only the source-address form is
|
|
460
503
|
# offered, mirroring {#min_addr}.
|
|
504
|
+
# With `masked_position: :first` the masked cells come first instead.
|
|
505
|
+
# @param masked_position [Symbol] `:last` (default) or `:first`.
|
|
461
506
|
# @return [CArray] length-nvalid int64
|
|
462
|
-
def sort_addr
|
|
507
|
+
def sort_addr(masked_position: :last)
|
|
463
508
|
out = CArray.int64(grouped.elements)
|
|
464
509
|
@k.times do |c|
|
|
465
510
|
lo, hi = @bounds[c], @bounds[c + 1]
|
|
466
511
|
next unless hi > lo
|
|
467
512
|
# View-local sort order of the segment (0..size-1), lifted to grouped
|
|
468
513
|
# slots, then mapped back to source addresses via perm.
|
|
469
|
-
out[lo...hi] = perm[grouped[lo...hi].sort_addr + lo]
|
|
514
|
+
out[lo...hi] = perm[grouped[lo...hi].sort_addr(masked_position: masked_position) + lo]
|
|
470
515
|
end
|
|
471
516
|
out
|
|
472
517
|
end
|
|
473
518
|
|
|
474
519
|
# @overload wsum(weights)
|
|
475
|
-
# Per-segment weighted sum
|
|
520
|
+
# Per-segment weighted sum, matching `CArray#wsum`: an integer value with
|
|
521
|
+
# integer weights gives an integer total, others float64. `weights` is
|
|
476
522
|
# a per-cell weight CArray in the source order (same elements as the value).
|
|
477
|
-
# Empty / all-masked segment -> 0
|
|
523
|
+
# Empty / all-masked segment -> 0 (the additive identity). A cell is
|
|
478
524
|
# skipped iff its value OR its weight is masked (core's contract).
|
|
479
525
|
# @param weights [CArray]
|
|
480
526
|
# @return [CArray]
|
|
481
|
-
|
|
527
|
+
# @raise [TypeError] when `weights` is a number (one weight on every
|
|
528
|
+
# cell is `w * sum`).
|
|
529
|
+
def wsum(weights, axis: nil)
|
|
530
|
+
CArray.send(:refuse_scalar_weights, "wsum", "the value") if weights.is_a?(Numeric)
|
|
482
531
|
return axis_wsum_wmean(weights, axis, :wsum)[0] if axis
|
|
532
|
+
if weights.is_a?(CArray)
|
|
533
|
+
products, unsigned = @source.send(:integer_weighted_products, weights)
|
|
534
|
+
if products
|
|
535
|
+
total = scatter_weights(products, false).segments(offsets: @bounds).sum
|
|
536
|
+
return CArray.send(:integer_weighted_total, total, unsigned)
|
|
537
|
+
end
|
|
538
|
+
end
|
|
483
539
|
kernel = kernel_reads?
|
|
484
540
|
wg = scatter_weights(weights, kernel)
|
|
485
541
|
return kernel_weighted(wg)[0] if kernel
|
|
@@ -491,7 +547,10 @@ class CASegmentIterator < CAIterator
|
|
|
491
547
|
# segment, and one whose weights sum to zero, is masked.
|
|
492
548
|
# @param weights [CArray]
|
|
493
549
|
# @return [CArray]
|
|
494
|
-
|
|
550
|
+
# @raise [TypeError] when `weights` is a number (one weight on every
|
|
551
|
+
# cell gives {#mean}).
|
|
552
|
+
def wmean(weights, axis: nil)
|
|
553
|
+
CArray.send(:refuse_scalar_weights, "wmean", "the value") if weights.is_a?(Numeric)
|
|
495
554
|
return axis_wsum_wmean(weights, axis, :wmean)[1] if axis
|
|
496
555
|
kernel = kernel_reads?
|
|
497
556
|
wg = scatter_weights(weights, kernel)
|
|
@@ -506,11 +565,15 @@ class CASegmentIterator < CAIterator
|
|
|
506
565
|
# @yieldparam members [CArray]
|
|
507
566
|
# @return [CArray] length-k
|
|
508
567
|
# @overload reduce(init) { |acc, elem| ... }
|
|
509
|
-
# Per-segment fold: each segment's members are folded element
|
|
510
|
-
# element
|
|
568
|
+
# Per-segment fold: each segment's present members are folded element
|
|
569
|
+
# by element from a copy of `init` (`init.dup`). A masked member is not
|
|
570
|
+
# passed; a segment with none present answers `init`.
|
|
511
571
|
# @param init [Object] initial accumulator.
|
|
512
572
|
# @return [CArray] length-k
|
|
513
|
-
def reduce
|
|
573
|
+
def reduce(*args, data_type: nil, &blk)
|
|
574
|
+
if args.size > 1
|
|
575
|
+
raise ArgumentError, "reduce: wrong number of arguments (given #{args.size}, expected 0..1)"
|
|
576
|
+
end
|
|
514
577
|
raise LocalJumpError, "no block given (yield)" unless blk
|
|
515
578
|
dt = data_type || CA_OBJECT
|
|
516
579
|
if args.empty?
|
|
@@ -518,8 +581,8 @@ class CASegmentIterator < CAIterator
|
|
|
518
581
|
else
|
|
519
582
|
init = args[0]
|
|
520
583
|
per_segment(dt) { |s|
|
|
521
|
-
acc = init
|
|
522
|
-
s.each { |e| acc = blk.call(acc, e) }
|
|
584
|
+
acc = init.dup
|
|
585
|
+
s.each { |e| acc = blk.call(acc, e) unless UNDEF.equal?(e) }
|
|
523
586
|
acc
|
|
524
587
|
}
|
|
525
588
|
end
|
|
@@ -543,7 +606,7 @@ class CASegmentIterator < CAIterator
|
|
|
543
606
|
# along it, every other axis kept. A segment iterator has no axis: form.
|
|
544
607
|
# @yieldparam members [CArray]
|
|
545
608
|
# @return [CArray] shaped like the source value
|
|
546
|
-
def map
|
|
609
|
+
def map(data_type: nil, axis: nil, &block)
|
|
547
610
|
raise LocalJumpError, "no block given (yield)" unless block
|
|
548
611
|
return axis_map(axis, data_type, &block) if axis
|
|
549
612
|
# Apply the block per segment, assembled in grouped (segment-contiguous)
|
|
@@ -588,7 +651,7 @@ class CASegmentIterator < CAIterator
|
|
|
588
651
|
CArray.send(:define_named_methods, self,
|
|
589
652
|
[:cumsum, :cumprod, :cummax, :cummin, :cumcount], :segment_scan)
|
|
590
653
|
|
|
591
|
-
private def segment_scan
|
|
654
|
+
private def segment_scan(op, axis: nil)
|
|
592
655
|
axis ? axis_scan(op, axis) : scan(op)
|
|
593
656
|
end
|
|
594
657
|
|
|
@@ -599,12 +662,12 @@ class CASegmentIterator < CAIterator
|
|
|
599
662
|
# a cell in no segment is UNDEF. A negative `n` looks ahead.
|
|
600
663
|
# @param n [Integer]
|
|
601
664
|
# @return [CArray]
|
|
602
|
-
def lag
|
|
665
|
+
def lag(n = 1, axis: nil)
|
|
603
666
|
unless n.is_a?(Integer)
|
|
604
667
|
raise TypeError, "lag: n must be an Integer (got #{n.class})"
|
|
605
668
|
end
|
|
606
669
|
return axis_lag(n, axis) if axis
|
|
607
|
-
out = blank_like(@
|
|
670
|
+
out = blank_like(@source, @src_shape)
|
|
608
671
|
out[] = UNDEF
|
|
609
672
|
m = grouped.elements
|
|
610
673
|
return out if m.zero?
|
|
@@ -619,26 +682,26 @@ class CASegmentIterator < CAIterator
|
|
|
619
682
|
pos = CArray.int64(m).seq! - start
|
|
620
683
|
lagged[n > 0 ? pos.lt(n) : pos.ge(@bounds[seg + 1] - start + n)] = UNDEF
|
|
621
684
|
end
|
|
622
|
-
out.reshape(@
|
|
685
|
+
out.reshape(@source.elements)[perm] = lagged
|
|
623
686
|
out
|
|
624
687
|
end
|
|
625
688
|
|
|
626
689
|
# @overload strip_mask(method:)
|
|
627
690
|
# Returns the values with each masked cell filled from its segment, as
|
|
628
|
-
# `CArray#strip_mask(method:)` fills a 1-D array: `:forward`
|
|
691
|
+
# `CArray#strip_mask(method:)` fills a 1-D array: `:forward`
|
|
629
692
|
# carries the segment's last present value forward, `:backward`
|
|
630
|
-
# (`:
|
|
693
|
+
# (`:backward`) its next one back. A fill never reaches across into another
|
|
631
694
|
# segment, so a masked cell with nothing to carry in its segment stays
|
|
632
695
|
# masked; a cell in no segment is UNDEF. The value is not modified.
|
|
633
|
-
# @param method [Symbol] `:forward
|
|
696
|
+
# @param method [Symbol] `:forward` or `:backward`.
|
|
634
697
|
# @return [CArray] source-shaped
|
|
635
|
-
def strip_mask
|
|
636
|
-
backward = CAIterator.hold_backward
|
|
698
|
+
def strip_mask(method:, axis: nil)
|
|
699
|
+
backward = CAIterator.send(:hold_backward?, method)
|
|
637
700
|
return axis_hold(method, axis) if axis
|
|
638
|
-
if @
|
|
639
|
-
return scatter_segments(->(shape) { blank_like(@
|
|
701
|
+
if @source.face? && ! face_output?
|
|
702
|
+
return scatter_segments(->(shape) { blank_like(@source, shape) }) { |s| s.strip_mask(method: method) }
|
|
640
703
|
end
|
|
641
|
-
out = blank_like(@
|
|
704
|
+
out = blank_like(@source, @src_shape)
|
|
642
705
|
out[] = UNDEF
|
|
643
706
|
m = grouped.elements
|
|
644
707
|
return out if m.zero?
|
|
@@ -649,7 +712,7 @@ class CASegmentIterator < CAIterator
|
|
|
649
712
|
slot[grouped.is_masked] = backward ? m : -1 if grouped.has_mask?
|
|
650
713
|
seg = CArray.segment_index(offsets: @bounds)
|
|
651
714
|
if backward
|
|
652
|
-
source = slot.
|
|
715
|
+
source = slot.flip.cummin.flip
|
|
653
716
|
found = source.lt(@bounds[seg + 1])
|
|
654
717
|
else
|
|
655
718
|
source = slot.cummax
|
|
@@ -657,27 +720,28 @@ class CASegmentIterator < CAIterator
|
|
|
657
720
|
end
|
|
658
721
|
filled = grouped[source.pmin(m - 1).pmax(0)].copy
|
|
659
722
|
filled[found.not] = UNDEF
|
|
660
|
-
out.reshape(@
|
|
723
|
+
out.reshape(@source.elements)[perm] = filled
|
|
661
724
|
out
|
|
662
725
|
end
|
|
663
726
|
|
|
664
|
-
# @overload
|
|
727
|
+
# @overload rank_index(descending: false, method: :ordinal, kind: :quick)
|
|
665
728
|
# Returns each cell's rank among the members of its segment, source-shaped
|
|
666
|
-
# int64, as `CArray#
|
|
729
|
+
# int64, as `CArray#rank_index` ranks a 1-D array: `0` is the smallest, ties
|
|
667
730
|
# are broken by position (`:ordinal`) or share a rank (`:dense`), and
|
|
668
731
|
# `descending:` reverses the order. A masked cell, and a cell in no
|
|
669
732
|
# segment, is UNDEF.
|
|
670
733
|
# @return [CArray]
|
|
671
|
-
def
|
|
672
|
-
|
|
734
|
+
def rank_index(axis: nil, descending: false, method: :ordinal, kind: :quick)
|
|
735
|
+
descending = CArray.send(:bool_arg, descending, "descending", "rank_index")
|
|
736
|
+
return axis_rank_index(axis, descending: descending, method: method, kind: kind) if axis
|
|
673
737
|
out = CArray.int64(*@src_shape)
|
|
674
738
|
out[] = UNDEF
|
|
675
739
|
# The keywords are checked by the core, on no cells, so a segment
|
|
676
|
-
# iterator refuses what CArray#
|
|
677
|
-
CArray.int64(0).
|
|
740
|
+
# iterator refuses what CArray#rank_index refuses.
|
|
741
|
+
CArray.int64(0).rank_index(descending: descending, method: method, kind: kind)
|
|
678
742
|
# The core ranks every value once, so the order and the ties are its own.
|
|
679
743
|
# Ranked in the asked direction, so tied cells keep their order either way.
|
|
680
|
-
ranks = grouped.
|
|
744
|
+
ranks = grouped.rank_index(method: :dense, kind: kind, descending: descending)
|
|
681
745
|
m = grouped.elements
|
|
682
746
|
return out if m.zero?
|
|
683
747
|
seg = CArray.segment_index(offsets: @bounds)
|
|
@@ -699,7 +763,7 @@ class CASegmentIterator < CAIterator
|
|
|
699
763
|
local[sorted] = CArray.int64(m).seq! - first
|
|
700
764
|
end
|
|
701
765
|
local[present.not] = UNDEF
|
|
702
|
-
out.reshape(@
|
|
766
|
+
out.reshape(@source.elements)[perm] = local
|
|
703
767
|
out
|
|
704
768
|
end
|
|
705
769
|
|
|
@@ -728,30 +792,32 @@ class CASegmentIterator < CAIterator
|
|
|
728
792
|
# With no segment at all the kernel has no group to size, so it is given
|
|
729
793
|
# one that no cell belongs to: every cell comes back UNDEF, as it does when
|
|
730
794
|
# there are segments and a cell is in none of them.
|
|
731
|
-
def scan
|
|
795
|
+
def scan(op)
|
|
732
796
|
CArray.__name_errors_after_method__(op) { scan_answered(op) }
|
|
733
797
|
end
|
|
734
798
|
|
|
735
|
-
def scan_answered
|
|
799
|
+
def scan_answered(op)
|
|
736
800
|
# An empty Face has no cell to ask the core about, and nothing to scan.
|
|
737
|
-
if @
|
|
801
|
+
if @source.face? && @source.elements.zero?
|
|
738
802
|
return op == :cumcount ? CArray.int64(*@src_shape) : blank_output(@src_shape)
|
|
739
803
|
end
|
|
740
804
|
answer = core_scan_answer(op)
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
805
|
+
by_core = -> { scatter_segments(->(shape) { blank_like(answer, shape) }, widen: true) { |s| s.public_send(op) } }
|
|
806
|
+
integer = CArray.__fast_paths_enabled__(:segment_scan_kernel) &&
|
|
807
|
+
CAIterator.send(:integer_running?, op, @source, answer)
|
|
808
|
+
return by_core.call unless integer || kernel_scans?(op, answer)
|
|
744
809
|
bundle = @k > 0 ? [codes, @k, [0]]
|
|
745
|
-
: [CArray.int64(@
|
|
746
|
-
scan_source.reshape(@
|
|
747
|
-
|
|
748
|
-
|
|
810
|
+
: [CArray.int64(@source.elements).fill(-1), 1, [0]]
|
|
811
|
+
out = scan_source.reshape(@source.elements)
|
|
812
|
+
.__scan_by_axis_groups__([0], [bundle], op)
|
|
813
|
+
.reshape(*@src_shape)
|
|
814
|
+
integer ? CAIterator.send(:integer_running, out, answer, &by_core) : out
|
|
749
815
|
end
|
|
750
816
|
|
|
751
817
|
# The core's scan of one cell of the value: the scan answers in its data
|
|
752
818
|
# type and Face. A scan the core refuses raises here, in the core's words.
|
|
753
|
-
def core_scan_answer
|
|
754
|
-
(@core_scan_answer ||= {})[op] ||= CAIterator.core_probe
|
|
819
|
+
def core_scan_answer(op)
|
|
820
|
+
(@core_scan_answer ||= {})[op] ||= CAIterator.send(:core_probe, @source).public_send(op)
|
|
755
821
|
end
|
|
756
822
|
|
|
757
823
|
# Whether the axis-group scan kernel gives the core's answer. It reads
|
|
@@ -760,14 +826,14 @@ class CASegmentIterator < CAIterator
|
|
|
760
826
|
# object stays an object), cummax / cummin in the value's data type and
|
|
761
827
|
# cumcount in int64 -- which is not the core's answer for every data type
|
|
762
828
|
# (the core counts a boolean's running sum in uint64).
|
|
763
|
-
def kernel_scans?
|
|
829
|
+
def kernel_scans?(op, answer)
|
|
764
830
|
return false unless CArray.__fast_paths_enabled__(:segment_scan_kernel)
|
|
765
|
-
return false if @
|
|
831
|
+
return false if @source.face? || answer.face? || @source.complex? || @source.data_type == CA_FIXLEN
|
|
766
832
|
kernel_type =
|
|
767
833
|
case op
|
|
768
834
|
when :cumcount then CA_INT64
|
|
769
|
-
when :cumsum, :cumprod then @
|
|
770
|
-
else @
|
|
835
|
+
when :cumsum, :cumprod then @source.data_type == CA_OBJECT ? CA_OBJECT : CA_FLOAT64
|
|
836
|
+
else @source.data_type
|
|
771
837
|
end
|
|
772
838
|
answer.data_type == kernel_type
|
|
773
839
|
end
|
|
@@ -775,29 +841,35 @@ class CASegmentIterator < CAIterator
|
|
|
775
841
|
# Applies the block to each segment's members in grouped order and scatters
|
|
776
842
|
# the answers back to the source cells; a cell in no segment is UNDEF.
|
|
777
843
|
# +blank+ builds an empty array of a given shape that can hold the answers.
|
|
778
|
-
|
|
844
|
+
# +widen+ is for the scans: an integer running total out of range answers
|
|
845
|
+
# in float64, and the result follows it. A map keeps the type it was given.
|
|
846
|
+
def scatter_segments(blank, widen: false)
|
|
779
847
|
out = blank.call(@src_shape)
|
|
780
848
|
out[] = UNDEF
|
|
781
849
|
return out if grouped.elements.zero?
|
|
782
850
|
transformed = blank.call([grouped.elements])
|
|
783
851
|
@k.times do |c|
|
|
784
852
|
lo, hi = @bounds[c], @bounds[c + 1]
|
|
785
|
-
|
|
853
|
+
next unless hi > lo
|
|
854
|
+
answer = yield(grouped[lo...hi])
|
|
855
|
+
transformed = CAIterator.send(:widen_for_running, transformed, answer) if widen
|
|
856
|
+
transformed[lo...hi] = surface_for(transformed, answer)
|
|
786
857
|
end
|
|
787
|
-
out.
|
|
858
|
+
out = CAIterator.send(:widen_for_running, out, transformed) if widen
|
|
859
|
+
out.reshape(@source.elements)[perm] = transformed
|
|
788
860
|
out
|
|
789
861
|
end
|
|
790
862
|
|
|
791
863
|
# A blank array for #map: of +data_type+ when one is given, otherwise of the
|
|
792
864
|
# same kind as the value.
|
|
793
|
-
def blank_output
|
|
794
|
-
data_type ? CArray.new(data_type, shape) : blank_like(@
|
|
865
|
+
def blank_output(shape, data_type = nil)
|
|
866
|
+
data_type ? CArray.new(data_type, shape) : blank_like(@source, shape)
|
|
795
867
|
end
|
|
796
868
|
|
|
797
869
|
# An answer as +out+ can hold it. A Face goes into an object array as its
|
|
798
870
|
# surface values; stored as it is, the object array would receive its
|
|
799
871
|
# storage bytes.
|
|
800
|
-
def surface_for
|
|
872
|
+
def surface_for(out, answer)
|
|
801
873
|
return answer unless out.data_type == CA_OBJECT && answer.is_a?(CArray) && answer.face?
|
|
802
874
|
answer.to_a
|
|
803
875
|
end
|
|
@@ -805,7 +877,7 @@ class CASegmentIterator < CAIterator
|
|
|
805
877
|
# A blank array of +shape+ of the same kind as +model+: its data type, or its
|
|
806
878
|
# Face. A Face that holds no new value cannot be written cell by cell, so its
|
|
807
879
|
# values are held as the surface objects they are.
|
|
808
|
-
def blank_like
|
|
880
|
+
def blank_like(model, shape)
|
|
809
881
|
if model.face? && !model.__send__(:holds_new_values?)
|
|
810
882
|
CArray.new(CA_OBJECT, shape)
|
|
811
883
|
else
|
|
@@ -815,7 +887,7 @@ class CASegmentIterator < CAIterator
|
|
|
815
887
|
|
|
816
888
|
# The values as they were when the iterator was built, in source order.
|
|
817
889
|
# Every no-axis reduction works from the contiguous copy taken then; the
|
|
818
|
-
# scans read @
|
|
890
|
+
# scans read @source, so a write through the source between two calls used to
|
|
819
891
|
# be visible to a cumsum and not to a sum, off one iterator.
|
|
820
892
|
#
|
|
821
893
|
# Rebuilt rather than copied a second time: @perm is exactly the classified
|
|
@@ -826,7 +898,7 @@ class CASegmentIterator < CAIterator
|
|
|
826
898
|
def scan_source
|
|
827
899
|
@scan_source ||=
|
|
828
900
|
begin
|
|
829
|
-
snap = @
|
|
901
|
+
snap = @source.template
|
|
830
902
|
snap.reshape(snap.elements)[perm] = grouped
|
|
831
903
|
snap
|
|
832
904
|
end
|
|
@@ -847,7 +919,7 @@ class CASegmentIterator < CAIterator
|
|
|
847
919
|
# first time one of them asks, since the reductions do not need it.
|
|
848
920
|
def codes
|
|
849
921
|
@codes ||= begin
|
|
850
|
-
c = CArray.int64(@
|
|
922
|
+
c = CArray.int64(@source.elements).fill(-1)
|
|
851
923
|
if grouped.elements > 0
|
|
852
924
|
c[@origin...(@origin + grouped.elements)] =
|
|
853
925
|
CArray.segment_index(offsets: @bounds)
|
|
@@ -863,7 +935,9 @@ class CASegmentIterator < CAIterator
|
|
|
863
935
|
# cells, so wg lines up with @grouped.
|
|
864
936
|
# The weighted kernel reads float64 weights; the per-segment fallback hands
|
|
865
937
|
# them to the core as they are, so an object weight stays exact.
|
|
866
|
-
def scatter_weights
|
|
938
|
+
def scatter_weights(weights, as_float64 = true)
|
|
939
|
+
CArray.send(:refuse_ruby_array, "wsum/wmean", "a weight array") if weights.is_a?(Array)
|
|
940
|
+
layout = grouped # raises where only the axis: form answers
|
|
867
941
|
unless weights.elements == codes.elements
|
|
868
942
|
raise ArgumentError,
|
|
869
943
|
"wsum/wmean: weights.elements (#{weights.elements}) != " \
|
|
@@ -873,14 +947,14 @@ class CASegmentIterator < CAIterator
|
|
|
873
947
|
wf = wf.reshape(wf.elements)
|
|
874
948
|
# An empty index into an empty source is out of range, so take the empty
|
|
875
949
|
# layout directly.
|
|
876
|
-
|
|
950
|
+
layout.elements > 0 ? wf[perm].copy : wf[0...0].copy
|
|
877
951
|
end
|
|
878
952
|
|
|
879
953
|
# Map a per-segment index to the flat source address via the
|
|
880
954
|
# permutation (grouped slot -> source index). The min/max sits at grouped slot
|
|
881
955
|
# offsets[c] + local_index[c]; perm carries it back to the source. Empty
|
|
882
956
|
# segments (masked local index) stay masked.
|
|
883
|
-
def segment_addr
|
|
957
|
+
def segment_addr(local_index)
|
|
884
958
|
out = CArray.int64(@k)
|
|
885
959
|
@k.times do |c|
|
|
886
960
|
out[c] = local_index.is_masked[c] ? UNDEF
|
|
@@ -892,7 +966,7 @@ class CASegmentIterator < CAIterator
|
|
|
892
966
|
# Fused per-segment weighted sum + weighted mean (one C pass over the grouped
|
|
893
967
|
# copy, weights in segment order). Returns [wsum, wmean]; wmean is masked where a
|
|
894
968
|
# segment has no present (value AND weight) pair. Numeric value data types only.
|
|
895
|
-
def kernel_weighted
|
|
969
|
+
def kernel_weighted(wg)
|
|
896
970
|
ws = CArray.float64(@k)
|
|
897
971
|
wm = CArray.float64(@k)
|
|
898
972
|
grouped.send(:__reduceat_wsum_wmean__, @offsets, wg, ws, wm)
|
|
@@ -902,7 +976,7 @@ class CASegmentIterator < CAIterator
|
|
|
902
976
|
# Per-segment weighted fallback for the value data types the kernel does not
|
|
903
977
|
# read (object): delegate each segment to CArray#wsum / #wmean, answering in
|
|
904
978
|
# the data type the core answers in. Empty segments take the given identity.
|
|
905
|
-
def fold_weighted
|
|
979
|
+
def fold_weighted(op, wg, empty)
|
|
906
980
|
out = CArray.new(core_reduce_type(op, CA_FLOAT64([[1.0]])), [@k])
|
|
907
981
|
@k.times do |c|
|
|
908
982
|
lo, hi = @bounds[c], @bounds[c + 1]
|
|
@@ -925,9 +999,9 @@ class CASegmentIterator < CAIterator
|
|
|
925
999
|
# and 2000 for :last (1M cells on an M2 Max).
|
|
926
1000
|
EDGE_KERNEL_MAX_LENGTH = { first: 300, last: 2000 }.freeze
|
|
927
1001
|
|
|
928
|
-
def edge_values
|
|
1002
|
+
def edge_values(op)
|
|
929
1003
|
n = grouped.elements
|
|
930
|
-
if n.zero? || (@
|
|
1004
|
+
if n.zero? || (@source.face? && ! face_output?) || ! CArray.__fast_paths_enabled__(:segment_edge_kernel) ||
|
|
931
1005
|
n >= EDGE_KERNEL_MAX_LENGTH.fetch(op) * @k
|
|
932
1006
|
return per_segment(core_reduce_kind(op)) { |s| s.public_send(op) }
|
|
933
1007
|
end
|
|
@@ -937,7 +1011,7 @@ class CASegmentIterator < CAIterator
|
|
|
937
1011
|
end
|
|
938
1012
|
lo, hi = @bounds[0...-1], @bounds[1..-1]
|
|
939
1013
|
if op == :first
|
|
940
|
-
nearest = slot.
|
|
1014
|
+
nearest = slot.flip.cummin.flip
|
|
941
1015
|
at = nearest[lo.pmin(n - 1)]
|
|
942
1016
|
else
|
|
943
1017
|
nearest = slot.cummax
|
|
@@ -954,9 +1028,10 @@ class CASegmentIterator < CAIterator
|
|
|
954
1028
|
# segment (zero-width segment) yields the shared empty array — a zero-length
|
|
955
1029
|
# slice cannot be taken directly, and an empty array carries the same reduction
|
|
956
1030
|
# contract we want (identity for sum, UNDEF for ratios).
|
|
957
|
-
def segment_slice
|
|
1031
|
+
def segment_slice(c)
|
|
1032
|
+
g = grouped # raises for a per-fiber-only iterator
|
|
958
1033
|
lo, hi = @bounds[c], @bounds[c + 1]
|
|
959
|
-
hi > lo ?
|
|
1034
|
+
hi > lo ? g[lo...hi] : @empty
|
|
960
1035
|
end
|
|
961
1036
|
|
|
962
1037
|
# Single-pass reduceat moments (count / sum / min / max per segment), computed
|
|
@@ -974,7 +1049,7 @@ class CASegmentIterator < CAIterator
|
|
|
974
1049
|
# core's answer for each group; a development build run with
|
|
975
1050
|
# CARRAY_GENERAL_PATH set reduces every group by the core instead, which
|
|
976
1051
|
# is how the two are compared.
|
|
977
|
-
def kernel_reads?
|
|
1052
|
+
def kernel_reads?(data_type = grouped.data_type)
|
|
978
1053
|
MONOID_TYPES.include?(data_type) && CArray.__fast_paths_enabled__(:segment_reduce_kernel)
|
|
979
1054
|
end
|
|
980
1055
|
|
|
@@ -1026,11 +1101,11 @@ class CASegmentIterator < CAIterator
|
|
|
1026
1101
|
# type but a boolean widens, `prod` on an object stays an object). A payload
|
|
1027
1102
|
# the core refuses to fold this way raises here, with the core's own error.
|
|
1028
1103
|
# Probes the core with a one-cell array of the value's data type and takes
|
|
1029
|
-
# the answer's. Asks @
|
|
1104
|
+
# the answer's. Asks @source rather than the grouped copy, which has the same
|
|
1030
1105
|
# data type but does not exist on an iterator that answers only the
|
|
1031
1106
|
# per-fiber form -- and `accumulate(axis:)`, the one axis: member that needs
|
|
1032
1107
|
# this, is exactly the case that would have found it missing.
|
|
1033
|
-
def core_reduce_type
|
|
1108
|
+
def core_reduce_type(op, *args)
|
|
1034
1109
|
kind = core_reduce_kind(op, *args)
|
|
1035
1110
|
kind ? kind.data_type : CA_OBJECT
|
|
1036
1111
|
end
|
|
@@ -1038,10 +1113,10 @@ class CASegmentIterator < CAIterator
|
|
|
1038
1113
|
# The core's answer itself for the one-cell probe, or nil for a Face with no
|
|
1039
1114
|
# cell to probe. Its class is the one the per-segment answers come in, which
|
|
1040
1115
|
# for a Face need not be the values' own (a CATime's spread is a CATimedelta).
|
|
1041
|
-
def core_reduce_kind
|
|
1116
|
+
def core_reduce_kind(op, *args)
|
|
1042
1117
|
memo = (@core_reduce_kind ||= {})
|
|
1043
1118
|
return memo[[op, args]] if memo.key?([op, args])
|
|
1044
|
-
probe = CAIterator.core_probe
|
|
1119
|
+
probe = CAIterator.send(:core_probe, @source)
|
|
1045
1120
|
memo[[op, args]] = probe && probe.public_send(op, *args, axis: 1)
|
|
1046
1121
|
end
|
|
1047
1122
|
|
|
@@ -1050,7 +1125,45 @@ class CASegmentIterator < CAIterator
|
|
|
1050
1125
|
# into; another -- a const string's records index a shared pool, a
|
|
1051
1126
|
# categorical's codes index a vocabulary -- has no blank form to fill.
|
|
1052
1127
|
def face_output?
|
|
1053
|
-
@
|
|
1128
|
+
@source.face? && @source.__send__(:holds_new_values?)
|
|
1129
|
+
end
|
|
1130
|
+
|
|
1131
|
+
# Whether the value is an integer array, whose sum and product the core
|
|
1132
|
+
# answers in int64 / uint64 (float64 past that range).
|
|
1133
|
+
def integer_payload?
|
|
1134
|
+
@source.integer? && ! @source.face?
|
|
1135
|
+
end
|
|
1136
|
+
|
|
1137
|
+
# Below 2**53 every integer is a float64, and a float64 total of integers
|
|
1138
|
+
# whose partial totals stay below it is exact.
|
|
1139
|
+
EXACT_IN_FLOAT64 = 2.0 ** 53
|
|
1140
|
+
|
|
1141
|
+
# The core's sum or product of an integer value, from a fused kernel's
|
|
1142
|
+
# float64 answer +out+. The kernel's answer is exact while every partial
|
|
1143
|
+
# total stays below 2**53. For a sum, the largest magnitude in a piece
|
|
1144
|
+
# times its count bounds the partial totals; for a product of integers, the
|
|
1145
|
+
# partial products only grow in magnitude (until a factor 0 makes them 0),
|
|
1146
|
+
# so the answer bounds them. Within the bound the answer is read back in
|
|
1147
|
+
# the core's integer type; beyond it, the block answers piece by piece
|
|
1148
|
+
# through the core.
|
|
1149
|
+
def integer_total(op, out, min: nil, max: nil, count: nil)
|
|
1150
|
+
exact = if op == :sum
|
|
1151
|
+
(max.float64.abs.pmax(min.float64.abs) * count.float64).lt(EXACT_IN_FLOAT64).all
|
|
1152
|
+
else
|
|
1153
|
+
out.abs.lt(EXACT_IN_FLOAT64).all
|
|
1154
|
+
end
|
|
1155
|
+
exact ? out.to_type(core_reduce_type(op)) : yield
|
|
1156
|
+
end
|
|
1157
|
+
|
|
1158
|
+
# An integer value's sum or product, each segment folded by the core: the
|
|
1159
|
+
# answers are integers, or floats where one is out of range, and the output
|
|
1160
|
+
# is float64 if any of them is.
|
|
1161
|
+
def per_segment_total(op)
|
|
1162
|
+
answers = Array.new(@k) { |c| segment_slice(c).public_send(op) }
|
|
1163
|
+
type = answers.all?(Integer) ? core_reduce_type(op) : CA_FLOAT64
|
|
1164
|
+
out = CArray.new(type, [@k])
|
|
1165
|
+
answers.each_with_index { |v, c| out[c] = v }
|
|
1166
|
+
out
|
|
1054
1167
|
end
|
|
1055
1168
|
|
|
1056
1169
|
# Build a length-k typed output by folding each segment's members with the
|
|
@@ -1058,15 +1171,15 @@ class CASegmentIterator < CAIterator
|
|
|
1058
1171
|
# non-numeric value data type): each segment is delegated to the same CArray
|
|
1059
1172
|
# reduction, so the per-segment result matches `CArray#<reduction>` over that
|
|
1060
1173
|
# segment's members — the mask carries the "insufficient present data" contract
|
|
1061
|
-
#
|
|
1174
|
+
# with no extra code (an all-masked segment reduces like an empty one; identity-bearing
|
|
1062
1175
|
# reductions return their identity, ratios return UNDEF).
|
|
1063
|
-
def per_segment
|
|
1176
|
+
def per_segment(kind)
|
|
1064
1177
|
data_type = kind.is_a?(CArray) ? kind.data_type : kind
|
|
1065
1178
|
out = if kind.nil?
|
|
1066
1179
|
CArray.new(CA_OBJECT, [@k])
|
|
1067
1180
|
elsif ! (kind.is_a?(CArray) && kind.face?)
|
|
1068
1181
|
bytes = if kind.is_a?(CArray) then kind.bytes
|
|
1069
|
-
elsif data_type == @
|
|
1182
|
+
elsif data_type == @source.data_type then @source.bytes
|
|
1070
1183
|
else 0 end
|
|
1071
1184
|
CArray.new(data_type, [@k], bytes: bytes)
|
|
1072
1185
|
elsif kind.__send__(:holds_new_values?)
|
|
@@ -1087,67 +1200,67 @@ class CASegmentIterator < CAIterator
|
|
|
1087
1200
|
|
|
1088
1201
|
# ---- no axis: form -------------------------------------------------------
|
|
1089
1202
|
|
|
1090
|
-
def axis_by_core
|
|
1203
|
+
def axis_by_core(*)
|
|
1091
1204
|
raise NotImplementedError, "#{self.class} has no axis: form"
|
|
1092
1205
|
end
|
|
1093
1206
|
|
|
1094
|
-
def axis_by_masked_copy
|
|
1207
|
+
def axis_by_masked_copy(*)
|
|
1095
1208
|
raise NotImplementedError, "#{self.class} has no axis: form"
|
|
1096
1209
|
end
|
|
1097
1210
|
|
|
1098
|
-
def axis_counts
|
|
1211
|
+
def axis_counts(*)
|
|
1099
1212
|
raise NotImplementedError, "#{self.class} has no axis: form"
|
|
1100
1213
|
end
|
|
1101
1214
|
|
|
1102
|
-
def axis_mean
|
|
1215
|
+
def axis_mean(*)
|
|
1103
1216
|
raise NotImplementedError, "#{self.class} has no axis: form"
|
|
1104
1217
|
end
|
|
1105
1218
|
|
|
1106
|
-
def axis_moments
|
|
1219
|
+
def axis_moments(*)
|
|
1107
1220
|
raise NotImplementedError, "#{self.class} has no axis: form"
|
|
1108
1221
|
end
|
|
1109
1222
|
|
|
1110
|
-
def axis_order_stat
|
|
1223
|
+
def axis_order_stat(*)
|
|
1111
1224
|
raise NotImplementedError, "#{self.class} has no axis: form"
|
|
1112
1225
|
end
|
|
1113
1226
|
|
|
1114
|
-
def axis_scan
|
|
1227
|
+
def axis_scan(*)
|
|
1115
1228
|
raise NotImplementedError, "#{self.class} has no axis: form"
|
|
1116
1229
|
end
|
|
1117
1230
|
|
|
1118
|
-
def axis_lag
|
|
1231
|
+
def axis_lag(*)
|
|
1119
1232
|
raise NotImplementedError, "#{self.class} has no axis: form"
|
|
1120
1233
|
end
|
|
1121
1234
|
|
|
1122
|
-
def
|
|
1235
|
+
def axis_rank_index(*)
|
|
1123
1236
|
raise NotImplementedError, "#{self.class} has no axis: form"
|
|
1124
1237
|
end
|
|
1125
1238
|
|
|
1126
|
-
def axis_hold
|
|
1239
|
+
def axis_hold(*)
|
|
1127
1240
|
raise NotImplementedError, "#{self.class} has no axis: form"
|
|
1128
1241
|
end
|
|
1129
1242
|
|
|
1130
|
-
def axis_map
|
|
1243
|
+
def axis_map(*)
|
|
1131
1244
|
raise NotImplementedError, "#{self.class} has no axis: form"
|
|
1132
1245
|
end
|
|
1133
1246
|
|
|
1134
|
-
def axis_prod
|
|
1247
|
+
def axis_prod(*)
|
|
1135
1248
|
raise NotImplementedError, "#{self.class} has no axis: form"
|
|
1136
1249
|
end
|
|
1137
1250
|
|
|
1138
|
-
def axis_sum
|
|
1251
|
+
def axis_sum(*)
|
|
1139
1252
|
raise NotImplementedError, "#{self.class} has no axis: form"
|
|
1140
1253
|
end
|
|
1141
1254
|
|
|
1142
|
-
def axis_wsum_wmean
|
|
1255
|
+
def axis_wsum_wmean(*)
|
|
1143
1256
|
raise NotImplementedError, "#{self.class} has no axis: form"
|
|
1144
1257
|
end
|
|
1145
1258
|
|
|
1146
1259
|
# The present cells per segment a weighted reduction counts: those whose
|
|
1147
1260
|
# value and weight are both present, as CArray#wsum / #wmean count them.
|
|
1148
|
-
def present_pair_count
|
|
1261
|
+
def present_pair_count(weights, axis)
|
|
1149
1262
|
if axis
|
|
1150
|
-
paired = @
|
|
1263
|
+
paired = @source.copy
|
|
1151
1264
|
paired[weights.is_masked] = UNDEF if weights.has_mask?
|
|
1152
1265
|
return paired.group_by_category(@cat).count_not_masked(axis: axis)
|
|
1153
1266
|
end
|
|
@@ -1169,7 +1282,7 @@ class CASegmentIterator < CAIterator
|
|
|
1169
1282
|
count count_not_masked count_masked].freeze
|
|
1170
1283
|
|
|
1171
1284
|
# A count of the mask reads every cell of the segment, as the core's does.
|
|
1172
|
-
def self.mask_count?
|
|
1285
|
+
def self.mask_count?(op, args)
|
|
1173
1286
|
op == :count_not_masked || op == :count_masked ||
|
|
1174
1287
|
(op == :count && (args.empty? || args[0].equal?(UNDEF)))
|
|
1175
1288
|
end
|
|
@@ -1179,7 +1292,16 @@ class CASegmentIterator < CAIterator
|
|
|
1179
1292
|
# The segment reduction already leaves an empty segment as the core does,
|
|
1180
1293
|
# so only a min_count asking for more is applied here. The reduction the
|
|
1181
1294
|
# member is made of is the block (super).
|
|
1182
|
-
private def reduce_counted
|
|
1295
|
+
private def reduce_counted(op, args, kw, &block)
|
|
1296
|
+
if op == :percentile
|
|
1297
|
+
return CAIterator.send(:percentile_in_form, args) { |p|
|
|
1298
|
+
reduce_counted_one(op, [p], kw.dup, &block)
|
|
1299
|
+
}
|
|
1300
|
+
end
|
|
1301
|
+
reduce_counted_one(op, args, kw, &block)
|
|
1302
|
+
end
|
|
1303
|
+
|
|
1304
|
+
private def reduce_counted_one(op, args, kw)
|
|
1183
1305
|
min_count = kw.delete(:min_count)
|
|
1184
1306
|
fill_value = kw.delete(:fill_value)
|
|
1185
1307
|
need = CArray.__present_cells_needed_per_result_cell__(op, min_count, 0)
|
|
@@ -1202,7 +1324,7 @@ class CASegmentIterator < CAIterator
|
|
|
1202
1324
|
if short
|
|
1203
1325
|
(result.is_a?(Array) ? result : [result]).each { |r| r[short] = UNDEF }
|
|
1204
1326
|
end
|
|
1205
|
-
CArray.__fill_undefined_result_cells__(result, fill_value)
|
|
1327
|
+
CArray.__fill_undefined_result_cells__(result, fill_value, op.to_sym)
|
|
1206
1328
|
end
|
|
1207
1329
|
end
|
|
1208
1330
|
prepend ReductionCountAndFill
|
|
@@ -1212,8 +1334,8 @@ class CASegmentIterator < CAIterator
|
|
|
1212
1334
|
# place, so the result shows the source's attributes, as CArray#lag and
|
|
1213
1335
|
# CArray#strip_mask(method:) do.
|
|
1214
1336
|
prepend(Module.new do
|
|
1215
|
-
def lag
|
|
1216
|
-
def strip_mask
|
|
1337
|
+
def lag(*args, **keywords) = @source.__send__(:carry_attrs, super)
|
|
1338
|
+
def strip_mask(**keywords) = @source.__send__(:carry_attrs, super)
|
|
1217
1339
|
end)
|
|
1218
1340
|
end
|
|
1219
1341
|
|
|
@@ -1237,7 +1359,7 @@ class CArray
|
|
|
1237
1359
|
# @raise [ArgumentError] for decreasing or empty offsets, a negative or
|
|
1238
1360
|
# masked length, segments reaching outside `self`, or unless exactly one
|
|
1239
1361
|
# of `offsets:` and `lengths:` is given.
|
|
1240
|
-
def segments
|
|
1362
|
+
def segments(offsets: nil, lengths: nil)
|
|
1241
1363
|
CASegmentIterator.new(self, offsets: offsets, lengths: lengths)
|
|
1242
1364
|
end
|
|
1243
1365
|
end
|