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
|
@@ -35,7 +35,8 @@ class CArray
|
|
|
35
35
|
# @param opts [Hash] forwarded to the underlying reduction (`axis:`,
|
|
36
36
|
# `keep_axis:`, ...).
|
|
37
37
|
# @return [Boolean, CArray] a scalar, or an array when an axis is given.
|
|
38
|
-
def any
|
|
38
|
+
def any(skip_masked: true, **opts)
|
|
39
|
+
skip_masked = CArray.send(:bool_arg, skip_masked, "skip_masked", "any")
|
|
39
40
|
return any_skipping_masked(**opts) if skip_masked
|
|
40
41
|
kleene_fold(:any, opts)
|
|
41
42
|
end
|
|
@@ -51,7 +52,8 @@ class CArray
|
|
|
51
52
|
# @param opts [Hash] forwarded to the underlying reduction (`axis:`,
|
|
52
53
|
# `keep_axis:`, ...).
|
|
53
54
|
# @return [Boolean, CArray] a scalar, or an array when an axis is given.
|
|
54
|
-
def all
|
|
55
|
+
def all(skip_masked: true, **opts)
|
|
56
|
+
skip_masked = CArray.send(:bool_arg, skip_masked, "skip_masked", "all")
|
|
55
57
|
return all_skipping_masked(**opts) if skip_masked
|
|
56
58
|
kleene_fold(:all, opts)
|
|
57
59
|
end
|
|
@@ -67,7 +69,8 @@ class CArray
|
|
|
67
69
|
# @param opts [Hash] forwarded to the underlying reduction (`axis:`,
|
|
68
70
|
# `keep_axis:`, ...).
|
|
69
71
|
# @return [Boolean, CArray] a scalar, or an array when an axis is given.
|
|
70
|
-
def none
|
|
72
|
+
def none(skip_masked: true, **opts)
|
|
73
|
+
skip_masked = CArray.send(:bool_arg, skip_masked, "skip_masked", "none")
|
|
71
74
|
return none_skipping_masked(**opts) if skip_masked
|
|
72
75
|
kleene_fold(:none, opts)
|
|
73
76
|
end
|
|
@@ -79,7 +82,7 @@ class CArray
|
|
|
79
82
|
# any, a false for all, a true for none); elsewhere a masked cell leaves the
|
|
80
83
|
# answer undetermined. Going through that reduction checks the data type,
|
|
81
84
|
# the axes and the keywords exactly as `skip_masked: true` does.
|
|
82
|
-
def kleene_fold
|
|
85
|
+
def kleene_fold(kind, opts)
|
|
83
86
|
r = case kind
|
|
84
87
|
when :any then any_skipping_masked(**opts)
|
|
85
88
|
when :all then all_skipping_masked(**opts)
|
data/lib/carray/categorical.rb
CHANGED
|
@@ -72,6 +72,7 @@ class CACategorical < CAObject
|
|
|
72
72
|
CA_INT64 => "q", CA_UINT64 => "Q",
|
|
73
73
|
}.freeze
|
|
74
74
|
Ractor.make_shareable(UNPACK_FORMAT)
|
|
75
|
+
private_constant :UNPACK_FORMAT
|
|
75
76
|
|
|
76
77
|
# The exclusion sentinel per codes data_type: the all-ones bit pattern, read
|
|
77
78
|
# as type-max for an unsigned data type and as -1 for a signed one. Either way it
|
|
@@ -225,7 +226,7 @@ class CACategorical < CAObject
|
|
|
225
226
|
# row's category is the tuple of its component codes, labelled by the
|
|
226
227
|
# tuple of the component labels. One masked component leaves the row in
|
|
227
228
|
# no category, as a masked single key does.
|
|
228
|
-
private_class_method def self.from_keys
|
|
229
|
+
private_class_method def self.from_keys(cols)
|
|
229
230
|
return cols.first.categorize if cols.size == 1
|
|
230
231
|
nrow = cols.first.elements
|
|
231
232
|
cats = cols.map(&:categorize)
|
|
@@ -258,15 +259,19 @@ class CACategorical < CAObject
|
|
|
258
259
|
# @overload initialize(codes, labels)
|
|
259
260
|
# Allocates a READONLY {CACategorical} Face whose storage is
|
|
260
261
|
# `codes` and whose vocabulary is `labels`. The label list is
|
|
261
|
-
# copied and frozen
|
|
262
|
+
# copied and frozen, and each label that is not frozen is copied and
|
|
263
|
+
# frozen, so codes always index a stable vocabulary.
|
|
262
264
|
# @param codes [CArray] integer code storage.
|
|
263
265
|
# @param labels [Array, CArray] category vocabulary.
|
|
264
266
|
def initialize(codes, labels)
|
|
265
267
|
# Own a frozen copy of the vocabulary: the categorical is READONLY and its
|
|
266
|
-
# codes index into labels, so the label list
|
|
267
|
-
#
|
|
268
|
-
#
|
|
269
|
-
|
|
268
|
+
# codes index into labels, so neither the label list nor a label may
|
|
269
|
+
# change under it. A cell reads as its label object, so a label left
|
|
270
|
+
# mutable could be changed through any cell, for every cell of its code.
|
|
271
|
+
# A label that is not frozen is copied before it is frozen, so a caller's
|
|
272
|
+
# array and objects are never frozen as a side effect.
|
|
273
|
+
list = labels.respond_to?(:to_a) ? labels.to_a : Array(labels)
|
|
274
|
+
@labels = list.map { |label| label.frozen? ? label : label.dup.freeze }.freeze
|
|
270
275
|
super(CA_FIXLEN, codes.shape,
|
|
271
276
|
bytes: codes.bytes,
|
|
272
277
|
storage: codes.data_type,
|
|
@@ -306,7 +311,7 @@ class CACategorical < CAObject
|
|
|
306
311
|
|
|
307
312
|
# Face hook: carry the vocabulary across lifted views (slice / reshape / …).
|
|
308
313
|
# The codes ride along automatically as the Face's parent.
|
|
309
|
-
def copy_state(src)
|
|
314
|
+
private def copy_state(src)
|
|
310
315
|
@labels = src.labels
|
|
311
316
|
end
|
|
312
317
|
|
|
@@ -323,13 +328,13 @@ class CACategorical < CAObject
|
|
|
323
328
|
# so agreeing would mean silently materialising fresh codes inside what the
|
|
324
329
|
# caller asked for as a view. Build the shared vocabulary up front instead:
|
|
325
330
|
# `keys.categorize(labels: shared)`.
|
|
326
|
-
def face_state_compatible?(other)
|
|
331
|
+
private def face_state_compatible?(other)
|
|
327
332
|
other.is_a?(CACategorical) && @labels == other.labels
|
|
328
333
|
end
|
|
329
334
|
|
|
330
335
|
# Face hook: decode a per-cell code into its category label, through the one
|
|
331
336
|
# decode every code-to-label path shares (see #label_at).
|
|
332
|
-
def storage_to_element(raw)
|
|
337
|
+
private def storage_to_element(raw)
|
|
333
338
|
code = raw.is_a?(String) ? raw.unpack1(UNPACK_FORMAT.fetch(face_storage.data_type)) : raw
|
|
334
339
|
label_at(code)
|
|
335
340
|
end
|
|
@@ -337,7 +342,7 @@ class CACategorical < CAObject
|
|
|
337
342
|
# Face hook: a value written into a cell (a fill_value of shift and
|
|
338
343
|
# window) is a label, and the cell holds its code.
|
|
339
344
|
# @raise [ArgumentError] if the value is not one of the labels.
|
|
340
|
-
def element_to_storage
|
|
345
|
+
private def element_to_storage(value)
|
|
341
346
|
code_for_fill(value)
|
|
342
347
|
end
|
|
343
348
|
|
|
@@ -353,7 +358,7 @@ class CACategorical < CAObject
|
|
|
353
358
|
# The new cells are excluded (UNDEF) unless `fill_value` names one of
|
|
354
359
|
# the labels, since a zero code would read as the first label.
|
|
355
360
|
# @raise [ArgumentError] when `fill_value` is not a label.
|
|
356
|
-
def resize
|
|
361
|
+
def resize(*newdim, fill_value: UNDEF)
|
|
357
362
|
CACategorical.from_codes(codes.resize(*newdim, fill_value: code_for_fill(fill_value)),
|
|
358
363
|
labels)
|
|
359
364
|
end
|
|
@@ -364,7 +369,7 @@ class CACategorical < CAObject
|
|
|
364
369
|
# unless the block returns one of the labels.
|
|
365
370
|
# @raise [ArgumentError] when the block returns something that is not
|
|
366
371
|
# a label.
|
|
367
|
-
def insert_block
|
|
372
|
+
def insert_block(offset, bsize, &block)
|
|
368
373
|
fill = block ? code_for_fill(block.call) : UNDEF
|
|
369
374
|
CACategorical.from_codes(codes.insert_block(offset, bsize) { fill }, labels)
|
|
370
375
|
end
|
|
@@ -442,10 +447,6 @@ class CACategorical < CAObject
|
|
|
442
447
|
@category_sizes = out.freeze
|
|
443
448
|
end
|
|
444
449
|
|
|
445
|
-
# Back-compatible alias; {#category_sizes} is the canonical name (a histogram
|
|
446
|
-
# `bincount` reading is misleading for per-category cell counts).
|
|
447
|
-
alias bincount category_sizes
|
|
448
|
-
|
|
449
450
|
# ---- value-hash discovery family --------------------------------------
|
|
450
451
|
#
|
|
451
452
|
# The codes are storage and the labels are the values, so this family has to
|
|
@@ -464,7 +465,8 @@ class CACategorical < CAObject
|
|
|
464
465
|
# with no cells is not included (use {#labels} for the vocabulary).
|
|
465
466
|
# @param sort [Boolean] when true, sort the labels ascending.
|
|
466
467
|
# @return [CArray] object CArray of labels.
|
|
467
|
-
def unique
|
|
468
|
+
def unique(sort: false)
|
|
469
|
+
sort = sort_flag(sort, "unique")
|
|
468
470
|
u = labels_for(codes.unique)
|
|
469
471
|
sort ? u.sort : u
|
|
470
472
|
end
|
|
@@ -474,7 +476,7 @@ class CACategorical < CAObject
|
|
|
474
476
|
# is the aligned-to-{#labels} counterpart, which keeps the empty ones.
|
|
475
477
|
# @param sort [false, :count, :value] pair ordering.
|
|
476
478
|
# @return [Array(CArray, CArray)]
|
|
477
|
-
def value_counts
|
|
479
|
+
def value_counts(sort: false)
|
|
478
480
|
unless [false, :count, :value].include?(sort)
|
|
479
481
|
raise ArgumentError, "value_counts: sort must be false, :count, or :value"
|
|
480
482
|
end
|
|
@@ -493,7 +495,7 @@ class CACategorical < CAObject
|
|
|
493
495
|
# modal values come back sorted ascending, which for labels is not the
|
|
494
496
|
# code order.
|
|
495
497
|
# @return [CArray, Array<CArray>]
|
|
496
|
-
def mode
|
|
498
|
+
def mode(axis: nil)
|
|
497
499
|
label_values.mode(axis: axis)
|
|
498
500
|
end
|
|
499
501
|
|
|
@@ -502,7 +504,7 @@ class CACategorical < CAObject
|
|
|
502
504
|
# An unknown label matches nothing. Masked cells stay masked.
|
|
503
505
|
# @param values [Array, CArray, CACategorical] labels to test against.
|
|
504
506
|
# @return [CArray]
|
|
505
|
-
def is_in
|
|
507
|
+
def is_in(values)
|
|
506
508
|
refuse_range_set(values, "is_in")
|
|
507
509
|
wanted = label_list(values).filter_map do |l|
|
|
508
510
|
# nan-order: looks a label up and orders nothing; == does not find a NaN.
|
|
@@ -513,21 +515,21 @@ class CACategorical < CAObject
|
|
|
513
515
|
|
|
514
516
|
# @overload intersection(other, sort: false)
|
|
515
517
|
# @return [CArray] object CArray of the labels present in both.
|
|
516
|
-
def intersection
|
|
518
|
+
def intersection(other, sort: false)
|
|
517
519
|
refuse_range_set(other, "intersection")
|
|
518
520
|
unique.intersection(label_array(other), sort: sort)
|
|
519
521
|
end
|
|
520
522
|
|
|
521
523
|
# @overload difference(other, sort: false)
|
|
522
524
|
# @return [CArray] object CArray of the labels only `self` has.
|
|
523
|
-
def difference
|
|
525
|
+
def difference(other, sort: false)
|
|
524
526
|
refuse_range_set(other, "difference")
|
|
525
527
|
unique.difference(label_array(other), sort: sort)
|
|
526
528
|
end
|
|
527
529
|
|
|
528
530
|
# @overload union(other, sort: false)
|
|
529
531
|
# @return [CArray] object CArray of the labels of either side.
|
|
530
|
-
def union
|
|
532
|
+
def union(other, sort: false)
|
|
531
533
|
refuse_range_set(other, "union")
|
|
532
534
|
unique.union(label_array(other), sort: sort)
|
|
533
535
|
end
|
|
@@ -535,7 +537,7 @@ class CACategorical < CAObject
|
|
|
535
537
|
# @overload locate_addr(ref)
|
|
536
538
|
# @return [CArray] for each cell, its label's address in `ref` (UNDEF when
|
|
537
539
|
# the label does not occur there).
|
|
538
|
-
def locate_addr
|
|
540
|
+
def locate_addr(ref)
|
|
539
541
|
label_values.locate_addr(label_array(ref))
|
|
540
542
|
end
|
|
541
543
|
|
|
@@ -553,7 +555,7 @@ class CACategorical < CAObject
|
|
|
553
555
|
# cell by cell, a masked cell staying masked; a categorical goes through its
|
|
554
556
|
# own codes, so the two vocabularies need not agree. Anything else is one
|
|
555
557
|
# label, as CArray#eq takes it.
|
|
556
|
-
def codes_of
|
|
558
|
+
def codes_of(other)
|
|
557
559
|
return @labels.index(other) || @labels.size unless other.is_a?(CArray)
|
|
558
560
|
if other.is_a?(CACategorical)
|
|
559
561
|
table = other.labels.map { |l| @labels.index(l) || @labels.size }
|
|
@@ -567,20 +569,20 @@ class CACategorical < CAObject
|
|
|
567
569
|
end
|
|
568
570
|
|
|
569
571
|
# The code a fill value stands for: UNDEF stays UNDEF, a label is its code.
|
|
570
|
-
def code_for_fill
|
|
572
|
+
def code_for_fill(value)
|
|
571
573
|
return UNDEF if UNDEF.equal?(value)
|
|
572
574
|
code = label_position(value)
|
|
573
575
|
raise ArgumentError, "#{value.inspect} is not one of the labels #{labels.inspect}" unless code
|
|
574
576
|
code
|
|
575
577
|
end
|
|
576
578
|
|
|
577
|
-
def label_at
|
|
579
|
+
def label_at(code)
|
|
578
580
|
(code < 0 || code >= @labels.size) ? nil : @labels[code]
|
|
579
581
|
end
|
|
580
582
|
|
|
581
583
|
# Labels for a code array. The discovery kernels skip masked cells, so what
|
|
582
584
|
# reaches here is real codes — but it is decoded like any other code.
|
|
583
|
-
def labels_for
|
|
585
|
+
def labels_for(code_array)
|
|
584
586
|
CA_OBJECT(code_array.to_a.map { |c| label_at(c) })
|
|
585
587
|
end
|
|
586
588
|
|
|
@@ -591,11 +593,11 @@ class CACategorical < CAObject
|
|
|
591
593
|
|
|
592
594
|
# An operand's labels as a plain Array (a masked cell comes through as UNDEF,
|
|
593
595
|
# which matches no label and so drops out of the set).
|
|
594
|
-
def label_list
|
|
596
|
+
def label_list(other)
|
|
595
597
|
other.respond_to?(:to_a) ? other.to_a : Array(other)
|
|
596
598
|
end
|
|
597
599
|
|
|
598
|
-
def label_array
|
|
600
|
+
def label_array(other)
|
|
599
601
|
CA_OBJECT(label_list(other))
|
|
600
602
|
end
|
|
601
603
|
|
|
@@ -606,7 +608,7 @@ class CACategorical < CAObject
|
|
|
606
608
|
# The scatter path (axis_group) handles the monoid reductions (sum / mean /
|
|
607
609
|
# min / max / variance) in one pass. Order statistics (median / percentile)
|
|
608
610
|
# cannot be scattered: they need every value of a group held together. These
|
|
609
|
-
# two accessors are the
|
|
611
|
+
# two accessors are the parts that other engine uses — lay the data
|
|
610
612
|
# out as category-contiguous blocks, then select per block.
|
|
611
613
|
#
|
|
612
614
|
# The grouping plan — group_addr (the counting sort, the dominant cost) plus
|
|
@@ -628,7 +630,7 @@ class CACategorical < CAObject
|
|
|
628
630
|
# / category_sizes). Optional — the plan is built lazily on first grouping
|
|
629
631
|
# access — but useful to pay the counting sort once ahead of a batch.
|
|
630
632
|
# @return [self]
|
|
631
|
-
def build_grouping
|
|
633
|
+
private def build_grouping
|
|
632
634
|
group_addr # pulls category_sizes; reduceat_index shares category_sizes
|
|
633
635
|
reduceat_index
|
|
634
636
|
self
|
|
@@ -670,7 +672,7 @@ class CACategorical < CAObject
|
|
|
670
672
|
# Returns an int64 CArray, one entry per {#labels}, of segment start
|
|
671
673
|
# offsets aligned to {#labels}. Pair with {#category_sizes} for lengths.
|
|
672
674
|
# @return [CArray]
|
|
673
|
-
def reduceat_index
|
|
675
|
+
private def reduceat_index
|
|
674
676
|
return @reduceat_index if @reduceat_index
|
|
675
677
|
# Every boundary but the last; the copy makes it an entity the
|
|
676
678
|
# reduceat kernels can read directly.
|
|
@@ -684,7 +686,8 @@ class CACategorical < CAObject
|
|
|
684
686
|
# category of `self` with no cells does not carry over; a masked cell
|
|
685
687
|
# is excluded.
|
|
686
688
|
# @return [CACategorical]
|
|
687
|
-
def categorize
|
|
689
|
+
def categorize(labels: nil, sort_labels: false)
|
|
690
|
+
sort_labels = CArray.send(:bool_arg, sort_labels, "sort_labels", "categorize")
|
|
688
691
|
if labels || sort_labels
|
|
689
692
|
return label_values.categorize(labels: labels, sort_labels: sort_labels)
|
|
690
693
|
end
|
|
@@ -695,7 +698,7 @@ class CACategorical < CAObject
|
|
|
695
698
|
|
|
696
699
|
# The position of +label+ among the labels, found by ==, a NaN label by a
|
|
697
700
|
# NaN, as join and at match NaN keys; nil when it is not one of them.
|
|
698
|
-
private def label_position
|
|
701
|
+
private def label_position(label)
|
|
699
702
|
return @labels.index(label) unless label.is_a?(Float) && label.nan?
|
|
700
703
|
@labels.index { |l| l.is_a?(Float) && l.nan? }
|
|
701
704
|
end
|
|
@@ -725,7 +728,8 @@ class CACategorical < CAObject
|
|
|
725
728
|
# not a label, when `base:` and `full: true` are both given, when
|
|
726
729
|
# there are no labels to leave one out of, or when the categorical is
|
|
727
730
|
# not one-dimensional.
|
|
728
|
-
def contrast
|
|
731
|
+
def contrast(scheme = :dummy, base: nil, full: false)
|
|
732
|
+
full = CArray.send(:bool_arg, full, "full", "contrast")
|
|
729
733
|
unless scheme == :dummy
|
|
730
734
|
raise ArgumentError, "contrast: unknown scheme #{scheme.inspect} (only :dummy is defined)"
|
|
731
735
|
end
|
|
@@ -773,6 +777,19 @@ end
|
|
|
773
777
|
|
|
774
778
|
|
|
775
779
|
class CArray
|
|
780
|
+
# The order sort_labels: puts two labels in: the order sort puts the cells
|
|
781
|
+
# of an array in (object_order_cmp); two labels <=> cannot order are
|
|
782
|
+
# refused, by name.
|
|
783
|
+
private def label_order(a, b)
|
|
784
|
+
c = CArray.send(:object_order_cmp, a, b)
|
|
785
|
+
if c.nil?
|
|
786
|
+
raise ArgumentError,
|
|
787
|
+
"categorize(sort_labels: true): cannot order the labels " \
|
|
788
|
+
"#{a.inspect} and #{b.inspect}"
|
|
789
|
+
end
|
|
790
|
+
c
|
|
791
|
+
end
|
|
792
|
+
|
|
776
793
|
# Build a CACategorical from self read as category keys (= the values whose
|
|
777
794
|
# distinct levels become the categories). Codes are dense 0-based in the
|
|
778
795
|
# order labels appear (first-appearance by default, or ascending sorted
|
|
@@ -790,29 +807,23 @@ class CArray
|
|
|
790
807
|
# vocabulary is fixed and keys outside it become masked
|
|
791
808
|
# (excluded). `sort_labels:` is ignored when an explicit `labels`
|
|
792
809
|
# is given (the caller has already chosen the order).
|
|
793
|
-
# @param labels [Array, CArray, nil] fixed vocabulary; `nil`
|
|
810
|
+
# @param labels [Array, CArray, Range, nil] fixed vocabulary; `nil`
|
|
794
811
|
# enables discovery.
|
|
795
812
|
# @param sort_labels [Boolean] when discovering (labels: nil), sort
|
|
796
813
|
# the discovered vocabulary ascending after collecting it.
|
|
797
814
|
# @return [CACategorical]
|
|
798
815
|
# @raise [ArgumentError] when explicit `labels` contain
|
|
799
816
|
# duplicates.
|
|
817
|
+
# @raise [TypeError] when `labels` is not a list (a String, say).
|
|
800
818
|
# @raise [CArray::DataTypeError] when `sort_labels: true` discovers
|
|
801
819
|
# the labels of a complex array, which have no order.
|
|
802
|
-
# The order sort_labels: puts two labels in: the order sort puts the cells
|
|
803
|
-
# of an array in (object_order_cmp); two labels <=> cannot order are
|
|
804
|
-
# refused, by name.
|
|
805
|
-
private def label_order(a, b)
|
|
806
|
-
c = CArray.send(:object_order_cmp, a, b)
|
|
807
|
-
if c.nil?
|
|
808
|
-
raise ArgumentError,
|
|
809
|
-
"categorize(sort_labels: true): cannot order the labels " \
|
|
810
|
-
"#{a.inspect} and #{b.inspect}"
|
|
811
|
-
end
|
|
812
|
-
c
|
|
813
|
-
end
|
|
814
|
-
|
|
815
820
|
def categorize(labels: nil, sort_labels: false)
|
|
821
|
+
sort_labels = CArray.send(:bool_arg, sort_labels, "sort_labels", "categorize")
|
|
822
|
+
unless labels.nil? || labels.is_a?(Array) || labels.is_a?(CArray) ||
|
|
823
|
+
labels.is_a?(Range) || labels.is_a?(Enumerator::ArithmeticSequence)
|
|
824
|
+
raise TypeError, "categorize: labels: is a list of labels (an Array, " \
|
|
825
|
+
"a CArray or a Range), not a #{labels.class}"
|
|
826
|
+
end
|
|
816
827
|
# Automatic appearance-order vocabulary: one linear pass (C
|
|
817
828
|
# __factorize_appearance__) returns both codes and levels directly, over the
|
|
818
829
|
# integer / float / object / fixlen / boolean lanes (boolean rides the uint8
|
|
@@ -886,7 +897,8 @@ end
|
|
|
886
897
|
# carried here, around all of them.
|
|
887
898
|
[CArray, CACategorical, CAConstString].each do |klass|
|
|
888
899
|
klass.prepend(Module.new do
|
|
889
|
-
def categorize
|
|
900
|
+
def categorize(labels: nil, sort_labels: false)
|
|
901
|
+
sort_labels = CArray.send(:bool_arg, sort_labels, "sort_labels", "categorize")
|
|
890
902
|
carry_attrs(super)
|
|
891
903
|
end
|
|
892
904
|
end)
|