easygraph package

Subpackages

Submodules

easygraph.convert module

easygraph.convert.dict_to_hypergraph(data, max_order=None, is_dynamic=False)[source]

A function to read a file in a standardized JSON format.

Parameters
  • data (dict) – A dictionary in the hypergraph JSON format

  • max_order (int, optional) – Maximum order of edges to add to the hypergraph

Returns

The loaded hypergraph

Return type

A Hypergraph object

Raises

EasyGraphError – If the JSON is not in a format that can be loaded.

See also

read_json

easygraph.convert.from_dgl(g) Union[Graph, DiGraph][source]

Convert a DGL graph to an EasyGraph graph.

Parameters

g (DGLGraph) – A DGL graph

Raises

ImportError – If DGL is not installed.

Returns

Converted EasyGraph graph

Return type

Union[Graph, DiGraph]

easygraph.convert.from_dict_of_dicts(d, create_using=None, multigraph_input=False)[source]
easygraph.convert.from_dict_of_lists(d, create_using=None)[source]
easygraph.convert.from_edgelist(edgelist, create_using=None)[source]

Returns a graph from a list of edges.

Parameters
  • edgelist (list or iterator) – Edge tuples

  • create_using (EasyGraph graph constructor, optional (default=eg.Graph)) – Graph type to create. If graph instance, then cleared before populated.

Examples

>>> edgelist = [(0, 1)]  # single edge (0,1)
>>> G = eg.from_edgelist(edgelist)

or

>>> G = eg.Graph(edgelist)  # use Graph constructor
easygraph.convert.from_networkx(g: Union[nx.Graph, nx.DiGraph]) Union[Graph, DiGraph][source]

Convert a NetworkX graph to an EasyGraph graph.

Parameters

g (Union[nx.Graph, nx.DiGraph]) – A NetworkX graph

Returns

Converted EasyGraph graph

Return type

Union[Graph, DiGraph]

easygraph.convert.from_pyg(data: torch_geometric.data.Data, node_attrs: Optional[Iterable[str]] = None, edge_attrs: Optional[Iterable[str]] = None, graph_attrs: Optional[Iterable[str]] = None, to_undirected: Optional[Union[bool, str]] = False, remove_self_loops: bool = False) Any[source]

Converts a torch_geometric.data.Data instance to a easygraph.Graph if to_undirected is set to True, or a directed easygraph.DiGraph otherwise.

Parameters
  • data (torch_geometric.data.Data) – The data object.

  • node_attrs (iterable of str, optional) – The node attributes to be copied. (default: None)

  • edge_attrs (iterable of str, optional) – The edge attributes to be copied. (default: None)

  • graph_attrs (iterable of str, optional) – The graph attributes to be copied. (default: None)

  • to_undirected (bool or str, optional) – If set to True or “upper”, will return a easygraph.Graph instead of a easygraph.DiGraph. The undirected graph will correspond to the upper triangle of the corresponding adjacency matrix. Similarly, if set to “lower”, the undirected graph will correspond to the lower triangle of the adjacency matrix. (default: False)

  • remove_self_loops (bool, optional) – If set to True, will not include self loops in the resulting graph. (default: False)

Examples

>>> import torch_geometric as pyg
>>> Data = pyg.data.Data  # type: ignore
>>> edge_index = torch.tensor([
...     [0, 1, 1, 2, 2, 3],
...     [1, 0, 2, 1, 3, 2],
... ])
>>> data = Data(edge_index=edge_index, num_nodes=4)
>>> from_pyg(data)
<easygraph.classes.digraph.DiGraph at 0x2713fdb40d0>
easygraph.convert.to_dgl(g: Union[Graph, DiGraph])[source]

Convert an EasyGraph graph to a DGL graph.

Parameters

g (Union[Graph, DiGraph]) – An EasyGraph graph

Raises

ImportError – If DGL is not installed.

Returns

Converted DGL graph

Return type

DGLGraph

easygraph.convert.to_easygraph_graph(data, create_using=None, multigraph_input=False)[source]

Make a EasyGraph graph from a known data structure.

The preferred way to call this is automatically from the class constructor

>>> d = {0: {1: {"weight": 1}}}  # dict-of-dicts single edge (0,1)
>>> G = eg.Graph(d)

instead of the equivalent

