import dit
from collections import Counter

def extract_count(g,layers):
    c = Counter()
    for u,v in g.edges():
        word = ''
        for layer in layers:
            if layer in g[u][v]['multiplex']:
                word += '1'
            else:
                word += '0'
        c[word] += 1

    nnode = g.number_of_nodes()
    nedge = g.number_of_edges()
    if g.is_directed():
        non_edge = nnode*(nnode-1)-nedge
    else:
        non_edge = nnode*(nnode-1)/2-nedge
    word = '0'*len(layers)
    c[word] += non_edge
    return c
        
def mutual_information(g,layers):
    counts = extract_count(g,layers)
    total = float(sum(counts.values()))
    probs = map(lambda x:x/total,counts.values())
    dist = dit.Distribution(counts.keys(),probs)

    if len(layers) == 2:
        return dit.shannon.mutual_information(dist,[0],[1])
    else:
        raise Exception("Not implemented.")

def intersect(mg1,mg2,method="Merge"):
    pass

def union(mg1,mg2,method="Merge"):
    pass

def transfer_entropy(g1,g2,layers=None):
    if not layers:
        layers = set(g1.layers())&set(g2.layers())
    for layer1,layer2 in itertools.combination(layers,2):
        counts = extract_count(g1,g2,layer1,layer2)
@@ -35,16 +35,46 @@
  Counter
+%0Afrom Multinet import Multinet
 %0A%0Adef ex
@@ -955,84 +955,1007 @@
 t(mg
-1,mg2,method=%22Merge%22):%0A    pass%0A%0Adef union(mg1,mg2,method=%22Merge%22):%0A    pass
+s,prefixs):%0A    if len(mgs) != len(prefixs):%0A        raise Exception(%22Length does not match.'%22)%0A    if len(mgs) %3C 1:%0A        return None%0A    g = Multinet()%0A    nodes = set(mgs%5B0%5D.nodes())%0A    for mg in mgs:%0A        nodes &= set(mg.nodes())%0A    g.add_nodes_from(nodes)%0A    %0A    for i,mg in enumerate(mgs):%0A        for u,v in mg.edges():%0A            if u in nodes and v in nodes:%0A                for layer in mg%5Bu%5D%5Bv%5D%5B'multiplex'%5D:%0A                    g.add_edge(u,v,prefixs%5Bi%5D+layer,mg%5Bu%5D%5Bv%5D%5B'multiplex'%5D%5Blayer%5D)%0A    return g%0A%0Adef union(mgs,prefixs):%0A    if len(mgs) != len(prefixs):%0A        raise Exception(%22Length does not match.'%22)%0A    if len(mgs) %3C 1:%0A        return None%0A    g = Multinet()%0A    nodes = set()%0A    for mg in mgs:%0A        nodes %7C= set(mg.nodes())%0A    g.add_nodes_from(nodes)%0A    %0A    for i,mg in enumerate(mgs):%0A        for u,v in mg.edges():%0A            for layer in mg%5Bu%5D%5Bv%5D%5B'multiplex'%5D:%0A                g.add_edge(u,v,prefixs%5Bi%5D+layer,mg%5Bu%5D%5Bv%5D%5B'multiplex'%5D%5Blayer%5D)%0A    return g%0A
 %0A%0Ade

Add helpers