Exemple #1
0
def read_pajek(path, encoding='UTF-8', project=False, auto_table=False):
    """Reimplemented method for reading Pajek files; written in
    C++ for maximum performance.

    :param path: File or file name to write.
    :type path: string

    :param encoding: Encoding of input text file, default 'UTF-8'.
    :type encoding: string

    :param project: Determines whether the input file is a Pajek project file,
        possibly containing multiple networks and other data. If :obj:`True`,
        a list of networks is returned instead of just a network. Default is
        :obj:`False`.
    :type project: boolean.

    Return the network (or a list of networks if project=:obj:`True`) of type
    :obj:`Orange.network.Graph` or :obj:`Orange.network.DiGraph`.


    Examples

    >>> G=Orange.network.nx.path_graph(4)
    >>> Orange.network.readwrite.write_pajek(G, "test.net")
    >>> G=Orange.network.readwrite.read_pajek("test.net")

    To create a Graph instead of a MultiGraph use

    >>> G1=Orange.network.Graph(G)

    References

    See http://vlado.fmf.uni-lj.si/pub/networks/pajek/doc/draweps.htm
    for format information.

    """

    path = _check_network_dir(path)
    G = _wrap(rwpajek.read_pajek(path))

    # Additionally read values into Table; needed to get G nodes properly sorted
    # (Consult OWNxFile.readDataFile(), orangeom.GraphLayout.readPajek(), and the Pajek format spec)
    import shlex, numpy as np
    rows, metas, remapping = [], [], {}
    with open(path) as f:
        for line in f:
            if line.lower().startswith('*vertices'):
                nvertices = int(line.split()[1])
                break
        # Read vertices lines
        for line in f:
            parts = shlex.split(line)[:4]
            if len(parts) == 1:
                i = label = parts[0]
            elif len(parts) == 2:
                i, label = parts
                metas.append((label,))
            elif len(parts) == 4:
                i, label, x, y = parts
                x, y = float(x), float(y)
                rows.append((x, y))
                metas.append((label,))
            i = int(i) - 1  # -1 because pajek is 1-indexed
            remapping[label] = i
            nvertices -= 1
            if not nvertices: break
    from Orange.data import Domain, Table, ContinuousVariable, StringVariable
    # Construct x-y-label table (added in OWNxFile.readDataFile())
    table = None
    if rows:
        domain = Domain([ContinuousVariable('x'), ContinuousVariable('y')],
                        metas=[StringVariable('label')])
        table = Table.from_numpy(domain, np.array(rows, dtype=float),
                                 metas=np.array(metas, dtype=str))
    elif metas:
        domain = Domain([], metas=[StringVariable('label')])
        table = Table.from_numpy(domain, np.zeros((len(metas), 0)),
                                 metas=np.array(metas, dtype=str))
    if table is not None and auto_table:
        G.set_items(table)
    # Relabel nodes to integers, sorted by appearance
    for node in G.node:
        G.node[node]['label'] = node
    nx.relabel_nodes(G, remapping, copy=False)
    assert not table or len(table) == G.number_of_nodes(), 'There was a bug in NetworkX. Please update to git if need be'
    return G
