コード例 #1
0
def main():

    data = load_test_data("../../data/testDefinitions.sqlite")
    lengths = []
    g = gt.Graph(directed=False)
    vertices = {}

    for test in data.tests.values():
        vertices[test.id] = g.add_vertex()

    for equipment in data.test_equipment.values():
        all_items = []
        for test in data.tests.values():
            if equipment in test.testEquipment:
                all_items.append(test)
        if len(all_items) > 0:
            lengths.append(len(all_items))
        if len(all_items) > 1:

            for i in range(0, len(all_items)-1):
                for j in range(i, len(all_items)-1):
                    if i != j:
                        g.add_edge(vertices[all_items[i].id], vertices[all_items[j].id])

    pos = gt.arf_layout(g, max_iter=4)
    deg = g.degree_property_map("total")
    gt.graph_draw(g, pos=pos,  vertex_fill_color=deg, vorder=deg, vertex_text=deg, output_size=(800, 600), output="test-testEquipment.png")
    plt.hist(lengths)
    plt.title("Equipment-Test Histogram")
    plt.xlabel("Equipment required by X tests.")
    plt.ylabel("Frequency")
    plt.show()
def label_graph(instance, i, use_simplicial):
    edges = np.genfromtxt("res/new_synthetic/" + instance + ".csv",
                          delimiter=",",
                          dtype=np.int)[:, :2]
    n = np.max(edges - 1)
    g = gt.Graph(directed=False)
    g.add_vertex(n)
    g.add_edge_list(edges)

    #print(edges)

    #print(np.sort(g.degree_property_map("total").a))

    A, B = florians_procedure(g, use_simplicial)

    pos = np.where(A)[0]
    pos = g.new_vertex_property("bool", vals=A)

    simplicial_string = "_simplicial_start"
    if not use_simplicial:
        simplicial_string = ""
    file = open(
        "res/new_synthetic/labels/" + instance + "_" + str(i) +
        simplicial_string + "_positive.csv", 'w')
    writer = csv.writer(file)
    writer.writerows(np.where(A)[0].reshape((-1, 1)))
    #print(len(np.where(A)[0])/n)
    gt.graph_draw(g,
                  pos=gt.arf_layout(g, max_iter=0),
                  vertex_fill_color=pos,
                  output="res/new_synthetic/images/" + instance + "_" +
                  str(i) + "_" + simplicial_string + ".svg")
コード例 #3
0
ファイル: import.py プロジェクト: galaunay/biblio-network
 def make_article_graph(self, layout="arf"):
     """Make an article graph"""
     self.graph = Graph(directed=False)
     # add vertex
     self.graph.add_vertex(len(self.db))
     # add properties
     cb = self.graph.new_vertex_property("int", self.db['Cited by'].values)
     self.graph.vertex_properties['nmb_citation'] = cb
     # Add links
     auths = list(self.author_betweeness.keys())
     auth2ind = {auths[i]: i
                 for i in range(len(auths))}
     auth2pub = self._get_author_publication()
     for _, pubs in auth2pub.items():
         if len(pubs) < 2:
             continue
         combis = itertools.combinations(pubs, 2)
         self.graph.add_edge_list(list(combis))
     # layout
     if layout == "arf":
         self.layout_pos = arf_layout(self.graph)
     elif layout == "sfpd":
         self.layout_pos = sfdp_layout(self.graph)
     elif layout == "fr":
         self.layout_pos = fruchterman_reingold_layout(self.graph)
     elif layout == "radial":
         self.layout_pos = radial_tree_layout(self.graph,
                                              auth2ind['Logan, B.E.'])
     else:
         raise ValueError()
コード例 #4
0
def influencer_network_anlaysis(year=2020):
    path_2016 = '/home/crossb/packaged_ci/graphs/2016/'
    path_2020 = '/home/crossb/packaged_ci/graphs/2020/'
    top_n_influencers = 30
    biased_graphs = load_graphs_gt(path_2020)
    biased_influencers = top_influencers(top_n_influencers, biased_graphs)
    influencer_network = top_influencer_network(biased_graphs, biased_influencers)
    #influencer_network.save(os.path.join(WORKING_DIR, 'influencer_network.gt'))
    stats = network_characteristics_gt(influencer_network)
    print("Influencer network stats")
    for stat, value in stats.items():
        print("{}: {}".format(stat, value))

    most_infl_influencers = top_influencers(10, {'top': influencer_network})
    print("Most influential:", most_infl_influencers)


    # save influencer network
    gt.save('data/2020/influencer_network.gt')

    # networkit stats
    nk_graph = load_from_graphtool_nk('data/2020/influencer_network.gt')
    characteristics = network_characteristics_nk(nk_graph, 10)

    for stat, value in characteristics.items():
        if "centrality" in stat:
            print("{}: {}".format(
                stat,
                ','.join(['(Node: {}: {})'.format(influencer_network.vp.user_id[n], v) for (n, v) in value])))
        else:
            print("{}: {}".format(stat, value))

    # Draw with the vertices as pie charts
    vprops = {'pie_fractions': influencer_network.vp.pie_fractions,
              'shape': influencer_network.vp.shape,
              'text': influencer_network.vp.text,
              'text_color': influencer_network.vp.text_color,
              'size': influencer_network.vp.size,
              'pie_colors': influencer_network.vp.pie_colors,
              'text_position': 200,
              'font_size': 14,
              'text_offset': [0.0, 1.0]
              }
    eprops = {'color': 'lightgray'}
    # r=1000 leads to cool graph
    #pos = gt.fruchterman_reingold_layout(influencer_network, r=35.0, circular=True, n_iter=2000)
    pos = gt.arf_layout(influencer_network, d=4, max_iter=0)
    gt.graph_draw(influencer_network, pos=pos, vprops=vprops,
                  eprops=eprops, output='top_influencers.svg')
    #with cairo.SVGSurface('top_influencers.svg', 1024, 1280) as surface:
    #    cr = cairo.Context(surface)
    #    gt.cairo_draw(influencer_network, pos, cr, vprops=vprops, eprops=eprops,
    #                  ecmap=(cm.get_cmap(), 0.2),
    #                  output='top_influencers.svg')

    return
