Example #1
0
def createTemplate():
    import SimpSOM as sps
    path = pathlib.Path().absolute()
    path = str(path) + '\\GE_withstage_20.csv'
    #path = "C:\\PR_Proj_Thesis\\PRAD\\SOM_template\\GE_withstage_20.csv"
    # Read file
    df = pd.read_csv(path)
    # Fill na
    df.fillna(0, inplace=True)
    print("df shape = ", df.shape)
    #    labels_old = df['TUMOR_STAGE'].values
    df.drop(['TUMOR_STAGE', 'PATIENT_ID'], axis=1, inplace=True)
    raw_data = df.values
    #Create transpose ; such that each column is one patient
    raw_data = raw_data.T
    # applying scaling to make values between some range 0-1/-1-2 ,as need for Kohens SOM
    scaler = MinMaxScaler(copy=True, feature_range=(0, 1))
    scaler.fit(raw_data)
    raw_data = scaler.transform(raw_data)

    ht = 10
    wd = 10
    no_of_epocs = 1000

    net = sps.somNet(ht, wd, raw_data, PBC=False)
    net.colorEx = False

    Learning_rate = 0.05
    net.PCI = True  #The weights will be initialised with PCA.
    """ Switch to activate periodic boundary conditions. """
    net.PBC = True
    net.train(Learning_rate, no_of_epocs)
    col_num = raw_data.shape[0]

    node_list = [i for i in range(col_num)]
    new_lbl = [str(j) for j in range(col_num)]
    bmu = net.project(raw_data, labels=new_lbl)
    pos = bmu
    G = nx.chvatal_graph()
    nx.draw_networkx_nodes(G,
                           pos,
                           nodelist=node_list,
                           node_color='w',
                           edgecolors=[0, 0, 0],
                           node_size=500,
                           alpha=0.8)

    new_lbl_dict = dict(enumerate(new_lbl))

    nx.draw_networkx_labels(G, pos, new_lbl_dict, font_size=10)

    plt.axis('on')
    plt.savefig(str(count) + '_Template.png')  #, bbox_inches='tight', dpi=72)
    #    plt.show()
    return pos
Example #2
0
    def test_other_graph(self):
        g = nx.chvatal_graph()
        nx.set_node_attributes(g, 'infected', {n: False for n in g.nodes()})
        g.node[0]['infected'] = True

        g_i = nx.Graph()
        g_i.add_nodes_from([0])

        sz = AlgorithmSZ()
        source_estimation = sz.run(g, g_i)
        print("Source of rumor is %s" % source_estimation)
Example #3
0
    def test_other_graph(self):
        g = nx.chvatal_graph()
        nx.set_node_attributes(g, 'infected', {n: False for n in g.nodes()})
        g.node[0]['infected'] = True

        g_i = nx.Graph()
        g_i.add_nodes_from([0])

        sz = AlgorithmSZ()
        source_estimation = sz.run(g, g_i)
        print("Source of rumor is %s" % source_estimation)
Example #4
0
    def test_other_graph(self):
        g = nx.chvatal_graph()
        nx.set_node_attributes(g, 'infected', {n: False for n in g.nodes()})
        g.node[0]['infected'] = True

        sz = AlgorithmSZ()
        source_estimation = sz.run(g, v=0)
        print("Source of rumor is %s" % source_estimation)

        nx.draw_networkx(g, node_color=['b' if g.node[n]['infected'] else 'r' for n in g])
        plt.show()
def small_graph_tests():
    if not total_coloring_test("Complete graph on 3 vertices",
                               networkx.complete_graph(3), 3):
        return False
    if not total_coloring_test("Cycle of length 5", networkx.cycle_graph(5),
                               4):
        return False
    if not total_coloring_test("Star graph on 5 vertices",
                               networkx.star_graph(4), 5):
        return False
    if not total_coloring_test("Petersen graph", networkx.petersen_graph(), 4):
        return False
    if not total_coloring_test("Chvatal graph", networkx.chvatal_graph(), 5):
        return False
    return True
