Graph Collection

A graph collection is a collection of graphs.

Create a graph

from collaboration_detection.datastructures import GraphCollection

# Create a graph collection
graph_collection = GraphCollection(collection_id="myGraphCollection4711")
# Create a graph with two vertices and one edge
my_graph = graph_collection.new_graph()
v1 = my_graph.get_vertex("My Node")
v2 = my_graph.get_vertex("My second Node")
e = v1.add_edge(v2)
# Get the graph with the id 0
my_graph2 = graph_collection.graphs[0]
assert my_graph.graph_id == my_graph2.graph_id == 0

Graph Cluster

Graphs can be assigned to a cluster inside the Graph Collection.

# Get the cluster with cluster_id = 0
cluster_0 = graph_collection.clusters[0]
cluster_0.graphs # All graphs of cluster 0
cluster_0.representative # The representative of the cluster
cluster_0.attributes # Attributes for this cluster

Import and export

# Export/Import
# .. as pickle file
graph_collection.export("myGraphs.gc")
graph_collection = GraphCollection.load("myGraphs.gc")
# .. as string representation
graph_collection.save_label_mapping("labels.pickel")  # save the labels individually
graph_collection.save_str_rep("graphs.db")
# Load with label mapping, because the str representation does not contain labels!
graph_collection = GraphCollection(
    label_list="labels.pickel"
)
graph_collection.load_graphs_from_str_rep_file("graphs.db")
# Single graph as networkx graphs
graph_collection.as_networkx_digraphs()
# Single graph as svg or png:
graph_collection.graphs[0].to_dot_digraph("myGraph.svg") # or *.png
# Single graph as adjacency matrix
matrix = graph_collection.graphs[0].as_adjacency_matrix
# Single graph as networkx graph
nx_graph= graph_collection.graphs[0].as_networkx_digraph

Sync with a neo4j database

from collaboration_detection.datastructures.neo4jstorage import GraphCollectionRepository

repo = GraphCollectionRepository(uri="neo4j://localhost:7687", user="neo4j", password="pw")
# Upload the graph collection to a neo4j database
repo.upload_graph_collection(graph_collection)
# Download a graph collection from a neo4j database
graph_collection = repo.download_graph_collection(collection_id="myGraphCollection4711")