コード例 #5
0
    def plot(self):

        layout = arf_layout(self.view)
        # also arf_layout? not sure how easy is to draw a tree with multiple roots
        # if we want to see features there

        self.ax.set_axis_off()

        graph_draw(self.view, pos=layout, mplfig=self.ax)
        logger.debug("Plot done")
        plt.savefig(self._get_plot_file())
コード例 #6
0
ファイル: ttc.py プロジェクト: jcheong0428/py-school-match
    def generate_image(self, cycle_edges, iteration_n=0):
        """Generates an image of a graph.

        :param cycle_edges: Edges which are part of the main cycle (they will be highlighted in red).
        :type cycle_edges: list
        :param iteration_n: Number of iteration of the algorithm (this is added in the filename of the image).
        :type iteration_n: int

        .. DANGER::
        This is an experimental feature.
        """
        edge_color = self.__graph.new_edge_property("vector<float>")
        edge_width = self.__graph.new_edge_property("int")

        for edge in self.__graph.edges():
            if edge in cycle_edges:
                edge_color[edge] = [1., 0.2, 0.2, 0.999]
                edge_width[edge] = 7
            else:
                edge_color[edge] = [0., 0., 0., 0.3]
                edge_width[edge] = 4

        vertex_shape = self.__graph.new_vertex_property("string")
        vertex_size = self.__graph.new_vertex_property("int")

        for vertex in self.__graph.vertices():
            if self.__entity_type[vertex] == "st":
                vertex_shape[vertex] = "circle"
                vertex_size[vertex] = 1
            else:
                vertex_shape[vertex] = "double_circle"
                vertex_size[vertex] = 100

        # pos = sfdp_layout(self.__graph, C=10, p=5, theta=2, gamma=1)
        pos = arf_layout(self.__graph, d=0.2, a=3)
        graph_draw(
            self.__graph,
            pos=pos,
            vertex_text=self.__entity_id,
            vertex_font_size=
            1,  # ToDo: Move image related code outside the class.
            vertex_fill_color=[0.97, 0.97, 0.97, 1],
            vertex_color=[0.05, 0.05, 0.05, 0.95],
            vertex_shape=vertex_shape,
            edge_color=edge_color,
            edge_pen_width=edge_width,
            output_size=(1000, 1000),
            bg_color=[1., 1., 1., 1],
            output=self.generate_filename(iteration_n))
コード例 #7
0
def output_json_fd_lay(u, model):
    if model == "FR":
        pos = gt.fruchterman_reingold_layout(u, n_iter=100, r=100, a=10)
    if model == "Ran":
        pos = gt.random_layout(g, dim=2)
    if model == "ARF":
        pos = gt.arf_layout(g, max_iter=0)

    # gt.sfdp_layout(u, p=2.6,K=40,C=1)
    nodes = []
    convert = []
    for v in u.vertices():
        s_node = {}
        s_node["name"] = v_userid[v]["userid"]
        s_node['gender'] = v_gender[v]["gender"]
        s_node['schregion'] = v_schregion[v]['schregion']
        s_node['school'] = v_school[v]['school']
        s_node['major'] = v_major[v]['major']
        s_node['marriage'] = v_marriage[v]['marriage']
        s_node['grade'] = v_grade[v]['grade']
        s_node['liveplace'] = v_liveplace[v]['liveplace']
        s_node['height'] = v_height[v]['height']
        s_node['weight'] = v_weight[v]['weight']
        s_node['scorelevel'] = v_scorelevel[v]['scorelevel']
        s_node['birthyear'] = v_birthyear[v]['birthyear']
        s_node["out-degree"] = v.out_degree()
        s_node["in-degree"] = v.in_degree()
        s_node["cx"] = pos[v][0]
        s_node["cy"] = pos[v][1]
        convert.append(v_userid[v]["userid"])
        # o_node={}
        # o_node[v_userid[v]["userid"]]=s_node
        nodes.append(s_node)
    all_data = {}
    all_data['nodes'] = nodes
    links = []
    for e in u.edges():
        s_edge = {}
        s_edge['source'] = convert.index(str(e_source[e]))
        s_edge['target'] = convert.index(str(e_target[e]))
        s_edge['date'] = e_date[e]['date']
        s_edge['dailycount'] = e_dailycount[e]['dailycount']
        s_edge['weight'] = len(u.edge(e.target(), e.source(), all_edges=True, add_missing=False)) + \
            len(u.edge(e.source(), e.target(), all_edges=True, add_missing=False))
        links.append(s_edge)
    all_data['links'] = links
    # gt.graph_draw(u, pos=pos, output="graph-draw.png")
    return(all_data)
