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
data/lib/carray/bincount_nd.rb
CHANGED
|
@@ -34,7 +34,7 @@
|
|
|
34
34
|
#
|
|
35
35
|
# full_counts shape = fiber_shape + (L_0 + 1, ..., L_{M-1} + 1)
|
|
36
36
|
# counts = full_counts[..., 0...L_0, ..., 0...L_{M-1}]
|
|
37
|
-
#
|
|
37
|
+
# over(axis: k) = samples whose dim-k label was >= L_k (marginal)
|
|
38
38
|
#
|
|
39
39
|
# Weighted accumulation, streaming `add`, `+` composition and mask handling
|
|
40
40
|
# all follow histogram: a sample is dropped iff any of its channels is masked
|
|
@@ -42,8 +42,7 @@
|
|
|
42
42
|
# label NaN cannot occur (float label arrays are rejected); weight NaN is
|
|
43
43
|
# skipped like histogram.
|
|
44
44
|
#
|
|
45
|
-
# NOTE (implementation): a layout-dependent hybrid,
|
|
46
|
-
# (devel/bench_bincount_nd_gate.rb):
|
|
45
|
+
# NOTE (implementation): a layout-dependent hybrid, chosen by benchmark:
|
|
47
46
|
# - FLAT (no fiber): Ruby `ravel + bincount`. The discrete "binning" is a
|
|
48
47
|
# cheap, vectorisable ravel, so the separate vectorised ravel + the tuned
|
|
49
48
|
# `bincount` kernel beats fusing it into a scalar scatter loop.
|
|
@@ -77,7 +76,7 @@ class CArray
|
|
|
77
76
|
# @param weights_data_type [Symbol, nil] `data_type` for weighted
|
|
78
77
|
# accumulators; `nil` for pure counts (int64).
|
|
79
78
|
# @return [BincountND]
|
|
80
|
-
def initialize
|
|
79
|
+
def initialize(lengths:, fiber_shape: [], weights_data_type: nil)
|
|
81
80
|
raise ArgumentError, "lengths must be a non-empty list" if lengths.empty?
|
|
82
81
|
lengths.each_with_index do |l, k|
|
|
83
82
|
unless l.is_a?(Integer)
|
|
@@ -100,7 +99,12 @@ class CArray
|
|
|
100
99
|
end
|
|
101
100
|
private_class_method :new
|
|
102
101
|
|
|
103
|
-
attr_reader :
|
|
102
|
+
attr_reader :fiber_shape, :full_counts
|
|
103
|
+
|
|
104
|
+
# Read by #+ from the other accumulator. The lengths are counts.shape
|
|
105
|
+
# after fiber_shape.
|
|
106
|
+
attr_reader :m, :lengths
|
|
107
|
+
protected :m, :lengths
|
|
104
108
|
|
|
105
109
|
# @overload counts
|
|
106
110
|
# Returns the in-range counts view with shape
|
|
@@ -115,7 +119,7 @@ class CArray
|
|
|
115
119
|
# Upper-overflow marginal on dim `axis` (= samples whose dim-axis label
|
|
116
120
|
# was >= length[axis]); other dims marginalised. shape = fiber_shape.
|
|
117
121
|
# For M=1, axis: may be omitted.
|
|
118
|
-
# @overload
|
|
122
|
+
# @overload over(axis: nil)
|
|
119
123
|
# Returns the upper-overflow marginal on dimension `axis`
|
|
120
124
|
# (samples whose dim-axis label was `>= lengths[axis]`);
|
|
121
125
|
# other dimensions are marginalised. For 1-D accumulators
|
|
@@ -123,9 +127,9 @@ class CArray
|
|
|
123
127
|
# @param axis [Integer, nil] dimension to marginalise.
|
|
124
128
|
# @return [CArray]
|
|
125
129
|
# @raise [ArgumentError] when `axis` is required but omitted.
|
|
126
|
-
def
|
|
130
|
+
def over(axis: nil)
|
|
127
131
|
raise ArgumentError, "axis: keyword required (M=#{@m})" if axis.nil? && @m > 1
|
|
128
|
-
ax = axis.nil? ? 0 : CArray.normalize_axis(axis, @m, "
|
|
132
|
+
ax = axis.nil? ? 0 : CArray.normalize_axis(axis, @m, "over")
|
|
129
133
|
base = [nil] * @fiber_shape.size
|
|
130
134
|
bin_idx = (0...@m).map { |k| k == ax ? @lengths[k] : nil } # overflow cell on ax
|
|
131
135
|
slice = @full_counts[*(base + bin_idx)]
|
|
@@ -133,6 +137,17 @@ class CArray
|
|
|
133
137
|
slice
|
|
134
138
|
end
|
|
135
139
|
|
|
140
|
+
# @overload inspect
|
|
141
|
+
# Returns the class, the data type and shape of {#counts}, and
|
|
142
|
+
# either {#fiber_shape} or, with no fiber axes, the sample {#total}.
|
|
143
|
+
# The counts themselves are not shown.
|
|
144
|
+
# @return [String]
|
|
145
|
+
def inspect
|
|
146
|
+
c = counts
|
|
147
|
+
tail = @fiber_shape.empty? ? "total=#{total}" : "fiber_shape=#{@fiber_shape}"
|
|
148
|
+
"#<#{self.class} counts=#{c.data_type_name}#{c.shape} #{tail}>"
|
|
149
|
+
end
|
|
150
|
+
|
|
136
151
|
# @overload total
|
|
137
152
|
# Returns the per-fiber sample total (in-range plus overflow)
|
|
138
153
|
# with shape `fiber_shape`.
|
|
@@ -141,11 +156,12 @@ class CArray
|
|
|
141
156
|
sum_along_bin_axes(@full_counts)
|
|
142
157
|
end
|
|
143
158
|
|
|
144
|
-
# @overload
|
|
159
|
+
# @overload outlier_total
|
|
145
160
|
# Returns the per-fiber count of samples that overflowed on
|
|
146
|
-
# any dimension.
|
|
161
|
+
# any dimension. The name is the one Histogram uses; a label here
|
|
162
|
+
# can only fall out of range above.
|
|
147
163
|
# @return [CArray]
|
|
148
|
-
def
|
|
164
|
+
def outlier_total
|
|
149
165
|
sum_along_bin_axes(@full_counts) - sum_along_bin_axes(counts)
|
|
150
166
|
end
|
|
151
167
|
|
|
@@ -163,7 +179,7 @@ class CArray
|
|
|
163
179
|
# @return [self]
|
|
164
180
|
# @raise [ArgumentError] on shape / axis / label / weighted
|
|
165
181
|
# mismatch.
|
|
166
|
-
def add
|
|
182
|
+
def add(chunk, axis: nil, weights: nil)
|
|
167
183
|
# Keep the labels in their native integer type (no int64 coercion): an
|
|
168
184
|
# int32 label array stays int32 through the ravel, and `bincount` picks
|
|
169
185
|
# a uint32 output when the table fits. Forcing int64 would materialise
|
|
@@ -214,6 +230,7 @@ class CArray
|
|
|
214
230
|
|
|
215
231
|
if weights
|
|
216
232
|
raise ArgumentError, "weights given but accumulator is unweighted" unless @weighted
|
|
233
|
+
CArray.send(:refuse_ruby_array, "bincount_nd", "a weight array") if weights.is_a?(Array)
|
|
217
234
|
# coerce: the accumulator's type, declared by weights_data_type:
|
|
218
235
|
weights = CArray.wrap_readonly(weights, @counts_data_type)
|
|
219
236
|
expected_w_shape = chunk.shape.dup
|
|
@@ -234,7 +251,7 @@ class CArray
|
|
|
234
251
|
# ~4.4 vs ~1.6 ns/sample). With fibers we loop one small ravel+bincount
|
|
235
252
|
# per fiber so each fiber's counts slice stays L1-resident, rather than
|
|
236
253
|
# one giant bincount over the whole F*total_ext table (which is cache-
|
|
237
|
-
# cold and ~1.7x slower).
|
|
254
|
+
# cold and ~1.7x slower).
|
|
238
255
|
ext_sizes = @lengths.map { |l| l + 1 } # +1: upper overflow cell
|
|
239
256
|
strides_ext = ext_sizes.each_with_index.map { |_, k| ext_sizes[(k + 1)..].inject(1, :*) }
|
|
240
257
|
total_ext = ext_sizes.inject(:*)
|
|
@@ -305,7 +322,7 @@ class CArray
|
|
|
305
322
|
# @param other [BincountND] compatible accumulator.
|
|
306
323
|
# @return [BincountND]
|
|
307
324
|
# @raise [ArgumentError] when structure does not match.
|
|
308
|
-
def +
|
|
325
|
+
def +(other)
|
|
309
326
|
raise ArgumentError, "type mismatch" unless other.is_a?(BincountND)
|
|
310
327
|
raise ArgumentError, "M mismatch" unless @m == other.m
|
|
311
328
|
raise ArgumentError, "lengths mismatch" unless @lengths == other.lengths
|
|
@@ -331,7 +348,7 @@ class CArray
|
|
|
331
348
|
|
|
332
349
|
private
|
|
333
350
|
|
|
334
|
-
def sum_along_bin_axes
|
|
351
|
+
def sum_along_bin_axes(arr)
|
|
335
352
|
out = arr
|
|
336
353
|
@m.times { out = out.accumulate(axis: out.ndim - 1) }
|
|
337
354
|
out
|
|
@@ -354,7 +371,7 @@ class CArray
|
|
|
354
371
|
# axis pair.
|
|
355
372
|
# @param weights [CArray, nil] optional per-sample weights.
|
|
356
373
|
# @return [BincountND]
|
|
357
|
-
def bincount_nd
|
|
374
|
+
def bincount_nd(lengths:, axis: [-2, -1], weights: nil)
|
|
358
375
|
raise ArgumentError, "lengths must be an Array of per-dim extents" unless lengths.is_a?(Array)
|
|
359
376
|
axis = [-2, -1] if axis.nil?
|
|
360
377
|
unless axis.is_a?(Array) && axis.size == 2
|
|
@@ -24,7 +24,7 @@
|
|
|
24
24
|
# padded entity is built (unlike CAWindowIterator) and the interior stays on
|
|
25
25
|
# the source buffer.
|
|
26
26
|
#
|
|
27
|
-
# The remainder is not the user's concern. There is no boundary-policy
|
|
27
|
+
# The remainder is not the user's concern. There is no boundary-policy option:
|
|
28
28
|
# the sole behaviour is full coverage — every cell belongs to a tile, the
|
|
29
29
|
# edge tiles simply fold whatever cells are present (present-only), and the
|
|
30
30
|
# output is the ceil tile grid. A partial edge tile carries fewer real cells,
|
|
@@ -59,14 +59,13 @@ class CABlockIterator < CAIterator
|
|
|
59
59
|
|
|
60
60
|
# @overload initialize(source, *blocks)
|
|
61
61
|
# Builds a block iterator tiling `source` with a per-axis tile size. Each
|
|
62
|
-
# `blocks[i]` is
|
|
63
|
-
#
|
|
64
|
-
#
|
|
65
|
-
# argument is taken as the per-axis size list.
|
|
62
|
+
# `blocks[i]` is an Integer tile size; the tiles start at index 0. A
|
|
63
|
+
# single Array argument is taken as the per-axis size list. To start the
|
|
64
|
+
# tiles elsewhere, slice first: `a[2..-1].blocks(3)`.
|
|
66
65
|
#
|
|
67
66
|
# @param source [CArray] the array to tile.
|
|
68
|
-
# @param blocks [Array<Integer
|
|
69
|
-
def initialize
|
|
67
|
+
# @param blocks [Array<Integer>] per-axis tile sizes.
|
|
68
|
+
def initialize(source, *blocks)
|
|
70
69
|
blocks = blocks[0] if blocks.size == 1 && blocks[0].is_a?(Array)
|
|
71
70
|
|
|
72
71
|
unless blocks.size == source.ndim
|
|
@@ -76,23 +75,20 @@ class CABlockIterator < CAIterator
|
|
|
76
75
|
end
|
|
77
76
|
# Each tile is read through a view with one axis per tile position and
|
|
78
77
|
# one per cell in the tile, twice the source's rank.
|
|
79
|
-
if 2 * source.ndim >
|
|
78
|
+
if 2 * source.ndim > CArray::RANK_MAX
|
|
80
79
|
raise ArgumentError,
|
|
81
80
|
"blocks: a rank #{source.ndim} array would need #{2 * source.ndim} " \
|
|
82
|
-
"axes for its tiles, beyond the limit of #{
|
|
83
|
-
"blocks works up to rank #{
|
|
81
|
+
"axes for its tiles, beyond the limit of #{CArray::RANK_MAX}; " \
|
|
82
|
+
"blocks works up to rank #{CArray::RANK_MAX / 2}"
|
|
84
83
|
end
|
|
85
84
|
|
|
86
85
|
@sndim = source.ndim
|
|
87
|
-
offsets = Array.new(@sndim, 0)
|
|
88
86
|
@sizes = Array.new(@sndim)
|
|
89
87
|
blocks.each_with_index do |b, i|
|
|
90
88
|
if b.is_a?(Range)
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
offsets[i] = b.begin
|
|
95
|
-
@sizes[i] = b.end - b.begin + (b.exclude_end? ? 0 : 1)
|
|
89
|
+
raise TypeError,
|
|
90
|
+
"blocks: a tile size must be an Integer (got #{b.inspect}); " \
|
|
91
|
+
"to start the tiles elsewhere, slice first: a[2..-1].blocks(3)"
|
|
96
92
|
elsif b.is_a?(Integer)
|
|
97
93
|
@sizes[i] = b
|
|
98
94
|
else
|
|
@@ -103,16 +99,8 @@ class CABlockIterator < CAIterator
|
|
|
103
99
|
end
|
|
104
100
|
end
|
|
105
101
|
|
|
106
|
-
#
|
|
107
|
-
|
|
108
|
-
@source =
|
|
109
|
-
if source.scalar?
|
|
110
|
-
source.flatten # a CScalar takes no index; its one-cell view does
|
|
111
|
-
elsif offsets.all?(&:zero?)
|
|
112
|
-
source
|
|
113
|
-
else
|
|
114
|
-
source[*offsets.map { |o| o..-1 }]
|
|
115
|
-
end
|
|
102
|
+
# A CScalar takes no index; its one-cell view does.
|
|
103
|
+
@source = source.scalar? ? source.flatten : source
|
|
116
104
|
|
|
117
105
|
n = @source.shape
|
|
118
106
|
@q = @sndim.times.map { |i| n[i] / @sizes[i] } # full tiles per axis
|
|
@@ -127,11 +115,6 @@ class CABlockIterator < CAIterator
|
|
|
127
115
|
self
|
|
128
116
|
end
|
|
129
117
|
|
|
130
|
-
# @overload source
|
|
131
|
-
# Returns the array being tiled (with any leading offset already applied).
|
|
132
|
-
# @return [CArray]
|
|
133
|
-
attr_reader :source
|
|
134
|
-
|
|
135
118
|
# ---- region decomposition ---------------------------------------------
|
|
136
119
|
#
|
|
137
120
|
# The tile grid splits into regions by whether each axis sits on its interior
|
|
@@ -213,17 +196,17 @@ class CABlockIterator < CAIterator
|
|
|
213
196
|
# assembling the Face itself would copy its surface bytes into an array that
|
|
214
197
|
# no longer knows it is one. A CAConstString's storage points into its own
|
|
215
198
|
# buffer, which the regions do not share, so it is assembled as strings.
|
|
216
|
-
def face_grid_part
|
|
199
|
+
def face_grid_part(r)
|
|
217
200
|
r.is_a?(CAConstString) ? r.to_string.parent : r.parent
|
|
218
201
|
end
|
|
219
202
|
|
|
220
|
-
def face_grid
|
|
203
|
+
def face_grid(grid, face)
|
|
221
204
|
return CArray.const_string(grid, encoding: face.encoding) if face.is_a?(CAConstString)
|
|
222
|
-
grid.face_lift
|
|
205
|
+
grid.send(:face_lift, face)
|
|
223
206
|
end
|
|
224
207
|
|
|
225
208
|
# Fold every region with a single core reduction `op` (the tier-1 shape).
|
|
226
|
-
def fold
|
|
209
|
+
def fold(op, **kw)
|
|
227
210
|
assemble { |view, _tiles| view.public_send(op, axis: @tile_axes, **kw) }
|
|
228
211
|
end
|
|
229
212
|
|
|
@@ -321,7 +304,7 @@ class CABlockIterator < CAIterator
|
|
|
321
304
|
[:sum, :accumulate, :prod, :mean, :min, :max, :variance, :stddev,
|
|
322
305
|
:variancep, :stddevp], :fold_reduction)
|
|
323
306
|
|
|
324
|
-
private def fold_reduction
|
|
307
|
+
private def fold_reduction(op, min_count: nil, fill_value: nil)
|
|
325
308
|
if FACE_WINNER_INDEX.key?(op) && @source.face?
|
|
326
309
|
return face_winner(FACE_WINNER_INDEX[op], min_count, fill_value)
|
|
327
310
|
end
|
|
@@ -333,8 +316,8 @@ class CABlockIterator < CAIterator
|
|
|
333
316
|
|
|
334
317
|
CArray.send(:define_named_methods, self, [:all, :any], :fold_with_keywords)
|
|
335
318
|
|
|
336
|
-
private def fold_with_keywords
|
|
337
|
-
fold(op, **CAIterator.reduce_keywords
|
|
319
|
+
private def fold_with_keywords(op, **kw)
|
|
320
|
+
fold(op, **CAIterator.send(:reduce_keywords, op, kw))
|
|
338
321
|
end
|
|
339
322
|
|
|
340
323
|
# @!method first
|
|
@@ -367,9 +350,9 @@ class CABlockIterator < CAIterator
|
|
|
367
350
|
# partial edge tile); `count(UNDEF)` counts masked cells; `count(v)` counts
|
|
368
351
|
# cells equal to `v`.
|
|
369
352
|
# @return [CArray] tile-grid shaped
|
|
370
|
-
def count
|
|
353
|
+
def count(*args, **kw)
|
|
371
354
|
return count_not_masked(**kw) if args.empty?
|
|
372
|
-
kw = CAIterator.
|
|
355
|
+
kw = CAIterator.send(:reduce_keywords, :count, kw)
|
|
373
356
|
# block_view is a CAStride, so #count is not shadowed; dispatch
|
|
374
357
|
# CArray#count explicitly anyway, matching the family regularity.
|
|
375
358
|
assemble { |view, _| count_cells(view, *args, axis: @tile_axes, **kw) }
|
|
@@ -379,16 +362,16 @@ class CABlockIterator < CAIterator
|
|
|
379
362
|
# Per-tile count of present (non-masked) cells. For a partial edge tile
|
|
380
363
|
# this is the real cell count (the OOB cells are not present).
|
|
381
364
|
# @return [CArray]
|
|
382
|
-
def count_not_masked
|
|
383
|
-
fold(:count_not_masked, **CAIterator.
|
|
365
|
+
def count_not_masked(**kw)
|
|
366
|
+
fold(:count_not_masked, **CAIterator.send(:reduce_keywords, :count_not_masked, kw))
|
|
384
367
|
end
|
|
385
368
|
|
|
386
369
|
# @overload count_masked
|
|
387
370
|
# Per-tile count of masked cells. Equals `elements - count_not_masked`;
|
|
388
371
|
# the OOB cells of a partial edge tile count here.
|
|
389
372
|
# @return [CArray]
|
|
390
|
-
def count_masked
|
|
391
|
-
kw = CAIterator.
|
|
373
|
+
def count_masked(**kw)
|
|
374
|
+
kw = CAIterator.send(:reduce_keywords, :count_masked, kw)
|
|
392
375
|
need = CArray.__present_cells_needed_per_result_cell__(:count_masked, kw[:min_count], 0)
|
|
393
376
|
out = elements - count_not_masked
|
|
394
377
|
# A count of the mask reads every cell of the tile.
|
|
@@ -414,7 +397,7 @@ class CABlockIterator < CAIterator
|
|
|
414
397
|
# @overload minmax(min_count: nil, fill_value: nil)
|
|
415
398
|
# Per-tile `[min, max]` (two tile-grid-shaped CArrays, a single fused pass).
|
|
416
399
|
# @return [Array<CArray>]
|
|
417
|
-
def minmax
|
|
400
|
+
def minmax(min_count: nil, fill_value: nil)
|
|
418
401
|
if @source.face?
|
|
419
402
|
return [min(min_count: min_count, fill_value: fill_value),
|
|
420
403
|
max(min_count: min_count, fill_value: fill_value)]
|
|
@@ -426,17 +409,21 @@ class CABlockIterator < CAIterator
|
|
|
426
409
|
end
|
|
427
410
|
|
|
428
411
|
# @!method min_index
|
|
429
|
-
# Per-tile position of the minimum, as a flat index within the tile
|
|
430
|
-
# partial edge tile
|
|
412
|
+
# Per-tile position of the minimum, as a flat index within the tile. A
|
|
413
|
+
# partial edge tile is indexed as a full tile, the shape {#each} yields,
|
|
414
|
+
# so every index unravels by the same tile shape.
|
|
431
415
|
# @return [CArray] tile-grid shaped
|
|
432
416
|
# @!method max_index
|
|
433
417
|
# Per-tile position of the maximum (tile-local flat index).
|
|
434
418
|
# @return [CArray]
|
|
435
419
|
[:min_index, :max_index].each do |op|
|
|
436
420
|
class_eval <<~RUBY, __FILE__, __LINE__ + 1
|
|
437
|
-
def #{op}
|
|
438
|
-
kw = CAIterator.reduce_keywords
|
|
439
|
-
|
|
421
|
+
def #{op}(**kw)
|
|
422
|
+
kw = CAIterator.send(:reduce_keywords, :#{op}, kw)
|
|
423
|
+
# fill_value: is not a position, so it is put in after the move.
|
|
424
|
+
fill = kw.delete(:fill_value)
|
|
425
|
+
out = assemble { |view, tiles| full_tile_index(view.#{op}(axis: @tile_axes, **kw), tiles) }
|
|
426
|
+
CArray.__fill_undefined_result_cells__(out, fill)
|
|
440
427
|
end
|
|
441
428
|
RUBY
|
|
442
429
|
end
|
|
@@ -447,19 +434,19 @@ class CABlockIterator < CAIterator
|
|
|
447
434
|
# minima. Unlike `min_index` (the tile-local position) this indexes back
|
|
448
435
|
# into the original array. An all-masked tile is a masked cell.
|
|
449
436
|
# @return [CArray] tile-grid shaped int64
|
|
450
|
-
def min_addr
|
|
437
|
+
def min_addr(**kw); winner_addr(:min_index, kw); end
|
|
451
438
|
|
|
452
439
|
# @overload max_addr
|
|
453
440
|
# Per-tile flat source address of the maximum. See {#min_addr}.
|
|
454
441
|
# @return [CArray] tile-grid shaped int64
|
|
455
|
-
def max_addr
|
|
442
|
+
def max_addr(**kw); winner_addr(:max_index, kw); end
|
|
456
443
|
|
|
457
444
|
private
|
|
458
445
|
|
|
459
446
|
# Per-tile minimum or maximum of a Face source, gathered from the source by
|
|
460
447
|
# the winner's address so the result is the Face itself. An all-masked tile,
|
|
461
448
|
# and with `min_count` a tile with too few present cells, is a masked cell.
|
|
462
|
-
def face_winner
|
|
449
|
+
def face_winner(idx_op, min_count, fill_value)
|
|
463
450
|
addr = winner_addr(idx_op)
|
|
464
451
|
need = CArray.__present_cells_needed_per_result_cell__(idx_op == :min_index ? :min : :max, min_count, 0)
|
|
465
452
|
addr[count_not_masked.lt(need)] = UNDEF if need > 0
|
|
@@ -485,12 +472,28 @@ class CABlockIterator < CAIterator
|
|
|
485
472
|
out
|
|
486
473
|
end
|
|
487
474
|
|
|
475
|
+
# A flat index within a cut tile of shape `tiles`, as the flat index of the
|
|
476
|
+
# same cell within a full tile. Masked indices stay masked.
|
|
477
|
+
def full_tile_index(idx, tiles)
|
|
478
|
+
return idx if tiles == @sizes
|
|
479
|
+
rest = idx
|
|
480
|
+
full = nil
|
|
481
|
+
stride = 1
|
|
482
|
+
(@sndim - 1).downto(0) do |i|
|
|
483
|
+
coord = rest % tiles[i]
|
|
484
|
+
rest = rest / tiles[i]
|
|
485
|
+
full = full.nil? ? coord * stride : full + coord * stride
|
|
486
|
+
stride *= @sizes[i]
|
|
487
|
+
end
|
|
488
|
+
full
|
|
489
|
+
end
|
|
490
|
+
|
|
488
491
|
# Per-tile source address of the winner. The tile-local flat index (min_index /
|
|
489
492
|
# max_index) is looked up in a source-address companion tiled the same way, so
|
|
490
493
|
# the tile geometry is reused rather than recomputed. An all-masked tile yields
|
|
491
494
|
# a masked index; it is gathered at 0 and re-masked in the result.
|
|
492
|
-
def winner_addr
|
|
493
|
-
kw = CAIterator.reduce_keywords
|
|
495
|
+
def winner_addr(idx_op, kw = {})
|
|
496
|
+
kw = CAIterator.send(:reduce_keywords, idx_op, kw)
|
|
494
497
|
saddr = CArray.int64(*@source.shape).seq!
|
|
495
498
|
out = nil
|
|
496
499
|
each_region do |strip_ranges, tiles, out_ranges|
|
|
@@ -520,9 +523,11 @@ class CABlockIterator < CAIterator
|
|
|
520
523
|
# `source.reshape(source.elements)[bi.sort_addr]` is the source sorted within
|
|
521
524
|
# each tile. Multi-axis tiles are flattened (as for the order statistics). A
|
|
522
525
|
# partial edge tile sorts only its present cells. Masked values sort to the
|
|
523
|
-
# tail of their tile (as `CArray#sort`)
|
|
526
|
+
# tail of their tile (as `CArray#sort`), or to the head with
|
|
527
|
+
# `masked_position: :first`.
|
|
528
|
+
# @param masked_position [Symbol] `:last` (default) or `:first`.
|
|
524
529
|
# @return [CArray] source-shaped int64
|
|
525
|
-
def sort_addr
|
|
530
|
+
def sort_addr(masked_position: :last)
|
|
526
531
|
saddr = CArray.int64(*@source.shape).seq!
|
|
527
532
|
out = CArray.int64(*@source.shape)
|
|
528
533
|
out[] = UNDEF
|
|
@@ -531,7 +536,8 @@ class CABlockIterator < CAIterator
|
|
|
531
536
|
sview = saddr[*strip_ranges].block_view(*tiles)
|
|
532
537
|
grid = (0...@sndim).map { |i| vview.shape[i] }
|
|
533
538
|
cells = tiles.inject(1) { |p, t| p * t }
|
|
534
|
-
order = vview.copy.reshape(*(grid + [cells]))
|
|
539
|
+
order = vview.copy.reshape(*(grid + [cells]))
|
|
540
|
+
.sort_index(axis: @sndim, masked_position: masked_position)
|
|
535
541
|
src_sorted = sview.reshape(*(grid + [cells])).take_along_axis(order, axis: @sndim)
|
|
536
542
|
out[*strip_ranges].block_view(*tiles)[] = src_sorted.reshape(*(grid + tiles))
|
|
537
543
|
end
|
|
@@ -542,16 +548,20 @@ class CABlockIterator < CAIterator
|
|
|
542
548
|
# Per-tile weighted sum; `weights` is shaped like one full tile (`Π b_i`).
|
|
543
549
|
# At a partial edge tile the weight kernel is sliced to the present cells.
|
|
544
550
|
# @return [CArray]
|
|
545
|
-
|
|
546
|
-
|
|
551
|
+
# @raise [TypeError] when `weights` is a number, not one weight per
|
|
552
|
+
# cell of a tile.
|
|
553
|
+
def wsum(weights, **kw)
|
|
554
|
+
kw = CAIterator.send(:reduce_keywords, :wsum, kw)
|
|
547
555
|
weighted(weights) { |view, w| view.wsum(w, axis: @tile_axes, **kw) }
|
|
548
556
|
end
|
|
549
557
|
|
|
550
558
|
# @overload wmean(weights)
|
|
551
559
|
# Per-tile weighted mean; `weights` shaped like one full tile.
|
|
552
560
|
# @return [CArray]
|
|
553
|
-
|
|
554
|
-
|
|
561
|
+
# @raise [TypeError] when `weights` is a number, not one weight per
|
|
562
|
+
# cell of a tile.
|
|
563
|
+
def wmean(weights, **kw)
|
|
564
|
+
kw = CAIterator.send(:reduce_keywords, :wmean, kw)
|
|
555
565
|
weighted(weights) { |view, w| view.wmean(w, axis: @tile_axes, **kw) }
|
|
556
566
|
end
|
|
557
567
|
|
|
@@ -559,7 +569,9 @@ class CABlockIterator < CAIterator
|
|
|
559
569
|
|
|
560
570
|
# Broadcast a full-tile weight kernel over each region (sliced to the region's
|
|
561
571
|
# possibly-ragged tile) and yield (view, broadcast weights) for the reduction.
|
|
562
|
-
def weighted
|
|
572
|
+
def weighted(weights)
|
|
573
|
+
CArray.send(:refuse_ruby_array, "wsum/wmean", "a weight array") if weights.is_a?(Array)
|
|
574
|
+
CArray.send(:refuse_scalar_weights, "wsum/wmean", "a tile") if weights.is_a?(Numeric)
|
|
563
575
|
unless weights.shape == @sizes
|
|
564
576
|
raise ArgumentError,
|
|
565
577
|
"wsum/wmean: weights shape #{weights.shape.inspect} != " \
|
|
@@ -586,25 +598,30 @@ class CABlockIterator < CAIterator
|
|
|
586
598
|
|
|
587
599
|
# @overload median
|
|
588
600
|
# Per-tile median. @return [CArray]
|
|
589
|
-
def median
|
|
590
|
-
kw = CAIterator.
|
|
601
|
+
def median(**kw)
|
|
602
|
+
kw = CAIterator.send(:reduce_keywords, :median, kw)
|
|
591
603
|
order_stat { |v, axis| v.median(axis: axis, **kw) }
|
|
592
604
|
end
|
|
593
605
|
|
|
594
|
-
# @overload percentile(
|
|
595
|
-
# Per-tile percentile(s)
|
|
596
|
-
# of CArrays
|
|
606
|
+
# @overload percentile(p)
|
|
607
|
+
# Per-tile percentile(s), in the form `CArray#percentile` returns: one p
|
|
608
|
+
# gives one CArray, an Array of p an Array of CArrays, and several p
|
|
609
|
+
# (or a CArray of p) one CArray with the p axes in front.
|
|
597
610
|
# @return [CArray, Array<CArray>]
|
|
598
|
-
def percentile
|
|
599
|
-
kw = CAIterator.
|
|
600
|
-
|
|
611
|
+
def percentile(*pers, **kw)
|
|
612
|
+
kw = CAIterator.send(:reduce_keywords, :percentile, kw)
|
|
613
|
+
list, form = CArray.send(:percentile_arguments, pers)
|
|
614
|
+
answers = order_stat { |v, axis| v.percentile(list, axis: axis, **kw) }
|
|
615
|
+
CArray.send(:percentile_answer, answers, form)
|
|
601
616
|
end
|
|
602
617
|
|
|
603
618
|
# @overload quantile
|
|
604
619
|
# Per-tile five-number summary `[min, Q1, median, Q3, max]` (five CArrays).
|
|
605
620
|
# @return [Array<CArray>]
|
|
606
|
-
def quantile
|
|
607
|
-
|
|
621
|
+
def quantile(*args, **kw)
|
|
622
|
+
CAIterator.send(:refuse_quantile_arguments, args)
|
|
623
|
+
kw = CAIterator.send(:reduce_keywords, :quantile, kw)
|
|
624
|
+
order_stat { |v, axis| v.quantile(axis: axis, **kw) }
|
|
608
625
|
end
|
|
609
626
|
|
|
610
627
|
private
|
|
@@ -612,7 +629,7 @@ class CABlockIterator < CAIterator
|
|
|
612
629
|
# Drive an order statistic (yielded as `block.call(view, axis)`) per region:
|
|
613
630
|
# a 1-D tile passes the tile axis straight through; a multi-axis tile is
|
|
614
631
|
# materialized (peak O(region cells)) and its tile axes flattened into one.
|
|
615
|
-
def order_stat
|
|
632
|
+
def order_stat(&op)
|
|
616
633
|
assemble do |view, tiles|
|
|
617
634
|
if @sndim == 1
|
|
618
635
|
op.call(view, @tile_axes[0])
|
|
@@ -656,11 +673,15 @@ class CABlockIterator < CAIterator
|
|
|
656
673
|
# @yieldparam tile [CArray]
|
|
657
674
|
# @return [CArray] tile-grid shaped
|
|
658
675
|
# @overload reduce(init) { |acc, elem| ... }
|
|
659
|
-
# Per-tile fiber fold: each tile's cells are folded element by
|
|
660
|
-
# `init` (masked
|
|
676
|
+
# Per-tile fiber fold: each tile's present cells are folded element by
|
|
677
|
+
# element from a copy of `init` (`init.dup`). A masked cell and a cell
|
|
678
|
+
# past the edge are not passed; a tile with none present answers `init`.
|
|
661
679
|
# @param init [Object] initial accumulator.
|
|
662
680
|
# @return [CArray]
|
|
663
|
-
def reduce
|
|
681
|
+
def reduce(*args, data_type: nil, &blk)
|
|
682
|
+
if args.size > 1
|
|
683
|
+
raise ArgumentError, "reduce: wrong number of arguments (given #{args.size}, expected 0..1)"
|
|
684
|
+
end
|
|
664
685
|
raise LocalJumpError, "no block given (yield)" unless blk
|
|
665
686
|
out = CArray.new(data_type || CA_OBJECT, @shape)
|
|
666
687
|
tgv = tile_grid_view
|
|
@@ -670,8 +691,8 @@ class CABlockIterator < CAIterator
|
|
|
670
691
|
else
|
|
671
692
|
init = args[0]
|
|
672
693
|
CArray.each_index(*@shape) do |*g|
|
|
673
|
-
acc = init
|
|
674
|
-
tgv[*g, *nils].each { |e| acc = blk.call(acc, e) }
|
|
694
|
+
acc = init.dup
|
|
695
|
+
tgv[*g, *nils].each { |e| acc = blk.call(acc, e) unless UNDEF.equal?(e) }
|
|
675
696
|
out[*g] = acc
|
|
676
697
|
end
|
|
677
698
|
end
|
|
@@ -738,7 +759,7 @@ class CABlockIterator < CAIterator
|
|
|
738
759
|
[:cumsum, :cumprod, :cummax, :cummin, :cumcount], :scan_tiles)
|
|
739
760
|
|
|
740
761
|
# A scan takes no arguments here.
|
|
741
|
-
private def scan_tiles
|
|
762
|
+
private def scan_tiles(op, *args)
|
|
742
763
|
unless args.empty?
|
|
743
764
|
raise ArgumentError, "wrong number of arguments (given #{args.size}, expected 0)"
|
|
744
765
|
end
|
|
@@ -750,7 +771,7 @@ class CABlockIterator < CAIterator
|
|
|
750
771
|
# UNDEF where the tile has none, so a lag never reaches into another
|
|
751
772
|
# tile. A partial edge tile counts only the cells it has.
|
|
752
773
|
# @return [CArray] source-shaped
|
|
753
|
-
def lag
|
|
774
|
+
def lag(n = 1)
|
|
754
775
|
unless n.is_a?(Integer)
|
|
755
776
|
raise TypeError, "lag: n must be an Integer (got #{n.class})"
|
|
756
777
|
end
|
|
@@ -759,22 +780,22 @@ class CABlockIterator < CAIterator
|
|
|
759
780
|
|
|
760
781
|
# @overload strip_mask(method:)
|
|
761
782
|
# Per-tile fill of the masked cells, counting the tile's cells row-major:
|
|
762
|
-
# `:forward`
|
|
783
|
+
# `:forward` or `:backward`. A fill never reaches
|
|
763
784
|
# into another tile, so a masked cell with nothing to carry in its tile
|
|
764
785
|
# stays masked.
|
|
765
786
|
# @return [CArray] source-shaped
|
|
766
|
-
def strip_mask
|
|
767
|
-
CAIterator.hold_backward
|
|
787
|
+
def strip_mask(method:)
|
|
788
|
+
CAIterator.send(:hold_backward?, method)
|
|
768
789
|
tile_wise { |rows, axis| rows.strip_mask(method: method, axis: axis) }
|
|
769
790
|
end
|
|
770
791
|
|
|
771
|
-
# @overload
|
|
792
|
+
# @overload rank_index(descending: false, method: :ordinal, kind: :quick)
|
|
772
793
|
# Per-tile rank of each cell among the tile's cells (int64, `0` the
|
|
773
|
-
# smallest), as `CArray#
|
|
774
|
-
# the cells it has. The keywords are those of `CArray#
|
|
794
|
+
# smallest), as `CArray#rank_index` ranks them; a partial edge tile ranks only
|
|
795
|
+
# the cells it has. The keywords are those of `CArray#rank_index`.
|
|
775
796
|
# @return [CArray] source-shaped
|
|
776
|
-
def
|
|
777
|
-
tile_wise { |rows, axis| rows.
|
|
797
|
+
def rank_index(**kw)
|
|
798
|
+
tile_wise { |rows, axis| rows.rank_index(axis: axis, **kw) }
|
|
778
799
|
end
|
|
779
800
|
|
|
780
801
|
private
|
|
@@ -801,7 +822,7 @@ class CABlockIterator < CAIterator
|
|
|
801
822
|
# scatter back to a source-shaped result. The output data type is taken from the
|
|
802
823
|
# first tile's scan (all tiles share it); the padded margin is UNDEF and the
|
|
803
824
|
# OOB cells of a partial edge tile are cropped from the result.
|
|
804
|
-
def block_scan
|
|
825
|
+
def block_scan(op, *args)
|
|
805
826
|
tgv = tile_grid_view
|
|
806
827
|
nils = Array.new(@sndim, nil)
|
|
807
828
|
pout = nil
|
|
@@ -825,7 +846,7 @@ class CABlockIterator < CAIterator
|
|
|
825
846
|
# A source-shaped array that can hold what #tile_wise gives: of its kind,
|
|
826
847
|
# or an object array for a Face that holds no new value, which cannot be
|
|
827
848
|
# written cell by cell.
|
|
828
|
-
def tile_output
|
|
849
|
+
def tile_output(lagged)
|
|
829
850
|
if lagged.face? && !lagged.__send__(:holds_new_values?)
|
|
830
851
|
CArray.object(*@source.shape)
|
|
831
852
|
else
|
|
@@ -835,7 +856,7 @@ class CABlockIterator < CAIterator
|
|
|
835
856
|
|
|
836
857
|
# The values as +out+ can hold them: a Face going into an object array
|
|
837
858
|
# goes as its surface values, not its storage.
|
|
838
|
-
def tile_values
|
|
859
|
+
def tile_values(out, lagged)
|
|
839
860
|
return lagged unless out.data_type == CA_OBJECT && lagged.face?
|
|
840
861
|
CArray.object(*lagged.shape) { lagged.to_a }
|
|
841
862
|
end
|
|
@@ -858,7 +879,7 @@ class CABlockIterator < CAIterator
|
|
|
858
879
|
pad = CArray.new(storage.data_type, pshape, bytes: storage.bytes)
|
|
859
880
|
pad[] = UNDEF
|
|
860
881
|
pad[*interior] = storage
|
|
861
|
-
pad.face_lift
|
|
882
|
+
pad.send(:face_lift, @source)
|
|
862
883
|
else
|
|
863
884
|
pad = @source.__send__(:blank_like, pshape) # a Face stays one
|
|
864
885
|
pad[] = UNDEF
|
|
@@ -879,8 +900,8 @@ class CABlockIterator < CAIterator
|
|
|
879
900
|
# place, so the result shows the source's attributes, as CArray#lag and
|
|
880
901
|
# CArray#strip_mask(method:) do.
|
|
881
902
|
prepend(Module.new do
|
|
882
|
-
def lag
|
|
883
|
-
def strip_mask
|
|
903
|
+
def lag(*args, **keywords) = @source.__send__(:carry_attrs, super)
|
|
904
|
+
def strip_mask(**keywords) = @source.__send__(:carry_attrs, super)
|
|
884
905
|
end)
|
|
885
906
|
|
|
886
907
|
read_source_per_call :@padded_source, :@tile_grid_view
|
|
@@ -890,15 +911,15 @@ end
|
|
|
890
911
|
class CArray
|
|
891
912
|
# @overload blocks(*blocks)
|
|
892
913
|
# Returns a {CABlockIterator} tiling `self` with non-overlapping tiles of a
|
|
893
|
-
# per-axis size. Each argument is an Integer tile size
|
|
894
|
-
#
|
|
895
|
-
#
|
|
896
|
-
#
|
|
897
|
-
# @param blocks [Array<Integer
|
|
914
|
+
# per-axis size. Each argument is an Integer tile size; the tiles start
|
|
915
|
+
# at index 0. The remainder is covered by present-only edge tiles (ceil
|
|
916
|
+
# tile grid). Slice first to start the tiles elsewhere
|
|
917
|
+
# (`a[2..-1].blocks(3)`) or to keep only whole tiles.
|
|
918
|
+
# @param blocks [Array<Integer>] per-axis tile sizes.
|
|
898
919
|
# @return [CABlockIterator]
|
|
899
|
-
# @raise [ArgumentError] when `self` has a rank above `
|
|
920
|
+
# @raise [ArgumentError] when `self` has a rank above `CArray::RANK_MAX / 2`
|
|
900
921
|
# (8): the tiles are read through a view of twice the rank.
|
|
901
|
-
def blocks
|
|
922
|
+
def blocks(*blocks)
|
|
902
923
|
CABlockIterator.new(self, *blocks)
|
|
903
924
|
end
|
|
904
925
|
end
|