swh.alter.subgraph module#

class swh.alter.subgraph.Subgraph(*args, **kwargs)[source]#

Bases: Graph

A class to hold a small subset of the Software Heritage graph

Each vertex corresponds to a single SWHID and vice versa. The graph is directed and each edge represents a reference from an object in Software Heritage storage to another.

This is backed by the igraph library for convenience.

This class is intended to be subclassed to implement the more specific behaviors needed for the different stage of the object removal algorithm.

See also igraph.Graph constructor

classmethod copy(subgraph)[source]#

Create a new instance by copying vertices, edges and respective attributes from the given subgraph.

default_vertex_attributes: Dict[str, Any] = {}#

Vertex will get the following attributes on creation unless specified otherwise.

add_vertex(name: str, **kwargs) Vertex[source]#

Add or update a vertex.

A vertex with the given name will be created if it does not exist already.

Attributes for the vertex will be set to the one given as keyword arguments.

Returns:

a Vertex object corresponding to the added or updated vertex

add_swhids(swhids: Iterable[str]) Dict[str, int][source]#

Add a set of swhids to the subgraph.

Parameters:

swhids – a Set of SWHIDs.

Returns: a mapping between added SWHID strings and the corresponding vertex index

add_swhid(object_or_swhid, **kwargs) Vertex[source]#

Add or update a vertex for the given SWHID or object.

This is a convenience method to add vertex from either CoreSWHID, ExtendedSWHID, or any objects implementing a swhid() method returning one of those.

swhids() List[ExtendedSWHID][source]#

Returns all SWHID in this subgraph

add_edge(src: Vertex, dst: Vertex, skip_duplicates=False, **kwargs)[source]#

Add an edge with the given attributes.

When trying to add an edge that already exists: - if skip_duplicates is set to True, nothing will be done, - otherwise (the default), an exception will be raised.

Raises:

ValueError if the given edge already exists and skip_duplicates is False

select_ordered(*args, **kwargs) List[Vertex][source]#

Get vertices ordered by object type from origins to contents

dot_node_attributes(v: Vertex) List[str][source]#

Get a list of attributes in DOT format for the given vertex.

The default implementation defines a label with a formatted SWHID. This method is called by write_dot() and is meant to be subclassed to produce extra labels to highlight certain graph aspects.

write_dot(f: TextIO) None[source]#

Write a representation of this subgraph in DOT format to f.

The result can be processed using the dot command-line utility provided by the Graphviz package.