Class: GeneValidator::PairCluster

Inherits:
Object
  • Object
show all
Defined in:
lib/genevalidator/clusterization.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(objects) ⇒ PairCluster

Returns a new instance of PairCluster.



63
64
65
# File 'lib/genevalidator/clusterization.rb', line 63

def initialize(objects)
  @objects = objects
end

Instance Attribute Details

#objectsObject

a hash map containing the pair (object, no_occurences)



61
62
63
# File 'lib/genevalidator/clusterization.rb', line 61

def objects
  @objects
end

Instance Method Details

#add(cluster) ⇒ Object

Merges the current cluster with the one given as parameter clusters vector of Cluster objects



144
145
146
147
148
# File 'lib/genevalidator/clusterization.rb', line 144

def add(cluster)
  cluster.objects.each do |elem|
    objects[elem[0]] = elem[1]
  end
end

#densityObject

Returns the density of the cluster: how many values it contains



91
92
93
94
95
96
97
# File 'lib/genevalidator/clusterization.rb', line 91

def density
  d = 0
  objects.each do |elem|
    d += elem[1]
  end
  d
end

#distance(cluster, method = 0) ⇒ Object

Returns the euclidian distance between the current cluster and the one given as parameter Params: cluster: Cluster object method: 0 or 1 method = 0: do not into condseideration duplicate values method = 1: average linkage clusterization



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/genevalidator/clusterization.rb', line 106

def distance(cluster, method = 0)
  d = 0
  norm = 0

  cluster.objects.each do |elem1|
    objects.each do |elem2|
      if method == 1
        d += elem1[1] * elem2[1] * (elem1[0] - elem2[0]).abs
        norm += elem1[1] * elem2[1]
      else
        d += (elem1[0] - elem2[0]).abs
        norm = cluster.objects.length * objects.length
      end
    end
  end

  # group average distance
  d /= (norm + 0.0)
end

#meanObject

Returns the weighted mean value of the cluster



75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/genevalidator/clusterization.rb', line 75

def mean
  mean = Pair.new(0, 0)
  weight = 0

  objects.each do |object, n|
    (1..n).each do |_i|
      mean + object
      weight += 1
    end
  end
  mean / weight
  mean
end


67
68
69
70
71
# File 'lib/genevalidator/clusterization.rb', line 67

def print
  objects.each do |elem|
    warn "(#{elem[0].x},#{elem[0].y}): #{elem[1]}"
  end
end

#wss(objects = nil) ⇒ Object

Returns within cluster sum of squares



128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/genevalidator/clusterization.rb', line 128

def wss(objects = nil)
  if objects.nil?
    objects = @objects.map { |x| Array.new(x[1], x[0]) }.flatten
  end

  cluster_mean = mean
  ss = 0
  objects.each do |object|
    ss += (cluster_mean - object) * (cluster_mean - object)
  end
  ss
end