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/key_windows.rb
CHANGED
|
@@ -35,7 +35,7 @@ require "carray/distance_limit"
|
|
|
35
35
|
# reduction, takes no part in the reductions.
|
|
36
36
|
#
|
|
37
37
|
# The windows overlap, so the per-cell operations of the partition members
|
|
38
|
-
# (`map`, the scans, `lag`, `
|
|
38
|
+
# (`map`, the scans, `lag`, `rank_index`, `strip_mask`) are not offered.
|
|
39
39
|
class CAKeyWindowIterator < CAIterator
|
|
40
40
|
|
|
41
41
|
EXCLUDE = [nil, :lower, :upper, :both].freeze
|
|
@@ -52,13 +52,17 @@ class CAKeyWindowIterator < CAIterator
|
|
|
52
52
|
# interval : the grid step of the key, for {#is_complete}.
|
|
53
53
|
# at : nil, or the points the windows sit at, a 1-D CArray of the
|
|
54
54
|
# key's kind; the answers are then one per point.
|
|
55
|
-
def initialize
|
|
55
|
+
def initialize(value, on:, before: nil, after: nil, exclude: :lower,
|
|
56
56
|
by: nil, interval: nil, at: nil)
|
|
57
|
-
@
|
|
58
|
-
unless on.is_a?(CArray)
|
|
57
|
+
@source = value
|
|
58
|
+
unless on.is_a?(CArray)
|
|
59
|
+
raise TypeError,
|
|
60
|
+
"windows: on: is a CArray of one key per row (got #{on.class})"
|
|
61
|
+
end
|
|
62
|
+
unless on.ndim == 1 && on.elements == value.shape[0]
|
|
59
63
|
raise ArgumentError,
|
|
60
64
|
"windows: on: is a CArray of one key per row " \
|
|
61
|
-
"(#{value.
|
|
65
|
+
"(#{value.shape[0]} rows)"
|
|
62
66
|
end
|
|
63
67
|
unless EXCLUDE.include?(exclude)
|
|
64
68
|
raise ArgumentError,
|
|
@@ -79,7 +83,7 @@ class CAKeyWindowIterator < CAIterator
|
|
|
79
83
|
# Taken as it is now, as the windows are: first_key, last_key and bounds
|
|
80
84
|
# read it later.
|
|
81
85
|
@key = on.copy
|
|
82
|
-
@n = value.
|
|
86
|
+
@n = value.shape[0]
|
|
83
87
|
# Taken as they are now, as the windows are: bounds reads them later.
|
|
84
88
|
@points = at&.copy
|
|
85
89
|
@nout = at.nil? ? @n : at.elements
|
|
@@ -101,7 +105,6 @@ class CAKeyWindowIterator < CAIterator
|
|
|
101
105
|
rows = CArray.int64(@n).seq[valid].copy
|
|
102
106
|
perm = CArray.sort_addr(groups.project(rows), keys.project(rows), rows)
|
|
103
107
|
@order = rows.project(perm)
|
|
104
|
-
m = @order.elements
|
|
105
108
|
sk = keys.project(@order).copy
|
|
106
109
|
sg = groups.project(@order).int64.copy
|
|
107
110
|
|
|
@@ -149,17 +152,18 @@ class CAKeyWindowIterator < CAIterator
|
|
|
149
152
|
|
|
150
153
|
# The same windows over another value with one row per key (a CAFrame
|
|
151
154
|
# column): the sorting and the window bounds are not computed again.
|
|
152
|
-
def for_value
|
|
153
|
-
unless value.
|
|
155
|
+
def for_value(value) # :nodoc:
|
|
156
|
+
unless value.shape[0] == @n
|
|
154
157
|
raise ArgumentError,
|
|
155
158
|
"windows: the value has shape #{value.shape.inspect}; " \
|
|
156
159
|
"windows on a key take one row per key (#{@n} rows)"
|
|
157
160
|
end
|
|
158
161
|
other = dup
|
|
159
|
-
other.instance_variable_set(:@
|
|
162
|
+
other.instance_variable_set(:@source, value)
|
|
160
163
|
other.instance_variable_set(:@segments, segments_of(value))
|
|
161
164
|
other
|
|
162
165
|
end
|
|
166
|
+
private :for_value
|
|
163
167
|
|
|
164
168
|
# @overload inspect
|
|
165
169
|
# Returns a one-line summary.
|
|
@@ -250,67 +254,84 @@ class CAKeyWindowIterator < CAIterator
|
|
|
250
254
|
:count, :count_not_masked, :count_masked, :all, :any,
|
|
251
255
|
:min_index, :max_index], :segments_in_rows)
|
|
252
256
|
|
|
253
|
-
private def segments_in_rows
|
|
257
|
+
private def segments_in_rows(op, *args, **kw)
|
|
258
|
+
refuse_axis_form(kw)
|
|
254
259
|
over_columns { |seg| in_rows(seg.public_send(op, *args, **kw)) }
|
|
255
260
|
end
|
|
256
261
|
|
|
257
262
|
# @overload percentile(p)
|
|
258
|
-
# Returns each window's p-th percentile
|
|
263
|
+
# Returns each window's p-th percentile, with p in the forms
|
|
264
|
+
# `CArray#percentile` takes and the answer in the form p was written in.
|
|
259
265
|
# @return [CArray, Array<CArray>]
|
|
260
|
-
def percentile
|
|
261
|
-
|
|
266
|
+
def percentile(*pers, **kw)
|
|
267
|
+
refuse_axis_form(kw)
|
|
268
|
+
CAIterator.send(:percentile_in_form, pers) { |p|
|
|
269
|
+
over_columns { |seg| in_rows(seg.percentile(p, **kw)) }
|
|
270
|
+
}
|
|
262
271
|
end
|
|
263
272
|
|
|
264
273
|
# @overload quantile
|
|
265
274
|
# Returns each window's five-number summary `[min, Q1, median, Q3, max]`.
|
|
266
275
|
# @return [Array<CArray>]
|
|
267
|
-
def quantile
|
|
276
|
+
def quantile(*args, **kw)
|
|
277
|
+
refuse_axis_form(kw)
|
|
278
|
+
CAIterator.send(:refuse_quantile_arguments, args)
|
|
268
279
|
over_columns { |seg| in_rows(seg.quantile(**kw)) }
|
|
269
280
|
end
|
|
270
281
|
|
|
271
282
|
# @overload minmax
|
|
272
283
|
# Returns `[min, max]` of each window.
|
|
273
284
|
# @return [Array(CArray, CArray)]
|
|
274
|
-
def minmax
|
|
285
|
+
def minmax(**kw)
|
|
286
|
+
refuse_axis_form(kw)
|
|
275
287
|
over_columns { |seg| in_rows(seg.minmax(**kw)) }
|
|
276
288
|
end
|
|
277
289
|
|
|
278
290
|
# @overload wsum(weights)
|
|
279
291
|
# Returns each window's weighted sum; `weights` is shaped like the value.
|
|
280
292
|
# @return [CArray]
|
|
281
|
-
def wsum
|
|
293
|
+
def wsum(weights, **kw)
|
|
294
|
+
refuse_axis_form(kw)
|
|
295
|
+
CArray.send(:refuse_scalar_weights, "wsum", "the value") if weights.is_a?(Numeric)
|
|
282
296
|
over_columns { |seg, j| in_rows(seg.wsum(cells_of(weights, j), **kw)) }
|
|
283
297
|
end
|
|
284
298
|
|
|
285
299
|
# @overload wmean(weights)
|
|
286
300
|
# Returns each window's weighted mean.
|
|
287
301
|
# @return [CArray]
|
|
288
|
-
def wmean
|
|
302
|
+
def wmean(weights, **kw)
|
|
303
|
+
refuse_axis_form(kw)
|
|
304
|
+
CArray.send(:refuse_scalar_weights, "wmean", "the value") if weights.is_a?(Numeric)
|
|
289
305
|
over_columns { |seg, j| in_rows(seg.wmean(cells_of(weights, j), **kw)) }
|
|
290
306
|
end
|
|
291
307
|
|
|
292
308
|
# @overload min_addr(min_count: nil, fill_value: nil)
|
|
293
309
|
# Returns the row of each window's minimum (int64).
|
|
294
310
|
# @return [CArray]
|
|
295
|
-
def min_addr
|
|
296
|
-
|
|
311
|
+
def min_addr(fill_value: nil, **kw)
|
|
312
|
+
refuse_axis_form(kw)
|
|
313
|
+
over_columns { |seg| winner_row(seg.min_addr(**kw), fill_value, :min_addr) }
|
|
297
314
|
end
|
|
298
315
|
|
|
299
316
|
# @overload max_addr(min_count: nil, fill_value: nil)
|
|
300
317
|
# Returns the row of each window's maximum (int64).
|
|
301
318
|
# @return [CArray]
|
|
302
|
-
def max_addr
|
|
303
|
-
|
|
319
|
+
def max_addr(fill_value: nil, **kw)
|
|
320
|
+
refuse_axis_form(kw)
|
|
321
|
+
over_columns { |seg| winner_row(seg.max_addr(**kw), fill_value, :max_addr) }
|
|
304
322
|
end
|
|
305
323
|
|
|
306
324
|
# @overload reduce(*args, data_type: nil) { |piece| ... }
|
|
307
325
|
# Folds each window with the block, as {CASegmentIterator#reduce}.
|
|
308
326
|
# @return [CArray]
|
|
309
|
-
def reduce
|
|
327
|
+
def reduce(*args, **kw, &block)
|
|
328
|
+
if args.size > 1
|
|
329
|
+
raise ArgumentError, "reduce: wrong number of arguments (given #{args.size}, expected 0..1)"
|
|
330
|
+
end
|
|
310
331
|
if @segments.is_a?(Array)
|
|
311
332
|
raise NotImplementedError,
|
|
312
333
|
"reduce: windows over a value of several columns (shape " \
|
|
313
|
-
"#{@
|
|
334
|
+
"#{@source.shape.inspect}) have no block fold; reduce a column " \
|
|
314
335
|
"at a time (value[nil, j])"
|
|
315
336
|
end
|
|
316
337
|
in_rows(@segments.reduce(*args, **kw, &block))
|
|
@@ -324,15 +345,15 @@ class CAKeyWindowIterator < CAIterator
|
|
|
324
345
|
return to_enum(:each) unless block_given?
|
|
325
346
|
pieces = []
|
|
326
347
|
if @segments.is_a?(Array)
|
|
327
|
-
rest = [nil] * (@
|
|
348
|
+
rest = [nil] * (@source.ndim - 1)
|
|
328
349
|
(@offsets.elements - 1).times do |p|
|
|
329
350
|
rows = @src_addr[@offsets[p]...@offsets[p + 1]]
|
|
330
|
-
pieces << @
|
|
351
|
+
pieces << @source[rows, *rest]
|
|
331
352
|
end
|
|
332
353
|
else
|
|
333
354
|
@segments.each { |piece| pieces << piece }
|
|
334
355
|
end
|
|
335
|
-
empty = @
|
|
356
|
+
empty = @source[0...0, *[nil] * (@source.ndim - 1)]
|
|
336
357
|
@nout.times do |r|
|
|
337
358
|
p = @position[r]
|
|
338
359
|
yield(UNDEF.equal?(p) ? empty : pieces[p])
|
|
@@ -345,22 +366,20 @@ class CAKeyWindowIterator < CAIterator
|
|
|
345
366
|
private
|
|
346
367
|
|
|
347
368
|
# Answers per sorted row, put back in row order.
|
|
348
|
-
def in_rows
|
|
369
|
+
def in_rows(result)
|
|
349
370
|
return result.map { |r| in_rows(r) } if result.is_a?(Array)
|
|
350
371
|
result.project(@position)
|
|
351
372
|
end
|
|
352
373
|
|
|
353
374
|
# A winner's address among the gathered cells, as a row of self, in row
|
|
354
375
|
# order; fill_value fills the windows with none, as the core fills it.
|
|
355
|
-
def winner_row
|
|
356
|
-
|
|
357
|
-
out[out.is_masked] = fill_value unless fill_value.nil?
|
|
358
|
-
out
|
|
376
|
+
def winner_row(addr, fill_value, op)
|
|
377
|
+
CArray.__fill_undefined_result_cells__(in_rows(@src_addr.project(addr)), fill_value, op)
|
|
359
378
|
end
|
|
360
379
|
|
|
361
380
|
# The row at sorted position pos[i] of the window of sorted row i, UNDEF
|
|
362
381
|
# where the window is empty.
|
|
363
|
-
def edge_addr
|
|
382
|
+
def edge_addr(pos)
|
|
364
383
|
return CArray.int64(0) if pos.elements == 0
|
|
365
384
|
out = @order.project(pos)
|
|
366
385
|
out[(@hi - @lo).le(0)] = UNDEF
|
|
@@ -370,9 +389,9 @@ class CAKeyWindowIterator < CAIterator
|
|
|
370
389
|
# A weights array taken through the same cells as the value: one weight
|
|
371
390
|
# per row, or for a value of several columns, weights shaped like it
|
|
372
391
|
# (column +j+ of them).
|
|
373
|
-
def cells_of
|
|
392
|
+
def cells_of(weights, j = nil)
|
|
374
393
|
return weights unless weights.is_a?(CArray)
|
|
375
|
-
if j && weights.ndim > 1 && weights.
|
|
394
|
+
if j && weights.ndim > 1 && weights.shape[0] == @n
|
|
376
395
|
weights = weights.reshape(@n, weights.elements / @n)[nil, j]
|
|
377
396
|
end
|
|
378
397
|
return weights unless weights.elements == @n
|
|
@@ -382,7 +401,7 @@ class CAKeyWindowIterator < CAIterator
|
|
|
382
401
|
# The segment iterators the reductions run on: one for a value with one
|
|
383
402
|
# cell per row, and for a value of several columns (each row a profile)
|
|
384
403
|
# one per trailing column, every one over the same windows.
|
|
385
|
-
def segments_of
|
|
404
|
+
def segments_of(value)
|
|
386
405
|
return CASegmentIterator.new(value.project(@src_addr), offsets: @offsets) if value.ndim == 1
|
|
387
406
|
width = value.shape[1..].inject(1, :*)
|
|
388
407
|
flat = value.reshape(@n, width)
|
|
@@ -391,15 +410,22 @@ class CAKeyWindowIterator < CAIterator
|
|
|
391
410
|
end
|
|
392
411
|
end
|
|
393
412
|
|
|
413
|
+
# The windows are fixed by the key, so a reduction takes no axis. Refused
|
|
414
|
+
# here so that the refusal names this class, not the segment iterator the
|
|
415
|
+
# reduction is handed to.
|
|
416
|
+
def refuse_axis_form(kw)
|
|
417
|
+
raise NotImplementedError, "#{self.class} has no axis: form" unless kw[:axis].nil?
|
|
418
|
+
end
|
|
419
|
+
|
|
394
420
|
# A reduction of the windows: as it is for a value with one cell per row;
|
|
395
421
|
# for a value of several columns, of each column, the answers laid side
|
|
396
422
|
# by side in the value's trailing shape, as a group reduces a profile
|
|
397
423
|
# column along the rows.
|
|
398
|
-
def over_columns
|
|
424
|
+
def over_columns(&block)
|
|
399
425
|
return block.call(@segments, nil) unless @segments.is_a?(Array)
|
|
400
|
-
trail = @
|
|
426
|
+
trail = @source.shape[1..]
|
|
401
427
|
if @segments.empty?
|
|
402
|
-
one = block.call(CASegmentIterator.new(CArray.new(@
|
|
428
|
+
one = block.call(CASegmentIterator.new(CArray.new(@source.data_type, [0], bytes: @source.bytes),
|
|
403
429
|
offsets: CArray.int64(1)), nil)
|
|
404
430
|
return lay_out(Array.new(0), one, trail)
|
|
405
431
|
end
|
|
@@ -410,7 +436,7 @@ class CAKeyWindowIterator < CAIterator
|
|
|
410
436
|
# Answers of the columns (each one per row) as one array of the rows and
|
|
411
437
|
# the trailing shape; an Array of answers (minmax, quantile) answer by
|
|
412
438
|
# answer.
|
|
413
|
-
def lay_out
|
|
439
|
+
def lay_out(results, sample, trail)
|
|
414
440
|
if sample.is_a?(Array)
|
|
415
441
|
return sample.each_index.map { |k| lay_out(results.map { |r| r[k] }, sample[k], trail) }
|
|
416
442
|
end
|
|
@@ -425,7 +451,7 @@ class CAKeyWindowIterator < CAIterator
|
|
|
425
451
|
end
|
|
426
452
|
|
|
427
453
|
# The rows of every window, window after window.
|
|
428
|
-
def window_cells
|
|
454
|
+
def window_cells(counts, starts, total)
|
|
429
455
|
return CArray.int64(0) if total == 0
|
|
430
456
|
nz = counts.gt(0)
|
|
431
457
|
owner = CArray.int64(counts.elements).seq[nz].copy
|
|
@@ -448,7 +474,7 @@ class CAKeyWindowIterator < CAIterator
|
|
|
448
474
|
# Points (at:) are converted together with the keys -- joined, converted
|
|
449
475
|
# once, and split -- so that both are measured from the same origin on
|
|
450
476
|
# the same grid.
|
|
451
|
-
def key_numbers
|
|
477
|
+
def key_numbers(key, before, after, interval, points = nil)
|
|
452
478
|
if key.is_a?(CATime) || key.is_a?(CATimedelta)
|
|
453
479
|
if points
|
|
454
480
|
unless points.instance_of?(key.class)
|
|
@@ -456,13 +482,13 @@ class CAKeyWindowIterator < CAIterator
|
|
|
456
482
|
"windows: at: holds points of the key's kind, a #{key.class} " \
|
|
457
483
|
"(got #{CArray::Inspector.type_label(points)})"
|
|
458
484
|
end
|
|
459
|
-
unless
|
|
485
|
+
unless CATime::UnitAlgebra.same_group?(key.unit.base, points.unit.base)
|
|
460
486
|
raise ArgumentError,
|
|
461
487
|
"windows: at: holds points in #{points.unit.count} #{points.unit.base}, " \
|
|
462
488
|
"which cannot be measured on a key in #{key.unit.count} #{key.unit.base} " \
|
|
463
489
|
"(months and years have no fixed length); convert one with to_unit"
|
|
464
490
|
end
|
|
465
|
-
unit =
|
|
491
|
+
unit = CATime::UnitAlgebra.finer(key.unit, points.unit)
|
|
466
492
|
key, points = key.to_unit(unit), points.to_unit(unit)
|
|
467
493
|
end
|
|
468
494
|
widths = [before, after, interval].map { |w| w.nil? ? nil : duration_in_ticks(w, key.unit) }
|
|
@@ -530,53 +556,53 @@ class CAKeyWindowIterator < CAIterator
|
|
|
530
556
|
end
|
|
531
557
|
|
|
532
558
|
# Whether a width is a whole number (an infinite one is not).
|
|
533
|
-
def whole?
|
|
559
|
+
def whole?(w)
|
|
534
560
|
!(w.is_a?(Float) && w.infinite?) && w.to_r.denominator == 1
|
|
535
561
|
end
|
|
536
562
|
|
|
537
563
|
# A width on integer keys as an integer: its floor, or the int64 limit
|
|
538
564
|
# for an infinite one.
|
|
539
|
-
def integer_width
|
|
565
|
+
def integer_width(w)
|
|
540
566
|
return 2**63 - 1 if w.is_a?(Float) && w.infinite?
|
|
541
567
|
[w.to_r.floor, 2**63 - 1].min
|
|
542
568
|
end
|
|
543
569
|
|
|
544
570
|
# The keys followed by the points, one array, when there are points.
|
|
545
|
-
def joined
|
|
571
|
+
def joined(keys, points)
|
|
546
572
|
points ? CArray.concatenate([keys, points]) : keys
|
|
547
573
|
end
|
|
548
574
|
|
|
549
575
|
# The keys and the points (nil without them) back out of a joined array.
|
|
550
|
-
def split
|
|
576
|
+
def split(numbers, points)
|
|
551
577
|
return [numbers, nil] unless points
|
|
552
578
|
[numbers[0...@n].copy, numbers[@n...numbers.elements].copy]
|
|
553
579
|
end
|
|
554
580
|
|
|
555
|
-
def window_bound
|
|
581
|
+
def window_bound(width, op)
|
|
556
582
|
key = @points || @key
|
|
557
583
|
return key.copy if width.nil?
|
|
558
584
|
if key.is_a?(CATime) || key.is_a?(CATimedelta)
|
|
559
|
-
key = key.to_unit(
|
|
585
|
+
key = key.to_unit(CATime::UnitAlgebra.finer(key.unit, width.unit))
|
|
560
586
|
end
|
|
561
587
|
key.public_send(op, width)
|
|
562
588
|
end
|
|
563
589
|
|
|
564
590
|
# A duration in ticks of the key's unit, as a Rational.
|
|
565
|
-
def duration_in_ticks
|
|
591
|
+
def duration_in_ticks(w, unit)
|
|
566
592
|
count, base = w.value * w.unit.count, w.unit.base
|
|
567
|
-
unless
|
|
593
|
+
unless CATime::UnitAlgebra.same_group?(base, unit.base)
|
|
568
594
|
raise ArgumentError,
|
|
569
595
|
"windows: a width of #{count} #{base} cannot be measured on a key in " \
|
|
570
596
|
"#{unit.count} #{unit.base} (months and years have no fixed length)"
|
|
571
597
|
end
|
|
572
|
-
Rational(count) *
|
|
573
|
-
(unit.count *
|
|
598
|
+
Rational(count) * CATime::UnitAlgebra.base_ratio(base) /
|
|
599
|
+
(unit.count * CATime::UnitAlgebra.base_ratio(unit.base))
|
|
574
600
|
end
|
|
575
601
|
|
|
576
602
|
# An integer key as int64 numbers scaled by `scale`: the values
|
|
577
603
|
# themselves when they fit, else measured from the smallest value,
|
|
578
604
|
# refused when even the span does not fit.
|
|
579
|
-
def from_smallest
|
|
605
|
+
def from_smallest(key, scale, shift: false)
|
|
580
606
|
lo = key.min
|
|
581
607
|
return key.int64 if lo == UNDEF
|
|
582
608
|
hi = key.max
|
|
@@ -594,7 +620,7 @@ class CAKeyWindowIterator < CAIterator
|
|
|
594
620
|
scale == 1 ? numbers : numbers * scale
|
|
595
621
|
end
|
|
596
622
|
|
|
597
|
-
def check_widths
|
|
623
|
+
def check_widths(bw, aw, iv)
|
|
598
624
|
return unless iv
|
|
599
625
|
unless iv > 0
|
|
600
626
|
raise ArgumentError, "windows: interval: is positive"
|
|
@@ -610,7 +636,7 @@ class CAKeyWindowIterator < CAIterator
|
|
|
610
636
|
|
|
611
637
|
# A code per row for the by: keys, equal codes for equal keys, none for a
|
|
612
638
|
# row with a masked by: cell; zeros when there is no by:.
|
|
613
|
-
def group_codes
|
|
639
|
+
def group_codes(by)
|
|
614
640
|
return CArray.int64(@n).fill(0) if by.nil?
|
|
615
641
|
keys = by.is_a?(Array) ? by : [by]
|
|
616
642
|
if keys.empty?
|