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
116 117 118 119 120 121 |
# File 'lib/carray/slab_iterator.rb', line 116 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
133 134 135 |
# File 'lib/carray/slab_iterator.rb', line 133 def count_masked @reference.count_masked(axis: @slab_axes) end |
#count_not_masked ⇒ CArray
126 127 128 |
# File 'lib/carray/slab_iterator.rb', line 126 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
142 143 144 145 146 147 148 149 |
# File 'lib/carray/slab_iterator.rb', line 142 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
162 163 164 165 166 167 168 |
# File 'lib/carray/slab_iterator.rb', line 162 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
105 106 107 108 109 |
# File 'lib/carray/slab_iterator.rb', line 105 [:sum, :prod, :mean, :min, :max, :variance, :stddev, :all, :any, :variancep, :stddevp, :minmax, :min_index, :max_index, :min_addr, :max_addr].each do |op| define_method(op) { @reference.send(op, axis: @slab_axes) } end |
#percentile(*pers) ⇒ CArray+
174 175 176 177 178 179 180 181 |
# File 'lib/carray/slab_iterator.rb', line 174 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>
187 188 189 190 191 192 193 194 195 |
# File 'lib/carray/slab_iterator.rb', line 187 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
209 210 211 |
# File 'lib/carray/slab_iterator.rb', line 209 def sort_addr @reference.sort_addr(axis: single_sort_axis(:sort_addr)) end |
#sort_index ⇒ CArray
218 219 220 |
# File 'lib/carray/slab_iterator.rb', line 218 def sort_index @reference.sort_index(axis: single_sort_axis(:sort_index)) end |
#wmean(weights) ⇒ CArray
238 239 240 |
# File 'lib/carray/slab_iterator.rb', line 238 def wmean (weights) @reference.wmean(weights, axis: @slab_axes) end |
#wsum(weights) ⇒ CArray
231 232 233 |
# File 'lib/carray/slab_iterator.rb', line 231 def wsum (weights) @reference.wsum(weights, axis: @slab_axes) end |