>>> G = eg.from_dict_of_dicts(d)
Parameters
  • data (object to be converted) –

    Current known types are:

    any EasyGraph graph dict-of-dicts dict-of-lists container (e.g. set, list, tuple) of edges iterator (e.g. itertools.chain) that produces edges generator of edges Pandas DataFrame (row per edge) numpy matrix numpy ndarray scipy sparse matrix pygraphviz agraph

  • create_using (EasyGraph graph constructor, optional (default=eg.Graph)) – Graph type to create. If graph instance, then cleared before populated.

  • multigraph_input (bool (default False)) – If True and data is a dict_of_dicts, try to create a multigraph assuming dict_of_dict_of_lists. If data and create_using are both multigraphs then create a multigraph from a multigraph.

easygraph.convert.to_networkx(g: Union[Graph, DiGraph]) Union[nx.Graph, nx.DiGraph][source]

Convert an EasyGraph to a NetworkX graph.

Parameters

g (Union[Graph, DiGraph]) – An EasyGraph graph

Raises

ImportError is raised if NetworkX is not installed.

Returns

Converted NetworkX graph

Return type

Union[nx.Graph, nx.DiGraph]

easygraph.convert.to_pyg(G: Any, group_node_attrs: Optional[Union[List[str], all]] = None, group_edge_attrs: Optional[Union[List[str], all]] = None) torch_geometric.data.Data[source]

Converts a easygraph.Graph or easygraph.DiGraph to a torch_geometric.data.Data instance.

Parameters
  • G (easygraph.Graph or easygraph.DiGraph) – A easygraph graph.

  • group_node_attrs (List[str] or all, optional) – The node attributes to be concatenated and added to data.x. (default: None)

  • group_edge_attrs (List[str] or all, optional) – The edge attributes to be concatenated and added to data.edge_attr. (default: None)

Note

All group_node_attrs and group_edge_attrs values must be numeric.

Examples

>>> import torch_geometric as pyg
>>> pyg_to_networkx = pyg.utils.convert.to_networkx  # type: ignore
>>> networkx_to_pyg = pyg.utils.convert.from_networkx  # type: ignore
>>> Data = pyg.data.Data  # type: ignore
>>> edge_index = torch.tensor([
...     [0, 1, 1, 2, 2, 3],
...     [1, 0, 2, 1, 3, 2],
... ])
>>> data = Data(edge_index=edge_index, num_nodes=4)
>>> g = pyg_to_networkx(data)
>>> # A `Data` object is returned
>>> to_pyg(g)
Data(edge_index=[2, 6], num_nodes=4)

easygraph.exception module

Exceptions

Base exceptions and errors for EasyGraph.

exception easygraph.exception.EasyGraphAlgorithmError[source]

Bases: EasyGraphException

Exception for unexpected termination of algorithms.

exception easygraph.exception.EasyGraphError[source]

Bases: EasyGraphException

Exception for a serious error in EasyGraph

exception easygraph.exception.EasyGraphException[source]

Bases: Exception

Base class for exceptions in EasyGraph.

exception easygraph.exception.EasyGraphNoCycle[source]

Bases: EasyGraphUnfeasible

Exception for algorithms that should return a cycle when running on graphs where such a cycle does not exist.

exception easygraph.exception.EasyGraphNoPath[source]

Bases: EasyGraphUnfeasible

Exception for algorithms that should return a path when running on graphs where such a path does not exist.

exception easygraph.exception.EasyGraphNotImplemented[source]

Bases: EasyGraphException

Exception raised by algorithms not implemented for a type of graph.

exception easygraph.exception.EasyGraphPointlessConcept[source]

Bases: EasyGraphException

Raised when a null graph is provided as input to an algorithm that cannot use it.

The null graph is sometimes considered a pointless concept 1, thus the name of the exception.

References

1

Harary, F. and Read, R. “Is the Null Graph a Pointless Concept?” In Graphs and Combinatorics Conference, George Washington University. New York: Springer-Verlag, 1973.

exception easygraph.exception.EasyGraphUnbounded[source]

Bases: EasyGraphAlgorithmError

Exception raised by algorithms trying to solve a maximization or a minimization problem instance that is unbounded.

exception easygraph.exception.EasyGraphUnfeasible[source]

Bases: EasyGraphAlgorithmError

Exception raised by algorithms trying to solve a problem instance that has no feasible solution.

exception easygraph.exception.HasACycle[source]

Bases: EasyGraphException

Raised if a graph has a cycle when an algorithm expects that it will have no cycles.

exception easygraph.exception.NodeNotFound[source]

Bases: EasyGraphException

Exception raised if requested node is not present in the graph

Module contents