import networkx as nx
import matplotlib.pylab as plt
from plot_multigraph import plot_multigraph

graphs = [
  ("bull", nx.bull_graph()),
  ("chvatal", nx.chvatal_graph()),
  ("cubical", nx.cubical_graph()),
  ("desargues", nx.desargues_graph()),
  ("diamond", nx.diamond_graph()),
  ("dodecahedral", nx.dodecahedral_graph()),
  ("frucht", nx.frucht_graph()),
  ("heawood", nx.heawood_graph()),
  ("house", nx.house_graph()),
  ("house_x", nx.house_x_graph()),
  ("icosahedral", nx.icosahedral_graph()),
  ("krackhardt_kite", nx.krackhardt_kite_graph()),
  ("moebius_kantor", nx.moebius_kantor_graph()),
  ("octahedral", nx.octahedral_graph()),
  ("pappus", nx.pappus_graph()),
  ("petersen", nx.petersen_graph()),
  ("sedgewick_maze", nx.sedgewick_maze_graph()),
  ("tetrahedral", nx.tetrahedral_graph()),
  ("truncated_cube", nx.truncated_cube_graph()),
  ("truncated_tetrahedron", nx.truncated_tetrahedron_graph()),
]

plot_multigraph(graphs, 4, 5, node_size=50)
plt.savefig('graphs/small.png')

