Beispiel #1
0
def emptyNode_test():
    Friends = []
    G = Graph()
    G.add_nodes_from(Friends)
    obs = G.G
    exp = {}
    assert_equal(exp, obs)
Beispiel #2
0
def NotGraphDD_test():
    # case where graph is empty
    G_empt = Graph()
    obs = G_empt.degree_distribution()
    exp = {}
    # capture standard out
    assert_equal(exp, obs)
Beispiel #3
0
 def __init__(self):
     """INIT
     1) Inherit the graph class from nets
     2) Create the CBN and NIC data structures.
     """
     Graph.__init__(self)
     self.CBN = {}  # nodes are the key, community the value.
     self.NIC = {}  # communities are the key, nodes the values.
Beispiel #4
0
def listOList_test():
    G = Graph()
    G.add_nodes_from([[
        'A',
    ], ['B']])
    obs = G.G.keys()
    exp = {'A', 'B'}
    assert_equal(exp, obs)
Beispiel #5
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)
Beispiel #6
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)
Beispiel #7
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)
Beispiel #8
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)
Beispiel #9
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)
Beispiel #10
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)
Beispiel #11
0
def info_test():
    # return info of an empty graph
    G = Graph()
    obs = G.info()
    exp = {
        'num_nodes': 0,
        'num_edgeSets': 0,
        'mean_degree': [],
        'degree_std': [],
        'orph_edges': [],
        'orph_nodes': []
    }
    assert_equal(exp, obs)
Beispiel #12
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)
Beispiel #13
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)
Beispiel #14
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)
Beispiel #15
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)
Beispiel #16
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)
Beispiel #17
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)
Beispiel #18
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)
Beispiel #19
0
def writecomms(Graph, filename, delimiter="\t", edge_list=False):
    """This function takes in a graph in 'dictionary form' and 
    writes it to a file of text. There are three values it 
    requires: 1) the Graph() class or network that the user 
    wants written, 2) a file name for the file that will be printed, 
    and 3) the desired delimiter that will seperate values. 
    
    The Graph() lass is specific to this particular module. 
    The actual graph object in the Graph() class must be in 
    dictionary form. Here the nodes are dictionary keys
    and values of keys are the other nodes the key node are linked
    to.

    The default setting will only create a node to community list. if
    edge_list = True then it was print a and edge list as well as the 
    community that first column node belongs to. 
    
    The file name can be made up of any alpha numeric combination.
    However, it must be utf-8 encoding.
    
    The delimiter default is a tab denoted by '\t'. However, 
    the user can specify any utf-8 alpha-numeric character.
    
    This does not return anything, but prints to a file through
    standard out."""
    network_file = open(filename, "w")

    # G has to be a dictionary object
    # loop through each dictionary key
    if edge_list:
        for n in Graph.G:
            # loop through each item in a dictionary
            for ne in Graph.G[n]:
                comm = C.CBN[n][0]
                # write each edge as a seperate line in the output file
                network_file.write(''.join(
                    [n, delimiter, ne, delimiter, comm, '\n']))
                # print to console to make sure things look okay
                print(n, delimiter, ne)
        # close the file
        network_file.close()
    else:
        for n in Graph.nodes():
            # loop through each item in a dictionary
            comm = C.CBN[n][0]
            # write each edge as a seperate line in the output file
            network_file.write(''.join([n, delimiter, comm, '\n']))
            # print to console to make sure things look okay
            print(n, delimiter, ne)
        # close the file
        network_file.close()
Beispiel #20
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)
Beispiel #21
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)
Beispiel #22
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)
Beispiel #23
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)
Beispiel #24
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)
Beispiel #25
0
def moreThanOne_test():
    G = Graph()
    G.add_node(['A', 'B'])
    obs = G.G
    exp = {'A': []}
    assert_equal(exp, obs)
Beispiel #26
0
def emptySetNodes_test():
    G = Graph()
    obs = G.nodes()
    exp = set()
    # check nodes when the graph is empty
    assert_equal(exp, obs)