コード例 #8
0
 def __arf_layout(self, net, **params):
     """
     :Graph net: A Graph
     :string weight: Attribute name of an edge attribute with the respective weights
     :string pos: Attribute name of vector vertex property maps where the coordinates should be stored. If provided, this will also be used as the initial position of the vertices.
     """
     valid_params = {}
     vparam = ["pos"]
     for param_name in vparam:
         if params[param_name] in net.vp:
              valid_params[param_name] = net.vp[params[param_name]]
     eparam = ["weight"]
     for param_name in eparam:
         if params[param_name] in net.ep:
              valid_params[param_name] = net.ep[params[param_name]]
     return gt.arf_layout(net, **valid_params)
コード例 #9
0
ファイル: import.py プロジェクト: galaunay/biblio-network
    def make_author_graph(self, layout="arf"):
        """Make an author graph"""
        self.graph = Graph(directed=False)
        # add vertex
        auths = self.author_list
        self.graph.add_vertex(len(auths))
        # add links
        auth2ind = {auths[i]: i
                    for i in range(len(auths))}
        abet = []
        authbet = copy.deepcopy(self.author_betweeness)
        for auth in auths:
            for col, weight in authbet[auth].items():
                if col == auth:
                    continue
                self.graph.add_edge(auth2ind[auth], auth2ind[col])
                del authbet[col][auth]  # ensure that edges are not doubled
                abet.append(weight)
        # add properties
        cb = self.graph.new_edge_property("int", abet)
        self.graph.edge_properties['weight'] = cb
        # layout
        if layout == "arf":
            self.layout_pos = arf_layout(self.graph,
                                         weight=self.graph.ep.weight,
                                         pos=self.layout_pos,
                                         max_iter=10000)
        elif layout == "sfpd":
            self.layout_pos = sfdp_layout(self.graph,
                                          eweight=self.graph.ep.weight,
                                          pos=self.layout_pos)
        elif layout == "fr":
            self.layout_pos = fruchterman_reingold_layout(self.graph,
                                                          weight=self.graph.ep.weight,
                                                          circular=True,
                                                          pos=self.layout_pos)
        elif layout == "radial":
            nc = self.get_total_citation()
            main_auth_ind = np.argmax(list(nc.values()))
            main_auth = list(nc.keys())[main_auth_ind]
            self.layout_pos = radial_tree_layout(self.graph,
                                                 auth2ind[main_auth])
        elif layout == "planar":
            self.layout_pos = planar_layout(self.graph)

        else:
            raise ValueError()
コード例 #10
0
ファイル: app.py プロジェクト: aaha97/FlaskNGraphs
def network():
    path = flask.session['chosen_paths']
    name = flask.session['chosen_names']
    figdict={}

    df=''
    i1 = flask.session['filepath'][0]
    for path in flask.session['filepath']:
        DF = pd.read_csv(path)
        if path==i1:
            df = DF
        else:
            df = pd.concat([df,DF])
    df = df.drop(['datetime'],axis=1)
    thresh_min = int(flask.request.form.get('thresh_min',1))
    thresh_max = int(flask.request.form.get('thresh_max',-1))
    single_links = flask.request.form.get('single_links',False)
    if single_links != False:
        single_links=True
    df = thresh_filter(df,thresh_min,thresh_max,single_links)
    #actors = df['from'].tolist()+df['to'].tolist()
    #actors = list(set(actors))
    #actor_id = { str(actors[i]): i for i in xrange(len(actors)) }

    #df['from_id'] = df.apply(lambda x: actor_id[str(x['from'])],axis=1)
    #df['to_id'] = df.apply(lambda x: actor_id[str(x['to'])],axis=1)
    #df['label_prop'] = df.apply(lambda x: str(x['from_id'])+"->"+str(x['to_id']),axis=1)
    g = gt.Graph(directed=True)
    #g.add_vertex(n=len(actors))
    #g.add_edge_list(df[['from_id','to_id']].values)
    g.add_edge_list(df.values,hashed=True)
    #labels = g.new_ep(value_type="string",vals=df['label_prop'].tolist())
    pos = gt.arf_layout(g, max_iter=100,dt=1e-4)
    figfile = BytesIO()
    #figdata_png = gt.graphviz_draw(g,edge_text=labels,return_string=True)
    #gt.graph_draw(g,pos=pos,edge_text=labels, output=figfile,fmt='png')
    #print figdata_png[1]
    gt.graph_draw(g,pos=pos,output_size=(1000,1000), output=figfile,fmt='png')
    figfile.seek(0)
    figdata_png = figfile.getvalue()
    figdata_png = base64.b64encode(figdata_png)
    figdict['plot'] = figdata_png
    plt.clf()
    df.to_csv("test_file.csv",index=False)
    return flask.render_template('networkplot.html',imgs=figdict)
コード例 #11
0
def output_json_fd_lay_b(u, model):
    if model == "FR":
        pos = gt.fruchterman_reingold_layout(u, n_iter=100, r=100, a=10)
    if model == "Ran":
        pos = gt.random_layout(g, dim=2)
    if model == "ARF":
        pos = gt.arf_layout(g, max_iter=0)
    nodes = []
    convert = []
    for v in u.vertices():
        s_node = {}
        s_node["name"] = v_userid[v]["userid"]
        s_node['race'] = v_race[v]["race"]
        s_node['cohort'] = v_cohort[v]['cohort']
        s_node['gender'] = v_gender[v]['gender']
        s_node['c1net'] = v_c1net[v]['c1net']
        s_node['c2net'] = v_c2net[v]['c2net']
        s_node['c3net'] = v_c3net[v]['c3net']
        s_node['c4net'] = v_c4net[v]['c4net']
        s_node['c5net'] = v_c5net[v]['c5net']
        s_node['c6net'] = v_c6net[v]['c6net']
        s_node['c7net'] = v_c7net[v]['c7net']
        s_node["out-degree"] = v.out_degree()
        s_node["in-degree"] = v.in_degree()
        s_node["cx"] = (pos[v][0])
        s_node["cy"] = (pos[v][1])
        convert.append(v_userid[v]["userid"])
        # o_node={}
        # o_node[v_userid[v]["userid"]]=s_node
        nodes.append(s_node)
    all_data = {}
    all_data['nodes'] = nodes
    links = []
    for e in u.edges():
        s_edge = {}
        s_edge['source'] = convert.index(str(e_source[e]))
        s_edge['target'] = convert.index(str(e_target[e]))
        s_edge['type'] = e_type[e]['type']
        s_edge['year'] = e_year[e]['year']
        s_edge['weight'] = len(u.edge(e.target(), e.source(), all_edges=True, add_missing=False)) + \
            len(u.edge(e.source(), e.target(), all_edges=True, add_missing=False))
        links.append(s_edge)
    all_data['links'] = links
    return (all_data)
