Example #1
0
    def fit(self, data: np.ndarray, sight_k: int = None) -> Any:
        """Fits the model
        data: np.ndarray (samples, features)
            np
        sight_k: int
            the farthest point that a node is allowed to connect to when its closest neighbours are not allowed
        """
        self.data = data
        if sight_k is not None:
            self.sight_k = sight_k
        logging.debug(
            f"First search the {self.sight_k} nearest neighbours for {self.n_samples}"
        )
        np.random.seed(13)
        if self.metric == "correlation":
            self._nn = _NearestNeighbors(n_neighbors=self.sight_k + 1,
                                         metric=self.metric,
                                         p=self.minkowski_p,
                                         n_jobs=self.n_jobs,
                                         algorithm="brute")
            self._nn.fit(self.data)
        elif self.metric == "js":
            self._nn = NNDescent(data=self.data,
                                 metric=jensen_shannon_distance)
        else:
            self._nn = _NearestNeighbors(n_neighbors=self.sight_k + 1,
                                         metric=self.metric,
                                         p=self.minkowski_p,
                                         n_jobs=self.n_jobs,
                                         leaf_size=30)
            self._nn.fit(self.data)

        # call this to calculate bknn
        self.kneighbors_graph(mode='distance')
        return self
Example #2
0
    def surface_normals(S, number_of_neighbors):

        nbrs = _NearestNeighbors(n_neighbors=number_of_neighbors,
                                 algorithm='kd_tree').fit(S)
        distances, indices = nbrs.kneighbors(S)

        pca = _PCA()
        normals_S = []  #np.zeros(2)
        principle_axes = []  #np.zeros([2,2])
        surface_region = _np.zeros([1, S.shape[1]])

        for i in range(indices.shape[0]):

            for j in indices[i]:
                surface_region = _np.append(surface_region, S[j])

            surface_region = _np.reshape(surface_region, (-1, S.shape[1]))
            surface_region = _np.delete(surface_region, 0, 0)

            pca_region = pca.fit(surface_region)

            principle_axes.append(pca_region.components_)
            #principle_axes = _np.append(principle_axes,pca_region.components_)
            #normals_S = np.append(normals_S,pca_region.components_[1])
            normals_S.append(pca_region.components_[1])

        #normals_S = np.reshape(normals_S,(-1,2))
        #principle_axes = np.reshape(principle_axes,(-1,2,2))

        #return [np.delete(normals_S,0,0),principle_axes,distances]
        return normals_S, principle_axes, distances
Example #3
0
    def _init_estimator(self, sklearn_metric):
        """Initialize the sklearn nearest neighbors estimator.

        Args:
            sklearn_metric: (pyfunc or 'precomputed'): Metric compatible with
                sklearn API or matrix (n_samples, n_samples) with precomputed
                distances.

        Returns:
            Sklearn K Neighbors estimator initialized.

        """
        from sklearn.neighbors import NearestNeighbors as _NearestNeighbors

        return _NearestNeighbors(n_neighbors=self.n_neighbors,
                                 radius=self.radius,
                                 algorithm=self.algorithm,
                                 leaf_size=self.leaf_size,
                                 metric=sklearn_metric,
                                 metric_params=self.metric_params,
                                 n_jobs=self.n_jobs)
Example #4
0
def matching(reference, read, number_of_neighbors):
    
    nbrs = _NearestNeighbors(n_neighbors=number_of_neighbors, algorithm='ball_tree').fit(reference)
    distances, indices = nbrs.kneighbors(read)
    
    matched_points = []
    
    for i,ref_point in enumerate(indices):
        
        single_point_matches = []
        
        for j,k in enumerate(ref_point):
            x,y = reference[k]
            single_point_matches.append([x,y])
            
        #single_matched_point = np.reshape(single_matched_point,(-1,2))
        #single_matched_point = np.delete(single_matched_point,0,0)
        matched_points.append(single_point_matches)
        
        #for j,save_point in enumerate(single_matched_point):
            #matched_points[j,:,i] = save_point 
            
    return matched_points,distances,indices