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/frame/windows.rb
CHANGED
|
@@ -7,7 +7,7 @@ class CAFrame
|
|
|
7
7
|
# t + after. +exclude:+ leaves out the lower end (default), the upper end,
|
|
8
8
|
# both or neither, as CArray#is_between does. +by:+ names columns that
|
|
9
9
|
# must match exactly, so each window stays within one station; +interval:+
|
|
10
|
-
# gives the key's grid step for
|
|
10
|
+
# gives the key's grid step for CAWindowedFrame#is_complete. The rows need
|
|
11
11
|
# not be sorted; the answers come in row order. A row whose key is
|
|
12
12
|
# masked, NaN or infinite is in no window and has no window.
|
|
13
13
|
#
|
|
@@ -28,8 +28,8 @@ class CAFrame
|
|
|
28
28
|
# df.windows(on: "time", at: grid, after: "3 hours", exclude: :upper).mean
|
|
29
29
|
# # => a frame with a row per hour, the mean over [t, t + 3 hours)
|
|
30
30
|
#
|
|
31
|
-
# @return [
|
|
32
|
-
def windows
|
|
31
|
+
# @return [CAWindowedFrame]
|
|
32
|
+
def windows(on:, before: nil, after: nil, exclude: :lower, by: nil, interval: nil, at: nil)
|
|
33
33
|
on_name = column_key(on, "windows")
|
|
34
34
|
key = key_column(on_name, "windows")
|
|
35
35
|
by_names = by.nil? ? [] : column_keys(by.is_a?(Array) ? by : [by], "windows")
|
|
@@ -41,16 +41,16 @@ class CAFrame
|
|
|
41
41
|
before: before, after: after, exclude: exclude,
|
|
42
42
|
by: by_cols.empty? ? nil : by_cols,
|
|
43
43
|
interval: interval, at: at)
|
|
44
|
-
|
|
44
|
+
CAWindowedFrame.new(self, plan, [on_name, *by_names], at: at)
|
|
45
45
|
end
|
|
46
46
|
end
|
|
47
47
|
|
|
48
48
|
# The windows of a CAFrame (CAFrame#windows): the reductions over each row's
|
|
49
49
|
# window, one answer per row, and what each window covered.
|
|
50
|
-
class
|
|
50
|
+
class CAWindowedFrame
|
|
51
51
|
include CAFrame::ColumnVerbs
|
|
52
52
|
|
|
53
|
-
def initialize
|
|
53
|
+
def initialize(frame, plan, key_names, at: nil)
|
|
54
54
|
@frame = frame
|
|
55
55
|
@plan = plan
|
|
56
56
|
@key_names = key_names
|
|
@@ -60,10 +60,42 @@ class WindowedFrame
|
|
|
60
60
|
# @overload [](name)
|
|
61
61
|
# Returns the CAKeyWindowIterator of one column.
|
|
62
62
|
# @return [CAKeyWindowIterator]
|
|
63
|
-
def []
|
|
64
|
-
@plan.for_value
|
|
63
|
+
def [](name)
|
|
64
|
+
@plan.send(:for_value, @frame.__send__(:named_column, @frame.__send__(:column_key, name, "windows"), "windows"))
|
|
65
65
|
end
|
|
66
66
|
|
|
67
|
+
# @!group What each window covered
|
|
68
|
+
|
|
69
|
+
# @!method first_addr
|
|
70
|
+
# Returns the first row of each row's window (int64), UNDEF where the
|
|
71
|
+
# window is empty or the row has none, as {CAKeyWindowIterator#first_addr} does.
|
|
72
|
+
# @return [CArray]
|
|
73
|
+
|
|
74
|
+
# @!method last_addr
|
|
75
|
+
# Returns the last row of each row's window (int64), UNDEF where the
|
|
76
|
+
# window is empty or the row has none, as {CAKeyWindowIterator#last_addr} does.
|
|
77
|
+
# @return [CArray]
|
|
78
|
+
|
|
79
|
+
# @!method first_key
|
|
80
|
+
# Returns the key of each window's first row, as {CAKeyWindowIterator#first_key} does.
|
|
81
|
+
# @return [CArray]
|
|
82
|
+
|
|
83
|
+
# @!method last_key
|
|
84
|
+
# Returns the key of each window's last row, as {CAKeyWindowIterator#last_key} does.
|
|
85
|
+
# @return [CArray]
|
|
86
|
+
|
|
87
|
+
# @!method elements
|
|
88
|
+
# Returns the number of rows in each window (int64), masked values
|
|
89
|
+
# included; UNDEF for a row with no window, as {CAKeyWindowIterator#elements} does.
|
|
90
|
+
# @return [CArray]
|
|
91
|
+
|
|
92
|
+
# @!method is_complete
|
|
93
|
+
# Returns whether each window holds a row at every grid point it was
|
|
94
|
+
# asked to cover (boolean). Needs `interval:`, as {CAKeyWindowIterator#is_complete} does.
|
|
95
|
+
# @return [CArray]
|
|
96
|
+
|
|
97
|
+
# @!endgroup
|
|
98
|
+
|
|
67
99
|
# What each window covered, one value per row: see CAKeyWindowIterator.
|
|
68
100
|
[:first_addr, :last_addr, :first_key, :last_key, :elements, :is_complete].each do |m|
|
|
69
101
|
define_method(m, &Ractor.make_shareable(proc { @plan.public_send(m) }))
|
|
@@ -77,6 +109,181 @@ class WindowedFrame
|
|
|
77
109
|
@plan.bounds
|
|
78
110
|
end
|
|
79
111
|
|
|
112
|
+
# @!group Reductions over each window
|
|
113
|
+
|
|
114
|
+
# @!method sum(*names, min_count: nil, fill_value: nil)
|
|
115
|
+
# Returns each window's sum, as {CArray#sum} computes it.
|
|
116
|
+
# @param names [Array<String>] the columns; default every column whose
|
|
117
|
+
# type has the reduction.
|
|
118
|
+
# @param min_count [Integer, nil] the present values a window needs;
|
|
119
|
+
# a window with fewer answers UNDEF.
|
|
120
|
+
# @param fill_value [Object, Hash, nil] the answer in place of UNDEF,
|
|
121
|
+
# one value or one per column name.
|
|
122
|
+
# @return [CAFrame, CArray] a CArray for one name, a frame otherwise;
|
|
123
|
+
# with `at:`, a frame with the points as its index.
|
|
124
|
+
|
|
125
|
+
# @!method mean(*names, min_count: nil, fill_value: nil)
|
|
126
|
+
# Returns each window's mean, as {CArray#mean} computes it.
|
|
127
|
+
# @param names [Array<String>] the columns; default every column whose
|
|
128
|
+
# type has the reduction.
|
|
129
|
+
# @param min_count [Integer, nil] the present values a window needs;
|
|
130
|
+
# a window with fewer answers UNDEF.
|
|
131
|
+
# @param fill_value [Object, Hash, nil] the answer in place of UNDEF,
|
|
132
|
+
# one value or one per column name.
|
|
133
|
+
# @return [CAFrame, CArray] a CArray for one name, a frame otherwise;
|
|
134
|
+
# with `at:`, a frame with the points as its index.
|
|
135
|
+
|
|
136
|
+
# @!method min(*names, min_count: nil, fill_value: nil)
|
|
137
|
+
# Returns each window's minimum, as {CArray#min} computes it.
|
|
138
|
+
# @param names [Array<String>] the columns; default every column whose
|
|
139
|
+
# type has the reduction.
|
|
140
|
+
# @param min_count [Integer, nil] the present values a window needs;
|
|
141
|
+
# a window with fewer answers UNDEF.
|
|
142
|
+
# @param fill_value [Object, Hash, nil] the answer in place of UNDEF,
|
|
143
|
+
# one value or one per column name.
|
|
144
|
+
# @return [CAFrame, CArray] a CArray for one name, a frame otherwise;
|
|
145
|
+
# with `at:`, a frame with the points as its index.
|
|
146
|
+
|
|
147
|
+
# @!method max(*names, min_count: nil, fill_value: nil)
|
|
148
|
+
# Returns each window's maximum, as {CArray#max} computes it.
|
|
149
|
+
# @param names [Array<String>] the columns; default every column whose
|
|
150
|
+
# type has the reduction.
|
|
151
|
+
# @param min_count [Integer, nil] the present values a window needs;
|
|
152
|
+
# a window with fewer answers UNDEF.
|
|
153
|
+
# @param fill_value [Object, Hash, nil] the answer in place of UNDEF,
|
|
154
|
+
# one value or one per column name.
|
|
155
|
+
# @return [CAFrame, CArray] a CArray for one name, a frame otherwise;
|
|
156
|
+
# with `at:`, a frame with the points as its index.
|
|
157
|
+
|
|
158
|
+
# @!method prod(*names, min_count: nil, fill_value: nil)
|
|
159
|
+
# Returns each window's product, as {CArray#prod} computes it.
|
|
160
|
+
# @param names [Array<String>] the columns; default every column whose
|
|
161
|
+
# type has the reduction.
|
|
162
|
+
# @param min_count [Integer, nil] the present values a window needs;
|
|
163
|
+
# a window with fewer answers UNDEF.
|
|
164
|
+
# @param fill_value [Object, Hash, nil] the answer in place of UNDEF,
|
|
165
|
+
# one value or one per column name.
|
|
166
|
+
# @return [CAFrame, CArray] a CArray for one name, a frame otherwise;
|
|
167
|
+
# with `at:`, a frame with the points as its index.
|
|
168
|
+
|
|
169
|
+
# @!method accumulate(*names, min_count: nil, fill_value: nil)
|
|
170
|
+
# Returns each window's sum in the column's own type, as {CArray#accumulate} computes it.
|
|
171
|
+
# @param names [Array<String>] the columns; default every column whose
|
|
172
|
+
# type has the reduction.
|
|
173
|
+
# @param min_count [Integer, nil] the present values a window needs;
|
|
174
|
+
# a window with fewer answers UNDEF.
|
|
175
|
+
# @param fill_value [Object, Hash, nil] the answer in place of UNDEF,
|
|
176
|
+
# one value or one per column name.
|
|
177
|
+
# @return [CAFrame, CArray] a CArray for one name, a frame otherwise;
|
|
178
|
+
# with `at:`, a frame with the points as its index.
|
|
179
|
+
|
|
180
|
+
# @!method median(*names, min_count: nil, fill_value: nil)
|
|
181
|
+
# Returns each window's median, as {CArray#median} computes it.
|
|
182
|
+
# @param names [Array<String>] the columns; default every column whose
|
|
183
|
+
# type has the reduction.
|
|
184
|
+
# @param min_count [Integer, nil] the present values a window needs;
|
|
185
|
+
# a window with fewer answers UNDEF.
|
|
186
|
+
# @param fill_value [Object, Hash, nil] the answer in place of UNDEF,
|
|
187
|
+
# one value or one per column name.
|
|
188
|
+
# @return [CAFrame, CArray] a CArray for one name, a frame otherwise;
|
|
189
|
+
# with `at:`, a frame with the points as its index.
|
|
190
|
+
|
|
191
|
+
# @!method variance(*names, min_count: nil, fill_value: nil)
|
|
192
|
+
# Returns each window's sample variance, as {CArray#variance} computes it.
|
|
193
|
+
# @param names [Array<String>] the columns; default every column whose
|
|
194
|
+
# type has the reduction.
|
|
195
|
+
# @param min_count [Integer, nil] the present values a window needs;
|
|
196
|
+
# a window with fewer answers UNDEF.
|
|
197
|
+
# @param fill_value [Object, Hash, nil] the answer in place of UNDEF,
|
|
198
|
+
# one value or one per column name.
|
|
199
|
+
# @return [CAFrame, CArray] a CArray for one name, a frame otherwise;
|
|
200
|
+
# with `at:`, a frame with the points as its index.
|
|
201
|
+
|
|
202
|
+
# @!method stddev(*names, min_count: nil, fill_value: nil)
|
|
203
|
+
# Returns each window's sample standard deviation, as {CArray#stddev} computes it.
|
|
204
|
+
# @param names [Array<String>] the columns; default every column whose
|
|
205
|
+
# type has the reduction.
|
|
206
|
+
# @param min_count [Integer, nil] the present values a window needs;
|
|
207
|
+
# a window with fewer answers UNDEF.
|
|
208
|
+
# @param fill_value [Object, Hash, nil] the answer in place of UNDEF,
|
|
209
|
+
# one value or one per column name.
|
|
210
|
+
# @return [CAFrame, CArray] a CArray for one name, a frame otherwise;
|
|
211
|
+
# with `at:`, a frame with the points as its index.
|
|
212
|
+
|
|
213
|
+
# @!method variancep(*names, min_count: nil, fill_value: nil)
|
|
214
|
+
# Returns each window's population variance, as {CArray#variancep} computes it.
|
|
215
|
+
# @param names [Array<String>] the columns; default every column whose
|
|
216
|
+
# type has the reduction.
|
|
217
|
+
# @param min_count [Integer, nil] the present values a window needs;
|
|
218
|
+
# a window with fewer answers UNDEF.
|
|
219
|
+
# @param fill_value [Object, Hash, nil] the answer in place of UNDEF,
|
|
220
|
+
# one value or one per column name.
|
|
221
|
+
# @return [CAFrame, CArray] a CArray for one name, a frame otherwise;
|
|
222
|
+
# with `at:`, a frame with the points as its index.
|
|
223
|
+
|
|
224
|
+
# @!method stddevp(*names, min_count: nil, fill_value: nil)
|
|
225
|
+
# Returns each window's population standard deviation, as {CArray#stddevp} computes it.
|
|
226
|
+
# @param names [Array<String>] the columns; default every column whose
|
|
227
|
+
# type has the reduction.
|
|
228
|
+
# @param min_count [Integer, nil] the present values a window needs;
|
|
229
|
+
# a window with fewer answers UNDEF.
|
|
230
|
+
# @param fill_value [Object, Hash, nil] the answer in place of UNDEF,
|
|
231
|
+
# one value or one per column name.
|
|
232
|
+
# @return [CAFrame, CArray] a CArray for one name, a frame otherwise;
|
|
233
|
+
# with `at:`, a frame with the points as its index.
|
|
234
|
+
|
|
235
|
+
# @!method first(*names)
|
|
236
|
+
# Returns each window's first value.
|
|
237
|
+
# @param names [Array<String>] the columns; default every column whose
|
|
238
|
+
# type has the reduction.
|
|
239
|
+
# @return [CAFrame, CArray] a CArray for one name, a frame otherwise;
|
|
240
|
+
# with `at:`, a frame with the points as its index.
|
|
241
|
+
# @raise [ArgumentError] on an unknown keyword.
|
|
242
|
+
|
|
243
|
+
# @!method last(*names)
|
|
244
|
+
# Returns each window's last value.
|
|
245
|
+
# @param names [Array<String>] the columns; default every column whose
|
|
246
|
+
# type has the reduction.
|
|
247
|
+
# @return [CAFrame, CArray] a CArray for one name, a frame otherwise;
|
|
248
|
+
# with `at:`, a frame with the points as its index.
|
|
249
|
+
# @raise [ArgumentError] on an unknown keyword.
|
|
250
|
+
|
|
251
|
+
# @!method nunique(*names)
|
|
252
|
+
# Returns the number of distinct present values in each window.
|
|
253
|
+
# @param names [Array<String>] the columns; default every column whose
|
|
254
|
+
# type has the reduction.
|
|
255
|
+
# @return [CAFrame, CArray] a CArray for one name, a frame otherwise;
|
|
256
|
+
# with `at:`, a frame with the points as its index.
|
|
257
|
+
# @raise [ArgumentError] on an unknown keyword.
|
|
258
|
+
|
|
259
|
+
# @!method count(*names)
|
|
260
|
+
# Returns the number of present values in each window.
|
|
261
|
+
# @param names [Array<String>] the columns; default every column whose
|
|
262
|
+
# type has the reduction.
|
|
263
|
+
# @return [CAFrame, CArray] a CArray for one name, a frame otherwise;
|
|
264
|
+
# with `at:`, a frame with the points as its index.
|
|
265
|
+
# @raise [ArgumentError] on an unknown keyword.
|
|
266
|
+
|
|
267
|
+
# @!method all(*names, skip_masked: false)
|
|
268
|
+
# Returns whether every value of each window is true.
|
|
269
|
+
# @param names [Array<String>] the columns; default every column whose
|
|
270
|
+
# type has the reduction.
|
|
271
|
+
# @param skip_masked [Boolean] as {CArray#all} takes it.
|
|
272
|
+
# @return [CAFrame, CArray] a CArray for one name, a frame otherwise;
|
|
273
|
+
# with `at:`, a frame with the points as its index.
|
|
274
|
+
# @raise [ArgumentError] on an unknown keyword.
|
|
275
|
+
|
|
276
|
+
# @!method any(*names, skip_masked: false)
|
|
277
|
+
# Returns whether a value of each window is true.
|
|
278
|
+
# @param names [Array<String>] the columns; default every column whose
|
|
279
|
+
# type has the reduction.
|
|
280
|
+
# @param skip_masked [Boolean] as {CArray#any} takes it.
|
|
281
|
+
# @return [CAFrame, CArray] a CArray for one name, a frame otherwise;
|
|
282
|
+
# with `at:`, a frame with the points as its index.
|
|
283
|
+
# @raise [ArgumentError] on an unknown keyword.
|
|
284
|
+
|
|
285
|
+
# @!endgroup
|
|
286
|
+
|
|
80
287
|
# The windowed reductions. Without names, a frame of every column whose
|
|
81
288
|
# type has the reduction (the on: and by: columns left out), on the
|
|
82
289
|
# frame's rows and index; given names, the CArray (one), as +df[name]+
|
|
@@ -101,8 +308,8 @@ class WindowedFrame
|
|
|
101
308
|
# @param names [Array<String>] the columns; default every column that has
|
|
102
309
|
# a percentile.
|
|
103
310
|
# @return [CAFrame, CArray] as the other windowed reductions return.
|
|
104
|
-
def percentile
|
|
105
|
-
CAFrame.__send__(:check_percentage, p)
|
|
311
|
+
def percentile(p, *names, min_count: nil, fill_value: nil)
|
|
312
|
+
p = CAFrame.__send__(:check_percentage, p)
|
|
106
313
|
window_reduce(:percentile, names, [p], min_count: min_count, fill_value: fill_value)
|
|
107
314
|
end
|
|
108
315
|
|
|
@@ -112,12 +319,16 @@ class WindowedFrame
|
|
|
112
319
|
#
|
|
113
320
|
# @param names [Array<String>] the columns; default every column that has
|
|
114
321
|
# a quantile.
|
|
322
|
+
# @param min_count [Integer, nil] as for {#percentile}.
|
|
323
|
+
# @param fill_value [Object, Hash, nil] as for {#percentile}.
|
|
115
324
|
# @return [CAFrame]
|
|
116
|
-
def quantile
|
|
117
|
-
|
|
325
|
+
def quantile(*names, min_count: nil, fill_value: nil)
|
|
326
|
+
refuse_unknown_fill_columns(:quantile, fill_value)
|
|
118
327
|
named = {}
|
|
119
328
|
over_named_columns(:quantile, names, @key_names) do |col, name|
|
|
120
|
-
|
|
329
|
+
fill = fill_value.is_a?(Hash) ? fill_value[name] : fill_value
|
|
330
|
+
kw = { min_count: min_count, fill_value: fill }.compact
|
|
331
|
+
named[name] = @plan.send(:for_value, col).quantile(**kw)
|
|
121
332
|
end
|
|
122
333
|
cols = CAFrame.__send__(:quantile_columns, named)
|
|
123
334
|
return CAFrame.new(cols, axis_name: @frame.axis_name, index: @frame.index&.copy) unless @at
|
|
@@ -127,18 +338,18 @@ class WindowedFrame
|
|
|
127
338
|
|
|
128
339
|
private
|
|
129
340
|
|
|
130
|
-
def window_reduction
|
|
341
|
+
def window_reduction(reduction, *names, min_count: nil, fill_value: nil)
|
|
131
342
|
window_reduce(reduction, names, min_count: min_count, fill_value: fill_value)
|
|
132
343
|
end
|
|
133
344
|
|
|
134
|
-
def window_value
|
|
135
|
-
|
|
136
|
-
window_reduce(reduction, names)
|
|
345
|
+
def window_value(reduction, *names, **keywords)
|
|
346
|
+
keywords = value_reduction_keywords(reduction, keywords)
|
|
347
|
+
window_reduce(reduction, names, **keywords)
|
|
137
348
|
end
|
|
138
349
|
|
|
139
350
|
# These reductions take no keyword. Without the **keywords above, Ruby
|
|
140
351
|
# would hand a keyword in as a column name.
|
|
141
|
-
def refuse_keywords
|
|
352
|
+
def refuse_keywords(keywords)
|
|
142
353
|
return if keywords.empty?
|
|
143
354
|
list = keywords.keys.map(&:inspect).join(", ")
|
|
144
355
|
raise ArgumentError, "unknown keyword#{keywords.size > 1 ? "s" : ""}: #{list}"
|
|
@@ -146,7 +357,7 @@ class WindowedFrame
|
|
|
146
357
|
|
|
147
358
|
# An N-D column (each row a profile) is reduced along the rows, each
|
|
148
359
|
# window keeping the trailing shape, as a group reduces it.
|
|
149
|
-
def window_reduce
|
|
360
|
+
def window_reduce(reduction, names, args = [], **keywords)
|
|
150
361
|
keywords = keywords.compact
|
|
151
362
|
if @at && names.include?(@key_names.first)
|
|
152
363
|
raise ArgumentError, "#{reduction}: #{@key_names.first.inspect} is the key, already the result's index"
|
|
@@ -161,9 +372,9 @@ class WindowedFrame
|
|
|
161
372
|
named[name] =
|
|
162
373
|
if %i[first last].include?(reduction) && col.ndim == 1 && args.empty? && kw.empty? &&
|
|
163
374
|
col.face? && !col.__send__(:holds_new_values?)
|
|
164
|
-
col[@plan.for_value
|
|
375
|
+
col[@plan.send(:for_value, present_rows(col)).public_send(reduction)].copy
|
|
165
376
|
else
|
|
166
|
-
@plan.for_value
|
|
377
|
+
@plan.send(:for_value, col).public_send(reduction, *args, **kw)
|
|
167
378
|
end
|
|
168
379
|
end
|
|
169
380
|
out = named if @at && !names.empty?
|
data/lib/carray/frame.rb
CHANGED
|
@@ -2,8 +2,7 @@
|
|
|
2
2
|
#
|
|
3
3
|
# Thin loader. The frame is a small Ruby layer over CArray: columns are
|
|
4
4
|
# genuine CArrays, so every CArray operation (mask / view / Face /
|
|
5
|
-
# memory_view) is reachable by escaping to a column.
|
|
6
|
-
# devel/MEMO_DATAFRAME_ON_CARRAY.md for the design.
|
|
5
|
+
# memory_view) is reachable by escaping to a column.
|
|
7
6
|
|
|
8
7
|
require 'carray/frame/frame'
|
|
9
8
|
require 'carray/frame/column_verbs'
|
data/lib/carray/fuse_source.rb
CHANGED
|
@@ -27,11 +27,11 @@ class CArray
|
|
|
27
27
|
|
|
28
28
|
# Runtime coercion, so the rewrite never has to work out what a name
|
|
29
29
|
# holds: anything that is not an array passes through untouched.
|
|
30
|
-
def self.shadow
|
|
30
|
+
def self.shadow(value)
|
|
31
31
|
value.is_a?(CArray) ? value.lazy : value
|
|
32
32
|
end
|
|
33
33
|
|
|
34
|
-
def self.evaluate
|
|
34
|
+
def self.evaluate(block)
|
|
35
35
|
result = compiled(block).call(block)
|
|
36
36
|
# An expression that is just an array is that array; the shadow put
|
|
37
37
|
# around it has nothing to fuse.
|
|
@@ -53,7 +53,7 @@ class CArray
|
|
|
53
53
|
# A Ractor other than the main one keeps its own table, and evaluates
|
|
54
54
|
# every block in its frame: the empty scope a lambda is compiled in is
|
|
55
55
|
# the top of a file, which only the main Ractor has.
|
|
56
|
-
def self.compiled
|
|
56
|
+
def self.compiled(block)
|
|
57
57
|
site = RubyVM::InstructionSequence.of(block)
|
|
58
58
|
return compile(block) unless site
|
|
59
59
|
sites = Ractor.current == Ractor.main ? SITES : (Ractor.current[:carray_fuse_sites] ||= new_sites)
|
|
@@ -64,7 +64,7 @@ class CArray
|
|
|
64
64
|
# them. `self` is the block's own, so instance variables and methods
|
|
65
65
|
# are what they were.
|
|
66
66
|
Compiled = Struct.new(:locals, :lambda) do
|
|
67
|
-
def call
|
|
67
|
+
def call(block)
|
|
68
68
|
scope = block.binding
|
|
69
69
|
values = locals.map { |name| scope.local_variable_get(name) }
|
|
70
70
|
scope.receiver.instance_exec(*values, &lambda)
|
|
@@ -75,7 +75,7 @@ class CArray
|
|
|
75
75
|
# or yields, or asks for its binding -- is evaluated there every time;
|
|
76
76
|
# only the rewrite is kept.
|
|
77
77
|
InFrame = Struct.new(:text, :file, :line) do
|
|
78
|
-
def call
|
|
78
|
+
def call(block)
|
|
79
79
|
eval(text, block.binding, file, line)
|
|
80
80
|
end
|
|
81
81
|
end
|
|
@@ -89,7 +89,7 @@ class CArray
|
|
|
89
89
|
Prism::BreakNode, Prism::NextNode,
|
|
90
90
|
Prism::RedoNode].freeze
|
|
91
91
|
|
|
92
|
-
def self.compile
|
|
92
|
+
def self.compile(block)
|
|
93
93
|
body = body_source(block)
|
|
94
94
|
text = rewrite(body)
|
|
95
95
|
file, line = block.source_location
|
|
@@ -102,7 +102,7 @@ class CArray
|
|
|
102
102
|
# Read on its own, a body names a local from outside as a call with no
|
|
103
103
|
# receiver and no arguments, so every one of them is found this way. A
|
|
104
104
|
# body that assigns to one has to keep its frame.
|
|
105
|
-
def self.free_locals
|
|
105
|
+
def self.free_locals(body, outer)
|
|
106
106
|
reads = []
|
|
107
107
|
needs_frame = false
|
|
108
108
|
Prism.parse(body).value.breadth_first_search do |node|
|
|
@@ -127,7 +127,7 @@ class CArray
|
|
|
127
127
|
# it sits in is reopened around the lambda, outermost first. The
|
|
128
128
|
# refinements active there are activated again. None of it sees the
|
|
129
129
|
# frame of the call that compiles it.
|
|
130
|
-
def self.compile_lambda
|
|
130
|
+
def self.compile_lambda(text, locals, scope, file, line)
|
|
131
131
|
nesting = scope.eval("Module.nesting").reverse
|
|
132
132
|
refinements = scope.eval("Module.used_modules")
|
|
133
133
|
Thread.current[:__carray_fuse_scope__] = [nesting, refinements]
|
|
@@ -145,7 +145,7 @@ class CArray
|
|
|
145
145
|
|
|
146
146
|
# -- the block's own text ---------------------------------------------
|
|
147
147
|
|
|
148
|
-
def self.body_source
|
|
148
|
+
def self.body_source(block)
|
|
149
149
|
text = extract(block)
|
|
150
150
|
wrapped = "proc " + text
|
|
151
151
|
node = Prism.parse(wrapped).value
|
|
@@ -158,7 +158,7 @@ class CArray
|
|
|
158
158
|
wrapped.byteslice(inner.location.start_offset...inner.location.end_offset)
|
|
159
159
|
end
|
|
160
160
|
|
|
161
|
-
def self.extract
|
|
161
|
+
def self.extract(block)
|
|
162
162
|
sequence = RubyVM::InstructionSequence.of(block) rescue nil
|
|
163
163
|
path = sequence && (sequence.absolute_path || sequence.path)
|
|
164
164
|
location = path && File.readable?(path) && code_location(sequence, path)
|
|
@@ -188,7 +188,7 @@ class CArray
|
|
|
188
188
|
# Ractor may be running them, and that can crash the process. Two Ractors
|
|
189
189
|
# dumping one sequence for the first time crash Ruby before 4.1, so the
|
|
190
190
|
# dump goes through a lock.
|
|
191
|
-
def self.code_location
|
|
191
|
+
def self.code_location(sequence, path)
|
|
192
192
|
line = sequence.first_lineno
|
|
193
193
|
found = []
|
|
194
194
|
pending = [Prism.parse_file(path).value]
|
|
@@ -224,16 +224,16 @@ class CArray
|
|
|
224
224
|
@spots = []
|
|
225
225
|
end
|
|
226
226
|
|
|
227
|
-
def visit_local_variable_read_node
|
|
228
|
-
def visit_instance_variable_read_node
|
|
229
|
-
def visit_constant_read_node
|
|
227
|
+
def visit_local_variable_read_node(node) = mark(node)
|
|
228
|
+
def visit_instance_variable_read_node(node) = mark(node)
|
|
229
|
+
def visit_constant_read_node(node) = mark(node)
|
|
230
230
|
|
|
231
231
|
# `Math::PI` is one name, not `Math` with something after it.
|
|
232
|
-
def visit_constant_path_node
|
|
232
|
+
def visit_constant_path_node(node)
|
|
233
233
|
mark(node)
|
|
234
234
|
end
|
|
235
235
|
|
|
236
|
-
def visit_call_node
|
|
236
|
+
def visit_call_node(node)
|
|
237
237
|
if node.name == :[] || node.name == :[]=
|
|
238
238
|
# An index is a position, not a value to fuse: `a[i]` shadows `a`
|
|
239
239
|
# and leaves `i` alone.
|
|
@@ -246,25 +246,28 @@ class CArray
|
|
|
246
246
|
|
|
247
247
|
private
|
|
248
248
|
|
|
249
|
-
def mark
|
|
249
|
+
def mark(node)
|
|
250
250
|
@spots << [node.location.start_offset, node.location.end_offset]
|
|
251
251
|
end
|
|
252
252
|
end
|
|
253
253
|
|
|
254
|
-
def self.rewrite
|
|
254
|
+
def self.rewrite(source)
|
|
255
255
|
visitor = Leaves.new
|
|
256
256
|
Prism.parse(source).value.accept(visitor)
|
|
257
257
|
out = source.dup
|
|
258
258
|
visitor.spots.sort_by { |start, _| -start }.each do |start, stop|
|
|
259
|
-
out[start...stop] = "::CArray
|
|
259
|
+
out[start...stop] = "::CArray.const_get(:FuseSource).shadow(#{source[start...stop]})"
|
|
260
260
|
end
|
|
261
261
|
out
|
|
262
262
|
end
|
|
263
263
|
end
|
|
264
|
+
# Loading this file defines the constant again, which drops the
|
|
265
|
+
# private_constant set beside the autoload.
|
|
266
|
+
private_constant :FuseSource
|
|
264
267
|
end
|
|
265
268
|
|
|
266
269
|
# A scope with no locals and no refinements in it, for compiling a block's
|
|
267
270
|
# expression where it can neither see nor hold on to the frame of the call
|
|
268
271
|
# that happens to compile it. Both are true only of the top of a file.
|
|
269
|
-
CArray
|
|
270
|
-
CArray
|
|
272
|
+
CArray.const_get(:FuseSource)::EMPTY_SCOPE = -> { binding }
|
|
273
|
+
CArray.const_get(:FuseSource).private_constant :EMPTY_SCOPE
|