コード例 #12
0
def draw_fibers(g, output_filename=None, edgetext=False, layout='random'):
    node_colors = g.vp.fiber_index
    edge_color = g.ep.edge_color
    color_name = g.vp.color_name  # string format for fiber index.
    regulation = g.ep.regulation

    regulation_text = g.new_edge_property('string')
    for e in g.edges():
        regulation_text[e] = str(regulation[e])

    #for v in g.get_vertices(): color_name[v] = str(node_colors[v])
    #for e in g.edges():
    #    source = e.source()
    #    edge_color[e] = node_colors[source]

    if layout == 'random': pos = gt.random_layout(g)
    elif layout == 'planar': pos = gt.planar_layout(g)
    elif layout == 'spring': pos = gt.arf_layout(g, d=2.1, max_iter=0)
    else: pos = gt.random_layout(g)

    if output_filename != None and edgetext == False:
        gt.graph_draw(g,
                      pos,
                      vertex_text=color_name,
                      vertex_color=node_colors,
                      edge_color=edge_color,
                      vertex_fill_color=node_colors,
                      output_size=(900, 900),
                      output=output_filename)
    elif output_filename != None and edgetext == True:
        gt.graph_draw(g,
                      pos,
                      vertex_text=color_name,
                      vertex_color=node_colors,
                      edge_color=edge_color,
                      vertex_fill_color=node_colors,
                      edge_text=regulation_text,
                      output_size=(900, 900),
                      output=output_filename)
コード例 #13
0
def main():

    data = load_test_data("../../data/testDefinitions.sqlite")
    lengths = []
    g = gt.Graph(directed=False)
    vertices = {}

    for test in data.tests.values():
        vertices[test.id] = g.add_vertex()

    for equipment in data.test_equipment.values():
        all_items = []
        for test in data.tests.values():
            if equipment in test.testEquipment:
                all_items.append(test)
        if len(all_items) > 0:
            lengths.append(len(all_items))
        if len(all_items) > 1:

            for i in range(0, len(all_items) - 1):
                for j in range(i, len(all_items) - 1):
                    if i != j:
                        g.add_edge(vertices[all_items[i].id],
                                   vertices[all_items[j].id])

    pos = gt.arf_layout(g, max_iter=4)
    deg = g.degree_property_map("total")
    gt.graph_draw(g,
                  pos=pos,
                  vertex_fill_color=deg,
                  vorder=deg,
                  vertex_text=deg,
                  output_size=(800, 600),
                  output="test-testEquipment.png")
    plt.hist(lengths)
    plt.title("Equipment-Test Histogram")
    plt.xlabel("Equipment required by X tests.")
    plt.ylabel("Frequency")
    plt.show()
