Exemple #1
0
    def testAdjacency(self):
        with temporary_file(
            """\
        # Test comment line
        0 1 1 0 0 0
        1 0 1 0 0 0
        1 1 0 0 0 0
        0 0 0 0 2 2
        0 0 0 2 0 2
        0 0 0 2 2 0
        """
        ) as tmpfname:
            g = Graph.Read_Adjacency(tmpfname)
            self.assertTrue(isinstance(g, Graph))
            self.assertTrue(
                g.vcount() == 6
                and g.ecount() == 18
                and g.is_directed()
                and "weight" not in g.edge_attributes()
            )
            g = Graph.Read_Adjacency(tmpfname, attribute="weight")
            self.assertTrue(isinstance(g, Graph))
            self.assertTrue(
                g.vcount() == 6
                and g.ecount() == 12
                and g.is_directed()
                and g.es["weight"] == [1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2]
            )

            g.write_adjacency(tmpfname)
Exemple #2
0
def read_graph(path_name, format=None):
    """
    Read a graph from a file.

    It can read some file formats, such as GraphML or CSV.
    XML-like files such as GraphML have to be written in a standard format.
    See example below for GraphML files.

    If format is None, it will be detected automatically.
    It might cause errors. It is preferred that the format has been set.

    The function creates a Graph with the igraph library.

    :param path_name: path of the file
    :type path_name: str
    :param format: format of the file
    :type format: str
    :return: the graph
    :rtype: igraph.Graph

    Here is an example of how the GraphML file has to be written.
    This GraphML file uses the standards of the igraph.graph.write_graphml() method.

    :Example:

    <?xml version="1.0" encoding="UTF-8"?>
    <graphml
            xmlns="http://graphml.graphdrawing.org/xmlns"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns
            http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd">
        <key id="v_id" for="node" attr.name="id" attr.type="string"/>
        <key id="e_weight" for="edge" attr.name="weight" attr.type="double"/>
        <key id="e_id" for="edge" attr.name="id" attr.type="string"/>
        <graph id="network_a" edgedefault="undirected">
            <node id="n0">
            <data key="v_id">1</data>
            </node>
            <node id="n1">
            <data key="v_id">2</data>
            </node>
            <node id="n2">
            <data key="v_id">3</data>
            </node>
            <edge source="n0" target="n1">
            <data key="e_weight">1</data>
            <data key="e_id">e0</data>
            </edge>
            <edge source="n1" target="n2">
            <data key="e_weight">1</data>
            <data key="e_id">e1</data>
            </edge>
            <edge source="n2" target="n3">
            <data key="e_weight">1</data>
            <data key="e_id">e2</data>
            </edge>
        </graph>
    </graphml>

    .. warning: In the GraphML file, the attribute which defines the weights of the edges has to be given the name "weight" with the attribute "attr.name".
    """

    graph = None

    if format is not None and format.lower() == consts.FILE_FORMAT_CSV:
        # The separator in CSV files is the comma.
        graph = Graph.Read_Adjacency(path_name,
                                     sep=",",
                                     comment_char="#",
                                     attribute=consts.EDGE_WEIGHT_ATTR)
    else:
        graph = Graph.Read(path_name, format)

    return graph
Exemple #3
0
import os
from scipy import stats as st

os.environ["path"] += ";C:\whut\cairo-windows-1.15.10\lib\x64"
import igraph
from igraph import Graph
import numpy as np
import string
# Load data and create graph
data = np.genfromtxt('./sociogram-employees-un.csv', delimiter=',', dtype=None)
# print(data)
graph = Graph()
graph = Graph.Read_Adjacency('./sociogram-employees-un.csv', ',')
graph.vs["name"] = list(string.ascii_uppercase)[:data.shape[0]]
graph.vs["label"] = graph.vs["name"]
graph.vs["color"] = "yellow"
graph.vs["shape"] = "circle"

igraph.plot(graph)
print("Network diameter is {}".format(graph.diameter()))
print("Network average closeness is {}".format(np.mean(graph.closeness())))
print("Network average network betweenness "
      "is {}".format(np.mean(graph.betweenness())))
print("Network density is {}".format(graph.density()))
print("Network average degree is {}".format(np.mean(graph.degree(mode=1))))
print("Network reciprocity is {}".format(graph.reciprocity()))
print("Network average transitivity "
      "is {}".format(graph.transitivity_avglocal_undirected()))
print("Network eccentricity is {}".format(np.mean(graph.eccentricity())))
avg_shortest = []
for v in graph.vs: