carray 3.0.6 → 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.
Files changed (277) hide show
  1. checksums.yaml +4 -4
  2. data/.yardopts +1 -1
  3. data/CHANGELOG.md +7 -1830
  4. data/README.md +62 -35
  5. data/carray.gemspec +16 -4
  6. data/docs/changelogs/CHANGELOG-3.0.md +2464 -0
  7. data/ext/ca_array_pool.c +4 -5
  8. data/ext/ca_axis_descriptor.h +45 -30
  9. data/ext/ca_axis_dispatch.c +7 -4
  10. data/ext/ca_axis_group.c +12 -15
  11. data/ext/ca_bincmp_dispatch.h +16 -38
  12. data/ext/ca_binop_dispatch.c +35 -0
  13. data/ext/ca_binop_dispatch.h +22 -46
  14. data/ext/ca_categorical_iterator.c +36 -34
  15. data/ext/ca_compare.c +0 -1
  16. data/ext/ca_compare.h +8 -1
  17. data/ext/ca_composite_dispatch.h +6 -0
  18. data/ext/ca_for_buffer.h +6 -1
  19. data/ext/ca_group_iter.c +17 -18
  20. data/ext/ca_iter_state_fields.h +196 -0
  21. data/ext/ca_iter_substrate.h +84 -103
  22. data/ext/ca_kernel_iterator.c +125 -89
  23. data/ext/ca_kernel_iterator.h +229 -1504
  24. data/ext/ca_kernel_iterator_internal.h +1167 -0
  25. data/ext/ca_moncmp_dispatch.h +17 -24
  26. data/ext/ca_monop_dispatch.h +25 -74
  27. data/ext/ca_obj_array.c +92 -165
  28. data/ext/ca_obj_bincmp.c +65 -48
  29. data/ext/ca_obj_binop.c +74 -49
  30. data/ext/ca_obj_bitarray.c +7 -21
  31. data/ext/ca_obj_bitfield.c +93 -26
  32. data/ext/ca_obj_block.c +11 -12
  33. data/ext/ca_obj_byte_swap.c +34 -34
  34. data/ext/ca_obj_const_string.c +464 -304
  35. data/ext/ca_obj_face.c +167 -121
  36. data/ext/ca_obj_face.h +32 -170
  37. data/ext/ca_obj_fake.c +127 -34
  38. data/ext/ca_obj_farray.c +13 -9
  39. data/ext/ca_obj_field.c +17 -13
  40. data/ext/ca_obj_fixlen_string.c +57 -34
  41. data/ext/ca_obj_grid.c +149 -113
  42. data/ext/ca_obj_meld.c +28 -26
  43. data/ext/ca_obj_moncmp.c +10 -21
  44. data/ext/ca_obj_monop.c +88 -73
  45. data/ext/ca_obj_object.c +60 -46
  46. data/ext/ca_obj_record.c +74 -22
  47. data/ext/ca_obj_reduce.c +10 -14
  48. data/ext/ca_obj_refer.c +146 -39
  49. data/ext/ca_obj_remap.c +11 -29
  50. data/ext/ca_obj_repeat.c +41 -23
  51. data/ext/ca_obj_roll.c +20 -28
  52. data/ext/ca_obj_select.c +17 -27
  53. data/ext/ca_obj_select_axis.c +35 -42
  54. data/ext/ca_obj_shift.c +22 -42
  55. data/ext/ca_obj_source.c +1 -5
  56. data/ext/ca_obj_stack.c +33 -29
  57. data/ext/ca_obj_stride.c +99 -62
  58. data/ext/ca_obj_string.c +8 -20
  59. data/ext/ca_obj_tile.c +14 -16
  60. data/ext/ca_obj_time.c +12 -23
  61. data/ext/ca_obj_timedelta.c +12 -23
  62. data/ext/ca_obj_transpose.c +8 -10
  63. data/ext/ca_obj_triop.c +48 -49
  64. data/ext/ca_obj_window.c +87 -99
  65. data/ext/ca_op_cmplx64.h +4 -0
  66. data/ext/ca_op_ipower.c +97 -27
  67. data/ext/ca_op_powi.h +9 -28
  68. data/ext/ca_pow5_table.h +2 -1
  69. data/ext/ca_rng_normal.h +4 -0
  70. data/ext/ca_rng_xoshiro256pp.h +3 -0
  71. data/ext/ca_sort_kernels.h +23 -26
  72. data/ext/ca_sweep_engine.c +92 -26
  73. data/ext/ca_sweep_engine.h +22 -15
  74. data/ext/ca_transform_common.c +3 -8
  75. data/ext/ca_triop_dispatch.h +20 -32
  76. data/ext/caframe_csv_reader.c +12 -12
  77. data/ext/carray.h +300 -1009
  78. data/ext/carray_access.c +320 -207
  79. data/ext/carray_address_basis.c +128 -59
  80. data/ext/carray_attribute.c +81 -104
  81. data/ext/carray_bincount.c +12 -14
  82. data/ext/carray_broadcast.c +31 -27
  83. data/ext/carray_call_cfunc.c +174 -285
  84. data/ext/carray_call_cfunc.h +36 -28
  85. data/ext/carray_cast.c +1191 -351
  86. data/ext/carray_cast_func.rb +68 -8
  87. data/ext/carray_class.c +6 -11
  88. data/ext/carray_conversion.c +98 -39
  89. data/ext/carray_copy.c +52 -26
  90. data/ext/carray_core.c +286 -132
  91. data/ext/carray_count.c +53 -22
  92. data/ext/carray_element.c +49 -28
  93. data/ext/carray_factorize.c +64 -63
  94. data/ext/carray_generate.c +26 -19
  95. data/ext/carray_histogram.c +9 -11
  96. data/ext/carray_hold.c +7 -9
  97. data/ext/carray_index_classifier.c +127 -230
  98. data/ext/carray_index_classifier.h +11 -10
  99. data/ext/carray_integer_total.c +523 -0
  100. data/ext/carray_internal.h +1364 -156
  101. data/ext/carray_key_windows.c +3 -3
  102. data/ext/carray_lazy.c +37 -37
  103. data/ext/carray_loop.c +27 -23
  104. data/ext/carray_mask.c +235 -150
  105. data/ext/carray_math_kernel.h +23 -47
  106. data/ext/carray_mathfunc.c +10 -3
  107. data/ext/carray_median_percentile.c +106 -100
  108. data/ext/carray_memory_view.c +83 -69
  109. data/ext/carray_operator.c +490 -128
  110. data/ext/carray_order.c +163 -84
  111. data/ext/carray_partition.c +14 -16
  112. data/ext/carray_random.c +48 -42
  113. data/ext/carray_round.c +33 -5
  114. data/ext/carray_scatter.c +57 -39
  115. data/ext/carray_segment.c +42 -46
  116. data/ext/carray_slab.c +77 -51
  117. data/ext/carray_slab.h +44 -33
  118. data/ext/carray_sort.c +37 -31
  119. data/ext/carray_sort_kernel.c +36 -10
  120. data/ext/carray_struct.c +44 -34
  121. data/ext/carray_test.c +131 -81
  122. data/ext/carray_time_text.c +30 -31
  123. data/ext/carray_undef.c +50 -7
  124. data/ext/carray_utils.c +373 -139
  125. data/ext/extconf.rb +24 -17
  126. data/ext/mk_call_cfunc.rb +25 -31
  127. data/ext/mk_pow5_table.rb +2 -1
  128. data/ext/mkkernel.rb +819 -488
  129. data/ext/ruby_carray.c +47 -42
  130. data/ext/version.h +7 -5
  131. data/lib/carray/attribute.rb +19 -18
  132. data/lib/carray/autoload_carray.rb +20 -15
  133. data/lib/carray/autoload_method_extension.rb +64 -57
  134. data/lib/carray/axis_group.rb +272 -185
  135. data/lib/carray/basics.rb +298 -56
  136. data/lib/carray/bincount_nd.rb +41 -18
  137. data/lib/carray/block_iterator.rb +167 -119
  138. data/lib/carray/boolean_reduce.rb +7 -4
  139. data/lib/carray/categorical.rb +84 -59
  140. data/lib/carray/categorical_iterator.rb +90 -67
  141. data/lib/carray/complex.rb +61 -41
  142. data/lib/carray/conditional.rb +98 -23
  143. data/lib/carray/const_string.rb +112 -46
  144. data/lib/carray/core_extensions.rb +20 -69
  145. data/lib/carray/distance_limit.rb +7 -5
  146. data/lib/carray/factories.rb +269 -0
  147. data/lib/carray/fixlen_string.rb +95 -12
  148. data/lib/carray/frame/column_verbs.rb +76 -9
  149. data/lib/carray/frame/concat.rb +51 -7
  150. data/lib/carray/frame/convert.rb +2 -1
  151. data/lib/carray/frame/csv_parser.rb +20 -5
  152. data/lib/carray/frame/frame.rb +73 -31
  153. data/lib/carray/frame/group.rb +247 -187
  154. data/lib/carray/frame/io.rb +36 -12
  155. data/lib/carray/frame/join.rb +56 -17
  156. data/lib/carray/frame/records.rb +7 -5
  157. data/lib/carray/frame/reshape.rb +27 -7
  158. data/lib/carray/frame/sort.rb +10 -8
  159. data/lib/carray/frame/verbs.rb +217 -134
  160. data/lib/carray/frame/windows.rb +254 -19
  161. data/lib/carray/frame.rb +1 -2
  162. data/lib/carray/fuse_source.rb +28 -23
  163. data/lib/carray/fusion.rb +30 -30
  164. data/lib/carray/histogram.rb +35 -16
  165. data/lib/carray/inspect.rb +7 -7
  166. data/lib/carray/iterator.rb +104 -23
  167. data/lib/carray/key_windows.rb +93 -61
  168. data/lib/carray/lazy.rb +208 -111
  169. data/lib/carray/limits.rb +80 -0
  170. data/lib/carray/mask_gap_fill.rb +21 -16
  171. data/lib/carray/math.rb +87 -50
  172. data/lib/carray/meld_reduce.rb +30 -8
  173. data/lib/carray/methods/align_addr.rb +29 -17
  174. data/lib/carray/methods/bin.rb +8 -1
  175. data/lib/carray/methods/bincount.rb +4 -2
  176. data/lib/carray/methods/bit_string.rb +19 -31
  177. data/lib/carray/methods/broadcast.rb +8 -3
  178. data/lib/carray/methods/choose.rb +5 -5
  179. data/lib/carray/methods/composition.rb +73 -72
  180. data/lib/carray/methods/{index.rb → coordinate.rb} +13 -13
  181. data/lib/carray/methods/discovery_along.rb +3 -3
  182. data/lib/carray/methods/factorize.rb +2 -0
  183. data/lib/carray/methods/gather_nd.rb +106 -103
  184. data/lib/carray/methods/insert_block.rb +2 -2
  185. data/lib/carray/methods/is_between.rb +6 -4
  186. data/lib/carray/methods/is_in.rb +183 -143
  187. data/lib/carray/methods/join.rb +2 -1
  188. data/lib/carray/methods/lag.rb +6 -2
  189. data/lib/carray/methods/locate_addr.rb +157 -94
  190. data/lib/carray/methods/mask_duplicates.rb +1 -1
  191. data/lib/carray/methods/meshgrid.rb +28 -16
  192. data/lib/carray/methods/mode.rb +67 -67
  193. data/lib/carray/methods/nunique.rb +9 -7
  194. data/lib/carray/methods/repeat.rb +23 -13
  195. data/lib/carray/methods/resize.rb +1 -1
  196. data/lib/carray/methods/snap.rb +24 -20
  197. data/lib/carray/methods/string_format.rb +2 -2
  198. data/lib/carray/methods/unique.rb +4 -2
  199. data/lib/carray/methods/value_counts.rb +1 -1
  200. data/lib/carray/mkmf.rb +25 -28
  201. data/lib/carray/percentile_arguments.rb +82 -0
  202. data/lib/carray/record_order.rb +50 -20
  203. data/lib/carray/rng.rb +1 -0
  204. data/lib/carray/runtime.rb +195 -17
  205. data/lib/carray/segment_iterator.rb +262 -137
  206. data/lib/carray/serialize.rb +635 -566
  207. data/lib/carray/slab_iterator.rb +114 -114
  208. data/lib/carray/stack.rb +14 -15
  209. data/lib/carray/string.rb +16 -22
  210. data/lib/carray/string_operation_extension.rb +97 -52
  211. data/lib/carray/struct.rb +122 -164
  212. data/lib/carray/struct_builder.rb +247 -73
  213. data/lib/carray/time.rb +663 -365
  214. data/lib/carray/window_iterator.rb +232 -155
  215. data/lib/carray.rb +6 -11
  216. data/yard-stubs/ca_obj_array.rb +20 -135
  217. data/yard-stubs/ca_obj_bitarray.rb +1 -8
  218. data/yard-stubs/ca_obj_bitfield.rb +6 -2
  219. data/yard-stubs/ca_obj_byte_swap.rb +21 -7
  220. data/yard-stubs/ca_obj_fake.rb +3 -1
  221. data/yard-stubs/ca_obj_farray.rb +1 -1
  222. data/yard-stubs/ca_obj_field.rb +8 -6
  223. data/yard-stubs/ca_obj_refer.rb +10 -5
  224. data/yard-stubs/ca_obj_roll.rb +7 -6
  225. data/yard-stubs/ca_obj_shift.rb +2 -3
  226. data/yard-stubs/ca_obj_stride.rb +5 -7
  227. data/yard-stubs/ca_obj_tile.rb +1 -1
  228. data/yard-stubs/ca_obj_time.rb +43 -0
  229. data/yard-stubs/ca_obj_transpose.rb +1 -1
  230. data/yard-stubs/ca_obj_window.rb +28 -16
  231. data/yard-stubs/carray_access.rb +55 -6
  232. data/yard-stubs/carray_attribute.rb +26 -38
  233. data/yard-stubs/carray_cast.rb +121 -162
  234. data/yard-stubs/carray_class.rb +3 -4
  235. data/yard-stubs/carray_conversion.rb +6 -7
  236. data/yard-stubs/carray_copy.rb +2 -1
  237. data/yard-stubs/carray_count.rb +10 -5
  238. data/yard-stubs/carray_element.rb +4 -3
  239. data/yard-stubs/carray_limits.rb +100 -0
  240. data/yard-stubs/carray_loop.rb +0 -20
  241. data/yard-stubs/carray_mask.rb +59 -41
  242. data/yard-stubs/carray_math.rb +162 -100
  243. data/yard-stubs/carray_mathfunc.rb +415 -3
  244. data/yard-stubs/carray_median_percentile.rb +29 -11
  245. data/yard-stubs/carray_memory_view.rb +2 -2
  246. data/yard-stubs/carray_order.rb +72 -33
  247. data/yard-stubs/carray_random.rb +6 -6
  248. data/yard-stubs/carray_reduce.rb +353 -0
  249. data/yard-stubs/carray_rng.rb +92 -0
  250. data/yard-stubs/carray_scatter.rb +12 -3
  251. data/yard-stubs/carray_slab.rb +7 -4
  252. data/yard-stubs/carray_sort.rb +7 -6
  253. data/yard-stubs/carray_test.rb +18 -5
  254. data/yard-stubs/carray_undef.rb +44 -5
  255. data/yard-stubs/ruby_carray.rb +75 -40
  256. metadata +63 -25
  257. data/CHANGELOG.v1.md +0 -126
  258. data/ext/carray_build_flags.h +0 -3
  259. data/ext/carray_config.h +0 -28
  260. data/ext/carray_data_type.c +0 -90
  261. data/ext/carray_kernels_bincmp.c +0 -6457
  262. data/ext/carray_kernels_binop.c +0 -31534
  263. data/ext/carray_kernels_init.c +0 -1734
  264. data/ext/carray_kernels_moncmp.c +0 -2104
  265. data/ext/carray_kernels_monop.c +0 -25387
  266. data/ext/carray_kernels_reduce_aggregate.c +0 -27545
  267. data/ext/carray_kernels_reduce_boolean.c +0 -1924
  268. data/ext/carray_kernels_reduce_cumulative.c +0 -11646
  269. data/ext/carray_kernels_reduce_extreme.c +0 -21631
  270. data/ext/carray_kernels_reduce_variance.c +0 -5981
  271. data/ext/carray_kernels_scan.c +0 -4302
  272. data/ext/carray_kernels_search.c +0 -37776
  273. data/ext/carray_kernels_sort.c +0 -10814
  274. data/ext/carray_kernels_triop.c +0 -1864
  275. data/lib/carray/construct.rb +0 -360
  276. data/lib/carray/data_type_extension.rb +0 -261
  277. data/lib/carray/data_type_limits.rb +0 -91
