pyrdf2vec.samplers.sampler module

class pyrdf2vec.samplers.sampler.Sampler(inverse=False, split=False)

Bases: abc.ABC

Base class of the sampling strategies.

_is_support_remote

True if the sampling strategy can be used with a remote Knowledge Graph, False Otherwise Defaults to False.

_random_state

The random state to use to keep random determinism with the sampling strategy. Defaults to None.

_vertices_deg

The degree of the vertices. Defaults to {}.

_visited

Tags vertices that appear at the max depth or of which all their children are tagged. Defaults to set.

inverse

True if the inverse algorithm must be used, False otherwise. Defaults to False.

split

True if the split algorithm must be used, False otherwise. Defaults to False.

abstract fit(kg)

Fits the sampling strategy.

Parameters

kg (KG) – The Knowledge Graph.

Raises

SamplerNotSupported – If there is an attempt to use an invalid sampling strategy to a remote Knowledge Graph.

Return type

None

abstract get_weight(hop)

Gets the weight of a hop in the Knowledge Graph.

Parameters

hop (Tuple[Any, Any]) – The hop of a vertex in a (predicate, object) form to get the weight.

Returns

The weight of a given hop.

Raises

NotImplementedError – If this method is called, without having provided an implementation.

get_weights(hops)

Gets the weights of the provided hops.

Parameters

hops (List[Tuple[Any, Any]]) – The hops to get the weights.

Return type

Optional[List[float]]

Returns

The weights to the edge of the Knowledge Graph.

property random_state: Optional[int]

Gets the random state.

Return type

Optional[int]

Returns

The random state.

sample_hop(kg, walk, is_last_hop, is_reverse=False)

Samples an unvisited random hop in the (predicate, object) form, according to the weight of hops for a given walk.

Parameters
  • kg (KG) – The Knowledge Graph.

  • walk (Tuple[Any, ...]) – The walk with one or several vertices.

  • is_last_hop (bool) – True if the next hop to be visited is the last one for the desired depth, False otherwise.

  • is_reverse (bool) – True to get the parent neighbors instead of the child neighbors, False otherwise. Defaults to False.

Return type

Optional[Tuple[Any, Any]]

Returns

An unvisited hop in the (predicate, object) form.

property visited: Set[Tuple[Tuple[Any, Any], int]]

Gets the tagged vertices that appear at the max depth or of which all their children are tagged.

Return type

Set[Tuple[Tuple[Any, Any], int]]

Returns

The tagged vertices.

exception pyrdf2vec.samplers.sampler.SamplerNotSupported

Bases: Exception

Base exception class for the lack of support of a sampling strategy for the extraction of walks via a SPARQL endpoint server.