def __considerMovementOld__(G, currentSolution, node):
    # This function will give 4 scores indicating the relative score of each of
    # the groups. Note, that this is not necessarily the actual likelihood, but
    # we guarentee that the differences are preserved.
    oldGroup = currentSolution[node]
    result = []
    for i in range(4):
        # print(i)
        currentSolution[node] = i
        likelihoodTest = ma.__likelihood__(G, currentSolution)
        result.append(likelihoodTest)
    currentSolution[node] = oldGroup
    return result
def test_likelihood_certain():
    cs = [[0, 1, 0, 0], [0, 1, 0, 0], [0, 1, 1, 1], [0, 0, 0, 0]]
    randomSeeds = []
    randomSeeds.append([3, 24])
    randomSeeds.append([17, 27])
    randomSeeds.append([65, 79])
    randomSeeds.append([39, 53])
    randomSeeds.append([92, 44])
    randomSeeds.append([20, 13])
    randomSeeds.append([61, 45])
    randomSeeds.append([38, 29])
    randomSeeds.append([21, 46])
    randomSeeds.append([9, 5])
    randomSeeds.append([24, 42])
    randomSeeds.append([28, 66])
    randomSeeds.append([48, 37])
    randomSeeds.append([17, 74])
    randomSeeds.append([74, 95])
    randomSeeds.append([69, 1])
    randomSeeds.append([21, 41])
    randomSeeds.append([36, 80])
    randomSeeds.append([48, 52])
    randomSeeds.append([30, 99])
    randomSeeds.append([30, 11])
    randomSeeds.append([44, 62])
    randomSeeds.append([81, 60])
    randomSeeds.append([58, 17])
    randomSeeds.append([42, 86])
    randomSeeds.append([9, 27])
    randomSeeds.append([25, 73])
    randomSeeds.append([69, 88])
    randomSeeds.append([86, 78])
    randomSeeds.append([6, 18])
    for seeds in randomSeeds:
        np.random.seed(seeds[0])
        rd.seed(seeds[1])
        for n in [100, 300, 600]:
            G = nx.DiGraph()
            G.add_nodes_from(range(n))
            coms = [rd.randint(0, 3) for i in range(n)]
            for i in range(n):
                for j in range(n):
                    if cs[coms[i]][coms[j]]:
                        G.add_edge(i, j)
            l1 = ma.__likelihood__(G, coms)
            assert (l1 == 0)
def test_likelihood_certainNegative():
    # This tests if the reversal works
    cs = [[0, 1, 0, 0], [0, 1, 0, 0], [0, 1, 1, 1], [0, 0, 0, 0]]
    randomSeeds = []
    randomSeeds.append([23, 13])
    randomSeeds.append([35, 52])
    randomSeeds.append([79, 87])
    randomSeeds.append([50, 29])
    randomSeeds.append([63, 73])
    randomSeeds.append([100, 10])
    randomSeeds.append([55, 22])
    randomSeeds.append([25, 10])
    randomSeeds.append([60, 31])
    randomSeeds.append([14, 5])
    randomSeeds.append([69, 59])
    randomSeeds.append([78, 34])
    randomSeeds.append([90, 86])
    randomSeeds.append([50, 90])
    randomSeeds.append([65, 21])
    randomSeeds.append([51, 12])
    randomSeeds.append([36, 5])
    randomSeeds.append([90, 2])
    randomSeeds.append([35, 86])
    randomSeeds.append([86, 65])
    randomSeeds.append([85, 47])
    randomSeeds.append([34, 95])
    randomSeeds.append([95, 57])
    randomSeeds.append([85, 29])
    randomSeeds.append([70, 56])
    for seeds in randomSeeds:
        np.random.seed(seeds[0])
        rd.seed(seeds[1])
        for n in [200, 400, 500]:
            G = nx.DiGraph()
            G.add_nodes_from(range(n))
            coms = [rd.randint(0, 3) for i in range(n)]
            for i in range(n):
                for j in range(n):
                    if not cs[coms[i]][coms[j]]:
                        G.add_edge(i, j)
            l1 = ma.__likelihood__(G, coms)
            assert (l1 == 0)
