Class: HexWrench::Weka::Explorer::Header
- Inherits:
-
Object
- Object
- HexWrench::Weka::Explorer::Header
- Defined in:
- lib/hexwrench/weka/explorer.rb
Overview
represents an ARFF-like header
Instance Attribute Summary collapse
-
#model ⇒ Object
readonly
Returns the value of attribute model.
-
#perspective ⇒ Object
readonly
Returns the value of attribute perspective.
-
#perspectives ⇒ Object
readonly
Returns the value of attribute perspectives.
-
#relation_name ⇒ Object
readonly
Returns the value of attribute relation_name.
-
#resources_cnt ⇒ Object
readonly
Returns the value of attribute resources_cnt.
Instance Method Summary collapse
- #add_resource(resource) ⇒ Object
- #add_resource_to_instances(resource, instances) ⇒ Object
- #add_values_to_instances(values, instances) ⇒ Object
- #attribute(name) ⇒ Object
- #attribute_for_feature(feat) ⇒ Object
- #attributes ⇒ Object
- #attributes_pairs ⇒ Object
- #create_attributes_pairs ⇒ Object
- #create_fast_vector ⇒ Object
- #create_instances ⇒ Object
- #fast_vector ⇒ Object
- #features ⇒ Object
- #header(name) ⇒ Object
-
#headers ⇒ Object
headers for related attributes.
-
#initialize(model, perspective_tree, resources_cnt = 0, name = "dummy") ⇒ Header
constructor
A new instance of Header.
- #instances ⇒ Object
- #new_data_instances ⇒ Object
- #values_for_resources(resource) ⇒ Object
Constructor Details
#initialize(model, perspective_tree, resources_cnt = 0, name = "dummy") ⇒ Header
Returns a new instance of Header.
9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/hexwrench/weka/explorer.rb', line 9 def initialize(model, perspective_tree, resources_cnt=0, name="dummy") @model = model if perspective_tree.is_a?(Array) @perspective = perspective_tree[0] @perspectives = perspective_tree[1] else @perspective = perspective_tree @perspectives = {} end @resources_cnt = resources_cnt @relation_name = name end |
Instance Attribute Details
#model ⇒ Object (readonly)
Returns the value of attribute model.
8 9 10 |
# File 'lib/hexwrench/weka/explorer.rb', line 8 def model @model end |
#perspective ⇒ Object (readonly)
Returns the value of attribute perspective.
8 9 10 |
# File 'lib/hexwrench/weka/explorer.rb', line 8 def perspective @perspective end |
#perspectives ⇒ Object (readonly)
Returns the value of attribute perspectives.
8 9 10 |
# File 'lib/hexwrench/weka/explorer.rb', line 8 def perspectives @perspectives end |
#relation_name ⇒ Object (readonly)
Returns the value of attribute relation_name.
8 9 10 |
# File 'lib/hexwrench/weka/explorer.rb', line 8 def relation_name @relation_name end |
#resources_cnt ⇒ Object (readonly)
Returns the value of attribute resources_cnt.
8 9 10 |
# File 'lib/hexwrench/weka/explorer.rb', line 8 def resources_cnt @resources_cnt end |
Instance Method Details
#add_resource(resource) ⇒ Object
150 151 152 |
# File 'lib/hexwrench/weka/explorer.rb', line 150 def add_resource(resource) add_resource_to_instances(resource, instances) end |
#add_resource_to_instances(resource, instances) ⇒ Object
146 147 148 |
# File 'lib/hexwrench/weka/explorer.rb', line 146 def add_resource_to_instances(resource, instances) add_values_to_instances(values_for_resources(resource), instances) end |
#add_values_to_instances(values, instances) ⇒ Object
142 143 144 |
# File 'lib/hexwrench/weka/explorer.rb', line 142 def add_values_to_instances(values, instances) instances.add(Weka::Instance.new(1.0, values.to_java(Java::double))) end |
#attribute(name) ⇒ Object
28 29 30 31 |
# File 'lib/hexwrench/weka/explorer.rb', line 28 def attribute(name) pair = attributes_pairs.find{|n, attr| n == name} pair.last if pair end |
#attribute_for_feature(feat) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/hexwrench/weka/explorer.rb', line 64 def attribute_for_feature(feat) name = File.join(relation_name, feat.sym.to_s) case feat when NumericFeature Weka::Attribute.new(name) when DateFeature Weka::Attribute.new(name, feat.weka_format) when NominalFeature Weka::Attribute.new(name, feat.labels_fv) when StringFeature construct = Weka::Attribute.java_class.constructor(java.lang.String, Weka::FastVector) construct.new_instance(name, nil).to_java when RelationFeature = feat. = perspectives[feat.sym] || :default cnt = 0 #XXX could be taken more cleverly if support in Welo's relationship header = Header.new(, , cnt, name) headers[feat.sym] = header construct = Weka::Attribute.java_class.constructor(java.lang.String, Weka::Instances) construct.new_instance(name, header.instances).to_java else raise ArgumentError, "don't know how to handler #{feat} to make an attribute" end end |
#attributes ⇒ Object
33 34 35 |
# File 'lib/hexwrench/weka/explorer.rb', line 33 def attributes attributes_pairs.map{|name, attr| attr} end |
#attributes_pairs ⇒ Object
37 38 39 |
# File 'lib/hexwrench/weka/explorer.rb', line 37 def attributes_pairs @attributes_pairs ||= create_attributes_pairs end |
#create_attributes_pairs ⇒ Object
49 50 51 52 53 |
# File 'lib/hexwrench/weka/explorer.rb', line 49 def create_attributes_pairs features.map do |feat| [feat.sym, attribute_for_feature(feat)] end end |
#create_fast_vector ⇒ Object
91 92 93 94 95 96 97 |
# File 'lib/hexwrench/weka/explorer.rb', line 91 def create_fast_vector fv = Weka::FastVector.new attributes.each do |attribute| fv.add_element(attribute) end fv end |
#create_instances ⇒ Object
99 100 101 |
# File 'lib/hexwrench/weka/explorer.rb', line 99 def create_instances Weka::Instances.new(relation_name, fast_vector, resources_cnt) end |
#fast_vector ⇒ Object
41 42 43 |
# File 'lib/hexwrench/weka/explorer.rb', line 41 def fast_vector @fast_vector ||= create_fast_vector end |
#features ⇒ Object
22 23 24 25 26 |
# File 'lib/hexwrench/weka/explorer.rb', line 22 def features model.perspective(perspective).fields.map do |sym| model.feature(sym) end end |
#header(name) ⇒ Object
60 61 62 |
# File 'lib/hexwrench/weka/explorer.rb', line 60 def header(name) headers[name] end |
#headers ⇒ Object
headers for related attributes
56 57 58 |
# File 'lib/hexwrench/weka/explorer.rb', line 56 def headers @headers ||= {} end |
#instances ⇒ Object
45 46 47 |
# File 'lib/hexwrench/weka/explorer.rb', line 45 def instances @instances ||= create_instances end |
#new_data_instances ⇒ Object
139 140 |
# File 'lib/hexwrench/weka/explorer.rb', line 139 def new_data_instances end |
#values_for_resources(resource) ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/hexwrench/weka/explorer.rb', line 103 def values_for_resources(resource) features.map do |feat| rb_val = resource.send(feat.sym) attribute = attribute(feat.sym) val = case feat when DateFeature date_str = if rb_val.respond_to?(:strftime) fmt = feat.format || DateFeature.default_format rb_val.send(:strftime, fmt) else rb_val end attribute.parseDate(date_str) when NominalFeature attribute.indexOfValue(rb_val.to_s.to_java) when StringFeature attribute.addStringValue(rb_val.to_java) when RelationFeature header = header(feat.sym) raise NotImplementedError, "no header built for #{feat} yet" unless header data_instances = Weka::Instances.new(attribute.relation, 0) if feat.relationship.many? rb_val.each do |rb_val_| header.add_resource_to_instances(rb_val_, data_instances) end else header.add_resource_to_instances(rb_val, data_instances) end attribute.addRelation(data_instances) else rb_val end val end end |