Module: ColumnsMatcher::Graph

Includes:
GRATR
Defined in:
lib/columns_matcher/graph.rb

Constant Summary collapse

STUB_VALUE =
1000

Class Method Summary collapse

Class Method Details

.build_dependency_graph(columns) ⇒ Object

column names as the keys and column values as the values Returns a graph of vertices and edges, with weights



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/columns_matcher/graph.rb', line 12

def self.build_dependency_graph(columns)
	combinations_of_column_names = Utilities::get_combinations(columns.keys)

   graph = UndirectedGraph.new

 	combinations_of_column_names.map do |combination_of_column_names|
     first_column_name = combination_of_column_names.first
     second_column_name = combination_of_column_names.last

 		first_column_values = columns[first_column_name]
 		second_column_values = columns[second_column_name]

     if first_column_name == second_column_name
       entropy_of_column = Statistics::entropy(first_column_values)

       graph[first_column_name] = entropy_of_column
     else
       mutual_information_between_columns = Statistics::mutual_information(first_column_values, second_column_values)

       graph.add_edge!(first_column_name, second_column_name, mutual_information_between_columns)
     end
 	end

   graph
end

.get_mappings(pairings, first_graph, second_graph) ⇒ Object

pairings is a 2D array of sub-arrays of size 2 - the results of the Munkres assignment algorithm first_graph and second_graph are UndirectedGraph’s Returns mappings of columns with the keys as columns to match and values as columns to match against



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/columns_matcher/graph.rb', line 57

def self.get_mappings(pairings, first_graph, second_graph)
  first_graph_vertices = first_graph.vertices
  second_graph_vertices = second_graph.vertices

  mappings = Hash.new

  pairings.sort {|first, second| first.first <=> second.first }.each do |pair|
    first_graph_vertex = first_graph_vertices[pair.last]
    second_graph_vertex = second_graph_vertices[pair.first]

    mappings[second_graph_vertex] = first_graph_vertex
  end

  mappings
end

.match_graphs(graph_to_match_against, graph_to_match) ⇒ Object

graph_to_match_against and graph_to_match are are UndirectedGraph’s Returns mappings of columns with the keys as columns to match and values as columns to match against



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/columns_matcher/graph.rb', line 40

def self.match_graphs(graph_to_match_against, graph_to_match)
   graph_to_match_against_vectors = Converter::convert_from_vertices_to_vectors(graph_to_match_against)
   graph_to_match_vectors = Converter::convert_from_vertices_to_vectors(graph_to_match)

   cost_matrix = Converter::convert_from_vectors_to_euclidian_distance_cost_matrix(graph_to_match_vectors, graph_to_match_against_vectors)

   pairings = Statistics::munkres_assignment_algorithm(cost_matrix)

   mappings = Graph::get_mappings(pairings, graph_to_match_against, graph_to_match)

   mappings
end