@@ -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 WindowedFrame#is_complete. The rows need
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 [WindowedFrame]
32
- def windows (on:, before: nil, after: nil, exclude: :lower, by: nil, interval: nil, at: nil)
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
- WindowedFrame.new(self, plan, [on_name, *by_names], at: at)
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 WindowedFrame
50
+ class CAWindowedFrame
51
51
  include CAFrame::ColumnVerbs
52
52
 
53
- def initialize (frame, plan, key_names, at: nil)
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 [] (name)
64
- @plan.for_value(@frame[@frame.__send__(:column_key, name, "windows")])
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 (p, *names, min_count: nil, fill_value: nil)
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,35 +319,63 @@ 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 (*names)
325
+ def quantile(*names, min_count: nil, fill_value: nil)
326
+ refuse_unknown_fill_columns(:quantile, fill_value)
117
327
  named = {}
118
328
  over_named_columns(:quantile, names, @key_names) do |col, name|
119
- named[name] = @plan.for_value(col).quantile
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)
120
332
  end
121
333
  cols = CAFrame.__send__(:quantile_columns, named)
122
- return CAFrame.new(cols, axis_name: @frame.axis_name, index: @frame.index) unless @at
334
+ return CAFrame.new(cols, axis_name: @frame.axis_name, index: @frame.index&.copy) unless @at
123
335
  key = @frame.__send__(:key_column, @key_names.first, "windows")
