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
|
@@ -39,7 +39,7 @@
|
|
|
39
39
|
# `fill_value:` (replace an UNDEF result) — both passed straight through to
|
|
40
40
|
# the core reduction — express the full spectrum from "fold whatever is
|
|
41
41
|
# present" to "full windows only, edges filled". There is no window-specific
|
|
42
|
-
# strictness
|
|
42
|
+
# strictness option.
|
|
43
43
|
#
|
|
44
44
|
# The class name is kept from 2.0 (the concept — a window — is stable);
|
|
45
45
|
# this is the Ruby family member that supersedes the C engine. Loaded
|
|
@@ -68,41 +68,44 @@ class CAWindowIterator < CAIterator
|
|
|
68
68
|
# Each `ranges[i]` is a `lo..hi` giving the window's offset span around
|
|
69
69
|
# an anchor (`a.windows(-1..1)` is a centred width-3 window; `0..2` is
|
|
70
70
|
# forward-looking). `bounds:` selects the margin policy (`:skip` /
|
|
71
|
-
# `:nearest` / `:truncate`). `fill_value:` is
|
|
72
|
-
#
|
|
71
|
+
# `:nearest` / `:fill` / `:truncate`). `fill_value:` is the value of the
|
|
72
|
+
# `:fill` margin; given without `bounds:`, it makes the margin `:fill`.
|
|
73
|
+
# `:fill` without `fill_value:` raises, and so does `fill_value:` with
|
|
74
|
+
# `:skip`, `:nearest` or `:truncate`, which take no fill value.
|
|
73
75
|
# Over a categorical or a const string, which hold no new value, it has
|
|
74
76
|
# to be a value some cell already holds.
|
|
75
77
|
#
|
|
76
|
-
#
|
|
77
|
-
# accepts a CAWindow view (the old `CAWindowIterator.new(a.window(...))`
|
|
78
|
-
# form): the geometry (offset ranges, bounds, fill value) is read back
|
|
79
|
-
# from the view. Given ranges, a CAWindow view is rolled over like any
|
|
80
|
-
# other array.
|
|
78
|
+
# A CAWindow view is rolled over like any other array.
|
|
81
79
|
#
|
|
82
|
-
# @param source [CArray
|
|
83
|
-
# view to read the geometry from.
|
|
80
|
+
# @param source [CArray] the array to roll over.
|
|
84
81
|
# @param ranges [Array<Range>] per-axis offset ranges.
|
|
85
|
-
# @param bounds [Symbol] `:skip` / `:nearest` / `:truncate`.
|
|
86
|
-
# @param fill_value [Object, nil]
|
|
82
|
+
# @param bounds [Symbol] `:skip` / `:nearest` / `:fill` / `:truncate`.
|
|
83
|
+
# @param fill_value [Object, nil] the `:fill` margin value.
|
|
84
|
+
# @raise [ArgumentError] when `fill_value:` is given with a `bounds:`
|
|
85
|
+
# other than `:fill`.
|
|
87
86
|
# @raise [ArgumentError] when a categorical or const string source holds
|
|
88
87
|
# no cell with `fill_value` (raised when the windows are first read).
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
88
|
+
# The boundary policies a window takes.
|
|
89
|
+
BOUNDS = [:skip, :nearest, :fill, :truncate].freeze
|
|
90
|
+
|
|
91
|
+
def initialize(source, *ranges, bounds: nil, fill_value: nil)
|
|
92
|
+
@source = source
|
|
93
|
+
@ranges = ranges.flatten(0)
|
|
94
|
+
# fill_value: without bounds: is bounds: :fill; with any other bounds:
|
|
95
|
+
# it would have nothing to fill, and is refused as window refuses it.
|
|
96
|
+
@bounds = bounds || (fill_value.nil? ? :skip : :fill)
|
|
97
|
+
@fill_value = fill_value
|
|
98
|
+
|
|
99
|
+
unless BOUNDS.include?(@bounds)
|
|
100
|
+
raise ArgumentError,
|
|
101
|
+
"windows: unknown bounds: #{@bounds.inspect} " \
|
|
102
|
+
"(use :skip, :nearest, :fill or :truncate)"
|
|
103
|
+
end
|
|
104
|
+
if @bounds == :fill && @fill_value.nil?
|
|
105
|
+
raise ArgumentError, "windows: bounds: :fill needs fill_value: (the value the cells outside take)"
|
|
106
|
+
end
|
|
107
|
+
if @bounds != :fill && ! @fill_value.nil?
|
|
108
|
+
raise ArgumentError, "windows: fill_value: is for bounds: :fill, not #{@bounds.inspect}"
|
|
106
109
|
end
|
|
107
110
|
|
|
108
111
|
unless @ranges.size == @source.ndim
|
|
@@ -112,11 +115,11 @@ class CAWindowIterator < CAIterator
|
|
|
112
115
|
end
|
|
113
116
|
# Each window is read through a view with one axis per anchor and one
|
|
114
117
|
# per cell in the window, twice the source's rank.
|
|
115
|
-
if 2 * @source.ndim >
|
|
118
|
+
if 2 * @source.ndim > CArray::RANK_MAX
|
|
116
119
|
raise ArgumentError,
|
|
117
120
|
"windows: a rank #{@source.ndim} array would need #{2 * @source.ndim} " \
|
|
118
|
-
"axes for its windows, beyond the limit of #{
|
|
119
|
-
"windows works up to rank #{
|
|
121
|
+
"axes for its windows, beyond the limit of #{CArray::RANK_MAX}; " \
|
|
122
|
+
"windows works up to rank #{CArray::RANK_MAX / 2}"
|
|
120
123
|
end
|
|
121
124
|
@ranges = @ranges.each_with_index.map { |r, k| window_offsets(r, k) }
|
|
122
125
|
|
|
@@ -125,9 +128,6 @@ class CAWindowIterator < CAIterator
|
|
|
125
128
|
@lefts = @ranges.map { |r| [0, -r.begin].max } # left margin per axis
|
|
126
129
|
@rights = @ranges.map { |r| [0, r.end ].max } # right margin per axis
|
|
127
130
|
|
|
128
|
-
# A constant fill_value: overrides :skip (constant margin escape hatch).
|
|
129
|
-
@bounds = :constant if @fill_value != nil && @bounds == :skip
|
|
130
|
-
|
|
131
131
|
# Trailing window axes of the sliding_windows view: [ndim .. 2*ndim-1].
|
|
132
132
|
@window_axes = (@sndim...(2 * @sndim)).to_a
|
|
133
133
|
|
|
@@ -151,11 +151,6 @@ class CAWindowIterator < CAIterator
|
|
|
151
151
|
self
|
|
152
152
|
end
|
|
153
153
|
|
|
154
|
-
# @overload source
|
|
155
|
-
# Returns the array being rolled over.
|
|
156
|
-
# @return [CArray]
|
|
157
|
-
attr_reader :source
|
|
158
|
-
|
|
159
154
|
# @overload bounds
|
|
160
155
|
# Returns the boundary policy symbol.
|
|
161
156
|
# @return [Symbol]
|
|
@@ -163,15 +158,24 @@ class CAWindowIterator < CAIterator
|
|
|
163
158
|
|
|
164
159
|
# ---- padded entity + sliding_windows view -----------------------------
|
|
165
160
|
#
|
|
166
|
-
# The engine: pad the source (:skip -> UNDEF, :nearest -> edge, :
|
|
161
|
+
# The engine: pad the source (:skip -> UNDEF, :nearest -> edge, :fill
|
|
167
162
|
# -> value; :truncate does not pad), then take a sliding_windows view. Both
|
|
168
163
|
# are kept for one call, so the helpers a reduction runs share one pad and
|
|
169
164
|
# one view, and the next call pads the source as it is then.
|
|
170
165
|
|
|
171
166
|
# @overload sliding_view
|
|
172
|
-
# Returns
|
|
173
|
-
#
|
|
174
|
-
#
|
|
167
|
+
# Returns every window as one array: the anchor axes first ({#shape}),
|
|
168
|
+
# then one axis per window axis, so `sliding_view[i, nil]` is the window
|
|
169
|
+
# at anchor `i` of a 1-D source. The reductions of this iterator are the
|
|
170
|
+
# core's reductions of this array over the window axes
|
|
171
|
+
# (`it.mean == it.sliding_view.mean(axis: [1])` for a 1-D source), so a
|
|
172
|
+
# computation the iterator does not offer can be written on it directly.
|
|
173
|
+
#
|
|
174
|
+
# A cell outside the source reads as `bounds:` says: UNDEF for `:skip`,
|
|
175
|
+
# the nearest edge for `:nearest`, `fill_value` for `:fill`. With
|
|
176
|
+
# `:truncate` it is a view of the source itself; otherwise it is a view of
|
|
177
|
+
# a padded copy made from the source as it is at this call, so a write to
|
|
178
|
+
# it does not reach the source.
|
|
175
179
|
# @return [CArray]
|
|
176
180
|
def sliding_view
|
|
177
181
|
@sliding_view ||= @shape.include?(0) ? no_windows : anchored_buffer.sliding_windows(*@widths)
|
|
@@ -181,7 +185,7 @@ class CAWindowIterator < CAIterator
|
|
|
181
185
|
|
|
182
186
|
# An axis's offsets as an inclusive Integer range, as Ruby reads the range
|
|
183
187
|
# given (`-1...1` is the offsets -1 and 0).
|
|
184
|
-
def window_offsets
|
|
188
|
+
def window_offsets(r, k)
|
|
185
189
|
unless r.is_a?(Range) && r.begin.is_a?(Integer) && r.end.is_a?(Integer)
|
|
186
190
|
raise TypeError,
|
|
187
191
|
"windows: axis #{k} takes a Range of Integer offsets, got #{r.inspect}"
|
|
@@ -203,7 +207,7 @@ class CAWindowIterator < CAIterator
|
|
|
203
207
|
cut = @shape.each_with_index.map { |n, k| n == unit.shape[k] ? nil : 0...n }
|
|
204
208
|
unit = unit[*cut, *[nil] * @sndim] unless cut.all?(&:nil?)
|
|
205
209
|
view = unit.broadcast_to(*(@shape + @widths))
|
|
206
|
-
@source.face? ? view.face_lift
|
|
210
|
+
@source.face? ? view.send(:face_lift, @source) : view
|
|
207
211
|
end
|
|
208
212
|
|
|
209
213
|
# The stretch of the padded buffer the windows are taken from: it begins at
|
|
@@ -232,11 +236,7 @@ class CAWindowIterator < CAIterator
|
|
|
232
236
|
case @bounds
|
|
233
237
|
when :skip then :masked
|
|
234
238
|
when :nearest then :edge
|
|
235
|
-
when :
|
|
236
|
-
else
|
|
237
|
-
raise ArgumentError,
|
|
238
|
-
"windows: unknown bounds #{@bounds.inspect} " \
|
|
239
|
-
"(expected :skip, :nearest, :truncate)"
|
|
239
|
+
when :fill then :fill
|
|
240
240
|
end
|
|
241
241
|
end
|
|
242
242
|
|
|
@@ -246,13 +246,13 @@ class CAWindowIterator < CAIterator
|
|
|
246
246
|
# escape hatches yield from the padded entity, so users never call it. A
|
|
247
247
|
# standalone public CArray#pad is a possible future promotion.
|
|
248
248
|
#
|
|
249
|
-
# :
|
|
249
|
+
# :fill margin cells set to `value` (0 when value is nil).
|
|
250
250
|
# :edge margin cells replicate the nearest source edge cell.
|
|
251
251
|
# :masked margin cells are UNDEF (masked); reductions skip them.
|
|
252
252
|
#
|
|
253
253
|
# One allocation, the source copied into the interior region, then the
|
|
254
254
|
# margins filled per mode.
|
|
255
|
-
def pad_source
|
|
255
|
+
def pad_source(src, lefts, rights, mode, value)
|
|
256
256
|
return pad_face(src, lefts, rights, mode, value) if src.face?
|
|
257
257
|
nd = src.ndim
|
|
258
258
|
shape = src.shape
|
|
@@ -261,7 +261,7 @@ class CAWindowIterator < CAIterator
|
|
|
261
261
|
|
|
262
262
|
# Fill the whole buffer first, then overwrite the interior with the source.
|
|
263
263
|
case mode
|
|
264
|
-
when :
|
|
264
|
+
when :fill
|
|
265
265
|
pad[] = (value.nil? ? 0 : value)
|
|
266
266
|
when :masked
|
|
267
267
|
pad[] = UNDEF
|
|
@@ -279,25 +279,25 @@ class CAWindowIterator < CAIterator
|
|
|
279
279
|
end
|
|
280
280
|
|
|
281
281
|
# A Face is padded on its storage and put back on at the end, so the margins
|
|
282
|
-
# hold storage -- UNDEF, a copy of an edge cell, or a `:
|
|
282
|
+
# hold storage -- UNDEF, a copy of an edge cell, or a `:fill` value the
|
|
283
283
|
# Face has turned into storage -- and the reductions over the windows are
|
|
284
284
|
# the Face's own. A buffer of the Face's surface type could not take the
|
|
285
285
|
# margin values at all.
|
|
286
|
-
def pad_face
|
|
286
|
+
def pad_face(src, lefts, rights, mode, value)
|
|
287
287
|
storage = src.__send__(:face_storage)
|
|
288
|
-
if mode == :
|
|
289
|
-
padded = pad_source(storage, lefts, rights, :
|
|
288
|
+
if mode == :fill && !value.nil?
|
|
289
|
+
padded = pad_source(storage, lefts, rights, :fill, margin_storage(src, storage, value))
|
|
290
290
|
else
|
|
291
291
|
padded = pad_source(storage, lefts, rights, mode, value)
|
|
292
292
|
end
|
|
293
|
-
padded.face_lift
|
|
293
|
+
padded.send(:face_lift, src)
|
|
294
294
|
end
|
|
295
295
|
|
|
296
|
-
# The storage of a `:
|
|
296
|
+
# The storage of a `:fill` margin value. A Face that holds new values
|
|
297
297
|
# turns the value into storage; one that holds none (a const string's
|
|
298
298
|
# records index a shared pool, a categorical's codes index its labels) can
|
|
299
299
|
# only repeat the storage of a cell that already holds the value.
|
|
300
|
-
def margin_storage
|
|
300
|
+
def margin_storage(src, storage, value)
|
|
301
301
|
if src.__send__(:holds_new_values?)
|
|
302
302
|
cell = src.__send__(:blank_like, [1])
|
|
303
303
|
cell[0] = value
|
|
@@ -317,7 +317,7 @@ class CAWindowIterator < CAIterator
|
|
|
317
317
|
# the right margin rows copy the last; done axis by axis over the whole
|
|
318
318
|
# buffer (including corners, which pick up the replicated neighbours from an
|
|
319
319
|
# earlier axis, matching the standard edge-pad of corners).
|
|
320
|
-
def replicate_edges
|
|
320
|
+
def replicate_edges(pad, lefts, rights, shape)
|
|
321
321
|
pad.ndim.times do |ax|
|
|
322
322
|
lo = lefts[ax]
|
|
323
323
|
hi = lefts[ax] + shape[ax] - 1 # last interior index on this axis
|
|
@@ -340,7 +340,7 @@ class CAWindowIterator < CAIterator
|
|
|
340
340
|
# A window away from the edges holds all of them, so the vector is the window
|
|
341
341
|
# width everywhere but the two ends, and only the ends -- at most one window
|
|
342
342
|
# width of cells each -- are counted out.
|
|
343
|
-
def axis_cell_counts
|
|
343
|
+
def axis_cell_counts(k)
|
|
344
344
|
length = @shape[k]
|
|
345
345
|
first = @ranges[k].begin
|
|
346
346
|
last = @ranges[k].end
|
|
@@ -359,7 +359,7 @@ class CAWindowIterator < CAIterator
|
|
|
359
359
|
|
|
360
360
|
# An index list of length `nd` that is `nil` (full range) on every axis
|
|
361
361
|
# except `ax`, which is pinned to `k`.
|
|
362
|
-
def axis_selector
|
|
362
|
+
def axis_selector(nd, ax, k)
|
|
363
363
|
sel = Array.new(nd)
|
|
364
364
|
sel[ax] = k
|
|
365
365
|
sel
|
|
@@ -374,7 +374,7 @@ class CAWindowIterator < CAIterator
|
|
|
374
374
|
# `sliding_view.<op>(axis: window_axes, ...)`. This inherits the core data type,
|
|
375
375
|
# mask, empty / all-masked (identity vs UNDEF) and epsilon-close contracts
|
|
376
376
|
# unchanged. `min_count:` / `fill_value:` pass straight to the core (the
|
|
377
|
-
# boundary strictness + result fill
|
|
377
|
+
# boundary strictness + result fill options).
|
|
378
378
|
|
|
379
379
|
# @!method sum(min_count: nil, fill_value: nil)
|
|
380
380
|
# Rolling sum.
|
|
@@ -480,7 +480,7 @@ class CAWindowIterator < CAIterator
|
|
|
480
480
|
[:sum, :accumulate, :prod, :mean, :min, :max, :variance, :stddev,
|
|
481
481
|
:variancep, :stddevp, :minmax, :min_index, :max_index].each do |op|
|
|
482
482
|
class_eval <<~RUBY, __FILE__, __LINE__ + 1
|
|
483
|
-
def #{op}
|
|
483
|
+
def #{op}(min_count: nil, fill_value: nil)
|
|
484
484
|
folded = @shape.include?(0) ? nil : fold_by_offset(:#{op}, min_count, fill_value)
|
|
485
485
|
return folded unless folded.nil?
|
|
486
486
|
kw = {}
|
|
@@ -494,12 +494,12 @@ class CAWindowIterator < CAIterator
|
|
|
494
494
|
# all / any take no min_count: or fill_value:, as the core ones do not.
|
|
495
495
|
[:all, :any].each do |op|
|
|
496
496
|
class_eval <<~RUBY, __FILE__, __LINE__ + 1
|
|
497
|
-
def #{op}
|
|
497
|
+
def #{op}(**kw)
|
|
498
498
|
if kw.empty?
|
|
499
499
|
folded = @shape.include?(0) ? nil : fold_by_offset(:#{op}, nil)
|
|
500
500
|
return folded unless folded.nil?
|
|
501
501
|
end
|
|
502
|
-
sliding_view.#{op}(axis: @window_axes, **CAIterator.reduce_keywords
|
|
502
|
+
sliding_view.#{op}(axis: @window_axes, **CAIterator.send(:reduce_keywords, :#{op}, kw))
|
|
503
503
|
end
|
|
504
504
|
RUBY
|
|
505
505
|
end
|
|
@@ -520,8 +520,8 @@ class CAWindowIterator < CAIterator
|
|
|
520
520
|
# These take no `min_count:` or `fill_value:`, as the core ones do not.
|
|
521
521
|
[:first, :last, :nunique].each do |op|
|
|
522
522
|
class_eval <<~RUBY, __FILE__, __LINE__ + 1
|
|
523
|
-
def #{op}
|
|
524
|
-
sliding_view.#{op}(axis: @window_axes, **CAIterator.reduce_keywords
|
|
523
|
+
def #{op}(**kw)
|
|
524
|
+
sliding_view.#{op}(axis: @window_axes, **CAIterator.send(:reduce_keywords, :#{op}, kw))
|
|
525
525
|
end
|
|
526
526
|
RUBY
|
|
527
527
|
end
|
|
@@ -532,15 +532,15 @@ class CAWindowIterator < CAIterator
|
|
|
532
532
|
# Unlike `min_index` (the position within the window) this indexes back into
|
|
533
533
|
# the original array. The winner's source cell is the anchor plus its window
|
|
534
534
|
# offset; with `bounds: :nearest` a winning margin cell resolves to the edge
|
|
535
|
-
# source cell it replicates, and with `bounds: :
|
|
535
|
+
# source cell it replicates, and with `bounds: :fill` (or `fill_value:`)
|
|
536
536
|
# a winning margin cell has no source address and is a masked result.
|
|
537
537
|
# @return [CArray] reference-shaped (or shrunk, for :truncate)
|
|
538
|
-
def min_addr
|
|
538
|
+
def min_addr(**kw); window_winner_addr(:min_index, kw); end
|
|
539
539
|
|
|
540
540
|
# @overload max_addr
|
|
541
541
|
# Rolling flat source address of the window maximum. See {#min_addr}.
|
|
542
542
|
# @return [CArray] reference-shaped (or shrunk, for :truncate)
|
|
543
|
-
def max_addr
|
|
543
|
+
def max_addr(**kw); window_winner_addr(:max_index, kw); end
|
|
544
544
|
|
|
545
545
|
private
|
|
546
546
|
|
|
@@ -557,7 +557,7 @@ class CAWindowIterator < CAIterator
|
|
|
557
557
|
# pass is a straight walk over contiguous memory. It costs one pass per
|
|
558
558
|
# offset, so it only pays while the window is small; the crossover moves with
|
|
559
559
|
# rank and data type, but stays above width 5 on every axis in every
|
|
560
|
-
# combination measured
|
|
560
|
+
# combination measured.
|
|
561
561
|
#
|
|
562
562
|
# What is decided here is the order of the fold and which pad it reads; the
|
|
563
563
|
# arithmetic is the core's elementwise kernel, and the result data type is
|
|
@@ -572,7 +572,7 @@ class CAWindowIterator < CAIterator
|
|
|
572
572
|
:all => :and!, :any => :or! }.freeze
|
|
573
573
|
|
|
574
574
|
# A boolean accumulates as the core's does, by parity, so its kernel is xor.
|
|
575
|
-
def offset_fold_kernel
|
|
575
|
+
def offset_fold_kernel(op)
|
|
576
576
|
return :xor! if op == :accumulate && @source.data_type == CA_BOOLEAN
|
|
577
577
|
OFFSET_FOLD.fetch(op)
|
|
578
578
|
end
|
|
@@ -582,7 +582,7 @@ class CAWindowIterator < CAIterator
|
|
|
582
582
|
|
|
583
583
|
# Runs the fold by offset, or returns nil when this window is not one it can
|
|
584
584
|
# answer for -- in which case the caller delegates as before.
|
|
585
|
-
def fold_by_offset
|
|
585
|
+
def fold_by_offset(op, min_count, fill_value = nil)
|
|
586
586
|
return nil unless CArray.__fast_paths_enabled__(:window_fold_by_offset)
|
|
587
587
|
need = CArray.__present_cells_needed_per_result_cell__(op, min_count, 0)
|
|
588
588
|
return nil unless op == :mean || OFFSET_FOLD.key?(op)
|
|
@@ -597,12 +597,27 @@ class CAWindowIterator < CAIterator
|
|
|
597
597
|
# neutral value; the core answers NaN there.
|
|
598
598
|
return nil if (op == :min || op == :max) && source_has_nan?
|
|
599
599
|
return nil if neutral_value(op).nil?
|
|
600
|
+
return nil if offset_fold_can_leave_the_range?(op)
|
|
600
601
|
|
|
601
602
|
folded = op == :mean ? fold_mean : accumulate_offsets(op)
|
|
602
603
|
folded = mask_empty_windows(folded) if EMPTY_WINDOW_IS_UNDEFINED.include?(op)
|
|
603
604
|
folded = apply_min_count(folded, need)
|
|
604
605
|
# A `fill_value:` on the call replaces a result that came out undefined.
|
|
605
|
-
CArray.__fill_undefined_result_cells__(folded, fill_value)
|
|
606
|
+
CArray.__fill_undefined_result_cells__(folded, fill_value, op)
|
|
607
|
+
end
|
|
608
|
+
|
|
609
|
+
# The core answers an integer sum or product in int64 / uint64, and in
|
|
610
|
+
# float64 where the total is out of that range; this path accumulates in
|
|
611
|
+
# the answer's type and would wrap instead. A window of at most
|
|
612
|
+
# OFFSET_FOLD_MAX_WIDTH cells per axis can not take a sum of cells of 4
|
|
613
|
+
# bytes or fewer out of int64, but a sum of 8-byte cells, or a product, can.
|
|
614
|
+
def offset_fold_can_leave_the_range?(op)
|
|
615
|
+
return false unless @source.integer?
|
|
616
|
+
case op
|
|
617
|
+
when :prod then true
|
|
618
|
+
when :sum, :mean then @source.bytes >= 8
|
|
619
|
+
else false
|
|
620
|
+
end
|
|
606
621
|
end
|
|
607
622
|
|
|
608
623
|
# The mean is the sum over the same offsets, divided by the number of cells
|
|
@@ -616,11 +631,13 @@ class CAWindowIterator < CAIterator
|
|
|
616
631
|
divisor[counts.eq(0)] = 1
|
|
617
632
|
end
|
|
618
633
|
# coerce: the mean's fixed compute type
|
|
619
|
-
accumulate_offsets(:sum).to_type(offset_fold_data_type(:mean))
|
|
634
|
+
sums = accumulate_offsets(:sum).to_type(offset_fold_data_type(:mean))
|
|
635
|
+
# An object mean divides with quo, as the core's does (exact for Integer).
|
|
636
|
+
sums.data_type == CA_OBJECT ? sums.send(:quo_i, divisor) : sums / divisor
|
|
620
637
|
end
|
|
621
638
|
|
|
622
639
|
# Walks the window offsets, accumulating each shifted plane into the result.
|
|
623
|
-
def accumulate_offsets
|
|
640
|
+
def accumulate_offsets(op, base = nil, kernel = nil, data_type = nil)
|
|
624
641
|
base ||= offset_fold_base(op)
|
|
625
642
|
kernel ||= offset_fold_kernel(op)
|
|
626
643
|
data_type ||= offset_fold_data_type(op)
|
|
@@ -644,7 +661,7 @@ class CAWindowIterator < CAIterator
|
|
|
644
661
|
# listed answer with their identity, which the accumulation already holds.
|
|
645
662
|
EMPTY_WINDOW_IS_UNDEFINED = [:min, :max, :mean].freeze
|
|
646
663
|
|
|
647
|
-
def mask_empty_windows
|
|
664
|
+
def mask_empty_windows(folded)
|
|
648
665
|
return folded unless windows_can_be_empty?
|
|
649
666
|
folded[window_cell_counts.eq(0)] = UNDEF
|
|
650
667
|
folded
|
|
@@ -665,13 +682,13 @@ class CAWindowIterator < CAIterator
|
|
|
665
682
|
end
|
|
666
683
|
|
|
667
684
|
def source_has_nan?
|
|
668
|
-
return true if @bounds == :
|
|
685
|
+
return true if @bounds == :fill && @fill_value.is_a?(Float) && @fill_value.nan?
|
|
669
686
|
return false unless @source.float? || @source.data_type == CA_OBJECT
|
|
670
687
|
@source.is_nan.any
|
|
671
688
|
end
|
|
672
689
|
|
|
673
690
|
# A result only where the window held `need` cells (CArray.__present_cells_needed_per_result_cell__).
|
|
674
|
-
def apply_min_count
|
|
691
|
+
def apply_min_count(folded, need)
|
|
675
692
|
return folded if need == 0
|
|
676
693
|
counts = window_cell_counts
|
|
677
694
|
if counts.is_a?(Integer)
|
|
@@ -688,7 +705,7 @@ class CAWindowIterator < CAIterator
|
|
|
688
705
|
# that cannot win, and the extreme of the array and its constant margin is
|
|
689
706
|
# one -- so no table of per-type limits is needed. Nil means there is none
|
|
690
707
|
# to be had (a source masked everywhere), and the caller delegates instead.
|
|
691
|
-
def neutral_value
|
|
708
|
+
def neutral_value(op)
|
|
692
709
|
@neutral_value ||= {}
|
|
693
710
|
return @neutral_value[op] if @neutral_value.key?(op)
|
|
694
711
|
@neutral_value[op] =
|
|
@@ -702,10 +719,10 @@ class CAWindowIterator < CAIterator
|
|
|
702
719
|
end
|
|
703
720
|
end
|
|
704
721
|
|
|
705
|
-
def source_extreme
|
|
722
|
+
def source_extreme(op)
|
|
706
723
|
value = @source.send(op)
|
|
707
724
|
return nil if value.equal?(UNDEF)
|
|
708
|
-
return value unless @bounds == :
|
|
725
|
+
return value unless @bounds == :fill
|
|
709
726
|
margin = @fill_value || 0
|
|
710
727
|
margin = margin ? 1 : 0 if @source.boolean? # the core answers 0 or 1
|
|
711
728
|
[value, margin].public_send(op)
|
|
@@ -719,7 +736,7 @@ class CAWindowIterator < CAIterator
|
|
|
719
736
|
# depends on the pair, the working set and the compiler; converting once is
|
|
720
737
|
# one behaviour everywhere. It costs a buffer in the wider type, which for
|
|
721
738
|
# a `sum` over bytes is the one case where it does not pay.
|
|
722
|
-
def offset_fold_base
|
|
739
|
+
def offset_fold_base(op)
|
|
723
740
|
@offset_fold_base ||= {}
|
|
724
741
|
@offset_fold_base[op] ||=
|
|
725
742
|
begin
|
|
@@ -727,7 +744,7 @@ class CAWindowIterator < CAIterator
|
|
|
727
744
|
if @source.has_mask?
|
|
728
745
|
pad_for(@source.strip_mask(neutral_value(op)), neutral_value(op))
|
|
729
746
|
elsif @bounds == :skip
|
|
730
|
-
pad_source(@source, @lefts, @rights, :
|
|
747
|
+
pad_source(@source, @lefts, @rights, :fill, neutral_value(op))
|
|
731
748
|
else
|
|
732
749
|
padded_entity
|
|
733
750
|
end
|
|
@@ -738,12 +755,12 @@ class CAWindowIterator < CAIterator
|
|
|
738
755
|
|
|
739
756
|
# Pads `values` the way this window's boundary policy says, with `outside`
|
|
740
757
|
# standing in for the margin where the policy does not fill one.
|
|
741
|
-
def pad_for
|
|
758
|
+
def pad_for(values, outside)
|
|
742
759
|
case @bounds
|
|
743
760
|
when :truncate then values
|
|
744
761
|
when :nearest then pad_source(values, @lefts, @rights, :edge, nil)
|
|
745
|
-
when :
|
|
746
|
-
else pad_source(values, @lefts, @rights, :
|
|
762
|
+
when :fill then pad_source(values, @lefts, @rights, :fill, @fill_value)
|
|
763
|
+
else pad_source(values, @lefts, @rights, :fill, outside)
|
|
747
764
|
end
|
|
748
765
|
end
|
|
749
766
|
|
|
@@ -759,8 +776,8 @@ class CAWindowIterator < CAIterator
|
|
|
759
776
|
@window_cell_counts =
|
|
760
777
|
if @source.has_mask?
|
|
761
778
|
present = @source.is_not_masked.int64
|
|
762
|
-
present = if @bounds == :
|
|
763
|
-
pad_source(present, @lefts, @rights, :
|
|
779
|
+
present = if @bounds == :fill
|
|
780
|
+
pad_source(present, @lefts, @rights, :fill, 1)
|
|
764
781
|
else
|
|
765
782
|
pad_for(present, 0)
|
|
766
783
|
end
|
|
@@ -781,10 +798,10 @@ class CAWindowIterator < CAIterator
|
|
|
781
798
|
|
|
782
799
|
# The data type the delegating path would have produced, asked of the core
|
|
783
800
|
# itself so the two paths cannot disagree.
|
|
784
|
-
def offset_fold_data_type
|
|
801
|
+
def offset_fold_data_type(op)
|
|
785
802
|
@offset_fold_data_type ||= {}
|
|
786
803
|
@offset_fold_data_type[op] ||=
|
|
787
|
-
CAIterator.core_probe
|
|
804
|
+
CAIterator.send(:core_probe, @source).public_send(op, axis: [1]).data_type
|
|
788
805
|
end
|
|
789
806
|
|
|
790
807
|
# Every offset within the window, as a list of per-axis positions into the
|
|
@@ -801,9 +818,9 @@ class CAWindowIterator < CAIterator
|
|
|
801
818
|
# source coordinate on each axis is anchor + offset + window-coordinate (the
|
|
802
819
|
# offset is the range's begin for a padded margin, 0 for :truncate). A margin
|
|
803
820
|
# winner (coordinate out of bounds) resolves per the boundary policy:
|
|
804
|
-
# :nearest clamps to the edge source cell, :
|
|
805
|
-
def window_winner_addr
|
|
806
|
-
kw = CAIterator.reduce_keywords
|
|
821
|
+
# :nearest clamps to the edge source cell, :fill / :skip mask the result.
|
|
822
|
+
def window_winner_addr(idx_op, kw = {})
|
|
823
|
+
kw = CAIterator.send(:reduce_keywords, idx_op, kw)
|
|
807
824
|
mi = send(idx_op, **kw.slice(:min_count)) # window-local flat index
|
|
808
825
|
n = @source.shape
|
|
809
826
|
wstride = Array.new(@sndim); acc = 1
|
|
@@ -838,29 +855,29 @@ class CAWindowIterator < CAIterator
|
|
|
838
855
|
# cells (the effective tap count, which drops near a :skip edge);
|
|
839
856
|
# `count(UNDEF)` counts masked cells; `count(v)` counts cells equal to `v`.
|
|
840
857
|
# @return [CArray]
|
|
841
|
-
def count
|
|
858
|
+
def count(*args, **kw)
|
|
842
859
|
return count_not_masked(**kw) if args.empty?
|
|
843
860
|
# The sliding_windows view is a CAStride, so its #count is not shadowed;
|
|
844
861
|
# dispatch CArray#count explicitly anyway, matching the family regularity.
|
|
845
862
|
count_cells(sliding_view, *args, axis: @window_axes,
|
|
846
|
-
**CAIterator.
|
|
863
|
+
**CAIterator.send(:reduce_keywords, :count, kw))
|
|
847
864
|
end
|
|
848
865
|
|
|
849
866
|
# @overload count_not_masked
|
|
850
867
|
# Rolling count of present (non-masked) cells -- the denominator of a
|
|
851
868
|
# renormalizing convolution.
|
|
852
869
|
# @return [CArray]
|
|
853
|
-
def count_not_masked
|
|
870
|
+
def count_not_masked(**kw)
|
|
854
871
|
sliding_view.count_not_masked(axis: @window_axes,
|
|
855
|
-
**CAIterator.
|
|
872
|
+
**CAIterator.send(:reduce_keywords, :count_not_masked, kw))
|
|
856
873
|
end
|
|
857
874
|
|
|
858
875
|
# @overload count_masked
|
|
859
876
|
# Rolling count of masked cells.
|
|
860
877
|
# @return [CArray]
|
|
861
|
-
def count_masked
|
|
878
|
+
def count_masked(**kw)
|
|
862
879
|
sliding_view.count_masked(axis: @window_axes,
|
|
863
|
-
**CAIterator.
|
|
880
|
+
**CAIterator.send(:reduce_keywords, :count_masked, kw))
|
|
864
881
|
end
|
|
865
882
|
|
|
866
883
|
# @overload elements
|
|
@@ -883,14 +900,15 @@ class CAWindowIterator < CAIterator
|
|
|
883
900
|
# the kernel (one line). Both are exposed under their literal names because
|
|
884
901
|
# the flip convention splits by domain (signal processing flips, image / DL
|
|
885
902
|
# does not). For these, the constant margin default is 0.0 (the value a tap
|
|
886
|
-
# reaching outside the source contributes); override with a :
|
|
903
|
+
# reaching outside the source contributes); override with a :fill fill.
|
|
887
904
|
|
|
888
905
|
# @overload correlate(kernel, min_count: nil, fill_value: nil)
|
|
889
906
|
# Rolling cross-correlation `out[i] = Σ_j a[i+j]·k[j]` (kernel not
|
|
890
907
|
# flipped). `kernel` has the shape of one window (`w_1 × ... × w_n`).
|
|
891
908
|
# @param kernel [CArray] weights shaped like a single window.
|
|
892
909
|
# @return [CArray]
|
|
893
|
-
def correlate
|
|
910
|
+
def correlate(kernel, min_count: nil, fill_value: nil)
|
|
911
|
+
CArray.send(:refuse_ruby_array, "correlate", "a kernel") if kernel.is_a?(Array)
|
|
894
912
|
unless kernel.shape == @widths
|
|
895
913
|
raise ArgumentError,
|
|
896
914
|
"correlate: kernel shape #{kernel.shape.inspect} != " \
|
|
@@ -917,15 +935,16 @@ class CAWindowIterator < CAIterator
|
|
|
917
935
|
# symmetric kernel.
|
|
918
936
|
# @param kernel [CArray] weights shaped like a single window.
|
|
919
937
|
# @return [CArray]
|
|
920
|
-
def convolve
|
|
938
|
+
def convolve(kernel, min_count: nil, fill_value: nil)
|
|
939
|
+
CArray.send(:refuse_ruby_array, "convolve", "a kernel") if kernel.is_a?(Array)
|
|
921
940
|
correlate(reverse_all_axes(kernel), min_count: min_count, fill_value: fill_value)
|
|
922
941
|
end
|
|
923
942
|
|
|
924
943
|
private
|
|
925
944
|
|
|
926
|
-
# Reverse a kernel on every axis (`CArray#
|
|
927
|
-
def reverse_all_axes
|
|
928
|
-
kernel.
|
|
945
|
+
# Reverse a kernel on every axis (`CArray#flip` with no argument flips all axes at once).
|
|
946
|
+
def reverse_all_axes(kernel)
|
|
947
|
+
kernel.flip
|
|
929
948
|
end
|
|
930
949
|
|
|
931
950
|
public
|
|
@@ -940,17 +959,18 @@ class CAWindowIterator < CAIterator
|
|
|
940
959
|
# @overload median(min_count: nil, fill_value: nil)
|
|
941
960
|
# Rolling median.
|
|
942
961
|
# @return [CArray]
|
|
943
|
-
def median
|
|
944
|
-
kw = CAIterator.
|
|
962
|
+
def median(**kw)
|
|
963
|
+
kw = CAIterator.send(:reduce_keywords, :median, kw)
|
|
945
964
|
order_stat { |view, axis| view.median(axis: axis, **kw) }
|
|
946
965
|
end
|
|
947
966
|
|
|
948
|
-
# @overload percentile(
|
|
949
|
-
# Rolling percentile(s)
|
|
950
|
-
# an
|
|
967
|
+
# @overload percentile(p, min_count: nil, fill_value: nil)
|
|
968
|
+
# Rolling percentile(s), in the form `CArray#percentile` returns: one p
|
|
969
|
+
# gives one CArray, an Array of p an Array of CArrays, and several p
|
|
970
|
+
# (or a CArray of p) one CArray with the p axes in front.
|
|
951
971
|
# @return [CArray, Array<CArray>]
|
|
952
|
-
def percentile
|
|
953
|
-
kw = CAIterator.
|
|
972
|
+
def percentile(*pers, **kw)
|
|
973
|
+
kw = CAIterator.send(:reduce_keywords, :percentile, kw)
|
|
954
974
|
order_stat { |view, axis| view.percentile(*pers, axis: axis, **kw) }
|
|
955
975
|
end
|
|
956
976
|
|
|
@@ -958,8 +978,10 @@ class CAWindowIterator < CAIterator
|
|
|
958
978
|
# Rolling five-number summary `[min, Q1, median, Q3, max]` (five CArrays),
|
|
959
979
|
# as `CArray#quantile`.
|
|
960
980
|
# @return [Array<CArray>]
|
|
961
|
-
def quantile
|
|
962
|
-
|
|
981
|
+
def quantile(*args, **kw)
|
|
982
|
+
CAIterator.send(:refuse_quantile_arguments, args)
|
|
983
|
+
kw = CAIterator.send(:reduce_keywords, :quantile, kw)
|
|
984
|
+
order_stat { |view, axis| view.quantile(axis: axis, **kw) }
|
|
963
985
|
end
|
|
964
986
|
|
|
965
987
|
private
|
|
@@ -987,22 +1009,28 @@ class CAWindowIterator < CAIterator
|
|
|
987
1009
|
# @overload wsum(weights)
|
|
988
1010
|
# Rolling weighted sum, `weights` shaped like a single window.
|
|
989
1011
|
# @return [CArray]
|
|
990
|
-
|
|
991
|
-
|
|
1012
|
+
# @raise [TypeError] when `weights` is a number, not one weight per
|
|
1013
|
+
# cell of a window.
|
|
1014
|
+
def wsum(weights, **kw)
|
|
1015
|
+
kw = CAIterator.send(:reduce_keywords, :wsum, kw)
|
|
992
1016
|
weighted(weights) { |sv, w, axis| sv.wsum(w, axis: axis, **kw) }
|
|
993
1017
|
end
|
|
994
1018
|
|
|
995
1019
|
# @overload wmean(weights)
|
|
996
1020
|
# Rolling weighted mean, `weights` shaped like a single window.
|
|
997
1021
|
# @return [CArray]
|
|
998
|
-
|
|
999
|
-
|
|
1022
|
+
# @raise [TypeError] when `weights` is a number, not one weight per
|
|
1023
|
+
# cell of a window.
|
|
1024
|
+
def wmean(weights, **kw)
|
|
1025
|
+
kw = CAIterator.send(:reduce_keywords, :wmean, kw)
|
|
1000
1026
|
weighted(weights) { |sv, w, axis| sv.wmean(w, axis: axis, **kw) }
|
|
1001
1027
|
end
|
|
1002
1028
|
|
|
1003
1029
|
private
|
|
1004
1030
|
|
|
1005
|
-
def weighted
|
|
1031
|
+
def weighted(weights)
|
|
1032
|
+
CArray.send(:refuse_ruby_array, "wsum/wmean", "a weight array") if weights.is_a?(Array)
|
|
1033
|
+
CArray.send(:refuse_scalar_weights, "wsum/wmean", "a window") if weights.is_a?(Numeric)
|
|
1006
1034
|
unless weights.shape == @widths
|
|
1007
1035
|
raise ArgumentError,
|
|
1008
1036
|
"wsum/wmean: weights shape #{weights.shape.inspect} != " \
|
|
@@ -1049,11 +1077,15 @@ class CAWindowIterator < CAIterator
|
|
|
1049
1077
|
# @yieldparam window [CArray]
|
|
1050
1078
|
# @return [CArray] reference-shaped (or shrunk, for :truncate)
|
|
1051
1079
|
# @overload reduce(init) { |acc, elem| ... }
|
|
1052
|
-
# Per-window fiber fold: each window's cells are folded element
|
|
1053
|
-
#
|
|
1080
|
+
# Per-window fiber fold: each window's present cells are folded element
|
|
1081
|
+
# by element from a copy of `init` (`init.dup`). A masked cell and a cell
|
|
1082
|
+
# outside the array (the `:skip` margin) are not passed.
|
|
1054
1083
|
# @param init [Object] initial accumulator.
|
|
1055
1084
|
# @return [CArray]
|
|
1056
|
-
def reduce
|
|
1085
|
+
def reduce(*args, data_type: nil, &blk)
|
|
1086
|
+
if args.size > 1
|
|
1087
|
+
raise ArgumentError, "reduce: wrong number of arguments (given #{args.size}, expected 0..1)"
|
|
1088
|
+
end
|
|
1057
1089
|
raise LocalJumpError, "no block given (yield)" unless blk
|
|
1058
1090
|
dt = data_type || CA_OBJECT
|
|
1059
1091
|
out = CArray.new(dt, @shape)
|
|
@@ -1064,8 +1096,8 @@ class CAWindowIterator < CAIterator
|
|
|
1064
1096
|
else
|
|
1065
1097
|
init = args[0]
|
|
1066
1098
|
each_anchor_index do |idx|
|
|
1067
|
-
acc = init
|
|
1068
|
-
sv[*idx, *nils].each { |e| acc = blk.call(acc, e) }
|
|
1099
|
+
acc = init.dup
|
|
1100
|
+
sv[*idx, *nils].each { |e| acc = blk.call(acc, e) unless UNDEF.equal?(e) }
|
|
1069
1101
|
out[*idx] = acc
|
|
1070
1102
|
end
|
|
1071
1103
|
end
|
|
@@ -1077,7 +1109,7 @@ class CAWindowIterator < CAIterator
|
|
|
1077
1109
|
# an element-wise transform has no well-defined scatter-back. Raises
|
|
1078
1110
|
# NotImplementedError; use {#reduce} for a custom per-window fold.
|
|
1079
1111
|
# @raise [NotImplementedError]
|
|
1080
|
-
def map
|
|
1112
|
+
def map(*)
|
|
1081
1113
|
raise NotImplementedError,
|
|
1082
1114
|
"#{self.class} has no map: overlapping windows share cells, so an " \
|
|
1083
1115
|
"element-wise scatter-back is ill-defined; use reduce for a custom " \
|
|
@@ -1091,7 +1123,7 @@ class CAWindowIterator < CAIterator
|
|
|
1091
1123
|
# NotImplementedError. (min_addr / max_addr are fine: the single winning
|
|
1092
1124
|
# cell of a window is a real source cell.)
|
|
1093
1125
|
# @raise [NotImplementedError]
|
|
1094
|
-
def sort_addr
|
|
1126
|
+
def sort_addr(*)
|
|
1095
1127
|
raise NotImplementedError,
|
|
1096
1128
|
"#{self.class} has no sort_addr: padded boundary cells have no " \
|
|
1097
1129
|
"source address and overlapping windows share cells, so a per-window " \
|
|
@@ -1155,7 +1187,7 @@ class CAWindowIterator < CAIterator
|
|
|
1155
1187
|
# position in one piece; overlapping windows put a cell in many windows.
|
|
1156
1188
|
# Use `source.lag(n, axis:)` for the lag along an axis.
|
|
1157
1189
|
# @raise [NotImplementedError]
|
|
1158
|
-
def lag
|
|
1190
|
+
def lag(*)
|
|
1159
1191
|
raise NotImplementedError,
|
|
1160
1192
|
"#{self.class} has no lag: a cell belongs to many overlapping windows, " \
|
|
1161
1193
|
"so it has no single position to step back from; use " \
|
|
@@ -1167,22 +1199,22 @@ class CAWindowIterator < CAIterator
|
|
|
1167
1199
|
# windows, so it has no single value to be filled with. Use
|
|
1168
1200
|
# `source.strip_mask(method:, axis:)` for a fill along an axis.
|
|
1169
1201
|
# @raise [NotImplementedError]
|
|
1170
|
-
def strip_mask
|
|
1202
|
+
def strip_mask(*, **)
|
|
1171
1203
|
raise NotImplementedError,
|
|
1172
1204
|
"#{self.class} has no strip_mask: a cell belongs to many overlapping " \
|
|
1173
1205
|
"windows, so it has no single fill; use source.strip_mask(method:, axis:) " \
|
|
1174
1206
|
"for a fill along an axis."
|
|
1175
1207
|
end
|
|
1176
1208
|
|
|
1177
|
-
# @!method
|
|
1209
|
+
# @!method rank_index
|
|
1178
1210
|
# Not supported for a window iterator: a cell belongs to many overlapping
|
|
1179
|
-
# windows, so it has no single rank. Use `source.
|
|
1211
|
+
# windows, so it has no single rank. Use `source.rank_index(axis:)` for the
|
|
1180
1212
|
# rank along an axis.
|
|
1181
1213
|
# @raise [NotImplementedError]
|
|
1182
|
-
def
|
|
1214
|
+
def rank_index(*, **)
|
|
1183
1215
|
raise NotImplementedError,
|
|
1184
|
-
"#{self.class} has no
|
|
1185
|
-
"so it has no single rank; use source.
|
|
1216
|
+
"#{self.class} has no rank_index: a cell belongs to many overlapping windows, " \
|
|
1217
|
+
"so it has no single rank; use source.rank_index(axis:) for the rank along " \
|
|
1186
1218
|
"an axis."
|
|
1187
1219
|
end
|
|
1188
1220
|
|
|
@@ -1205,19 +1237,19 @@ class CArray
|
|
|
1205
1237
|
# @overload windows(*ranges, bounds: :skip, fill_value: nil)
|
|
1206
1238
|
# Returns a {CAWindowIterator} rolling a per-axis offset window over
|
|
1207
1239
|
# `self`. Each `ranges[i]` is a `lo..hi` offset span (`a.windows(-1..1)`
|
|
1208
|
-
# is a centred width-3 window); `bounds:` selects the margin policy.
|
|
1209
|
-
# no ranges (`a.windows(a.window(...))` passing a CAWindow view) the
|
|
1210
|
-
# geometry is read from the view for backward compatibility.
|
|
1240
|
+
# is a centred width-3 window); `bounds:` selects the margin policy.
|
|
1211
1241
|
# @param ranges [Array<Range>] per-axis offset ranges.
|
|
1212
|
-
# @param bounds [Symbol] `:skip` / `:nearest` / `:truncate`.
|
|
1213
|
-
# @param fill_value [Object, nil] constant margin value
|
|
1242
|
+
# @param bounds [Symbol] `:skip` / `:nearest` / `:fill` / `:truncate`.
|
|
1243
|
+
# @param fill_value [Object, nil] constant margin value; alone it makes
|
|
1244
|
+
# the margin `:fill`, and with another `bounds:` it is refused.
|
|
1214
1245
|
# @return [CAWindowIterator]
|
|
1215
1246
|
# @overload windows(on:, before: nil, after: nil, exclude: :lower, by: nil, interval: nil, at: nil)
|
|
1216
1247
|
# Returns a {CAKeyWindowIterator}: a window per value bounded by a width
|
|
1217
1248
|
# of the key `on` (a time, a duration or a number) instead of a count of
|
|
1218
1249
|
# cells. The window of a value whose key is t holds the values whose
|
|
1219
1250
|
# key lies between t - before and t + after; `exclude:` leaves out the
|
|
1220
|
-
# lower end (default
|
|
1251
|
+
# lower end (default, so a value on the boundary of two windows is in
|
|
1252
|
+
# one of them), the upper end, both or neither. `by:` keeps
|
|
1221
1253
|
# each window within the values with the same by keys, and `interval:`
|
|
1222
1254
|
# gives the key's grid step for {CAKeyWindowIterator#is_complete}. A
|
|
1223
1255
|
# value whose key is masked, NaN or infinite is in no window and has no
|
|
@@ -1228,7 +1260,12 @@ class CArray
|
|
|
1228
1260
|
# temp.windows(on: time, before: "1 hour").mean
|
|
1229
1261
|
# temp.windows(on: time, at: hours, after: "3 hours", exclude: :upper).mean
|
|
1230
1262
|
# @return [CAKeyWindowIterator]
|
|
1231
|
-
|
|
1263
|
+
# The keywords of a window on a key (on: aside), which an offset window
|
|
1264
|
+
# refuses as needing on:.
|
|
1265
|
+
KEY_WINDOW_KEYWORDS = %i[before after exclude by interval at].freeze
|
|
1266
|
+
private_constant :KEY_WINDOW_KEYWORDS
|
|
1267
|
+
|
|
1268
|
+
def windows(*ranges, on: nil, **key_window)
|
|
1232
1269
|
unless on.nil?
|
|
1233
1270
|
unless ranges.empty?
|
|
1234
1271
|
raise ArgumentError, "windows: give offset ranges or on:, not both"
|
|
@@ -1243,15 +1280,19 @@ class CArray
|
|
|
1243
1280
|
require "carray/key_windows"
|
|
1244
1281
|
return CAKeyWindowIterator.new(self, on: on, **key_window)
|
|
1245
1282
|
end
|
|
1246
|
-
bounds = key_window.delete(:bounds)
|
|
1283
|
+
bounds = key_window.delete(:bounds)
|
|
1247
1284
|
fill_value = key_window.delete(:fill_value)
|
|
1248
|
-
|
|
1285
|
+
keyed = key_window.keys & KEY_WINDOW_KEYWORDS
|
|
1286
|
+
unknown = key_window.keys - KEY_WINDOW_KEYWORDS
|
|
1287
|
+
unless unknown.empty?
|
|
1249
1288
|
raise ArgumentError,
|
|
1250
|
-
"windows: #{
|
|
1251
|
-
"
|
|
1289
|
+
"windows: unknown keyword#{unknown.size > 1 ? 's' : ''}: " \
|
|
1290
|
+
"#{unknown.map(&:inspect).join(', ')}"
|
|
1252
1291
|
end
|
|
1253
|
-
|
|
1254
|
-
|
|
1292
|
+
unless keyed.empty?
|
|
1293
|
+
raise ArgumentError,
|
|
1294
|
+
"windows: #{keyed.map { |k| "#{k}:" }.join(', ')} " \
|
|
1295
|
+
"need on:, the key the widths are measured on"
|
|
1255
1296
|
end
|
|
1256
1297
|
CAWindowIterator.new(self, *ranges, bounds: bounds, fill_value: fill_value)
|
|
1257
1298
|
end
|