Class: CASlabIterator
- Inherits:
-
CAIterator
- Object
- CAIterator
- CASlabIterator
- Defined in:
- lib/carray/slab_iterator.rb
Overview
CASlabIterator.
Created by the C indexer dispatch when an index uses the :> slab-axis
sigil (e.g. ca[nil, :>, :>]). It is a thin sugar over each_slab /
map_slab / reduce_slab: :> axes become the slab handed to the block;
the remaining (sliced) axes are the outer iteration space.
ca[nil, :>].each { |row| ... } # = ca.each_slab(axis: 1)
ca[2..5, :>, :>].map { |slab| ... } # = ca[2..5,nil,nil].map_slab(axis: [-2,-1])
Surface is intentionally minimal (each / map / reduce); to_a / count / size and other Enumerable extras are deferred.
Naming note: distinct from the C-defined CArray::SlabIterator engine
in ext/carray_slab.c -- this is the Ruby-level sigil iterator that
delegates to each_slab / map_slab / reduce_slab. Loaded lazily via
autoload from lib/carray/autoload/autoload_base.rb on first
ca[..., :>] evaluation.
Instance Attribute Summary collapse
-
#reference ⇒ Object
readonly
The array being iterated (slab exposes it as
reference; block / window iterators expose theirs assource). -
#slab_axes ⇒ Object
readonly
The array being iterated (slab exposes it as
reference; block / window iterators expose theirs assource).
Attributes inherited from CAIterator
Instance Method Summary collapse
-
#count(v = <none>) ⇒ CArray
Per-slab count, delegating to
reference.count(..., axis: slab_axes). -
#count_masked ⇒ CArray
Per-slab count of masked cells.
-
#count_not_masked ⇒ CArray
Per-slab count of present (non-masked) cells.
-
#each({ |slab| ... }) {|slab| ... } ⇒ Enumerator, self
Yields each slab as an inner CArray.
-
#elements ⇒ CArray
Per-slab cell count (structural, mask-independent).
-
#initialize(reference, slab_axes) ⇒ CASlabIterator
constructor
Built from C via
CASlabIterator.new(reference, slab_axes):referenceis the sliced base view (with:>axes as full range),slab_axesare the positions marked with:>. -
#kernel_at_index(idx) ⇒ Object
Slab view at outer index
idx(Array, length = self.ndim). -
#map({ |slab| ... }) {|slab| ... } ⇒ CArray
Returns a new CArray built by applying the block to each slab, delegating to
map_slab. -
#median ⇒ CArray
Per-slab median.
- #op ⇒ Object
-
#percentile(*pers) ⇒ CArray+
Per-slab percentile(s).
-
#quantile ⇒ Array<CArray>
Per-slab five-number summary
[min, Q1, median, Q3, max](five CArrays), asCArray#quantile. - #reduce(*args, &block) ⇒ Object
-
#sort_addr ⇒ CArray
Per-slab sort by flat source address.
-
#sort_index ⇒ CArray
Per-slab sort by axis-local index (usable with take_along_axis).
-
#wmean(weights) ⇒ CArray
Per-slab weighted mean,
weightsshaped like the reference. -
#wsum(weights) ⇒ CArray
Per-slab weighted sum,
weightsshaped like the reference.
Constructor Details
#initialize(reference, slab_axes) ⇒ CASlabIterator
Built from C via CASlabIterator.new(reference, slab_axes): reference
is the sliced base view (with :> axes as full range), slab_axes are
the positions marked with :>.
24 25 26 27 28 29 30 31 32 |
# File 'lib/carray/slab_iterator.rb', line 24 def initialize (reference, slab_axes) @reference = reference @slab_axes = slab_axes rdim = reference.shape @outer_positions = (0...reference.ndim).reject { |k| slab_axes.include?(k) }.freeze @shape = @outer_positions.map { |k| rdim[k] } @ndim = @shape.size self end |
Instance Attribute Details
#reference ⇒ Object (readonly)
The array being iterated (slab exposes it as reference; block / window
iterators expose theirs as source). The base has no common accessor.
36 37 38 |
# File 'lib/carray/slab_iterator.rb', line 36 def reference @reference end |
#slab_axes ⇒ Object (readonly)
The array being iterated (slab exposes it as reference; block / window
iterators expose theirs as source). The base has no common accessor.
36 37 38 |
# File 'lib/carray/slab_iterator.rb', line 36 def slab_axes @slab_axes end |
Instance Method Details
#count(v = <none>) ⇒ CArray
125 126 127 128 129 130 |
# File 'lib/carray/slab_iterator.rb', line 125 def count (*args) return count_not_masked if args.empty? # CABlock / CAWindow shadow #count with a geometry accessor, and @reference # may be such a view, so dispatch CArray#count explicitly for count(v). CArray.instance_method(:count).bind_call(@reference, *args, axis: @slab_axes) end |
#count_masked ⇒ CArray
142 143 144 |
# File 'lib/carray/slab_iterator.rb', line 142 def count_masked @reference.count_masked(axis: @slab_axes) end |
#count_not_masked ⇒ CArray
135 136 137 |
# File 'lib/carray/slab_iterator.rb', line 135 def count_not_masked @reference.count_not_masked(axis: @slab_axes) end |
#each({ |slab| ... }) {|slab| ... } ⇒ Enumerator, self
52 53 54 55 |
# File 'lib/carray/slab_iterator.rb', line 52 def each (&block) return @reference.each_slab(axis: @slab_axes) unless block @reference.each_slab(axis: @slab_axes, &block) end |
#elements ⇒ CArray
151 152 153 154 155 156 157 158 |
# File 'lib/carray/slab_iterator.rb', line 151 def elements sz = @slab_axes.inject(1) { |p, ax| p * @reference.shape[ax] } # count_not_masked gives the correct output shape (and, unlike bare #count, # is not shadowed by CABlock / CAWindow); overwrite with the constant size. out = @reference.count_not_masked(axis: @slab_axes) out[] = sz out end |
#kernel_at_index(idx) ⇒ Object
Slab view at outer index idx (Array, length = self.ndim). Returns
@reference[*full_idx] where slab axes are nil (full range) and
outer axes take their values from idx.
41 42 43 44 45 |
# File 'lib/carray/slab_iterator.rb', line 41 def kernel_at_index (idx) full = Array.new(@reference.ndim) # nil at every position @outer_positions.each_with_index { |k, i| full[k] = idx[i] } @reference[*full] end |
#map({ |slab| ... }) {|slab| ... } ⇒ CArray
63 64 65 |
# File 'lib/carray/slab_iterator.rb', line 63 def map (&block) @reference.map_slab(axis: @slab_axes, &block) end |
#median ⇒ CArray
171 172 173 174 175 176 177 |
# File 'lib/carray/slab_iterator.rb', line 171 def median if @slab_axes.size == 1 @reference.median(axis: @slab_axes[0]) else @reference.reduce_slab(axis: @slab_axes) { |s| s.median } end end |
#cumsum ⇒ CArray #cumprod ⇒ CArray #cummax ⇒ CArray #cummin ⇒ CArray #cumcount ⇒ CArray
110 111 112 113 114 115 116 117 118 |
# File 'lib/carray/slab_iterator.rb', line 110 [:sum, :accumulate, :prod, :mean, :min, :max, :variance, :stddev, :all, :any, :variancep, :stddevp, :minmax, :min_index, :max_index, :min_addr, :max_addr].each do |op| class_eval <<~RUBY, __FILE__, __LINE__ + 1 def #{op} @reference.#{op}(axis: @slab_axes) end RUBY end |
#percentile(*pers) ⇒ CArray+
183 184 185 186 187 188 189 190 |
# File 'lib/carray/slab_iterator.rb', line 183 def percentile (*pers) if @slab_axes.size == 1 @reference.percentile(*pers, axis: @slab_axes[0]) else rs = pers.map { |p| @reference.reduce_slab(axis: @slab_axes) { |s| s.percentile(p) } } pers.size == 1 ? rs[0] : rs end end |
#quantile ⇒ Array<CArray>
196 197 198 199 200 201 202 203 204 |
# File 'lib/carray/slab_iterator.rb', line 196 def quantile if @slab_axes.size == 1 @reference.quantile(axis: @slab_axes[0]) else [0, 25, 50, 75, 100].map { |p| @reference.reduce_slab(axis: @slab_axes) { |s| s.percentile(p) } } end end |
#reduce({ |acc, slab| ... }) {|acc, slab| ... } ⇒ Object #reduce(init) {|acc, slab| ... } ⇒ Object
80 81 82 83 84 85 86 |
# File 'lib/carray/slab_iterator.rb', line 80 def reduce (*args, &block) if args.empty? @reference.reduce_slab(axis: @slab_axes, &block) else @reference.reduce_slab(axis: @slab_axes, init: args[0], &block) end end |
#sort_addr ⇒ CArray
218 219 220 |
# File 'lib/carray/slab_iterator.rb', line 218 def sort_addr @reference.sort_addr(axis: single_sort_axis(:sort_addr)) end |
#sort_index ⇒ CArray
227 228 229 |
# File 'lib/carray/slab_iterator.rb', line 227 def sort_index @reference.sort_index(axis: single_sort_axis(:sort_index)) end |
#wmean(weights) ⇒ CArray
247 248 249 |
# File 'lib/carray/slab_iterator.rb', line 247 def wmean (weights) @reference.wmean(weights, axis: @slab_axes) end |
#wsum(weights) ⇒ CArray
240 241 242 |
# File 'lib/carray/slab_iterator.rb', line 240 def wsum (weights) @reference.wsum(weights, axis: @slab_axes) end |