124
336
  CAFrame.new(cols, axis_name: @key_names.first, index: CAFrame.__send__(:key_labels, key, @at.copy))
125
337
  end
126
338
 
127
339
  private
128
340
 
129
- def window_reduction (reduction, *names, min_count: nil, fill_value: nil)
341
+ def window_reduction(reduction, *names, min_count: nil, fill_value: nil)
130
342
  window_reduce(reduction, names, min_count: min_count, fill_value: fill_value)
131
343
  end
132
344
 
133
- def window_value (reduction, *names)
134
- window_reduce(reduction, names)
345
+ def window_value(reduction, *names, **keywords)
346
+ keywords = value_reduction_keywords(reduction, keywords)
347
+ window_reduce(reduction, names, **keywords)
348
+ end
349
+
350
+ # These reductions take no keyword. Without the **keywords above, Ruby
351
+ # would hand a keyword in as a column name.
352
+ def refuse_keywords(keywords)
353
+ return if keywords.empty?
354
+ list = keywords.keys.map(&:inspect).join(", ")
355
+ raise ArgumentError, "unknown keyword#{keywords.size > 1 ? "s" : ""}: #{list}"
135
356
  end
136
357
 
137
358
  # An N-D column (each row a profile) is reduced along the rows, each
138
359
  # window keeping the trailing shape, as a group reduces it.