コード例 #14
0
def draw_graph(
    adata: AnnData,
    layout: _Layout = 'sfdp',
    #    init_pos: Union[str, bool, None] = None,
    #    root: Optional[int] = None,
    use_tree: bool = False,
    random_seed: Optional[int] = None,
    adjacency: Optional[spmatrix] = None,
    key_added_ext: Optional[str] = None,
    key: Optional[str] = 'schist',
    copy: bool = False,
    **kwds,
):
    """\
    Extends scanpy.tools.draw_graph function using some layouts available in 
    graph-tool library. Three layouts are available here:
    
    - SFDP spring-block layout.
    - ARF spring-block layout.
    - Fruchterman-Reingold spring-block layout.
    
    Fruchterman-Reingold is already available in scanpy, but here can be used
    to render the nested model tree. 
    
    In order to use these plotting function, the NestedBlockState needs to be
    saved when building the model, so `save_state=True` needs to be set.
    
    Parameters
    ----------
    adata
        Annotated data matrix. A NestedBlockState object needs to be saved
    layout
        A layout among 'sfdp', 'fr' or 'arf'. Other graph-tool layouts haven't been
        implemented.
    use_tree
        When this is set, the tree of the nested model is used to generate layout, 
        otherwise the layout only accounts for the neighborhood graph.    
    random_seed
        Random number to be used as seed for graph-tool
    adjacency
        Sparse adjacency matrix of the graph, defaults to
        `adata.uns['neighbors']['connectivities']`.
    key_added_ext
        By default, append `layout`.
    key
        The slot in `AnnData.uns` containing the state. Default is 'nsbm'
    copy
        Return a copy instead of writing to adata.
    **kwds
        Parameters of chosen igraph layout. See e.g. `fruchterman-reingold`_
        [Fruchterman91]_. One of the most important ones is `maxiter`.

        .. _fruchterman-reingold: http://igraph.org/python/doc/igraph.Graph-class.html#layout_fruchterman_reingold

    Returns
    -------
    Depending on `copy`, returns or updates `adata` with the following field.

    **X_draw_graph_layout** : `adata.obsm`
        Coordinates of graph layout. E.g. for layout='fa' (the default),
        the field is called 'X_draw_graph_fa'
    """
    if random_seed:
        np.random.seed(random_seed)
        gt.seed_rng(random_seed)

    n_cells = adata.shape[0]
    start = logg.info(f'drawing single-cell graph using layout {layout!r}')
    if layout not in _LAYOUTS:
        raise ValueError(f'Provide a valid layout, one of {_LAYOUTS}.')
    adata = adata.copy() if copy else adata
    if adjacency is None and 'neighbors' not in adata.uns:
        raise ValueError('You need to run `pp.neighbors` first '
                         'to compute a neighborhood graph.')
    if not key in adata.uns:
        raise ValueError(
            'You need to run `nested_model` before trying to run this function '
        )

    if use_tree and 'state' not in adata.uns[key]:
        raise ValueError(
            'When `use_tree` is set to `True`, a state should be saved'
            'running  `nested_model(adata, save_state=True)`.')
    if adjacency is None:
        adjacency = adata.uns['neighbors']['connectivities']

    g = get_graph_tool_from_adjacency(adjacency)
    weights = g.ep['weight']
    if use_tree:
        state = state_from_blocks(adata)
        g, _, _ = gt.get_hierarchy_tree(state, empty_branches=False)
        weights = None

    # actual drawing
    positions = np.zeros((n_cells, 2))
    if layout == 'fr':
        positions = gt.fruchterman_reingold_layout(g, weight=weights)
        positions = np.array([x for x in positions][:n_cells])
    elif layout == 'sfdp':
        positions = gt.sfdp_layout(g)
        positions = np.array([x for x in positions][:n_cells])
    elif layout == 'arf':
        positions = gt.arf_layout(g)
        positions = np.array([x for x in positions][:n_cells])

    adata.uns['draw_graph'] = {}
    adata.uns['draw_graph']['params'] = dict(layout=layout,
                                             random_seed=random_seed)
    key_added = f'X_draw_graph_{layout}'
    adata.obsm[key_added] = positions
    logg.info(
        '    finished',
        time=start,
        deep=('added\n'
              f'    {key_added!r}, graph_drawing coordinates (adata.obsm)'),
    )
    return adata if copy else None
def process_one_city(city_name, which=1, edges_percent=0.01, layout=1):
    g = gt.Graph()

    # load userid_bizid_star_tuple_for_graph
    base_dir = "/Users/sundeepblue/Bootcamp/allweek/week9/capstone/data/yelp_data/split_business_data_by_city/"

    folder_name = "userid_businessid_star_tuple.csv"  # note this is actually a folder not csv file
    full_folder_path = os.path.join(base_dir, city_name, folder_name)
    files = os.listdir(full_folder_path)
    tuples = []
    for f in files:
        if f.startswith("part-"):
            full_path = os.path.join(full_folder_path, f)
            print full_path
            with open(full_path, "r") as h:
                lines = h.readlines()
            tuples += lines
    print len(tuples)

    # populate edge list by parsing tuples
    edge_list = []
    for t in tuples:
        sp = t.strip().split(",")
        user_id = int(sp[0].strip())
        business_id = int(sp[1].strip())
        edge_list.append((user_id, business_id))

    num_edges = len(edge_list)
    num_chosen_edges = int(num_edges * edges_percent)

    # decide how many edges and in what type to be included in network
    if which == 1:
        random_indices = random.sample(range(len(edge_list)), num_chosen_edges)
        partial_edge_list = [edge_list[i] for i in random_indices]
        graph_type = "random_{}_edges".format(num_chosen_edges)
        print "{} edges were randomly chosen and added into graph!".format(
            num_chosen_edges)
    elif which == 2:
        partial_edge_list = edge_list[:num_chosen_edges]
        print "first {} edges were chosen and added into graph!".format(
            num_chosen_edges)
        graph_type = "first_{}_edges".format(num_chosen_edges)
    else:  # caution. this option is very time consuming if nodes number are big!
        partial_edge_list = edge_list
        print "use all {} edges to build graph!".format(len(edge_list))
        graph_type = "all_{}_edges".format(len(edge_list))

    g.add_edge_list(partial_edge_list)

    # Remove invalid vertices that added by graph-tool itself
    # Eg, if we add an edge by running "e = g.add_edge(1000, 2000)", by default, graph-tool will automatically
    # add 2000 vertices indexed from 1 to 2000. However, we only need two vertices (of indix 1000 and index 2000).
    # The code below scan all vertices, and mark those with non-zero indegree/outdegree as valid ones.

    print "Removing invalid vertices ..."
    valid_nodes = filter(lambda v: v.in_degree() != 0 or v.out_degree() != 0,
                         g.vertices())
    keep = g.new_vertex_property('bool')
    for v in valid_nodes:
        keep[v] = True
    g.set_vertex_filter(prop=keep)

    print g.num_vertices()
    print g.num_edges()

    # do not show vertex text
    print "Exporting graph image ..."
    output_file_name = "{}_graph_{}_{}.pdf".format(city_name, graph_type,
                                                   layout)
    if layout == 1:
        pos = gt.sfdp_layout(g)
    else:
        pos = gt.arf_layout(g)

    # output network graph
    gt.graph_draw(g,
                  pos=pos,
                  output_size=(2000, 2000),
                  vertex_color=[1, 1, 1, 0],
                  vertex_size=2,
                  edge_pen_width=1,
                  output=output_file_name)
    print "Done!"
