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.
- checksums.yaml +4 -4
- data/.yardopts +1 -1
- data/CHANGELOG.md +7 -1830
- data/README.md +62 -35
- 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 +12 -15
- data/ext/ca_bincmp_dispatch.h +16 -38
- data/ext/ca_binop_dispatch.c +35 -0
- data/ext/ca_binop_dispatch.h +22 -46
- 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 -1504
- 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 +92 -165
- data/ext/ca_obj_bincmp.c +65 -48
- data/ext/ca_obj_binop.c +74 -49
- data/ext/ca_obj_bitarray.c +7 -21
- data/ext/ca_obj_bitfield.c +93 -26
- data/ext/ca_obj_block.c +11 -12
- data/ext/ca_obj_byte_swap.c +34 -34
- data/ext/ca_obj_const_string.c +464 -304
- data/ext/ca_obj_face.c +167 -121
- data/ext/ca_obj_face.h +32 -170
- data/ext/ca_obj_fake.c +127 -34
- data/ext/ca_obj_farray.c +13 -9
- data/ext/ca_obj_field.c +17 -13
- data/ext/ca_obj_fixlen_string.c +57 -34
- data/ext/ca_obj_grid.c +149 -113
- data/ext/ca_obj_meld.c +28 -26
- data/ext/ca_obj_moncmp.c +10 -21
- data/ext/ca_obj_monop.c +88 -73
- data/ext/ca_obj_object.c +60 -46
- data/ext/ca_obj_record.c +74 -22
- data/ext/ca_obj_reduce.c +10 -14
- data/ext/ca_obj_refer.c +146 -39
- data/ext/ca_obj_remap.c +11 -29
- data/ext/ca_obj_repeat.c +41 -23
- data/ext/ca_obj_roll.c +20 -28
- data/ext/ca_obj_select.c +17 -27
- data/ext/ca_obj_select_axis.c +35 -42
- data/ext/ca_obj_shift.c +22 -42
- data/ext/ca_obj_source.c +1 -5
- data/ext/ca_obj_stack.c +33 -29
- data/ext/ca_obj_stride.c +99 -62
- data/ext/ca_obj_string.c +8 -20
- data/ext/ca_obj_tile.c +14 -16
- 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 +48 -49
- data/ext/ca_obj_window.c +87 -99
- data/ext/ca_op_cmplx64.h +4 -0
- data/ext/ca_op_ipower.c +97 -27
- data/ext/ca_op_powi.h +9 -28
- 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 +23 -26
- data/ext/ca_sweep_engine.c +92 -26
- data/ext/ca_sweep_engine.h +22 -15
- data/ext/ca_transform_common.c +3 -8
- data/ext/ca_triop_dispatch.h +20 -32
- data/ext/caframe_csv_reader.c +12 -12
- data/ext/carray.h +300 -1009
- data/ext/carray_access.c +320 -207
- data/ext/carray_address_basis.c +128 -59
- data/ext/carray_attribute.c +81 -104
- data/ext/carray_bincount.c +12 -14
- data/ext/carray_broadcast.c +31 -27
- data/ext/carray_call_cfunc.c +174 -285
- data/ext/carray_call_cfunc.h +36 -28
- data/ext/carray_cast.c +1191 -351
- data/ext/carray_cast_func.rb +68 -8
- data/ext/carray_class.c +6 -11
- data/ext/carray_conversion.c +98 -39
- data/ext/carray_copy.c +52 -26
- data/ext/carray_core.c +286 -132
- data/ext/carray_count.c +53 -22
- data/ext/carray_element.c +49 -28
- data/ext/carray_factorize.c +64 -63
- data/ext/carray_generate.c +26 -19
- data/ext/carray_histogram.c +9 -11
- data/ext/carray_hold.c +7 -9
- data/ext/carray_index_classifier.c +127 -230
- data/ext/carray_index_classifier.h +11 -10
- data/ext/carray_integer_total.c +523 -0
- data/ext/carray_internal.h +1364 -156
- data/ext/carray_key_windows.c +3 -3
- data/ext/carray_lazy.c +37 -37
- data/ext/carray_loop.c +27 -23
- data/ext/carray_mask.c +235 -150
- data/ext/carray_math_kernel.h +23 -47
- data/ext/carray_mathfunc.c +10 -3
- data/ext/carray_median_percentile.c +106 -100
- data/ext/carray_memory_view.c +83 -69
- data/ext/carray_operator.c +490 -128
- data/ext/carray_order.c +163 -84
- data/ext/carray_partition.c +14 -16
- data/ext/carray_random.c +48 -42
- data/ext/carray_round.c +33 -5
- data/ext/carray_scatter.c +57 -39
- data/ext/carray_segment.c +42 -46
- data/ext/carray_slab.c +77 -51
- data/ext/carray_slab.h +44 -33
- data/ext/carray_sort.c +37 -31
- data/ext/carray_sort_kernel.c +36 -10
- data/ext/carray_struct.c +44 -34
- data/ext/carray_test.c +131 -81
- data/ext/carray_time_text.c +30 -31
- data/ext/carray_undef.c +50 -7
- data/ext/carray_utils.c +373 -139
- data/ext/extconf.rb +24 -17
- data/ext/mk_call_cfunc.rb +25 -31
- data/ext/mk_pow5_table.rb +2 -1
- data/ext/mkkernel.rb +819 -488
- 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 +272 -185
- data/lib/carray/basics.rb +298 -56
- data/lib/carray/bincount_nd.rb +41 -18
- data/lib/carray/block_iterator.rb +167 -119
- data/lib/carray/boolean_reduce.rb +7 -4
- data/lib/carray/categorical.rb +84 -59
- data/lib/carray/categorical_iterator.rb +90 -67
- data/lib/carray/complex.rb +61 -41
- data/lib/carray/conditional.rb +98 -23
- data/lib/carray/const_string.rb +112 -46
- data/lib/carray/core_extensions.rb +20 -69
- data/lib/carray/distance_limit.rb +7 -5
- data/lib/carray/factories.rb +269 -0
- data/lib/carray/fixlen_string.rb +95 -12
- data/lib/carray/frame/column_verbs.rb +76 -9
- data/lib/carray/frame/concat.rb +51 -7
- data/lib/carray/frame/convert.rb +2 -1
- data/lib/carray/frame/csv_parser.rb +20 -5
- data/lib/carray/frame/frame.rb +73 -31
- data/lib/carray/frame/group.rb +247 -187
- data/lib/carray/frame/io.rb +36 -12
- data/lib/carray/frame/join.rb +56 -17
- data/lib/carray/frame/records.rb +7 -5
- data/lib/carray/frame/reshape.rb +27 -7
- data/lib/carray/frame/sort.rb +10 -8
- data/lib/carray/frame/verbs.rb +217 -134
- data/lib/carray/frame/windows.rb +254 -19
- data/lib/carray/frame.rb +1 -2
- data/lib/carray/fuse_source.rb +28 -23
- 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 +104 -23
- data/lib/carray/key_windows.rb +93 -61
- data/lib/carray/lazy.rb +208 -111
- 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 +8 -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 +73 -72
- data/lib/carray/methods/{index.rb → coordinate.rb} +13 -13
- data/lib/carray/methods/discovery_along.rb +3 -3
- data/lib/carray/methods/factorize.rb +2 -0
- data/lib/carray/methods/gather_nd.rb +106 -103
- 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 +183 -143
- data/lib/carray/methods/join.rb +2 -1
- data/lib/carray/methods/lag.rb +6 -2
- data/lib/carray/methods/locate_addr.rb +157 -94
- 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 +23 -13
- 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 +50 -20
- data/lib/carray/rng.rb +1 -0
- data/lib/carray/runtime.rb +195 -17
- data/lib/carray/segment_iterator.rb +262 -137
- data/lib/carray/serialize.rb +635 -566
- data/lib/carray/slab_iterator.rb +114 -114
- data/lib/carray/stack.rb +14 -15
- data/lib/carray/string.rb +16 -22
- data/lib/carray/string_operation_extension.rb +97 -52
- data/lib/carray/struct.rb +122 -164
- data/lib/carray/struct_builder.rb +247 -73
- data/lib/carray/time.rb +663 -365
- data/lib/carray/window_iterator.rb +232 -155
- 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 +6 -2
- 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 +10 -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 +28 -16
- data/yard-stubs/carray_access.rb +55 -6
- data/yard-stubs/carray_attribute.rb +26 -38
- data/yard-stubs/carray_cast.rb +121 -162
- 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 +10 -5
- 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 +162 -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 +72 -33
- 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 +12 -3
- data/yard-stubs/carray_slab.rb +7 -4
- data/yard-stubs/carray_sort.rb +7 -6
- data/yard-stubs/carray_test.rb +18 -5
- data/yard-stubs/carray_undef.rb +44 -5
- data/yard-stubs/ruby_carray.rb +75 -40
- metadata +63 -25
- 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 -25387
- 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 -261
- data/lib/carray/data_type_limits.rb +0 -91
data/lib/carray/categorical.rb
CHANGED
|
@@ -59,6 +59,8 @@ require "carray/methods/is_in" # refuse_range_set, for the set-valued verbs
|
|
|
59
59
|
# kernels are gated off (`cat + 1` raises; arithmetic on category codes is
|
|
60
60
|
# not meaningful).
|
|
61
61
|
class CACategorical < CAObject
|
|
62
|
+
private def holds_new_values? = false
|
|
63
|
+
|
|
62
64
|
|
|
63
65
|
# raw-byte unpack format per storage (codes) data_type, native endian.
|
|
64
66
|
# The FIXLEN surface delivers a per-cell fetch as an N-byte String; this
|
|
@@ -70,6 +72,7 @@ class CACategorical < CAObject
|
|
|
70
72
|
CA_INT64 => "q", CA_UINT64 => "Q",
|
|
71
73
|
}.freeze
|
|
72
74
|
Ractor.make_shareable(UNPACK_FORMAT)
|
|
75
|
+
private_constant :UNPACK_FORMAT
|
|
73
76
|
|
|
74
77
|
# The exclusion sentinel per codes data_type: the all-ones bit pattern, read
|
|
75
78
|
# as type-max for an unsigned data type and as -1 for a signed one. Either way it
|
|
@@ -223,7 +226,7 @@ class CACategorical < CAObject
|
|
|
223
226
|
# row's category is the tuple of its component codes, labelled by the
|
|
224
227
|
# tuple of the component labels. One masked component leaves the row in
|
|
225
228
|
# no category, as a masked single key does.
|
|
226
|
-
private_class_method def self.from_keys
|
|
229
|
+
private_class_method def self.from_keys(cols)
|
|
227
230
|
return cols.first.categorize if cols.size == 1
|
|
228
231
|
nrow = cols.first.elements
|
|
229
232
|
cats = cols.map(&:categorize)
|
|
@@ -256,15 +259,19 @@ class CACategorical < CAObject
|
|
|
256
259
|
# @overload initialize(codes, labels)
|
|
257
260
|
# Allocates a READONLY {CACategorical} Face whose storage is
|
|
258
261
|
# `codes` and whose vocabulary is `labels`. The label list is
|
|
259
|
-
# 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.
|
|
260
264
|
# @param codes [CArray] integer code storage.
|
|
261
265
|
# @param labels [Array, CArray] category vocabulary.
|
|
262
266
|
def initialize(codes, labels)
|
|
263
267
|
# Own a frozen copy of the vocabulary: the categorical is READONLY and its
|
|
264
|
-
# codes index into labels, so the label list
|
|
265
|
-
#
|
|
266
|
-
#
|
|
267
|
-
|
|
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
|
|
268
275
|
super(CA_FIXLEN, codes.shape,
|
|
269
276
|
bytes: codes.bytes,
|
|
270
277
|
storage: codes.data_type,
|
|
@@ -304,7 +311,7 @@ class CACategorical < CAObject
|
|
|
304
311
|
|
|
305
312
|
# Face hook: carry the vocabulary across lifted views (slice / reshape / …).
|
|
306
313
|
# The codes ride along automatically as the Face's parent.
|
|
307
|
-
def copy_state(src)
|
|
314
|
+
private def copy_state(src)
|
|
308
315
|
@labels = src.labels
|
|
309
316
|
end
|
|
310
317
|
|
|
@@ -321,13 +328,13 @@ class CACategorical < CAObject
|
|
|
321
328
|
# so agreeing would mean silently materialising fresh codes inside what the
|
|
322
329
|
# caller asked for as a view. Build the shared vocabulary up front instead:
|
|
323
330
|
# `keys.categorize(labels: shared)`.
|
|
324
|
-
def face_state_compatible?(other)
|
|
331
|
+
private def face_state_compatible?(other)
|
|
325
332
|
other.is_a?(CACategorical) && @labels == other.labels
|
|
326
333
|
end
|
|
327
334
|
|
|
328
335
|
# Face hook: decode a per-cell code into its category label, through the one
|
|
329
336
|
# decode every code-to-label path shares (see #label_at).
|
|
330
|
-
def storage_to_element(raw)
|
|
337
|
+
private def storage_to_element(raw)
|
|
331
338
|
code = raw.is_a?(String) ? raw.unpack1(UNPACK_FORMAT.fetch(face_storage.data_type)) : raw
|
|
332
339
|
label_at(code)
|
|
333
340
|
end
|
|
@@ -335,7 +342,7 @@ class CACategorical < CAObject
|
|
|
335
342
|
# Face hook: a value written into a cell (a fill_value of shift and
|
|
336
343
|
# window) is a label, and the cell holds its code.
|
|
337
344
|
# @raise [ArgumentError] if the value is not one of the labels.
|
|
338
|
-
def element_to_storage
|
|
345
|
+
private def element_to_storage(value)
|
|
339
346
|
code_for_fill(value)
|
|
340
347
|
end
|
|
341
348
|
|
|
@@ -351,7 +358,7 @@ class CACategorical < CAObject
|
|
|
351
358
|
# The new cells are excluded (UNDEF) unless `fill_value` names one of
|
|
352
359
|
# the labels, since a zero code would read as the first label.
|
|
353
360
|
# @raise [ArgumentError] when `fill_value` is not a label.
|
|
354
|
-
def resize
|
|
361
|
+
def resize(*newdim, fill_value: UNDEF)
|
|
355
362
|
CACategorical.from_codes(codes.resize(*newdim, fill_value: code_for_fill(fill_value)),
|
|
356
363
|
labels)
|
|
357
364
|
end
|
|
@@ -362,7 +369,7 @@ class CACategorical < CAObject
|
|
|
362
369
|
# unless the block returns one of the labels.
|
|
363
370
|
# @raise [ArgumentError] when the block returns something that is not
|
|
364
371
|
# a label.
|
|
365
|
-
def insert_block
|
|
372
|
+
def insert_block(offset, bsize, &block)
|
|
366
373
|
fill = block ? code_for_fill(block.call) : UNDEF
|
|
367
374
|
CACategorical.from_codes(codes.insert_block(offset, bsize) { fill }, labels)
|
|
368
375
|
end
|
|
@@ -373,12 +380,13 @@ class CACategorical < CAObject
|
|
|
373
380
|
# Returns a boolean CArray marking cells whose category equals
|
|
374
381
|
# `label`. A CArray is compared cell by cell, by label: another
|
|
375
382
|
# categorical is matched whatever order its labels are in, and a
|
|
376
|
-
# masked cell on either side gives UNDEF.
|
|
377
|
-
#
|
|
378
|
-
# nothing.
|
|
383
|
+
# masked cell on either side gives UNDEF. `UNDEF` asks the mask, as for
|
|
384
|
+
# {CArray#eq}: true where a cell is in no category. Anything else, a Ruby
|
|
385
|
+
# Array included, is one label. An unknown label matches nothing.
|
|
379
386
|
# @param label [Object, CArray] category to match, or one per cell.
|
|
380
387
|
# @return [CArray]
|
|
381
388
|
def eq(label)
|
|
389
|
+
return is_masked if label.equal?(UNDEF)
|
|
382
390
|
codes.eq(codes_of(label))
|
|
383
391
|
end
|
|
384
392
|
|
|
@@ -388,24 +396,31 @@ class CACategorical < CAObject
|
|
|
388
396
|
# cell.
|
|
389
397
|
# @return [CArray]
|
|
390
398
|
def ne(label)
|
|
399
|
+
return is_not_masked if label.equal?(UNDEF)
|
|
391
400
|
codes.ne(codes_of(label))
|
|
392
401
|
end
|
|
393
402
|
|
|
394
403
|
# @overload count(label)
|
|
395
404
|
# Returns the number of cells whose category equals `label`
|
|
396
|
-
# (0 for an unknown label).
|
|
405
|
+
# (0 for an unknown label). `UNDEF` counts the cells in no category,
|
|
406
|
+
# as {#count_masked}. The keywords of {CArray#count} (`axis:`,
|
|
407
|
+
# `keep_axis:`, `min_count:`, `fill_value:`) are taken in both forms.
|
|
397
408
|
# @param label [Object] category to count.
|
|
398
409
|
# @return [Integer]
|
|
399
410
|
# @overload count
|
|
400
411
|
# Returns the number of cells in some category (not masked), as
|
|
401
412
|
# {CArray#count} with no value does.
|
|
402
413
|
# @return [Integer]
|
|
403
|
-
def count(*args)
|
|
404
|
-
return count_not_masked if args.empty?
|
|
414
|
+
def count(*args, **kw)
|
|
415
|
+
return count_not_masked(**kw) if args.empty?
|
|
405
416
|
raise ArgumentError, "count: wrong number of arguments (given #{args.size}, expected 0..1)" if args.size > 1
|
|
406
417
|
label = args[0]
|
|
418
|
+
return count_masked(**kw) if label.equal?(UNDEF)
|
|
407
419
|
code = @labels.index(label)
|
|
408
|
-
|
|
420
|
+
return codes.count(code, **kw) if code
|
|
421
|
+
# an unknown label matches no cell; counted over the same mask so the
|
|
422
|
+
# keywords (axis:, min_count:, fill_value:) answer as for a known label
|
|
423
|
+
kw.empty? ? 0 : codes.ne(codes).count(true, **kw)
|
|
409
424
|
end
|
|
410
425
|
|
|
411
426
|
# Per-category counts as a length-k array aligned to #labels. Trailing empty
|
|
@@ -432,10 +447,6 @@ class CACategorical < CAObject
|
|
|
432
447
|
@category_sizes = out.freeze
|
|
433
448
|
end
|
|
434
449
|
|
|
435
|
-
# Back-compatible alias; {#category_sizes} is the canonical name (a histogram
|
|
436
|
-
# `bincount` reading is misleading for per-category cell counts).
|
|
437
|
-
alias bincount category_sizes
|
|
438
|
-
|
|
439
450
|
# ---- value-hash discovery family --------------------------------------
|
|
440
451
|
#
|
|
441
452
|
# The codes are storage and the labels are the values, so this family has to
|
|
@@ -454,7 +465,8 @@ class CACategorical < CAObject
|
|
|
454
465
|
# with no cells is not included (use {#labels} for the vocabulary).
|
|
455
466
|
# @param sort [Boolean] when true, sort the labels ascending.
|
|
456
467
|
# @return [CArray] object CArray of labels.
|
|
457
|
-
def unique
|
|
468
|
+
def unique(sort: false)
|
|
469
|
+
sort = sort_flag(sort, "unique")
|
|
458
470
|
u = labels_for(codes.unique)
|
|
459
471
|
sort ? u.sort : u
|
|
460
472
|
end
|
|
@@ -464,7 +476,7 @@ class CACategorical < CAObject
|
|
|
464
476
|
# is the aligned-to-{#labels} counterpart, which keeps the empty ones.
|
|
465
477
|
# @param sort [false, :count, :value] pair ordering.
|
|
466
478
|
# @return [Array(CArray, CArray)]
|
|
467
|
-
def value_counts
|
|
479
|
+
def value_counts(sort: false)
|
|
468
480
|
unless [false, :count, :value].include?(sort)
|
|
469
481
|
raise ArgumentError, "value_counts: sort must be false, :count, or :value"
|
|
470
482
|
end
|
|
@@ -483,7 +495,7 @@ class CACategorical < CAObject
|
|
|
483
495
|
# modal values come back sorted ascending, which for labels is not the
|
|
484
496
|
# code order.
|
|
485
497
|
# @return [CArray, Array<CArray>]
|
|
486
|
-
def mode
|
|
498
|
+
def mode(axis: nil)
|
|
487
499
|
label_values.mode(axis: axis)
|
|
488
500
|
end
|
|
489
501
|
|
|
@@ -492,29 +504,32 @@ class CACategorical < CAObject
|
|
|
492
504
|
# An unknown label matches nothing. Masked cells stay masked.
|
|
493
505
|
# @param values [Array, CArray, CACategorical] labels to test against.
|
|
494
506
|
# @return [CArray]
|
|
495
|
-
def is_in
|
|
507
|
+
def is_in(values)
|
|
496
508
|
refuse_range_set(values, "is_in")
|
|
497
|
-
wanted = label_list(values).filter_map
|
|
509
|
+
wanted = label_list(values).filter_map do |l|
|
|
510
|
+
# nan-order: looks a label up and orders nothing; == does not find a NaN.
|
|
511
|
+
l.is_a?(Float) && l.nan? ? @labels.index { |x| x.is_a?(Float) && x.nan? } : @labels.index(l)
|
|
512
|
+
end
|
|
498
513
|
codes.is_in(CArray.int64(wanted.size) { |i| wanted[i] })
|
|
499
514
|
end
|
|
500
515
|
|
|
501
516
|
# @overload intersection(other, sort: false)
|
|
502
517
|
# @return [CArray] object CArray of the labels present in both.
|
|
503
|
-
def intersection
|
|
518
|
+
def intersection(other, sort: false)
|
|
504
519
|
refuse_range_set(other, "intersection")
|
|
505
520
|
unique.intersection(label_array(other), sort: sort)
|
|
506
521
|
end
|
|
507
522
|
|
|
508
523
|
# @overload difference(other, sort: false)
|
|
509
524
|
# @return [CArray] object CArray of the labels only `self` has.
|
|
510
|
-
def difference
|
|
525
|
+
def difference(other, sort: false)
|
|
511
526
|
refuse_range_set(other, "difference")
|
|
512
527
|
unique.difference(label_array(other), sort: sort)
|
|
513
528
|
end
|
|
514
529
|
|
|
515
530
|
# @overload union(other, sort: false)
|
|
516
531
|
# @return [CArray] object CArray of the labels of either side.
|
|
517
|
-
def union
|
|
532
|
+
def union(other, sort: false)
|
|
518
533
|
refuse_range_set(other, "union")
|
|
519
534
|
unique.union(label_array(other), sort: sort)
|
|
520
535
|
end
|
|
@@ -522,7 +537,7 @@ class CACategorical < CAObject
|
|
|
522
537
|
# @overload locate_addr(ref)
|
|
523
538
|
# @return [CArray] for each cell, its label's address in `ref` (UNDEF when
|
|
524
539
|
# the label does not occur there).
|
|
525
|
-
def locate_addr
|
|
540
|
+
def locate_addr(ref)
|
|
526
541
|
label_values.locate_addr(label_array(ref))
|
|
527
542
|
end
|
|
528
543
|
|
|
@@ -540,7 +555,7 @@ class CACategorical < CAObject
|
|
|
540
555
|
# cell by cell, a masked cell staying masked; a categorical goes through its
|
|
541
556
|
# own codes, so the two vocabularies need not agree. Anything else is one
|
|
542
557
|
# label, as CArray#eq takes it.
|
|
543
|
-
def codes_of
|
|
558
|
+
def codes_of(other)
|
|
544
559
|
return @labels.index(other) || @labels.size unless other.is_a?(CArray)
|
|
545
560
|
if other.is_a?(CACategorical)
|
|
546
561
|
table = other.labels.map { |l| @labels.index(l) || @labels.size }
|
|
@@ -554,20 +569,20 @@ class CACategorical < CAObject
|
|
|
554
569
|
end
|
|
555
570
|
|
|
556
571
|
# The code a fill value stands for: UNDEF stays UNDEF, a label is its code.
|
|
557
|
-
def code_for_fill
|
|
572
|
+
def code_for_fill(value)
|
|
558
573
|
return UNDEF if UNDEF.equal?(value)
|
|
559
574
|
code = label_position(value)
|
|
560
575
|
raise ArgumentError, "#{value.inspect} is not one of the labels #{labels.inspect}" unless code
|
|
561
576
|
code
|
|
562
577
|
end
|
|
563
578
|
|
|
564
|
-
def label_at
|
|
579
|
+
def label_at(code)
|
|
565
580
|
(code < 0 || code >= @labels.size) ? nil : @labels[code]
|
|
566
581
|
end
|
|
567
582
|
|
|
568
583
|
# Labels for a code array. The discovery kernels skip masked cells, so what
|
|
569
584
|
# reaches here is real codes — but it is decoded like any other code.
|
|
570
|
-
def labels_for
|
|
585
|
+
def labels_for(code_array)
|
|
571
586
|
CA_OBJECT(code_array.to_a.map { |c| label_at(c) })
|
|
572
587
|
end
|
|
573
588
|
|
|
@@ -578,11 +593,11 @@ class CACategorical < CAObject
|
|
|
578
593
|
|
|
579
594
|
# An operand's labels as a plain Array (a masked cell comes through as UNDEF,
|
|
580
595
|
# which matches no label and so drops out of the set).
|
|
581
|
-
def label_list
|
|
596
|
+
def label_list(other)
|
|
582
597
|
other.respond_to?(:to_a) ? other.to_a : Array(other)
|
|
583
598
|
end
|
|
584
599
|
|
|
585
|
-
def label_array
|
|
600
|
+
def label_array(other)
|
|
586
601
|
CA_OBJECT(label_list(other))
|
|
587
602
|
end
|
|
588
603
|
|
|
@@ -593,7 +608,7 @@ class CACategorical < CAObject
|
|
|
593
608
|
# The scatter path (axis_group) handles the monoid reductions (sum / mean /
|
|
594
609
|
# min / max / variance) in one pass. Order statistics (median / percentile)
|
|
595
610
|
# cannot be scattered: they need every value of a group held together. These
|
|
596
|
-
# two accessors are the
|
|
611
|
+
# two accessors are the parts that other engine uses — lay the data
|
|
597
612
|
# out as category-contiguous blocks, then select per block.
|
|
598
613
|
#
|
|
599
614
|
# The grouping plan — group_addr (the counting sort, the dominant cost) plus
|
|
@@ -615,7 +630,7 @@ class CACategorical < CAObject
|
|
|
615
630
|
# / category_sizes). Optional — the plan is built lazily on first grouping
|
|
616
631
|
# access — but useful to pay the counting sort once ahead of a batch.
|
|
617
632
|
# @return [self]
|
|
618
|
-
def build_grouping
|
|
633
|
+
private def build_grouping
|
|
619
634
|
group_addr # pulls category_sizes; reduceat_index shares category_sizes
|
|
620
635
|
reduceat_index
|
|
621
636
|
self
|
|
@@ -657,7 +672,7 @@ class CACategorical < CAObject
|
|
|
657
672
|
# Returns an int64 CArray, one entry per {#labels}, of segment start
|
|
658
673
|
# offsets aligned to {#labels}. Pair with {#category_sizes} for lengths.
|
|
659
674
|
# @return [CArray]
|
|
660
|
-
def reduceat_index
|
|
675
|
+
private def reduceat_index
|
|
661
676
|
return @reduceat_index if @reduceat_index
|
|
662
677
|
# Every boundary but the last; the copy makes it an entity the
|
|
663
678
|
# reduceat kernels can read directly.
|
|
@@ -671,7 +686,8 @@ class CACategorical < CAObject
|
|
|
671
686
|
# category of `self` with no cells does not carry over; a masked cell
|
|
672
687
|
# is excluded.
|
|
673
688
|
# @return [CACategorical]
|
|
674
|
-
def categorize
|
|
689
|
+
def categorize(labels: nil, sort_labels: false)
|
|
690
|
+
sort_labels = CArray.send(:bool_arg, sort_labels, "sort_labels", "categorize")
|
|
675
691
|
if labels || sort_labels
|
|
676
692
|
return label_values.categorize(labels: labels, sort_labels: sort_labels)
|
|
677
693
|
end
|
|
@@ -682,7 +698,7 @@ class CACategorical < CAObject
|
|
|
682
698
|
|
|
683
699
|
# The position of +label+ among the labels, found by ==, a NaN label by a
|
|
684
700
|
# NaN, as join and at match NaN keys; nil when it is not one of them.
|
|
685
|
-
private def label_position
|
|
701
|
+
private def label_position(label)
|
|
686
702
|
return @labels.index(label) unless label.is_a?(Float) && label.nan?
|
|
687
703
|
@labels.index { |l| l.is_a?(Float) && l.nan? }
|
|
688
704
|
end
|
|
@@ -712,7 +728,8 @@ class CACategorical < CAObject
|
|
|
712
728
|
# not a label, when `base:` and `full: true` are both given, when
|
|
713
729
|
# there are no labels to leave one out of, or when the categorical is
|
|
714
730
|
# not one-dimensional.
|
|
715
|
-
def contrast
|
|
731
|
+
def contrast(scheme = :dummy, base: nil, full: false)
|
|
732
|
+
full = CArray.send(:bool_arg, full, "full", "contrast")
|
|
716
733
|
unless scheme == :dummy
|
|
717
734
|
raise ArgumentError, "contrast: unknown scheme #{scheme.inspect} (only :dummy is defined)"
|
|
718
735
|
end
|
|
@@ -760,6 +777,19 @@ end
|
|
|
760
777
|
|
|
761
778
|
|
|
762
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
|
+
|
|
763
793
|
# Build a CACategorical from self read as category keys (= the values whose
|
|
764
794
|
# distinct levels become the categories). Codes are dense 0-based in the
|
|
765
795
|
# order labels appear (first-appearance by default, or ascending sorted
|
|
@@ -777,29 +807,23 @@ class CArray
|
|
|
777
807
|
# vocabulary is fixed and keys outside it become masked
|
|
778
808
|
# (excluded). `sort_labels:` is ignored when an explicit `labels`
|
|
779
809
|
# is given (the caller has already chosen the order).
|
|
780
|
-
# @param labels [Array, CArray, nil] fixed vocabulary; `nil`
|
|
810
|
+
# @param labels [Array, CArray, Range, nil] fixed vocabulary; `nil`
|
|
781
811
|
# enables discovery.
|
|
782
812
|
# @param sort_labels [Boolean] when discovering (labels: nil), sort
|
|
783
813
|
# the discovered vocabulary ascending after collecting it.
|
|
784
814
|
# @return [CACategorical]
|
|
785
815
|
# @raise [ArgumentError] when explicit `labels` contain
|
|
786
816
|
# duplicates.
|
|
817
|
+
# @raise [TypeError] when `labels` is not a list (a String, say).
|
|
787
818
|
# @raise [CArray::DataTypeError] when `sort_labels: true` discovers
|
|
788
819
|
# the labels of a complex array, which have no order.
|
|
789
|
-
# The order sort_labels: puts two labels in: the order sort puts the cells
|
|
790
|
-
# of an array in (object_order_cmp); two labels <=> cannot order are
|
|
791
|
-
# refused, by name.
|
|
792
|
-
private def label_order(a, b)
|
|
793
|
-
c = CArray.send(:object_order_cmp, a, b)
|
|
794
|
-
if c.nil?
|
|
795
|
-
raise ArgumentError,
|
|
796
|
-
"categorize(sort_labels: true): cannot order the labels " \
|
|
797
|
-
"#{a.inspect} and #{b.inspect}"
|
|
798
|
-
end
|
|
799
|
-
c
|
|
800
|
-
end
|
|
801
|
-
|
|
802
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
|
|
803
827
|
# Automatic appearance-order vocabulary: one linear pass (C
|
|
804
828
|
# __factorize_appearance__) returns both codes and levels directly, over the
|
|
805
829
|
# integer / float / object / fixlen / boolean lanes (boolean rides the uint8
|
|
@@ -873,7 +897,8 @@ end
|
|
|
873
897
|
# carried here, around all of them.
|
|
874
898
|
[CArray, CACategorical, CAConstString].each do |klass|
|
|
875
899
|
klass.prepend(Module.new do
|
|
876
|
-
def categorize
|
|
900
|
+
def categorize(labels: nil, sort_labels: false)
|
|
901
|
+
sort_labels = CArray.send(:bool_arg, sort_labels, "sort_labels", "categorize")
|
|
877
902
|
carry_attrs(super)
|
|
878
903
|
end
|
|
879
904
|
end)
|