import networkx as nx


def split_graph(G):
    """splits graph(s) on interactions and return a dictionary of graphs with interaction as keys."""

    # Find all interactions to split the graph on
    split_by = "interaction"
    split_list = list()
    for u, v, d in G.edges(data=True):
        split_list.append(d[split_by])
    split_set = set(split_list)
    G_splits = dict()
    for split in split_set:
        G_split = nx.from_edgelist(
            [(u, v, d) for u, v, d in G.edges(data=True) if d[split_by] == split],
            create_using=nx.MultiDiGraph())

        G_split.add_nodes_from(G.nodes(data=True))
        G_splits[split] = G_split

    return G_splits