Exemplo n.º 1
0
def ClusteringCoefNodesDontExist_test():
    G = Graph()
    G.add_nodes_from(Friends)
    G.add_edges_from(Relationships)
    obs = G.clustering_coef(['Lexa', 'Roy', 'Demmi'])
    exp = {}
    assert_equal(exp, obs)
Exemplo n.º 2
0
def emptyNode_test():
    Friends = []
    G = Graph()
    G.add_nodes_from(Friends)
    obs = G.G
    exp = {}
    assert_equal(exp, obs)
Exemplo n.º 3
0
def listOList_test():
    G = Graph()
    G.add_nodes_from([[
        'A',
    ], ['B']])
    obs = G.G.keys()
    exp = {'A', 'B'}
    assert_equal(exp, obs)
Exemplo n.º 4
0
def clustCoefempty_test():
    G = Graph()
    G.add_nodes_from(Friends)
    G.add_edges_from(Relationships)
    # give an empty list
    obs = G.clustering_coef(node_list=[])
    exp = {}
    assert_equal(exp, obs)
Exemplo n.º 5
0
def degreeNodeList():
    G = Graph()
    G.add_nodes_from(Friends)
    G.add_edges_from(Relationships)
    # give and empty list
    obs = G.degree(node_L=[])
    exp = False
    assert_equal(exp, obs)
Exemplo n.º 6
0
def emptyEdges_test():
    G = Graph()
    G.add_nodes_from(Friends)
    G.add_edges_from(Relationships)
    # test "edges" when given a string and not a list of strings
    obs = G.edges('Alex')
    exp = [['Alex', 'Julie'], ['Alex', 'Michael'], ['Alex', 'Ved']]
    assert_equal(exp, obs)
Exemplo n.º 7
0
def hasNodeEmpty_test():
    G = Graph()
    G.add_nodes_from(Friends)
    G.add_edges_from(Relationships)
    # given an empty list to "has_node" should return empty list
    exp = False
    obs = G.has_node([])
    assert_equal(exp, obs)
Exemplo n.º 8
0
def relabelNodesUneven_test():
    G = Graph()
    G.add_nodes_from(Friends)
    G.add_edges_from(Relationships)
    Notsame = ['A', 'B', 'C', 'D', 'E', 'F']
    obs = relabel_nodes(G, Notsame)
    exp = False
    # get standard out
    assert_equal(exp, obs)
Exemplo n.º 9
0
def numNode_test():
    # create a small graph
    G = Graph()
    G.add_nodes_from(Friends)
    G.add_edges_from(Relationships)

    exp = {'Alex', 'Julie', 'Rob', 'Ved', 'Michael', 'Mark', 'Jeff'}
    obs = G.nodes()
    assert_equal(exp, obs)
Exemplo n.º 10
0
def addNoEdge_test():
    G = Graph()
    G.add_nodes_from(Friends)
    G.add_edges_from(Relationships)
    exp = G.edges()
    # remove an edge but give an empty list
    G.remove_edge([])
    obs = G.edges()
    assert_equal(exp, obs)
Exemplo n.º 11
0
def removePartEdge_test():
    G = Graph()
    G.add_nodes_from(Friends)
    G.add_edges_from(Relationships)
    exp = G.edges()
    # remove an edge but give a single part of the edge
    G.remove_edge(['Alex'])
    obs = G.edges()
    assert_equal(exp, obs)
Exemplo n.º 12
0
def removeEdgeMorethan2_test():
    G = Graph()
    G.add_nodes_from(Friends)
    G.add_edges_from(Relationships)
    # remove an edge but give three entries
    G.remove_edge(['Alex', 'Julie', 'Michael'])
    exp = 21
    obs = len(G.edges())
    assert_equal(exp, obs)
Exemplo n.º 13
0
def removeNodeSet_test():
    G = Graph()
    G.add_nodes_from(Friends)
    G.add_edges_from(Relationships)
    # try to remove an empty set
    G.remove_node({})
    exp = {'Alex', 'Julie', 'Rob', 'Ved', 'Michael', 'Mark', 'Jeff'}
    obs = G.nodes()
    assert_equal(exp, obs)
Exemplo n.º 14
0
def removeEmptylst_test():
    G = Graph()
    G.add_nodes_from(Friends)
    G.add_edges_from(Relationships)
    # try and remove an empty list
    G.remove_node(node=[])
    exp = {'Alex', 'Julie', 'Rob', 'Ved', 'Michael', 'Mark', 'Jeff'}
    obs = G.nodes()
    assert_equal(exp, obs)
Exemplo n.º 15
0
def removeEmptyStr_test():
    # try to remove a node but user gives an empty string
    G = Graph()
    G.add_nodes_from(Friends)
    G.add_edges_from(Relationships)
    G.remove_node('')
    exp = {'Alex', 'Julie', 'Rob', 'Ved', 'Michael', 'Mark', 'Jeff'}
    obs = G.nodes()
    assert_equal(exp, obs)
Exemplo n.º 16
0
def neighborsNoNode_test():
    G = Graph()
    G.add_nodes_from(Friends)
    G.add_edges_from(Relationships)
    # give empty list
    obs = G.neighbors([])
    # need to figure how to capture standard out
    exp = False
    assert_equal(exp, obs)
