Graphs

View as Markdown

After defining your Table objects, construct a Graph to connect them through primary key and foreign key relationships.

Creating a Graph

1graph = kumo.Graph(
2 # These are the tables that participate in the graph: the keys of this
3 # dictionary are the names of the tables, and the values are the Table
4 # objects that correspond to these names:
5 tables={
6 'customer': customer,
7 'stock': stock,
8 'transaction': transaction,
9 },
10
11 # These are the edges that define the primary key / foreign key
12 # relationships between the tables defined above. Here, `src_table`
13 # is the table that has the foreign key `fkey`, which maps to the
14 # table `dst_table`'s primary key:
15 edges=[
16 dict(src_table='transaction', fkey='StockCode', dst_table='stock'),
17 dict(src_table='transaction', fkey='CustomerID', dst_table='customer'),
18 ],
19)
20
21# Validate the graph's correctness:
22graph.validate(verbose=True)

Editing a Graph

Use the following methods to modify the graph after creation:

1# Add a table to an existing graph:
2graph.add_table('new_table_name', new_table)
3
4# Remove a table:
5graph.remove_table('table_name')
6
7# Add an edge between two tables:
8graph.link(kumo.Edge('src_table', 'fkey_column', 'dst_table'))
9
10# Remove an edge:
11graph.unlink('src_table', 'fkey_column', 'dst_table')

Snapshotting a Graph

Graph.snapshot() locks in the current version of all tables so that multiple training runs use the same data, even as the underlying source changes. You must also snapshot the graph before viewing edge health statistics, which report how many foreign key values match a primary key across each edge.

1# Snapshot the graph to lock in the current data:
2graph.snapshot()
3
4# View edge health statistics after snapshotting:
5stats = graph.get_edge_stats()
6print(stats)