Example #1
0
    def Voronoi_diagram(self,seeds,samples):
        """
        Defines the graph as the Voronoi diagram (VD)
        that links the seeds.
        The VD is defined using the sample points.

        Parameters
        ----------
        seeds: array of shape (self.V,dim)
        samples: array of shape (nsamples,dim)
        
        Note
        ----
        by default, the weights are a Gaussian function of the distance
        The implementation is not optimal
        """
        # checks
        if seeds.shape[0]!=self.V:
            raise ValueError,"The numberof seeds is not as expected"
        if np.size(seeds) == self.V:
            seeds = np.reshape(seeds,(np.size(seeds),1))
        if np.size(samples) == samples.shape[0]:
            samples = np.reshape(samples,(np.size(samples),1))
        if seeds.shape[1]!=samples.shape[1]:
            raise ValueError,"The seeds and samples do not belong \
                                  to the same space"

        #1. define the graph knn(samples,seeds,2)
        i,j,d = graph_cross_knn(samples,seeds,2)
        
        #2. put all the pairs i the target graph
        Ns = np.shape(samples)[0]
        self.E = Ns
        self.edges = np.array([j[2*np.arange(Ns)],j[2*np.arange(Ns)+1]]).T
        self.weights = np.ones(self.E)
                
        #3. eliminate the redundancies and set the weights
        self.cut_redundancies()
        self.symmeterize()
        self.set_gaussian(seeds)
Example #2
0
    def cross_knn(self,X,Y,k=1):
        """
        set the graph to be the k-nearest-neighbours graph of from X to Y

        Parameters
        ----------
        X,Y arrays of shape (self.V) or (self.V,p)
            and (self.W) or (self.W,p) respectively
            where p = dimension of the features
        k=1, int  is the number of neighbours considered
        
        Returns
        -------
        self.E, int the number of edges of the resulting graph
        
        Note
        ----
        It is assumed that the features are embedded in a
           (locally) Euclidian space 
        for the sake of speed it is advisable to give 
            PCA-preprocessed matrices X and Y.
        """
        self.check_feature_matrices(X,Y)
        try:
            k=int(k)
        except :
            "k cannot be cast to an int"
        if np.isnan(k):
            raise ValueError, 'k is nan'
        if np.isinf(k):
            raise ValueError, 'k is inf'
        i,j,d = graph_cross_knn(X,Y,k)
        self.E = np.size(i)
        self.edges = np.zeros((self.E,2),np.int)
        self.edges[:,0] = i
        self.edges[:,1] = j
        self.weights = np.array(d)
        return self.E