Class: Bmg::Operator::Summarize

Inherits:
Object
  • Object
show all
Includes:
Unary
Defined in:
lib/bmg/operator/summarize.rb

Overview

Summarize operator.

Makes a summarization by some attributes, applying aggregations to the corresponding images.

Instance Attribute Summary

Attributes included from Bmg::Operator

#type

Instance Method Summary collapse

Methods included from Unary

#bind

Methods included from Bmg::Operator

#inspect, #to_s

Methods included from Relation

#_count, #bind, #count, #debug, #delete, empty, #empty?, #insert, new, #one, #one_or_nil, #to_csv, #to_json, #to_xlsx, #type, #update, #visit, #with_type, #with_type_attrlist, #with_typecheck, #without_typecheck, #y_by_x, #ys_by_x

Methods included from Algebra

#allbut, #autosummarize, #autowrap, #constants, #extend, #group, #image, #join, #left_join, #matching, #materialize, #not_matching, #page, #project, #rename, #restrict, #spied, #summarize, #transform, #ungroup, #union, #unspied, #unwrap

Methods included from Algebra::Shortcuts

#exclude, #image, #images, #join, #left_join, #matching, #not_matching, #prefix, #rxmatch, #suffix, #ungroup, #unwrap, #where

Constructor Details

#initialize(type, operand, by, summarization) ⇒ Summarize

Returns a new instance of Summarize.



12
13
14
15
16
17
# File 'lib/bmg/operator/summarize.rb', line 12

def initialize(type, operand, by, summarization)
  @type = type
  @operand = operand
  @by = by
  @summarization = Summarizer.summarization(summarization)
end

Instance Method Details

#eachObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/bmg/operator/summarize.rb', line 25

def each
  return to_enum unless block_given?
  # summary key => summarization memo, starting with least
  result = Hash.new{|h,k|
    h[k] = Hash[@summarization.map{|k,v|
      [ k, v.least ]
    }]
  }
  # iterate each tuple
  @operand.each do |tuple|
    key = TupleAlgebra.project(tuple, @by)
    # apply them all and create a new memo
    result[key] = Hash[@summarization.map{|k,v|
      [ k, v.happens(result[key][k], tuple) ]
    }]
  end
  # Merge result keys and values
  result.each_pair do |by,sums|
    tuple = Hash[@summarization.map{|k,v|
      [ k, v.finalize(sums[k]) ]
    }].merge(by)
    yield(tuple)
  end
end

#to_astObject



50
51
52
# File 'lib/bmg/operator/summarize.rb', line 50

def to_ast
  [ :summarize, operand.to_ast, by, summarization ]
end