def test_likelihoodMaximisationHelper():
    # lets see if it moves an obviously out of place node
    randomSeeds = []
    randomSeeds.append([7689, 20976])
    randomSeeds.append([15496, 4486])
    randomSeeds.append([13618, 9202])
    randomSeeds.append([29661, 10301])
    randomSeeds.append([26463, 11866])
    randomSeeds.append([28511, 28487])
    randomSeeds.append([14730, 4889])
    randomSeeds.append([25214, 6170])
    randomSeeds.append([12996, 20817])
    randomSeeds.append([28007, 1731])
    randomSeeds.append([10107, 16513])
    randomSeeds.append([5278, 9295])
    randomSeeds.append([4990, 1071])
    randomSeeds.append([15233, 7555])
    randomSeeds.append([21541, 17502])
    randomSeeds.append([28841, 11279])
    randomSeeds.append([4321, 9281])
    randomSeeds.append([29605, 19962])
    randomSeeds.append([20434, 20193])
    randomSeeds.append([23008, 31084])
    randomSeeds.append([8756, 15553])
    randomSeeds.append([10718, 14326])
    randomSeeds.append([23555, 3397])
    randomSeeds.append([4450, 26465])
    randomSeeds.append([6891, 20693])
    likeMaxHelper = ma.__likelihoodMaximisationHelper__
    for seeds in randomSeeds:
        np.random.seed(seeds[0])
        rd.seed(seeds[1])
        for n in [40, 100, 120]:
            G = nx.DiGraph()
            G.add_nodes_from(range(n))
            coms = [rd.randint(0, 3) for i in range(n)]
            savedVars = ma.__constructSavedVars__(G, coms)
            cs = savedVars['cs']
            for i in range(n):
                for j in range(n):
                    if cs[coms[i]][coms[j]]:
                        G.add_edge(i, j)
            savedVars = ma.__constructSavedVars__(G, coms)
            for i in range(10):
                n1 = rd.randint(0, n - 1)
                coms1 = coms[:]
                coms1[n1] = (coms[n1] + 1) % 4
                savedVars = ma.__constructSavedVars__(G, coms)
                l1 = ma.__likelihood__(G, coms1)
                maxSol, curMax, chge = likeMaxHelper(G, coms1, l1, [
                    n1,
                ])
                assert (chge)
                assert (abs(curMax) < 10**(-10))
                for i123 in range(n):
                    assert (coms[i123] == maxSol[i123])
            for i in range(3):
                coms1 = coms[:]
                for j in range(i + 1):
                    n1 = rd.randint(0, n - 1)
                    coms1[n1] = (coms[n1] + 1) % 4
                savedVars = ma.__constructSavedVars__(G, coms)
                l1 = ma.__likelihood__(G, coms1)
                order = list(set(range(n)))
                rd.shuffle(order)
                maxSol, curMax, chge = likeMaxHelper(G, coms1, l1, order)
                assert (chge)
                assert (abs(curMax) < 10**(-10))
                for i123 in range(n):
                    assert (coms[i123] == maxSol[i123])
def test_likelihood():
    randomSeeds = []
    randomSeeds.append([44, 17])
    randomSeeds.append([40, 81])
    randomSeeds.append([96, 34])
    randomSeeds.append([25, 17])
    randomSeeds.append([63, 16])
    randomSeeds.append([56, 100])
    randomSeeds.append([56, 36])
    randomSeeds.append([21, 35])
    randomSeeds.append([15, 99])
    randomSeeds.append([39, 21])
    randomSeeds.append([90, 20])
    randomSeeds.append([22, 72])
    randomSeeds.append([63, 95])
    randomSeeds.append([81, 98])
    randomSeeds.append([97, 14])
    randomSeeds.append([90, 71])
    randomSeeds.append([43, 92])
    randomSeeds.append([72, 82])
    randomSeeds.append([46, 42])
    randomSeeds.append([21, 52])
    randomSeeds.append([63, 44])
    randomSeeds.append([81, 74])
    randomSeeds.append([2, 89])
    randomSeeds.append([61, 46])
    randomSeeds.append([50, 88])
    randomSeeds.append([6, 60])
    randomSeeds.append([23, 62])
    randomSeeds.append([75, 92])
    randomSeeds.append([76, 57])
    randomSeeds.append([70, 25])
    randomSeeds.append([85, 12])
    randomSeeds.append([29, 61])
    randomSeeds.append([83, 87])
    randomSeeds.append([16, 11])
    randomSeeds.append([36, 82])
    randomSeeds.append([23, 44])
    randomSeeds.append([26, 42])
    randomSeeds.append([60, 34])
    randomSeeds.append([39, 1])
    randomSeeds.append([32, 6])
    randomSeeds.append([79, 44])
    randomSeeds.append([52, 46])
    randomSeeds.append([4, 83])
    randomSeeds.append([86, 62])
    randomSeeds.append([10, 52])
    randomSeeds.append([45, 98])
    randomSeeds.append([27, 2])
    randomSeeds.append([81, 86])
    randomSeeds.append([17, 76])
    randomSeeds.append([67, 15])
    cs = [[0, 1, 0, 0], [0, 1, 0, 0], [0, 1, 1, 1], [0, 0, 0, 0]]
    for seeds in randomSeeds:
        np.random.seed(seeds[0])
        rd.seed(seeds[1])
        for n in [200, 300, 400]:
            coms = [rd.randint(0, 3) for i in range(n)]
            G = nx.erdos_renyi_graph(n, 0.1, directed=True)
            ein = 0
            eout = 0
            Tin = 0
            Tout = 0
            for i in range(n):
                for j in range(n):
                    if cs[coms[i]][coms[j]]:
                        Tin += 1
                    else:
                        Tout += 1
                    if G.has_edge(i, j):
                        if cs[coms[i]][coms[j]]:
                            ein += 1
                        else:
                            eout += 1
            p = ein / Tin
            q = eout / Tout
            l1 = 0
            l1 += log(p) * ein
            l1 += log(1 - p) * (Tin - ein)
            l1 += log(q) * eout
            l1 += log(1 - q) * (Tout - eout)
            assert (abs(l1 - ma.__likelihood__(G, coms)) < 10**(-8))