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).
- 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()
.
- 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:
input (
ndarray
) – A matrix where rows are cells and dimensions are columns. This is usually the principal components matrix fromrun_pca()
.options (
BuildNeighborIndexOptions
) – Optional parameters.
- Return type:
- 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).
- 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()
.- get(i)[source]#
- Parameters:
i (
int
) – Index of the cell of interest.- Return type:
- 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.
- 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 callingunserialize()
.- Return type:
- 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 ofserialize()
.- Return type:
- 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:
idx (
NeighborIndex
) – The nearest neighbor search index, usually built bybuild_neighbor_index()
.k (
int
) – Number of neighbors to find for each cell.options (
FindNearestNeighborsOptions
) – Optional parameters.
- Raises:
TypeError – If
idx
is not a nearest neighbor index.- Return type:
- Returns:
Object containing the
k
nearest neighbors for each cell.