Example #1
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)
Example #2
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)
Example #3
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)
Example #4
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)
Example #5
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()
Example #6
0
def emptySetNodes_test():
    G = Graph()
    obs = G.nodes()
    exp = set()
    # check nodes when the graph is empty
    assert_equal(exp, obs)