139
- def window_reduce (reduction, names, args = [], **keywords)
360
+ def window_reduce(reduction, names, args = [], **keywords)
140
361
  keywords = keywords.compact
362
+ if @at && names.include?(@key_names.first)
363
+ raise ArgumentError, "#{reduction}: #{@key_names.first.inspect} is the key, already the result's index"
364
+ end
365
+ fills = keywords.delete(:fill_value)
366
+ refuse_unknown_fill_columns(reduction, fills)
367
+ refuse_wrong_reduction_arguments(reduction, args, keywords)
141
368
  named = {}
142
369
  out = over_named_columns(reduction, names, @key_names, args) do |col, name|
143
- named[name] = @plan.for_value(col).public_send(reduction, *args, **keywords)
370
+ fill = fills.is_a?(Hash) ? fills[name] : fills
371
+ kw = fill.nil? ? keywords : keywords.merge(fill_value: fill)
372
+ named[name] =
373
+ if %i[first last].include?(reduction) && col.ndim == 1 && args.empty? && kw.empty? &&
374
+ col.face? && !col.__send__(:holds_new_values?)
375
+ col[@plan.send(:for_value, present_rows(col)).public_send(reduction)].copy
376
+ else
377
+ @plan.send(:for_value, col).public_send(reduction, *args, **kw)
378
+ end
144
379
  end