Example #7
0
 def test_chvatal(self):
     print '\nchvatal:',
     self.g = nx.chvatal_graph()
     a = programming_for_tree_decomposition(self.g, True)
     self.t = a.tree_decomposition()
     self.assertTrue(self.__test_all_conditions())
    def test_properties_named_small_graphs(self):
        G = nx.bull_graph()
        assert G.number_of_nodes() == 5
        assert G.number_of_edges() == 5
        assert sorted(d for n, d in G.degree()) == [1, 1, 2, 3, 3]
        assert nx.diameter(G) == 3
        assert nx.radius(G) == 2

        G = nx.chvatal_graph()
        assert G.number_of_nodes() == 12
        assert G.number_of_edges() == 24
        assert list(d for n, d in G.degree()) == 12 * [4]
        assert nx.diameter(G) == 2
        assert nx.radius(G) == 2

        G = nx.cubical_graph()
        assert G.number_of_nodes() == 8
        assert G.number_of_edges() == 12
        assert list(d for n, d in G.degree()) == 8 * [3]
        assert nx.diameter(G) == 3
        assert nx.radius(G) == 3

        G = nx.desargues_graph()
        assert G.number_of_nodes() == 20
        assert G.number_of_edges() == 30
        assert list(d for n, d in G.degree()) == 20 * [3]

        G = nx.diamond_graph()
        assert G.number_of_nodes() == 4
        assert sorted(d for n, d in G.degree()) == [2, 2, 3, 3]
        assert nx.diameter(G) == 2
        assert nx.radius(G) == 1

        G = nx.dodecahedral_graph()
        assert G.number_of_nodes() == 20
        assert G.number_of_edges() == 30
        assert list(d for n, d in G.degree()) == 20 * [3]
        assert nx.diameter(G) == 5
        assert nx.radius(G) == 5

        G = nx.frucht_graph()
        assert G.number_of_nodes() == 12
        assert G.number_of_edges() == 18
        assert list(d for n, d in G.degree()) == 12 * [3]
        assert nx.diameter(G) == 4
        assert nx.radius(G) == 3

        G = nx.heawood_graph()
        assert G.number_of_nodes() == 14
        assert G.number_of_edges() == 21
        assert list(d for n, d in G.degree()) == 14 * [3]
        assert nx.diameter(G) == 3
        assert nx.radius(G) == 3

        G = nx.hoffman_singleton_graph()
        assert G.number_of_nodes() == 50
        assert G.number_of_edges() == 175
        assert list(d for n, d in G.degree()) == 50 * [7]
        assert nx.diameter(G) == 2
        assert nx.radius(G) == 2

        G = nx.house_graph()
        assert G.number_of_nodes() == 5
        assert G.number_of_edges() == 6
        assert sorted(d for n, d in G.degree()) == [2, 2, 2, 3, 3]
        assert nx.diameter(G) == 2
        assert nx.radius(G) == 2

        G = nx.house_x_graph()
        assert G.number_of_nodes() == 5
        assert G.number_of_edges() == 8
        assert sorted(d for n, d in G.degree()) == [2, 3, 3, 4, 4]
        assert nx.diameter(G) == 2
        assert nx.radius(G) == 1

        G = nx.icosahedral_graph()
        assert G.number_of_nodes() == 12
        assert G.number_of_edges() == 30
        assert (list(
            d for n, d in G.degree()) == [5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5])
        assert nx.diameter(G) == 3
        assert nx.radius(G) == 3

        G = nx.krackhardt_kite_graph()
        assert G.number_of_nodes() == 10
        assert G.number_of_edges() == 18
        assert (sorted(
            d for n, d in G.degree()) == [1, 2, 3, 3, 3, 4, 4, 5, 5, 6])

        G = nx.moebius_kantor_graph()
        assert G.number_of_nodes() == 16
        assert G.number_of_edges() == 24
        assert list(d for n, d in G.degree()) == 16 * [3]
        assert nx.diameter(G) == 4

        G = nx.octahedral_graph()
        assert G.number_of_nodes() == 6
        assert G.number_of_edges() == 12
        assert list(d for n, d in G.degree()) == 6 * [4]
        assert nx.diameter(G) == 2
        assert nx.radius(G) == 2

        G = nx.pappus_graph()
        assert G.number_of_nodes() == 18
        assert G.number_of_edges() == 27
        assert list(d for n, d in G.degree()) == 18 * [3]
        assert nx.diameter(G) == 4

        G = nx.petersen_graph()
        assert G.number_of_nodes() == 10
        assert G.number_of_edges() == 15
        assert list(d for n, d in G.degree()) == 10 * [3]
        assert nx.diameter(G) == 2
        assert nx.radius(G) == 2

        G = nx.sedgewick_maze_graph()
        assert G.number_of_nodes() == 8
        assert G.number_of_edges() == 10
        assert sorted(d for n, d in G.degree()) == [1, 2, 2, 2, 3, 3, 3, 4]

        G = nx.tetrahedral_graph()
        assert G.number_of_nodes() == 4
        assert G.number_of_edges() == 6
        assert list(d for n, d in G.degree()) == [3, 3, 3, 3]
        assert nx.diameter(G) == 1
        assert nx.radius(G) == 1

        G = nx.truncated_cube_graph()
        assert G.number_of_nodes() == 24
        assert G.number_of_edges() == 36
        assert list(d for n, d in G.degree()) == 24 * [3]

        G = nx.truncated_tetrahedron_graph()
        assert G.number_of_nodes() == 12
        assert G.number_of_edges() == 18
        assert list(d for n, d in G.degree()) == 12 * [3]

        G = nx.tutte_graph()
        assert G.number_of_nodes() == 46
        assert G.number_of_edges() == 69
        assert list(d for n, d in G.degree()) == 46 * [3]

        # Test create_using with directed or multigraphs on small graphs
        pytest.raises(nx.NetworkXError,
                      nx.tutte_graph,
                      create_using=nx.DiGraph)
        MG = nx.tutte_graph(create_using=nx.MultiGraph)
        assert sorted(MG.edges()) == sorted(G.edges())
Example #9
0
import pytest
import os
from allegedb import ORM
import networkx as nx


scalefreestart = nx.MultiDiGraph(name='scale_free_graph_5')
scalefreestart.add_edges_from([(0, 1), (1, 2), (2, 0)])


testgraphs = [
    nx.chvatal_graph(),
    nx.scale_free_graph(5, create_using=scalefreestart),
    # nx.chordal_cycle_graph(5, create_using=nx.MultiGraph(name='chordal_cycle_graph_5')),
    # The standard networkx edges iterator decides to flip some edges about in arbitrary-seeming
    # ways that I haven't been able to replicate and it doesn't seem worth it.
]
# have to name it after creation because it clears the create_using
path_graph_9 = nx.path_graph(9)
path_graph_9.name = 'path_graph_9'
testgraphs.append(path_graph_9)


