corneto.graph.Graph#
- class corneto.graph.Graph(default_edge_type=EdgeType.DIRECTED, **kwargs)#
Bases:
BaseGraphConcrete graph implementation supporting directed/undirected edges and hyperedges.
Allows parallel edges (multiple edges between same vertices) and hyperedges (edges connecting multiple vertices). Edges and vertices can have attributes.
Examples
>>> graph = corneto.Graph() >>> graph.add_edge(1, 2) >>> graph.plot()
- Parameters:
default_edge_type (EdgeType)
- __init__(default_edge_type=EdgeType.DIRECTED, **kwargs)#
Initialize Graph.
- Parameters:
default_edge_type (EdgeType) – Default type for new edges
**kwargs – Additional graph attributes
- Return type:
None
Methods
__init__([default_edge_type])Initialize Graph.
add_edge(source, target[, type, ...])Add edge to the graph.
add_edges(edges[, type])Add multiple edges to the graph.
add_vertex(v, **kwargs)Add vertex to the graph.
add_vertices(vertices, **kwargs)Add multiple vertices to the graph.
bfs(starting_vertices[, reverse, undirected])Perform breadth-first search (BFS) traversal.
copy()Create deep copy of graph.
edge_subgraph(edges)Create subgraph induced by a set of edges.
edges([vertices])Get edges in the graph.
extract_subgraph([vertices, edges])Extract subgraph induced by vertices and/or edges.
filter_graph([filter_vertex, filter_edge])Create filtered graph based on vertex and edge predicates.
from_cobra_model(model)Create graph from COBRA metabolic model.
from_dict(data)Create a graph from a dictionary representation.
from_json(json_str)Create a graph from a JSON string.
from_miom_model(model)Create graph from MIOM metabolic model.
Create graph from NetworkX graph.
from_sif(sif_file[, delimiter, has_header, ...])Create graph from Simple Interaction Format (SIF) file.
from_sif_tuples(tuples)Create graph from iterable of SIF tuples.
from_tuples(tuples)Alias for corneto.io.load_graph_from_sif_tuples for backwards compatibility.
from_vertex_incidence(A, vertex_ids, edge_ids)Create graph from vertex incidence matrix and labels.
get_attr_edge(index)Get attributes of an edge.
get_attr_edges([indexes])Get attributes of multiple edges.
get_attr_from_edges(attr[, default])Get specific attribute from all edges.
get_attr_vertex(v)Get attributes of a vertex.
get_attr_vertices([vertices])Get attributes of multiple vertices.
get_common_incident_edges(vertices)Get common incident edges for multiple vertices.
get_edge(index)Get edge at specified index.
get_edges(indexes)Get multiple edges by their indices.
get_edges_by_attr(key, value)Get edges by specific attribute value.
Get global graph attributes.
get_incident_edges(vertices)Get incident edges for multiple vertices.
get_vertex(index)Get vertex by its index.
get_vertex_incidence_matrix_as_lists([values])Get vertex incidence matrix as lists.
hash()Compute hash of the graph based on its content.
in_edges(vertices)Get incoming edges for vertices.
is_hypergraph()Check if the graph is a hypergraph.
load(filename[, compression])Load graph from a saved file.
load_json(filepath[, compression])Load graph from a JSON file.
neighbors(vertex)Get neighbors of a vertex (ignoring edge direction).
out_edges(vertices)Get outgoing edges for vertices.
plot(**kwargs)Plot the graph using Graphviz.
plot_values([vertex_values, edge_values, ...])predecessors(vertices)Get predecessor vertices for multiple vertices.
prune([source, target])Prune the graph to include only reachable vertices.
reachability_analysis(input_nodes, output_nodes)Perform reachability analysis from input nodes to output nodes.
reverse()Create new graph with all edge directions reversed.
save(filename[, compression])Save graph to file using pickle serialization.
save_json(filepath[, indent, compression])Save graph to a JSON file.
subgraph(vertices)Create subgraph induced by a set of vertices.
successors(vertices)Get successor vertices for multiple vertices.
to_dict()Convert graph to a JSON-serializable dictionary.
to_dot(**kwargs)Convert graph to DOT format.
to_graphviz(**kwargs)Convert graph to Graphviz representation.
to_json(**kwargs)Serialize graph to JSON string.
toposort()Perform topological sort on the graph using Kahn's algorithm.
vertex_incidence_matrix([values, sparse])Get vertex incidence matrix.
Attributes
EEdges in the graph.
VVertices in the graph.
neNumber of edges in the graph.
num_edgesNumber of edges in the graph.
num_verticesNumber of vertices in the graph.
nvNumber of vertices in the graph.
shapeShape of the graph (number of vertices, number of edges).
verticesVertices in the graph.
- get_edge(index)#
Get edge at specified index.
- get_graph_attributes()#
Get global graph attributes.
- Returns:
Attributes object containing graph-level properties
- Return type:
Attributes
- extract_subgraph(vertices=None, edges=None)#
Extract subgraph induced by vertices and/or edges.
- reverse()#
Create new graph with all edge directions reversed.
- Returns:
New Graph with reversed edges
- Return type:
- filter_graph(filter_vertex=None, filter_edge=None)#
Create filtered graph based on vertex and edge predicates.
- copy()#
Create deep copy of graph.
- Returns:
New Graph with copied structure and attributes
- Return type:
- static from_tuples(tuples)#
Alias for corneto.io.load_graph_from_sif_tuples for backwards compatibility.
- to_dict()#
Convert graph to a JSON-serializable dictionary.
- Returns:
Dictionary representation of the graph
- Return type:
- to_json(**kwargs)#
Serialize graph to JSON string.
- Parameters:
**kwargs – Additional arguments passed to json.dumps()
- Returns:
JSON string representation of the graph
- Return type:
- classmethod from_dict(data)#
Create a graph from a dictionary representation.
- classmethod from_json(json_str)#
Create a graph from a JSON string.
- static from_networkx(G)#
Create graph from NetworkX graph.
- Parameters:
G (NxGraph | NxDiGraph) – NetworkX graph instance
- Returns:
Graph instance with equivalent structure
- save_json(filepath, indent=None, compression=None)#
Save graph to a JSON file.
- Parameters:
- Raises:
ValueError – If filepath is empty
- Return type:
None
- classmethod load_json(filepath, compression='auto')#
Load graph from a JSON file.
- Parameters:
- Returns:
New Graph instance loaded from the file
- Raises:
FileNotFoundError – If the file doesn’t exist
- Return type:
- static from_vertex_incidence(A, vertex_ids, edge_ids)#
Create graph from vertex incidence matrix and labels.
- Parameters:
- Returns:
Graph instance constructed from incidence matrix
- Raises:
ValueError – If dimensions of inputs don’t match
- static from_sif(sif_file, delimiter='\t', has_header=False, discard_self_loops=True, column_order=[0, 1, 2])#
Create graph from Simple Interaction Format (SIF) file.
- Parameters:
- Returns:
New Graph loaded from SIF file
- static from_sif_tuples(tuples)#
Create graph from iterable of SIF tuples.
- static from_cobra_model(model)#
Create graph from COBRA metabolic model.
- Parameters:
model (CobraModel) – COBRApy model instance
- Returns:
New Graph representing the metabolic network
- static from_miom_model(model)#
Create graph from MIOM metabolic model.
- Parameters:
model – MIOM model instance or path to compressed model file
- Returns:
New Graph representing the metabolic network