145
380
  out = named if @at && !names.empty?
146
381
  return out if out.is_a?(CArray) && !@at
@@ -149,7 +384,7 @@ class WindowedFrame
149
384
  key = @frame.__send__(:key_column, @key_names.first, "windows")
150
385
  CAFrame.new(out, axis_name: @key_names.first, index: CAFrame.__send__(:key_labels, key, @at.copy))
151
386
  else
152
- CAFrame.new(out, axis_name: @frame.axis_name, index: @frame.index)
387
+ CAFrame.new(out, axis_name: @frame.axis_name, index: @frame.index&.copy)
153
388
  end
154
389
  end
155
390
  end
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. See
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'
@@ -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 (value)
30
+ def self.shadow(value)
31
31
  value.is_a?(CArray) ? value.lazy : value
32
32
  end
33
33
 
34
- def self.evaluate (block)
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 (block)
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 (block)
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 (block)
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 (block)
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 (body, outer)
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 (text, locals, scope, file, line)
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 (block)
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 (block)
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)
@@ -185,8 +185,10 @@ class CArray
185
185
  # block that starts on the sequence's first line is found in the file's
186
186
  # syntax tree. Only when two start on that line is the sequence itself
187
187
  # dumped: InstructionSequence#to_a reads the instructions while another
188
- # Ractor may be running them, and that can crash the process.
189
- def self.code_location (sequence, path)
188
+ # Ractor may be running them, and that can crash the process. Two Ractors
189
+ # dumping one sequence for the first time crash Ruby before 4.1, so the
190
+ # dump goes through a lock.
191
+ def self.code_location(sequence, path)
190
192
  line = sequence.first_lineno