@pytest.fixture
def db():
    name = 'allegedb_load_test.db'
    if os.path.exists(name):
        os.remove(name)
    with ORM('sqlite:///' + name) as orm:
        for graph in testgraphs:
            {
import networkx as nx
import matplotlib.pylab as plt
from plot_multigraph import plot_multigraph

graphs = [
    ("bull", nx.bull_graph()),
    ("chvatal", nx.chvatal_graph()),
    ("cubical", nx.cubical_graph()),
    ("desargues", nx.desargues_graph()),
    ("diamond", nx.diamond_graph()),
    ("dodecahedral", nx.dodecahedral_graph()),
    ("frucht", nx.frucht_graph()),
    ("heawood", nx.heawood_graph()),
    ("house", nx.house_graph()),
    ("house_x", nx.house_x_graph()),
    ("icosahedral", nx.icosahedral_graph()),
    ("krackhardt_kite", nx.krackhardt_kite_graph()),
    ("moebius_kantor", nx.moebius_kantor_graph()),
    ("octahedral", nx.octahedral_graph()),
    ("pappus", nx.pappus_graph()),
    ("petersen", nx.petersen_graph()),
    ("sedgewick_maze", nx.sedgewick_maze_graph()),
    ("tetrahedral", nx.tetrahedral_graph()),
    ("truncated_cube", nx.truncated_cube_graph()),
    ("truncated_tetrahedron", nx.truncated_tetrahedron_graph()),
]

plot_multigraph(graphs, 4, 5, node_size=50)
plt.savefig('graphs/small.png')
                    vertices = int(words[2])
                    graph.add_nodes_from(range(vertices))
                if words[0] == 'e':
                    graph.add_edge(int(words[1]) - 1, int(words[2]) - 1)
        return graph
                
    def test(nx_graph, k, draw=False):
        graph = nx.to_numpy_matrix(nx_graph).astype(int).tolist()
        coloring = tabucol(graph, k, debug=True)
        if draw:
            values = [coloring[node] for node in nx_graph]
            nx.draw(nx_graph, node_color=values, pos=nx.shell_layout(nx_graph))
            plt.show()
except ImportError:
    print("Need networkx and matplotlib installed for testing.")

if __name__ == "__main__":
    graph = [[0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0],
             [1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0],
             [0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0],
             [0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 0],
             [1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0],
             [0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1],
             [1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1],
             [0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1],
             [0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0],
             [1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1],
             [0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0],
             [0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0]]
    test(nx.chvatal_graph(), 4, True)
Example #12
0
    return mcl


def clean_attributes(g):
    for v in g.nodes():
        if 'visited' in g.node[v]:
            del g.node[v]['visited']
    for u, v in g.edges():
        if 'visited' in g[u][v]:
            del g[u][v]['visited']


if __name__ == '__main__':
    targets = {
        'bull': nx.bull_graph(),  # 1-connected planar
        'chvatal': nx.chvatal_graph(),  # 4-connected non-planar
        'cubical': nx.cubical_graph(),  # 3-connected planar
        'desargues': nx.desargues_graph(),  # 3-connected non-planar
        'diamond': nx.diamond_graph(),  # 2-connected planar
        'dodecahedral': nx.dodecahedral_graph(),  # 3-connected planar
        'frucht': nx.frucht_graph(),  # 3-connected planar
        'heawood': nx.heawood_graph(),  # 3-connected non-planar
        'house': nx.house_graph(),  # 2-connected planar
        'house_x': nx.house_x_graph(),  # 2-connected planar
        'icosahedral': nx.icosahedral_graph(),  # 5-connected planar
        'krackhardt': nx.krackhardt_kite_graph(),  # 1-connected planar
        'moebius': nx.moebius_kantor_graph(),  # non-planar
        'octahedral': nx.octahedral_graph(),  # 4-connected planar
        'pappus': nx.pappus_graph(),  # 3-connected non-planar
        'petersen': nx.petersen_graph(),  # 3-connected non-planar
        'sedgewick': nx.sedgewick_maze_graph(),  # 1-connected planar
Example #13
0
def create_RGB_Images(pos, classlbl):
    gene_list_top14 = [
        'SPOP', 'FOXA1', 'CTNNB1', 'CLPTM1L', 'DPYSL2', 'NEIL1', 'PITPNM2',
        'ATM', 'EMG1', 'ETV3', 'BRAF', 'NKX3-1', 'ZMYM3', 'SALL1'
    ]
    gene_list = gene_list_top14
    path = pathlib.Path().absolute()
    pathGE = str(path) + '\\Mergefile_top20.csv'
    # Read file
    df = pd.read_csv(pathGE)
    df = df.loc[df['TUMOR_STAGE'] == classlbl]
    df.drop(['TUMOR_STAGE', 'PATIENT_ID'], axis=1, inplace=True)
    col_num = len(gene_list)  #14
    #Create node list of numbers then create lables and dict
    node_list = [i for i in range(col_num)]
    new_lbl = [str(j) for j in range(col_num)]
    new_lbl_dict = dict(enumerate(new_lbl))
    # loop through all rows and get RGB values for each patient
    row_count = df.shape[0]

    df.reset_index(inplace=True)
    for i in range(row_count):  #loop through all rows
        nodecolor = []
        for j in range(
                len(gene_list)):  # loop through all nodes (i.e 14 nodes)

            r_prefix = 'GE_' + gene_list[j]
            g_prefix = 'DM_' + gene_list[j]
            b_prefix = 'CNA_' + gene_list[j]

            #Check if tht column exixts

            R = round(df[r_prefix][i], 5) if (r_prefix in df.columns) else 0.0
            G = round(df[g_prefix][i], 5) if (g_prefix in df.columns) else 0.0
            B = round(df[b_prefix][i], 5) if (b_prefix in df.columns) else 0.0

            nodecolor.append([R, G, B])

#        print("node_color size = " ,len(nodecolor))
        G = nx.chvatal_graph()
        try:
            nx.draw_networkx_nodes(G,
                                   pos,
                                   nodelist=node_list,
                                   edgecolors=[0, 0, 0],
                                   node_color=nodecolor,
                                   node_size=500,
                                   alpha=0.8)

            nx.draw_networkx_labels(G, pos, new_lbl_dict, font_size=10)

            plt.axis('on')
            filename = str(i) + '_Template.png'
            path = "./training/" + str(classlbl)
            plt.savefig(os.path.join(path, filename))
        except:
            print("row = ", i, " node_color = ", nodecolor)


#        print(("\r Preparing Images... "+str(int(i*100.0/row_count))+"%" ), end=' ')
    print("\r Done Preparing Training Images ....for :", classlbl)
    return
Example #14
0
import pytest
import os
from allegedb import ORM
import networkx as nx

scalefreestart = nx.MultiDiGraph(name='scale_free_graph_5')
scalefreestart.add_edges_from([(0, 1), (1, 2), (2, 0)])

testgraphs = [
    nx.chvatal_graph(),
    nx.scale_free_graph(5, create_using=scalefreestart),
    # nx.chordal_cycle_graph(5, create_using=nx.MultiGraph(name='chordal_cycle_graph_5')),
    # The standard networkx edges iterator decides to flip some edges about in arbitrary-seeming
    # ways that I haven't been able to replicate and it doesn't seem worth it.
]
# have to name it after creation because it clears the create_using
path_graph_9 = nx.path_graph(9)
path_graph_9.name = 'path_graph_9'
testgraphs.append(path_graph_9)


@pytest.fixture
def db():
    name = 'allegedb_load_test.db'
    if os.path.exists(name):
        os.remove(name)
    with ORM('sqlite:///' + name) as orm:
        for graph in testgraphs:
            {
                nx.Graph: orm.new_graph,
                nx.DiGraph: orm.new_digraph,
 def test_chvatal(self):
     expected = False
     actual = is_planar(nx.chvatal_graph())
     self.assertEqual(expected, actual)
Example #16
0
            r_prefix = 'GE_' + gene_list[j]
            g_prefix = 'DM_' + gene_list[j]
            b_prefix = 'CNA_' + gene_list[j]

            #Check if tht column exixts

            R = round(df1[r_prefix][i], 5) if (r_prefix
                                               in df1.columns) else 0.0
            G = round(df1[g_prefix][i], 5) if (g_prefix
                                               in df1.columns) else 0.0
            B = round(df1[b_prefix][i], 5) if (b_prefix
                                               in df1.columns) else 0.0

            nodecolor.append([R, G, B])

        G = nx.chvatal_graph()
        try:
            nx.draw_networkx_nodes(G,
                                   pos,
                                   nodelist=node_list,
                                   edgecolors=[0, 0, 0],
                                   node_color=nodecolor,
                                   node_size=500,
                                   alpha=0.8)

            nx.draw_networkx_labels(G, pos, new_lbl_dict, font_size=10)

            plt.axis('on')
            filename = str(i) + '_Template.png'
            path = "./training/3A"
            plt.savefig(os.path.join(
Example #17
0
def small_graphs():
    print("Make small graph")
    G = nx.make_small_graph(
        ["adjacencylist", "C_4", 4, [[2, 4], [1, 3], [2, 4], [1, 3]]])
    draw_graph(G)
    G = nx.make_small_graph(
        ["adjacencylist", "C_4", 4, [[2, 4], [3], [4], []]])
    draw_graph(G)
    G = nx.make_small_graph(
        ["edgelist", "C_4", 4, [[1, 2], [3, 4], [2, 3], [4, 1]]])
    draw_graph(G)
    print("LCF graph")
    G = nx.LCF_graph(6, [3, -3], 3)
    draw_graph(G)
    G = nx.LCF_graph(14, [5, -5], 7)
    draw_graph(G)
    print("Bull graph")
    G = nx.bull_graph()
    draw_graph(G)
    print("Chvátal graph")
    G = nx.chvatal_graph()
    draw_graph(G)
    print("Cubical graph")
    G = nx.cubical_graph()
    draw_graph(G)
    print("Desargues graph")
    G = nx.desargues_graph()
    draw_graph(G)
    print("Diamond graph")
    G = nx.diamond_graph()
    draw_graph(G)
    print("Dodechaedral graph")
    G = nx.dodecahedral_graph()
    draw_graph(G)
    print("Frucht graph")
    G = nx.frucht_graph()
    draw_graph(G)
    print("Heawood graph")
    G = nx.heawood_graph()
    draw_graph(G)
    print("House graph")
    G = nx.house_graph()
    draw_graph(G)
    print("House X graph")
    G = nx.house_x_graph()
    draw_graph(G)
    print("Icosahedral graph")
    G = nx.icosahedral_graph()
    draw_graph(G)
    print("Krackhardt kite graph")
    G = nx.krackhardt_kite_graph()
    draw_graph(G)
    print("Moebius kantor graph")
    G = nx.moebius_kantor_graph()
    draw_graph(G)
    print("Octahedral graph")
    G = nx.octahedral_graph()
    draw_graph(G)
    print("Pappus graph")
    G = nx.pappus_graph()
    draw_graph(G)
    print("Petersen graph")
    G = nx.petersen_graph()
    draw_graph(G)
    print("Sedgewick maze graph")
    G = nx.sedgewick_maze_graph()
    draw_graph(G)
    print("Tetrahedral graph")
    G = nx.tetrahedral_graph()
    draw_graph(G)
    print("Truncated cube graph")
    G = nx.truncated_cube_graph()
    draw_graph(G)
    print("Truncated tetrahedron graph")
    G = nx.truncated_tetrahedron_graph()
    draw_graph(G)
    print("Tutte graph")
    G = nx.tutte_graph()
    draw_graph(G)