Exemplo n.º 17
0
def relabelNodesBlank_test():
    G = Graph()
    G.add_nodes_from(Friends)
    G.add_edges_from(Relationships)
    Blank = ['', '', '', '', '', '', '']
    # give a blank list, a list with stuff, but they are all the same
    # and are empty character strings
    obs = relabel_nodes(G, Blank)
    exp = {'': ['', '', '']}
    assert_equal(exp, obs)
Exemplo n.º 18
0
def Graph_setup():
    Friends = ['Alex', 'Julie', 'Rob', 'Ved', 'Michael', 'Mark', 'Jeff']
    Relationships = [['Alex', 'Julie'], ['Julie', 'Alex'], ['Alex', 'Michael'],
                     ['Michael', 'Alex'], ['Julie', 'Michael'],
                     ['Michael', 'Julie'], ['Mark', 'Michael'],
                     ['Michael', 'Mark'], ['Jeff', 'Mark'], ['Mark', 'Jeff'],
                     ['Ved', 'Michael'], ['Michael', 'Ved'], ['Alex', 'Ved'],
                     ['Ved', 'Alex'], ['Ved', 'Jeff'], ['Jeff', 'Ved'],
                     ['Ved', 'Rob'], ['Rob', 'Ved'], ['Julie', 'Rob'],
                     ['Rob', 'Julie'], ['Rob', 'Jeff'], ['Jeff', 'Rob']]
    G = Graph()
    G.add_nodes_from(Friends)
    G.add_edges_from(Relationships)
Exemplo n.º 19
0
def subgraphNoneNode_test():
    G = Graph()
    G.add_nodes_from(Friends)
    G.add_edges_from(Relationships)
    # A node in the subgraph list does not exist in "Nodes"
    sub_list = ['Alex', 'Michael', 'Julie', 'Matt']
    obs = subgraph(G, sub_list).G
    exp = {
        'Alex': ['Julie', 'Michael'],
        'Julie': ['Alex', 'Michael'],
        'Michael': ['Alex', 'Julie', 'Ved']
    }
    assert_equal(exp, obs)
Exemplo n.º 20
0
    def configuration_model(self):
        """configuration_model()
        Build a configuration model using the degree distribution
        of the current graph."""
        DD = self.degree_distribution()
        # build a second dictionary from DD where each node is a key
        # and the value is the node's degree distribution.
        keys_DD = DD.keys()
        cm_DD = {}
        num_edges = 0
        # loop through all degrees and find all the nodes with that
        # degree
        for nn in keys_DD:
            # grab each node with a particular degree
            nodes = DD[nn]
            # put that node with its degree into a dictionary where
            # the node is the key, and its degree is the value
            # there should only be 1 value for each key
            for d in nodes:
                num_edges = num_edges + nn
                cm_DD[d] = [nn]

        # loops through all the nodes, and randomly
        # connect them with other nodes, creating a
        # new randomly generated graph.
        nodes = list(cm_DD.keys())
        num_nodes = len(nodes)
        edge_listCM = []
        # create a graph and add all nodes
        CMG = Graph()
        CMG.add_nodes_from(nodes)
        choices = list(range(0, num_nodes))
        NN_remain = len(choices)
        while NN_remain > 0:
            #print(choices)
            # randomly choose a "originating node"
            pick_n1 = random.choice(choices)
            node1 = nodes[pick_n1]
            C_index1 = choices.index(pick_n1)
            # randomly choose another node for the
            # "orginating node" to connect to
            # ("connecting node")
            if C_index1 == len(choices) - 1:
                choices2 = choices[0:C_index1]
            elif C_index1 == 0:
                choices2 = choices[1:len(choices) + 1]
            else:
                P1 = choices[0:C_index1]
                P2 = choices[C_index1 + 1:len(choices)]
                choices2 = P1 + P2

            pick_n2 = random.choice(choices2)
            node2 = nodes[pick_n2]
            # create an edge
            edge = [node1, node2]
            rev_edge = [node2, node1]
            # add the node to the graph
            EB1 = CMG.add_edge(edge)
            #EB2 = CMG.add_edge(rev_edge)
            #print(EB1, EB2)

            # if the edge sets did not exist then remove a degree
            # from the two randomly choosen nodes, remove a node from
            # NN_remain if the degree of a node reaches 0. If
            # the edge set already existed then choose another edge set
            if EB1:
                node_val1 = [cm_DD[node1][0] - 1]
                node_val2 = [cm_DD[node2][0] - 1]
                #print(node_val1, node_val2)
                cm_DD[node1] = node_val1
                cm_DD[node2] = node_val2
                if cm_DD[node1][0] == 0:
                    #print(choices)
                    #print("P1", pick_n1)
                    #print(choices)
                    choices.remove(pick_n1)
                if cm_DD[node2][0] == 0:
                    #print(choices2)
                    #print("P2", pick_n2)
                    choices.remove(pick_n2)
                    #print(choices)
            NN_remain = len(choices)
            #print(NN_remain)
            #print(NN_remain, choices)
            #print(cm_DD)
        #num_edges = num_edges/2
        #for e in num_edges:
        #random.random()
        #return(cm_DD)
        return (CMG.G)