def test_mnmf():
    """
    Test the MNMF procedure.
    """
    graph = nx.newman_watts_strogatz_graph(100, 5, 0.3)

    model = MNMF()

    model.fit(graph)
    memberships = model.get_memberships()
    
    indices = [k for k, v in memberships.items()].sort()
    nodes = [node for node in graph.nodes()].sort()

    assert graph.number_of_nodes() == len(memberships)
    assert indices == nodes
    assert type(memberships) == dict

    embedding = model.get_embedding()

    assert embedding.shape[0] == graph.number_of_nodes()
    assert embedding.shape[1] == model.dimensions
    assert type(embedding) == np.ndarray

    centers = model.get_cluster_centers()

    assert centers.shape[0] == model.clusters
    assert centers.shape[1] == model.dimensions
    assert type(centers) == np.ndarray

    graph = nx.newman_watts_strogatz_graph(200, 5, 0.3)

    model = MNMF(dimensions=8)

    model.fit(graph)
    memberships = model.get_memberships()
    
    indices = [k for k, v in memberships.items()].sort()
    nodes = [node for node in graph.nodes()].sort()

    assert graph.number_of_nodes() == len(memberships)
    assert indices == nodes
    assert type(memberships) == dict

    embedding = model.get_embedding()

    assert embedding.shape[0] == graph.number_of_nodes()
    assert embedding.shape[1] == model.dimensions
    assert type(embedding) == np.ndarray

    centers = model.get_cluster_centers()

    assert centers.shape[0] == model.clusters
    assert centers.shape[1] == model.dimensions
    assert type(centers) == np.ndarray
Esempio n. 2
0
def mnmf(population, neighbors, probability):
    g = nx.newman_watts_strogatz_graph(population, neighbors, probability)
    model = MNMF()
    model.fit(g)

    #print(model.get_memberships())
    #print(model.get_embedding())
    #print(model.get_cluster_centers())
    return [
        model.get_memberships(),
        model.get_embedding(),
        model.get_cluster_centers()
    ]