ScipySparseAnchorSearcher

class ScipySparseAnchorSearcher(max_iter=5)[source]

Bases: AnchorSearcher

Find closest anchors using scipy.sparse.

Initialize the searcher.

Parameters

max_iter (int) – the maximum number of hops to consider

Methods Summary

__call__(edge_index, anchors, k[, num_entities])

Find the \(k\) closest anchor nodes for each entity.

bfs(anchors, adjacency, max_iter, k)

Determine the candidate pool using breadth-first search.

create_adjacency(edge_index[, num_entities])

Create a sparse adjacency matrix from a given edge index.

iter_extra_repr()

Iterate over the components of the extra_repr().

select(pool, k)

Select \(k\) anchors from the given pools.

Methods Documentation

__call__(edge_index, anchors, k, num_entities=None)[source]

Find the \(k\) closest anchor nodes for each entity.

Parameters
  • edge_index (ndarray) – shape: (2, m) the edge index

  • anchors (ndarray) – shape: (a,) the selected anchor entity Ids

  • k (int) – the number of closest anchors to return

  • num_entities (Optional[int]) – the number of entities

Return type

ndarray

Returns

shape: (n, k), -1 <= res < a the Ids of the closest anchors

static bfs(anchors, adjacency, max_iter, k)[source]

Determine the candidate pool using breadth-first search.

Parameters
  • anchors (ndarray) – shape: (a,) the anchor node IDs

  • adjacency (spmatrix) – shape: (n, n) the adjacency matrix

  • max_iter (int) – the maximum number of hops to consider

  • k (int) – the minimum number of anchor nodes to reach

Return type

ndarray

Returns

shape: (n, a) a boolean array indicating whether anchor \(j\) is in the set of \(k\) closest anchors for node \(i\)

static create_adjacency(edge_index, num_entities=None)[source]

Create a sparse adjacency matrix from a given edge index.

Parameters
  • edge_index (ndarray) – shape: (2, m) the edge index

  • num_entities (Optional[int]) – the number of entities. Can be inferred from edge_index

Return type

spmatrix

Returns

shape: (n, n) a square sparse adjacency matrix

iter_extra_repr()[source]

Iterate over the components of the extra_repr().

This method is typically overridden. A common pattern would be

def iter_extra_repr(self) -> Iterable[str]:
    yield from super().iter_extra_repr()
    yield "<key1>=<value1>"
    yield "<key2>=<value2>"
Return type

Iterable[str]

Returns

an iterable over individual components of the extra_repr()

static select(pool, k)[source]

Select \(k\) anchors from the given pools.

Parameters
  • pool (ndarray) – shape: (n, a) the anchor candidates for each node (a binary array)

  • k (int) – the number of candidates to select

Return type

ndarray

Returns

shape: (n, k) the selected anchors. May contain -1 if there is an insufficient number of candidates