def read_pajek(path, encoding='UTF-8', project=False, auto_table=False):
    """Reimplemented method for reading Pajek files; written in
    C++ for maximum performance.

    :param path: File or file name to write.
    :type path: string

    :param encoding: Encoding of input text file, default 'UTF-8'.
    :type encoding: string

    :param project: Determines whether the input file is a Pajek project file,
        possibly containing multiple networks and other data. If :obj:`True`,
        a list of networks is returned instead of just a network. Default is
        :obj:`False`.
    :type project: boolean.

    Return the network (or a list of networks if project=:obj:`True`) of type
    :obj:`Orange.network.Graph` or :obj:`Orange.network.DiGraph`.


    Examples

    >>> G = orangecontrib..network.nx.path_graph(4)
    >>> orangecontrib..network.readwrite.write_pajek(G, "test.net")
    >>> G = orangecontrib.network.readwrite.read_pajek("test.net")

    To create a Graph instead of a MultiGraph use

    >>> G1 = orangecontrib.network.Graph(G)

    References

    See http://vlado.fmf.uni-lj.si/pub/networks/pajek/doc/draweps.htm
    for format information.
    """
    path = _check_network_dir(path)
    G = _wrap(rwpajek.read_pajek(path))

    # Additionally read values into Table; needed to get G nodes properly sorted
    # (Consult OWNxFile.readDataFile(), orangeom.GraphLayout.readPajek(), and the Pajek format spec)
    import shlex, numpy as np
    rows, metas, remapping = [], [], {}
    with open(path, encoding='utf-8') as f:
        for line in f:
            if line.lower().startswith('*vertices'):
                nvertices = int(line.split()[1])
                break
        # Read vertices lines
        for line in f:
            parts = shlex.split(line)[:4]
            if len(parts) == 1:
                i = label = parts[0]
            elif len(parts) == 2:
                i, label = parts
                metas.append((label, ))
            elif len(parts) == 4:
                i, label, x, y = parts
                # The format specification was never set in stone, it seems
                try:
                    x, y = float(x), float(y)
                except ValueError:
                    metas.append((label, x, y))
                else:
                    rows.append((x, y))
                    metas.append((label, ))
            i = int(i) - 1  # -1 because pajek is 1-indexed
            remapping[label] = i
            nvertices -= 1
            if not nvertices: break
    from Orange.data import Domain, ContinuousVariable, StringVariable
    # Construct x-y-label table (added in OWNxFile.readDataFile())
    table = None
    vars = [ContinuousVariable('x'), ContinuousVariable('y')] if rows else []
    meta_vars = [
        StringVariable('label ' + str(i))
        for i in range(len(metas[0]) if metas else 0)
    ]
    if rows or metas:
        domain = Domain(vars, metas=meta_vars)
        table = Table.from_numpy(domain,
                                 np.array(rows, dtype=float).reshape(
                                     len(metas),
                                     len(rows[0]) if rows else 0),
                                 metas=np.array(metas, dtype=str))
    if table is not None and auto_table:
        G.set_items(table)
    # Relabel nodes to integers, sorted by appearance
    for node in G.node:
        G.node[node]['label'] = node
    nx.relabel_nodes(G, remapping, copy=False)
    if table is not None and len(table) != G.number_of_nodes():
        raise PajekBug(
            "There is a bug in your version of NetworkX reading Pajek files. "
            "Please update your NetworkX installation.")
    return G
def load(graph_path):
    return pajek.read_pajek(graph_path)
Exemple #4
0
def read_pajek(path, encoding='UTF-8', project=False, auto_table=False):
    """Reimplemented method for reading Pajek files; written in
    C++ for maximum performance.

    :param path: File or file name to write.
    :type path: string

    :param encoding: Encoding of input text file, default 'UTF-8'.
    :type encoding: string

    :param project: Determines whether the input file is a Pajek project file,
        possibly containing multiple networks and other data. If :obj:`True`,
        a list of networks is returned instead of just a network. Default is
        :obj:`False`.
    :type project: boolean.

    Return the network (or a list of networks if project=:obj:`True`) of type
    :obj:`Orange.network.Graph` or :obj:`Orange.network.DiGraph`.


    Examples

    >>> G=Orange.network.nx.path_graph(4)
    >>> Orange.network.readwrite.write_pajek(G, "test.net")
    >>> G=Orange.network.readwrite.read_pajek("test.net")

    To create a Graph instead of a MultiGraph use

    >>> G1=Orange.network.Graph(G)

    References

    See http://vlado.fmf.uni-lj.si/pub/networks/pajek/doc/draweps.htm
    for format information.

    """

    path = _check_network_dir(path)
    G = _wrap(rwpajek.read_pajek(path))

    # Additionally read values into Table; needed to get G nodes properly sorted
    # (Consult OWNxFile.readDataFile(), orangeom.GraphLayout.readPajek(), and the Pajek format spec)
    import shlex, numpy as np
    rows, remapping = [], {}
    with open(path) as f:
        for line in f:
            if line.lower().startswith('*vertices'):
                nvertices = int(line.split()[1])
                break
        # Read vertices lines
        for line in f:
            i, label, x, y = shlex.split(line)[:4]
            i, x, y = int(i) - 1, float(x), float(
                y)  # -1 because pajek is 1-indexed
            remapping[label] = i
            rows.append((x, y))
            nvertices -= 1
            if not nvertices: break
    # Construct x-y table (added in OWNxFile.readDataFile())
    domain = Orange.data.Domain([
        Orange.data.ContinuousVariable('x'),
        Orange.data.ContinuousVariable('y')
    ])
    table = Orange.data.Table.from_numpy(domain, np.array(rows, dtype=float))
    G.set_items(table)
    # Relabel nodes to integers, sorted by appearance
    for node in G.node:
        G.node[node]['label'] = node
    nx.relabel_nodes(G, remapping, copy=False)
    assert len(table) == G.number_of_nodes(
    ), 'There was a bug in NetworkX. Please update to git if need be'
    return G