191
193
  found = []
192
194
  pending = [Prism.parse_file(path).value]
@@ -206,7 +208,7 @@ class CArray
206
208
  start, whole = found.first
207
209
  [start.start_line, start.start_column, whole.end_line, whole.end_column]
208
210
  else
209
- sequence.to_a[4][:code_location]
211
+ CArray.send(:instruction_sequence_to_a, sequence)[4][:code_location]
210
212
  end
211
213
  end
212
214
 
@@ -222,16 +224,16 @@ class CArray
222
224
  @spots = []
223
225
  end
224
226
 
225
- def visit_local_variable_read_node (node) = mark(node)
226
- def visit_instance_variable_read_node (node) = mark(node)
227
- def visit_constant_read_node (node) = mark(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)
228
230
 
229
231
  # `Math::PI` is one name, not `Math` with something after it.
230
- def visit_constant_path_node (node)
232
+ def visit_constant_path_node(node)
231
233
  mark(node)
232
234
  end
233
235
 
234
- def visit_call_node (node)
236
+ def visit_call_node(node)
235
237
  if node.name == :[] || node.name == :[]=
236
238
  # An index is a position, not a value to fuse: `a[i]` shadows `a`
237
239
  # and leaves `i` alone.
@@ -244,25 +246,28 @@ class CArray
244
246
 
245
247
  private
246
248
 
247
- def mark (node)
249
+ def mark(node)
248
250
  @spots << [node.location.start_offset, node.location.end_offset]
249
251
  end
250
252
  end
251
253
 
252
- def self.rewrite (source)
254
+ def self.rewrite(source)
253
255
  visitor = Leaves.new
254
256
  Prism.parse(source).value.accept(visitor)
255
257
  out = source.dup
256
258
  visitor.spots.sort_by { |start, _| -start }.each do |start, stop|
257
- out[start...stop] = "::CArray::FuseSource.shadow(#{source[start...stop]})"
259
+ out[start...stop] = "::CArray.const_get(:FuseSource).shadow(#{source[start...stop]})"
258
260
  end
259
261
  out
260
262
  end
261
263
  end
264
+ # Loading this file defines the constant again, which drops the
265
+ # private_constant set beside the autoload.
266
+ private_constant :FuseSource
262
267
  end
263
268
 
264
269
  # A scope with no locals and no refinements in it, for compiling a block's
265
270
  # expression where it can neither see nor hold on to the frame of the call
266
271
  # that happens to compile it. Both are true only of the top of a file.
267
- CArray::FuseSource::EMPTY_SCOPE = -> { binding }
268
- CArray::FuseSource.private_constant :EMPTY_SCOPE
272
+ CArray.const_get(:FuseSource)::EMPTY_SCOPE = -> { binding }
273
+ CArray.const_get(:FuseSource).private_constant :EMPTY_SCOPE