Module: ColumnsMatcher::Converter
- Defined in:
- lib/columns_matcher/converter.rb
Class Method Summary collapse
-
.convert_from_array_to_numeric_array(values) ⇒ Object
values is an array of any type Returns an array of numeric types.
-
.convert_from_array_to_r_vector(values) ⇒ Object
values is an array of numeric types Returns an R vector.
-
.convert_from_vectors_to_euclidian_distance_cost_matrix(first_vectors, second_vectors) ⇒ Object
first_vectors and second_vectors are arrays of vectors in the format of [Vertex weight, [Edges’ weights in descending order]] Returns a square cost matrix - cost calculated using Euclidian distance - for input into Munkres assignment algorithm.
-
.convert_from_vertices_to_vectors(graph) ⇒ Object
graph is an UndirectedGraph Returns an array of vectors in the format of [Vertex weight, [Edges’ weights in descending order]] for input into calculation of Euclidian distance.
Class Method Details
.convert_from_array_to_numeric_array(values) ⇒ Object
values is an array of any type Returns an array of numeric types
5 6 7 |
# File 'lib/columns_matcher/converter.rb', line 5 def self.convert_from_array_to_numeric_array(values) values.collect {|value| value.is_a?(String) ? value.sum : value } end |
.convert_from_array_to_r_vector(values) ⇒ Object
values is an array of numeric types Returns an R vector
11 12 13 |
# File 'lib/columns_matcher/converter.rb', line 11 def self.convert_from_array_to_r_vector(values) "c(#{values.join(',')})" end |
.convert_from_vectors_to_euclidian_distance_cost_matrix(first_vectors, second_vectors) ⇒ Object
first_vectors and second_vectors are arrays of vectors in the format of [Vertex weight, [Edges’ weights in descending order]] Returns a square cost matrix - cost calculated using Euclidian distance - for input into Munkres assignment algorithm
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/columns_matcher/converter.rb', line 30 def self.convert_from_vectors_to_euclidian_distance_cost_matrix(first_vectors, second_vectors) combinations_of_vectors = first_vectors.product(second_vectors) euclidian_distances = combinations_of_vectors.map {|combination_of_vector| Statistics::euclidian_distance(combination_of_vector.first, combination_of_vector.last) } squared_vector = nil cost_matrix = nil size = first_vectors.size >= second_vectors.size ? first_vectors.size : second_vectors.size squared_vector = Array.new(size ** 2) { Graph::STUB_VALUE } (0..(first_vectors.size - 1)).each do |row| (0..(second_vectors.size - 1)).each do |column| index = (row * size) + column squared_vector[index] = euclidian_distances[index] end end cost_matrix = squared_vector.each_slice(size).to_a end |
.convert_from_vertices_to_vectors(graph) ⇒ Object
graph is an UndirectedGraph Returns an array of vectors in the format of [Vertex weight, [Edges’ weights in descending order]] for input into calculation of Euclidian distance
18 19 20 21 22 23 24 25 26 |
# File 'lib/columns_matcher/converter.rb', line 18 def self.convert_from_vertices_to_vectors(graph) graph_vectors = [] graph.vertices.each do |vertex| graph_vectors << [graph[vertex]] + graph.adjacent(vertex, { type: :edges }).collect{|edge| edge.label }.sort {|first, second| second <=> first } end graph_vectors end |