scranpy.nearest_neighbors package#

Submodules#

scranpy.nearest_neighbors.build_neighbor_index module#

class scranpy.nearest_neighbors.build_neighbor_index.BuildNeighborIndexOptions(approximate=True)[source]#

Bases: object

Optional arguments for build_neighbor_index().

approximate#

Whether to build an index for an approximate neighbor search. This sacrifices some accuracy for speed.

Defaults to True.

__annotations__ = {'approximate': <class 'bool'>}#
__dataclass_fields__ = {'approximate': Field(name='approximate',type=<class 'bool'>,default=True,default_factory=<dataclasses._MISSING_TYPE object>,init=True,repr=True,hash=None,compare=True,metadata=mappingproxy({}),_field_type=_FIELD)}#
__dataclass_params__ = _DataclassParams(init=True,repr=True,eq=True,order=False,unsafe_hash=False,frozen=False)#
__eq__(other)#

Return self==value.

__hash__ = None#
__repr__()#

Return repr(self).

approximate: bool = True#
class scranpy.nearest_neighbors.build_neighbor_index.NeighborIndex(ptr)[source]#

Bases: object

The nearest neighbor search index.

This should not be manually constructed but should be created by build_neighbor_index().

__del__()[source]#
num_cells()[source]#
Return type:

int

Returns:

Number of cells in this index.

num_dimensions()[source]#
Return type:

int

Returns:

Number of dimensions in this index.

property ptr: int#

Returns: Pointer to the search index in C++.

scranpy.nearest_neighbors.build_neighbor_index.build_neighbor_index(input, options=BuildNeighborIndexOptions(approximate=True))[source]#

Build a search index for finding nearest neighbors between cells, for input into functions like find_nearest_neighbors().

Parameters:
Return type:

NeighborIndex

Returns:

Nearest neighbor search index.

scranpy.nearest_neighbors.find_nearest_neighbors module#

class scranpy.nearest_neighbors.find_nearest_neighbors.FindNearestNeighborsOptions(num_threads=1)[source]#

Bases: object

Optional arguments for find_nearest_neighbors().

num_threads#

Number of threads to use. Defaults to 1.

__annotations__ = {'num_threads': <class 'int'>}#
__dataclass_fields__ = {'num_threads': Field(name='num_threads',type=<class 'int'>,default=1,default_factory=<dataclasses._MISSING_TYPE object>,init=True,repr=True,hash=None,compare=True,metadata=mappingproxy({}),_field_type=_FIELD)}#
__dataclass_params__ = _DataclassParams(init=True,repr=True,eq=True,order=False,unsafe_hash=False,frozen=False)#
__eq__(other)#

Return self==value.

__hash__ = None#
__repr__()#

Return repr(self).

num_threads: int = 1#
class scranpy.nearest_neighbors.find_nearest_neighbors.NeighborResults(ptr)[source]#

Bases: object

Nearest neighbor search results.

This should not be constructed manually but instead should be created by find_nearest_neighbors().

__del__()[source]#
get(i)[source]#
Parameters:

i (int) – Index of the cell of interest.

Return type:

SingleNeighborResults

Returns:

A tuple with indices and distances to the nearest neighbors for cell i. Neighbors are guaranteed to be sorted in order of increasing distance.

num_cells()[source]#
Return type:

int

Returns:

Number of cells in this object.

num_neighbors()[source]#
Return type:

int

Returns:

Number of neighbors used to build this object.

property ptr: int#

Returns: Pointer to the results object in C++.

serialize()[source]#

Serialize nearest neighbors for all cells, typically to save or transfer to a new process. This can be used to construct a new NeighborResults object by calling unserialize().

Return type:

SerializedNeighborResults

Returns:

A tuple with indices and distances, to be passed to unserialize.

classmethod unserialize(content)[source]#

Initialize an instance of this class from serialized nearest neighbor results.

Parameters:

content (SerializedNeighborResults) – Result of serialize().

Return type:

NeighborResults

Returns:

Instance of this class, constructed from the data in content.

class scranpy.nearest_neighbors.find_nearest_neighbors.SerializedNeighborResults(index, distance)#

Bases: tuple

Named tuple of serialized results from the nearest neighbor search.

index:

Row-major matrix containing 0-based indices of the neighbor neighbors for each cell. Each row is a cell and each column is a neighbor, ordered by increasing distance.

distance:

Row-major matrix containing distances to the nearest neighbors for each cell. Each row is a cell and each column is a neighbor, ordered by increasing distance.

__getnewargs__()#

Return self as a plain tuple. Used by copy and pickle.

static __new__(_cls, index, distance)#

Create new instance of SerializedNeighborResults(index, distance)

__repr__()#

Return a nicely formatted representation string

__slots__ = ()#
distance#

Alias for field number 1

index#

Alias for field number 0

class scranpy.nearest_neighbors.find_nearest_neighbors.SingleNeighborResults(index, distance)#

Bases: tuple

Named tuple of nearest neighbors for a single cell.

index:

Array containing 0-based indices of a cell’s neighbor neighbors, ordered by increasing distance.

distance:

Array containing distances to a cell’s nearest neighbors, ordered by increasing distance.

__getnewargs__()#

Return self as a plain tuple. Used by copy and pickle.

static __new__(_cls, index, distance)#

Create new instance of SingleNeighborResults(index, distance)

__repr__()#

Return a nicely formatted representation string

__slots__ = ()#
distance#

Alias for field number 1

index#

Alias for field number 0

scranpy.nearest_neighbors.find_nearest_neighbors.find_nearest_neighbors(idx, k, options=FindNearestNeighborsOptions(num_threads=1))[source]#

Find the nearest neighbors for each cell.

Parameters:
Raises:

TypeError – If idx is not a nearest neighbor index.

Return type:

NeighborResults

Returns:

Object containing the k nearest neighbors for each cell.

Module contents#