spatiomic.neighbor ================== .. py:module:: spatiomic.neighbor .. autoapi-nested-parse:: Exposes neighborhood graph functions. Classes ------- .. autoapisummary:: spatiomic.neighbor.knn_graph spatiomic.neighbor.snn_graph Package Contents ---------------- .. py:class:: knn_graph A class that exposes a static method for k-nearest neighbor graph construction. .. py:method:: create(data, batch = None, neighbor_count = 20, distance_metric = 'euclidean', method = 'simple', accuracy = 'accurate', distance_max = None, job_count = -1, use_gpu = True) :classmethod: Construct a k-nearest neighbor graph of the data. :param data: A channel-last array of the data points to be used for graph construction. :type data: Union[NDArray, Som] :param batch: The batch labels for the data points. Defaults to None. :type batch: Optional[Union[NDArray, List[int], List[str]]], optional :param neighbor_count: The neighbor count for the neighborhood graph. Defaults to 20. :type neighbor_count: int, optional :param distance_metric: The distance metric to be used for nearest neighbor calculation. Defaults to "euclidean". :type distance_metric: Literal["euclidean", "manhattan", "correlation", "cosine"], optional :param method: The method for nearest neighbor calculation. Defaults to "simple". :type method: Literal["simple", "batch_balanced"], optional :param accuracy: The accuracy of the nearest neighbor calculation. Defaults to "accurate". :type accuracy: Literal["fast", "accurate"], optional :param distance_max: The maximum distance for nearest neighbor calculation. Currently only supported for the `simple` method. Defaults to None. :type distance_max: Optional[float], optional :param job_count: Parallelization core count when method is `simple`. Defaults to -1. :type job_count: int, optional :param use_gpu: Whether to use the GPU for nearest neighbor calculation if possible. Defaults to True. :type use_gpu: bool, optional :raises ValueError: Raised when the `distance_max` parameter is used with the `batch_balanced` method. :raises NotImplementedError: Raised when the specified neighborhood identification method has not been implemented. :returns: The neighborhood graph. :rtype: Graph .. py:method:: get_edges(neighbor_idx) :staticmethod: Create an edgelist from a neighbor index array. :param neighbor_idx: The neighbor index array. :type neighbor_idx: NDArray :returns: The edgelist. :rtype: List .. py:class:: snn_graph A class that exposes a static method for shared nearest neighbor graph construction. .. py:method:: create(knn_graph, shared_count_threshold = 1, fix_lonely_nodes = True, fix_lonely_nodes_method = 'distance', distance_data = None, distance_metric = 'euclidean', accuracy = 'accurate', job_count = -1, use_gpu = True) :classmethod: Construct a shared nearest neighbor graph based on a k-nearest neighbor graph. The shared nearest neighbor graph is an undirected graph where two nodes are connected if they have at least `shared_count_threshold` neighbors in common. If `fix_lonely_nodes` is True, then the lonely nodes (nodes that don't have any neighbors in the k-nearest neighbor graph) are connected to their nearest neighbor. The method for connecting lonely nodes can be specified with `fix_lonely_nodes_method`. If `fix_lonely_nodes_method` is `random`, then the lonely nodes are connected to a random neighbor. If `fix_lonely_nodes_method` is `distance`, then the lonely nodes are connected to their nearest neighbor based on the distance between them. .. warning:: This method is not optimized for batch-balanced nearest neighbor calculation. :param knn_graph: A k-nearest neighbor graph. :type knn_graph: Graph :param shared_count_threshold: The minimum number of shared neighbors for two nodes to be connected. Defaults to 1. :type shared_count_threshold: int, optional :param fix_lonely_nodes: Whether to connect lonely nodes to the nearest neighbor. Defaults to True. :type fix_lonely_nodes: bool, optional :param fix_lonely_nodes_method: The method to use for connecting lonely nodes. Defaults to "distance". :type fix_lonely_nodes_method: Literal["random", "distance"], optional :param distance_data: The channel-last array of the data points used for knn graph construction. Used for distance calculation if fix_lonely_nodes_method is `distance`. Defaults to None. :type distance_data: Union[NDArray, Som] :param distance_metric: The distance metric to be used for nearest neighbor calculation. Defaults to "euclidean". :type distance_metric: Literal["euclidean", "manhattan", "correlation", "cosine"], optional :param accuracy: The accuracy of the nearest neighbor calculation. Used only when method is `simple` or `batch_balanced`. Defaults to "accurate". :type accuracy: Literal["fast", "accurate"], optional :param job_count: Parallelization core count when method is `simple`. Defaults to -1. :type job_count: int, optional :param use_gpu: Whether to use the GPU for nearest neighbor calculation if possible. Defaults to True. :type use_gpu: bool, optional :returns: The shared nearest neighbor graph. :rtype: Graph :raises ValueError: Raised when the distance data is not provided when using the distance method. :raises NotImplementedError: Raised when the specified method for connecting lonely nodes is not supported. .. py:method:: get_shared_neighbors(nodes, neighbors, shared_count_threshold = 1) :staticmethod: Get the shared neighbors between nodes. :param nodes: The nodes. :type nodes: List[int] :param neighbors: The neighbors of each node. :type neighbors: List[set] :param shared_count_threshold: The minimum number of shared neighbors for two nodes to be linked. Defaults to 1. :type shared_count_threshold: int, optional :returns: A list of edges. :rtype: List[tuple]