コード例 #16
0
import wizard_parse as wp
import branch_search
import re
import graph_tool.all as gt


dirs = ['inputs20', 'inputs35', 'inputs50', 'Staff_Inputs']

t = 0
for dir in dirs:
    files = wp.get_files(dir)
    # Process in ascending numeric order
    files.sort(key=lambda file:int(re.sub("[^0-9]", "", file)))
    for file in files:
        print('===')
        print('searching for solution to', file)
        w, c = wp.parse_partial(dir, file)
        p = branch_search.Party(dir, file)
        cnf = branch_search.CNF(p)
        t += branch_search.time_fn(cnf.find_assignment, [len(cnf.clauses)])
        order = cnf.create_ordering()
        # Create visualisations
        g = cnf.relationships
        pos = gt.arf_layout(g, max_iter=0)
        gt.graph_draw(g, pos=pos, output="visualisation" + wp.separator() + "{}_{}".format(t, file) + '.pdf')
        with open('outputs' + str(p.wizard_count) + wp.separator() + "{}_{}".format(p.wizard_count, files.index(file)), "w") as f:
            f.write(order)
        print(order)

print('average time taken:', t // len(t), 'seconds')
コード例 #17
0
ファイル: plot_graph.py プロジェクト: mike-live/parenclitic
    eweight[e] = weights[i]
    if weights[i] < 0:
        ecolor[e] = 'red'
    else:
        ecolor[e] = 'green'
    i += 1

g.edge_properties["weight"] = eweight
g.edge_properties["color"] = ecolor
'''

#pos = gt.planar_layout(g)
#pos = gt.radial_tree_layout(g, g.vertex(0))

for i in range(3):
    pos = gt.arf_layout(g, d = 1, a = 5, max_iter=0) # good
    #pos = gt.fruchterman_reingold_layout(g, n_iter=1000)
    #pos = gt.sfdp_layout(g, C = 1)
    #pos = gt.circle_layout(g)
    #gt.graph_draw(g, pos = pos, vertex_text=g.vertex_properties["name"], vertex_font_size=20, vertex_size=10, vertex_color = 'white', vertex_fill_color = 'blue', vertex_text_position=0, output_size=(2000, 1000), output="imgs/small_graph_top_" + str(i) + ".pdf")
    #gt.graph_draw(g, pos = pos, vertex_text=g.vertex_properties["name"], vertex_font_size=20, vertex_size=10, vertex_color = 'white', vertex_fill_color = 'blue', vertex_text_position=0, output_size=(2000, 1000), output="imgs/small_graph_top_" + str(i) + ".png")

    state = gt.minimize_blockmodel_dl(g) # , deg_corr=True, B_min = 10
    state.draw(pos=pos, vertex_shape=state.get_blocks(), vertex_text=g.vertex_properties["name"], vertex_font_size=20, vertex_size=20, edge_pen_width = 2, vertex_text_position=0, output="small_graph_top/small_graph_top_blocks_mdl_" + str(i) + ".pdf", output_size=(1500, 1000), fit_view=1.1)
    state.draw(pos=pos, vertex_shape=state.get_blocks(), vertex_text=g.vertex_properties["name"], vertex_font_size=20, vertex_size=20, edge_pen_width = 2, vertex_text_position=0, output="small_graph_top/small_graph_top_blocks_mdl_" + str(i) + ".png", output_size=(1500, 1000), fit_view=1.1)
    print(i)
    #gt.draw_hierarchy(state, layout="sfdp", vertex_text=g.vertex_properties["name"], vertex_font_size=24, vertex_text_position="centered", edge_color=g.edge_properties["color"], output_size=(2000, 1000), output="small_graph_mdl.pdf", fit_view = 0.8, hide = 2)

print(vchrom)
print(np.array(vchrom))
コード例 #18
0
def main():
    argLength = len(sys.argv)
    if (argLength == 1):
        print(
            "to create erdos renyi graph, use arguments: create_erdos_renyi <vertices> <probability>\n"
        )
        print(
            "to create regular graph, use arguments: create_regular <vertices> <degree>\n"
        )
        print("to show a graph, use arguments: show_graph <file path>\n")
        print(
            "to run fixation probability simulation, use arguments: fix_sim <file path> <ratio> <delta> <iterations>\n"
        )
        print(
            "to create regular cooperator-defector connected graph, use arguments: create_regular_bridge <vertices> <degree> <cooperators> <connected edges>\n"
        )
        print(
            "to calculate the critical ratio of a regular graph, use arguments: regular_crit <vertices> <degree>"
        )
        print(
            "to make a lot of bridge graphs of vertice v and degree d, use arguments: make_a_lot_of_bridge_graphs <vertices> <degree>"
        )
        print(
            "to run simulation on a lot of bridge graphs, use arguments: run_a_lot_of_simulations <vertices> <degree> <cooperator> <ratio> <delta> <iteration> <method> <overnight>"
        )
        print(
            "to run a standalone simulation, use arguments: single_run <vertices> <degree> <cooperator> <connected edges> <ratio> <delta> <iteration>"
        )
        print(
            "to check what which cooperators data points have already been done, use arguments: check_done <vertices> <degree> <delta>"
        )
        print(
            "<method> is either 'complete' or 'iterative' and if overnight=True it means you will be running for a looong time, probably"
        )
        print(
            "Note that in cooperator-defector connected graph <cooperator> and <connected edges> must be both even and <connected edges> is less than or equal to min{<cooperator>*<degree>, <totalVertices>*<degree> - <cooperator>*<degree>}\n"
        )
        print("animate requires PyGObject dependency")
    else:
        if (sys.argv[1] == "create_erdos_renyi"):
            totalVertices = int(sys.argv[2])
            connectProbability = int(sys.argv[3])
            graph = create_erdos_renyi(totalVertices, connectProbability)
            gt.graph_draw(graph, pos=gt.arf_layout(graph))
            fileName = "./graph_data/erdo_renyi_n" + str(
                totalVertices) + "_p" + str(connectProbability) + ".gt"
            graph.save(fileName)
            print("Graph created and saved to " + fileName)

        elif (sys.argv[1] == "create_regular"):
            totalVertices = int(sys.argv[2])
            degree = int(sys.argv[3])
            graph = create_regular_graph(totalVertices, degree)
            gt.graph_draw(graph, pos=gt.arf_layout(graph))
            fileName = "./graph_data/regular_n" + str(
                totalVertices) + "_d" + str(degree) + ".gt"
            graph.save(fileName)
            print("Graph created and saved to " + fileName)
        elif (sys.argv[1] == "fix_sim"):
            graphPath = sys.argv[2]
            baseGraphName = os.path.basename(graphPath)
            ratio = float(sys.argv[3])
            delta = float(sys.argv[4])
            iterations = int(sys.argv[5])
            #animate = bool(sys.argv[6])
            graph = gt.load_graph(graphPath)
            startTime = time.time()
            estimate = fixation_probability_simulation(graph, ratio, delta,
                                                       iterations)
            elapsedTime = round(time.time() - startTime, 2)
            currentTime = str(datetime.datetime.now())
            fileName = currentTime + "_fixation_simulation_on_" + baseGraphName
            sim_record = open(fileName, "w")
            sim_record.write("Simulation Result from " + currentTime + "\n")
            sim_record.write("Parameters:\n")
            sim_record.write("File Used: " + graphPath + "\n")
            sim_record.write("Ratio:" + str(ratio) + "\n")
            sim_record.write("Delta:" + str(delta) + "\n")
            sim_record.write("Iterations:" + str(iterations) + "\n")
            sim_record.write("Results:\n")
            sim_record.write("Simulation Duration:" + str(elapsedTime) +
                             " seconds\n")
            sim_record.write("Fixation Probability Estimate: " + str(estimate))
            sim_record.close()
            print("The estimate for fixation probability is: ", estimate)
            print("record saved to current directory, name: " + fileName)
        elif (sys.argv[1] == "show_graph"):
            graph = gt.load_graph(sys.argv[2])
            gt.graph_draw(graph,
                          pos=gt.arf_layout(graph),
                          vertex_fill_color=graph.vp.vertex_fill_color)
        elif (sys.argv[1] == "create_regular_bridge"):
            totalVertices = int(sys.argv[2])
            degree = int(sys.argv[3])
            totalCooperators = int(sys.argv[4])
            totalBridgeEdges = int(sys.argv[5])
            graph = create_regular_bridge_graph(totalVertices, degree,
                                                totalCooperators,
                                                totalBridgeEdges)
            gt.graph_draw(
                graph,
                pos=gt.arf_layout(graph),
                vertex_fill_color=graph.vertex_properties["vertex_fill_color"])
            fileName = "./graph_data/bridge_graph/regular_bridge_n" + str(
                totalVertices) + "_d" + str(degree) + "_coop" + str(
                    totalCooperators) + "_conn" + str(totalBridgeEdges) + ".gt"
            graph.save(fileName)
            print("Graph created, saved to: " + fileName)
        elif (sys.argv[1] == "regular_crit"):
            totalVertices = int(sys.argv[2])
            degree = int(sys.argv[3])
            print(calculate_regular_critaical_ratio(totalVertices, degree))
        elif (sys.argv[1] == "make_a_lot_of_bridge_graphs"):
            totalVertices = int(sys.argv[2])
            degree = int(sys.argv[3])
            totalDegree = totalVertices * degree
            for initCoop in range(totalVertices):
                print("current cooperator: " + str(initCoop))
                maxBridgeEdges = min(initCoop * degree,
                                     totalDegree - initCoop * degree)
                for initBridge in range(maxBridgeEdges):
                    print("current bridge edges: " + str(initBridge))
                    if ((not initCoop == 0) and initCoop % 2 == 0
                            and (not initBridge == 0) and initBridge % 2 == 0):
                        graph = create_regular_bridge_graph(
                            totalVertices, degree, initCoop, initBridge)
                        fileName = "./graph_data/bridge_graph/regular_bridge_n" + str(
                            totalVertices
                        ) + "_d" + str(degree) + "_coop" + str(
                            initCoop) + "_conn" + str(initBridge) + ".gt"
                        graph.save(fileName)
                        print("creating file: " + fileName)
            print("Done")
        elif (sys.argv[1] == "run_a_lot_of_simulations"):
            totalVertices = int(sys.argv[2])
            degree = int(sys.argv[3])
            totalCooperators = int(sys.argv[4])
            ratio = float(sys.argv[5])
            delta = float(sys.argv[6])
            iterations = int(sys.argv[7])
            method = str(sys.argv[8])
            overnight = str(sys.argv[9]) == "True"
            if (not (method == "iterative" or method == "complete")):
                print("Invalid Method!")
            else:
                batch_simulations(totalVertices, degree, totalCooperators,
                                  ratio, delta, iterations, method, overnight)
        elif (sys.argv[1] == "single_run"):
            totalVertices = int(sys.argv[2])
            degree = int(sys.argv[3])
            totalCooperators = int(sys.argv[4])
            totalBridgeEdges = int(sys.argv[5])
            ratio = float(sys.argv[6])
            delta = float(sys.argv[7])
            iterations = int(sys.argv[8])
            single_run(totalVertices, degree, totalCooperators,
                       totalBridgeEdges, ratio, delta, iterations)
        elif (sys.argv[1] == "check_done"):
            totalVertices = int(sys.argv[2])
            degree = int(sys.argv[3])
            check_done_list(totalVertices, degree, delta)
        elif (sys.argv[1] == "show_data"):
            npMatrix = np.load(sys.argv[2])
            fig, ax = plt.subplots()
            im = ax.imshow(npMatrix)
            plt.show()
            print(npMatrix)
コード例 #19
0
    def plot(
            self,
            source_list,
            sink_list,
            max_depth,
            max_paths
            ):

        print('WARNING: This is actually a plotting function!!!')

        num_source_nodes = len(source_list)
        num_sink_nodes = len(sink_list)

        # super_source_vertex = g.add_vertex()
        # super_sink_vertex = g.add_vertex()

        super_source_vertex = 'super_source_vertex'
        super_sink_vertex = 'super_sink_vertex'

        edge_list = list(zip([super_source_vertex] * num_source_nodes,
                             source_list))
        for e in edge_list:
            self.add_edge(*e)

        edge_list = list(zip(sink_list, [super_sink_vertex] *
                             num_sink_nodes))
        for e in edge_list:
            self.add_edge(*e)

        g = self.G

        pos = gt.arf_layout(g, max_iter=0)
        gt.graph_draw(g, pos=pos, vertex_text=self.G.vertex_index)
        time.sleep(1000)
        exit()

        gt.graph_draw(self.G, vertex_text=self.G.vertex_index)
        time.sleep(1000)

#        print(edge_list)

        # Add edges:
        #   \forall sink \in sink_list. sink -> super sink node

        edge_list = list(zip(sink_list, [dummy_super_sink_node] *
                             num_sink_nodes))
        H.add_edges_from(edge_list)

#        print(edge_list)

#        print('='*80)
        # TODO: WHY?
        # Switching this on with def path_gen(), results in empty path and no further results!!
        # #xplanation required!
#        for path in nx.all_simple_paths(H, dummy_super_source_node, dummy_super_sink_node):
#            print(path)
#        print('='*80)

        # TODO: how to do this with lambda?
        # Also, is this indeed correct?

        def path_gen():
            for i in nx.all_simple_paths(H, dummy_super_source_node,
                    dummy_super_sink_node):

                # Remove the first (super source)
                # and the last element (super sink)

                yield i[1:-1]

        # return lambda: [yield i[1:-1] for i in nx.all_simple_paths(H,
        # dummy_super_source_node, dummy_super_sink_node)]

        return path_gen()
コード例 #20
0
def get_pos(g, algorithm='sfdp'):
    if algorithm is 'sfdp':
        return gt.sfdp_layout(g)
    elif algorithm is 'arf':
        return gt.arf_layout(g, max_iter=2000)
コード例 #21
0
ファイル: graphGT.py プロジェクト: zutshi/S3CAMR
    def plot(
            self,
            source_list,
            sink_list,
            max_depth,
            max_paths
            ):

        print 'WARNING: This is actually a plotting function!!!'

        num_source_nodes = len(source_list)
        num_sink_nodes = len(sink_list)

        # super_source_vertex = g.add_vertex()
        # super_sink_vertex = g.add_vertex()

        super_source_vertex = 'super_source_vertex'
        super_sink_vertex = 'super_sink_vertex'

        edge_list = zip([super_source_vertex] * num_source_nodes, source_list)
        for e in edge_list:
            self.add_edge(*e)

        edge_list = zip(sink_list, [super_sink_vertex] * num_sink_nodes)
        for e in edge_list:
            self.add_edge(*e)

        g = self.G

        pos = gt.arf_layout(g, max_iter=0)
        gt.graph_draw(g, pos=pos, vertex_text=self.G.vertex_index)
        time.sleep(1000)
        exit()

        gt.graph_draw(self.G, vertex_text=self.G.vertex_index)
        time.sleep(1000)

#        print edge_list

        # Add edges:
        #   \forall sink \in sink_list. sink -> super sink node

        edge_list = zip(sink_list, [dummy_super_sink_node] * num_sink_nodes)
        H.add_edges_from(edge_list)

#        print edge_list

#        print '='*80
        # TODO: WHY?
        # Switching this on with def path_gen(), results in empty path and no further results!!
        # #xplanation required!
#        for path in nx.all_simple_paths(H, dummy_super_source_node, dummy_super_sink_node):
#            print path
#        print '='*80

        # TODO: how to do this with lambda?
        # Also, is this indeed correct?

        def path_gen():
            for i in nx.all_simple_paths(H, dummy_super_source_node,
                    dummy_super_sink_node):

                # Remove the first (super source)
                # and the last element (super sink)

                yield i[1:-1]

        # return lambda: [yield i[1:-1] for i in nx.all_simple_paths(H,
        # dummy_super_source_node, dummy_super_sink_node)]

        return path_gen()
コード例 #22
0
 def make_arf_graph(self, **args):
     if self.random_state is not None:
         np.random.seed(self.random_state)
     pos = self.g.new_edge_property("double")
     self.g.vertex_properties['pos'] = gt.arf_layout(self.g, **args)