Esempio n. 1
0
    def get_distance_matrix(self, neighborhood_radius = None, copy = True):
        """                 
        Parameters
        ----------
        neighborhood_radius : scalar, passed to distance_matrix. Value such that all
            distances beyond neighborhood_radius are considered infinite. 
            if this value is not passed the value of self.neighborhood_radius is used
        
        copy : boolean, whether to return a copied version of the distance matrix
        
        Returns
        -------
        self.distance_matrix : sparse Ndarray (N_obs, N_obs). Non explicit 0.0 values
            (e.g. diagonal) should be considered Infinite. 
        
        """
        if self.input_type == 'affinity':
            raise ValueError("input_method was passed as affinity. "
                            "Distance matrix cannot be computed.")

        
        if self.distance_matrix is None:
            # if there's no existing distance matrix we make one
            if ((neighborhood_radius is not None) and (neighborhood_radius != self.neighborhood_radius)):
                # a different radius was passed than self.neighborhood_radius
                self.neighborhood_radius = neighborhood_radius
            self.distance_matrix = distance_matrix(self.X, method = self.distance_method,
                                                    flindex = self.flindex, 
                                                    radius = self.neighborhood_radius,
                                                    cyindex = self.cyindex)
        else:
            # if there is an existing matrix we have to see if we need to overwrite
            if ((neighborhood_radius is not None) and (neighborhood_radius != self.neighborhood_radius)):
                # if there's a new radius we need to re-calculate
                if self.input_type == 'distance':
                    # but if we were passed distance this is impossible
                    raise ValueError("input_method was passed as distance."
                                    "requested radius not equal to self.neighborhood_radius."
                                    "distance matrix cannot be re-calculated.")
                else:
                    # if we were passed data then we need to re-calculate
                    self.neighborhood_radius = neighborhood_radius
                    self.distance_matrix = distance_matrix(self.X, method = self.distance_method,
                                                            flindex = self.flindex, 
                                                            radius = self.neighborhood_radius,
                                                            cyindex = self.cyindex)            
        
        if copy:
            return self.distance_matrix.copy()
        else:
            return self.distance_matrix
Esempio n. 2
0
def test_all_methods_close(almost_equal_decimals = 5):
    flindex = None
    try:
        import pyflann as pyf
    except ImportError:
        try: 
            # use "export FLANN_ROOT=<FLANN_ROOT>"to set enviromental variable 
            path_to_flann = os.environ['FLANN_ROOT'] + '/src/python'
            sys.path.insert(0, path_to_flann)
            import pyflann as pyf
        except ImportError:
            warnings.warn("pyflann not installed. Will not test pyflann method")
    t1 = time.clock()
    D1 = distance_matrix(X, method = 'cython') 
    print "cython version:",time.clock() - t1
    
    flindex = pyf.FLANN()
    flparams = flindex.build_index(X, algorithm = 'kmeans', target_precision = 0.9)
    t2 = time.clock()
    D2 = distance_matrix(X, method = 'pyflann', flindex = flindex)
    print "pyflann version:",time.clock() - t2
    
    t3 = time.clock()
    D3 = distance_matrix(X, method = 'cyflann')
    print "cyflann version:",time.clock() - t3
    
    t4 = time.clock()
    D4 = distance_matrix(X, method = 'brute')
    print "brute version:",time.clock() - t4
    
    D5 = distance_matrix(X)

    assert_array_almost_equal(D1.todense(), D, almost_equal_decimals)
    assert_array_almost_equal(D2.todense(), D, almost_equal_decimals)
    assert_array_almost_equal(D3.todense(), D, almost_equal_decimals)
    assert_array_almost_equal(D4.todense(), D, almost_equal_decimals)
    assert_array_almost_equal(D5.todense(), D, almost_equal_decimals)
Esempio n. 3
0
def test_laplacian_create_A_sparse():
    """
    Test that A_sparse is the same as A_dense for a small A matrix
    """
    rad = 2.
    n_samples = 6
    X = np.arange(n_samples)
    X = X[ :,np.newaxis]
    X = np.concatenate((X,np.zeros((n_samples,1),dtype=float)),axis=1)
    X = np.asarray( X, order="C" )
    test_dist_matrix = distance_matrix( X, radius = rad )

    A_dense = affinity_matrix( test_dist_matrix.toarray(), rad, symmetrize = False )
    A_sparse = affinity_matrix( sparse.csr_matrix( test_dist_matrix ), rad, symmetrize = False )
    A_spdense = A_sparse.toarray()
    A_spdense[ A_spdense == 0 ] = 1.

    print( 'A_dense')
    print( A_dense )
    print( 'A_sparse',  A_spdense )

    assert_array_equal( A_dense, A_spdense )
Esempio n. 4
0
def test_get_laplacian_matrix(almost_equal_decimals = 5):
    """ test different ways to call get_laplacian_matrix """
    # 1. (No existing laplacian, no passed type, no self.type)
    X = np.random.uniform(size = (10,10))
    dist_mat = distance_matrix(X, radius = 1)
    A = affinity_matrix(dist_mat, neighbors_radius = 1)
    Geometry = geom.Geometry(A, input_type = 'affinity')    
    ## Return default laplacian
    lapl = Geometry.get_laplacian_matrix()
    lapl2 = graph_laplacian(A)
    assert_array_almost_equal(lapl.todense(), lapl2.todense(),almost_equal_decimals)

    # 2. (No existing laplacian, no passed type, existing self.type)
    X = np.random.uniform(size = (10,10))
    dist_mat = distance_matrix(X, radius = 1)
    A = affinity_matrix(dist_mat, neighbors_radius = 1)
    for laplacian_type in laplacian_types:
        Geometry = geom.Geometry(A, input_type = 'affinity', laplacian_type = laplacian_type)    
        ## Return self.type laplacian
        lapl = Geometry.get_laplacian_matrix()
        lapl2 = graph_laplacian(A, normed = laplacian_type)
        assert_array_almost_equal(lapl.todense(), lapl2.todense(),almost_equal_decimals)

    # 3. (No existing laplacian, passed type, no self.type)
    X = np.random.uniform(size = (10,10))
    dist_mat = distance_matrix(X, radius = 1)
    A = affinity_matrix(dist_mat, neighbors_radius = 1)
    for laplacian_type in laplacian_types:
        Geometry = geom.Geometry(A, input_type = 'affinity')    
        ## Return passed type laplacian, set self.type to passed type
        lapl = Geometry.get_laplacian_matrix(laplacian_type = laplacian_type)
        lapl2 = graph_laplacian(A, normed = laplacian_type)
        assert_array_almost_equal(lapl.todense(), lapl2.todense(),almost_equal_decimals)
        assert(Geometry.laplacian_type == laplacian_type)

    # 4. (No existing laplacian, passed type equal to self.type)
    X = np.random.uniform(size = (10,10))
    dist_mat = distance_matrix(X, radius = 1)
    A = affinity_matrix(dist_mat, neighbors_radius = 1)
    for laplacian_type in laplacian_types:
        Geometry = geom.Geometry(A, input_type = 'affinity',laplacian_type = laplacian_type)    
        ## Return passed type laplacian
        lapl = Geometry.get_laplacian_matrix(laplacian_type = laplacian_type)
        lapl2 = graph_laplacian(A, normed = laplacian_type)
        assert_array_almost_equal(lapl.todense(), lapl2.todense(),almost_equal_decimals)
        assert(Geometry.laplacian_type == laplacian_type)

    # 5. (No existing laplacian, passed type not equal to self.type)
    X = np.random.uniform(size = (10,10))
    dist_mat = distance_matrix(X, radius = 1)
    A = affinity_matrix(dist_mat, neighbors_radius = 1)
    for laplacian_type in laplacian_types:
        if laplacian_type == 'geometric':
            existing_type = 'randomwalk'
        else:
            existing_type = 'geometric'
        Geometry = geom.Geometry(A, input_type = 'affinity', laplacian_type = existing_type)    
        ## Set type to passed type, return passed type laplacian
        lapl = Geometry.get_laplacian_matrix(laplacian_type = laplacian_type)
        lapl2 = graph_laplacian(A, normed = laplacian_type)
        assert_array_almost_equal(lapl.todense(), lapl2.todense(),almost_equal_decimals)
        assert(Geometry.laplacian_type == laplacian_type)
    
    # 6. (Existing laplacian, no passed type, no self.type)
    X = np.random.uniform(size = (10,10))
    dist_mat = distance_matrix(X, radius = 1)
    A = affinity_matrix(dist_mat, neighbors_radius = 1)
    Geometry = geom.Geometry(A, input_type = 'affinity')    
    lapl2 = graph_laplacian(A)
    Geometry.assign_laplacian_matrix(lapl2)
    ## Return existing laplacian
    lapl = Geometry.get_laplacian_matrix()
    assert_array_almost_equal(lapl.todense(), lapl2.todense(),almost_equal_decimals)

    # 7. (Existing laplacian, no passed type, existing self.type)
    X = np.random.uniform(size = (10,10))
    dist_mat = distance_matrix(X, radius = 1)
    A = affinity_matrix(dist_mat, neighbors_radius = 1)
    Geometry = geom.Geometry(A, input_type = 'affinity', laplacian_type = 'geometric')    
    lapl2 = graph_laplacian(A)
    Geometry.assign_laplacian_matrix(lapl2)
    ## Return existing laplacian
    lapl = Geometry.get_laplacian_matrix()
    assert_array_almost_equal(lapl.todense(), lapl2.todense(),almost_equal_decimals)

    # 8. (Existing laplacian, passed type, no self.type)
    X = np.random.uniform(size = (10,10))
    dist_mat = distance_matrix(X, radius = 1)
    A = affinity_matrix(dist_mat, neighbors_radius = 1)
    Geometry = geom.Geometry(A, input_type = 'affinity')    
    lapl2 = graph_laplacian(A)
    Geometry.assign_laplacian_matrix(lapl2)
    ## Calculate passed type, set self.type to passed type
    # this will warn that it will overwrite:
    with warnings.catch_warnings():
        warnings.simplefilter("ignore")
        lapl = Geometry.get_laplacian_matrix(laplacian_type = 'randomwalk')
    lapl2 = graph_laplacian(A, normed = 'randomwalk')
    assert_array_almost_equal(lapl.todense(), lapl2.todense(),almost_equal_decimals)
    
    # 9. (Existing laplacian, passed type equal to self.type)
    X = np.random.uniform(size = (10,10))
    dist_mat = distance_matrix(X, radius = 1)
    A = affinity_matrix(dist_mat, neighbors_radius = 1)
    for laplacian_type in laplacian_types:
        Geometry = geom.Geometry(A, input_type = 'affinity', laplacian_type = laplacian_type)    
        lapl2 = graph_laplacian(A, normed = laplacian_type)
        Geometry.assign_laplacian_matrix(lapl2, laplacian_type = laplacian_type)
        ## Return existing laplacian
        lapl = Geometry.get_laplacian_matrix(laplacian_type = laplacian_type)
        assert_array_almost_equal(lapl.todense(), lapl2.todense(),almost_equal_decimals)
        assert(Geometry.laplacian_type == laplacian_type)

    # 10. (Existing laplacian, passed type not equal to self.type)
    X = np.random.uniform(size = (10,10))
    dist_mat = distance_matrix(X, radius = 1)
    A = affinity_matrix(dist_mat, neighbors_radius = 1)
    for laplacian_type in laplacian_types:
        if laplacian_type == 'geometric':
            existing_type = 'randomwalk'
        else:
            existing_type = 'geometric'
        Geometry = geom.Geometry(A, input_type = 'affinity', laplacian_type = existing_type)    
        lapl2 = graph_laplacian(A, normed = existing_type)
        Geometry.assign_laplacian_matrix(lapl2, laplacian_type = existing_type)
        ## Calculate passed type, set self.type to passed type
        # this will warn that it will overwrite:
        with warnings.catch_warnings():
            warnings.simplefilter("ignore")
            lapl = Geometry.get_laplacian_matrix(laplacian_type = laplacian_type)
        lapl2 = graph_laplacian(A, normed = laplacian_type)
        assert_array_almost_equal(lapl.todense(), lapl2.todense(),almost_equal_decimals)
        assert(Geometry.laplacian_type == laplacian_type)
Esempio n. 5
0
def test_Geometry_distance(almost_equal_decimals = 5):
    geom = Geometry(X)
    D1 = geom.get_distance_matrix()
    D2 = distance_matrix(X)
    assert_array_almost_equal(D1.todense(), D2.todense(), almost_equal_decimals)
Esempio n. 6
0
def test_get_affinity_matrix(almost_equal_decimals=5):
    """ test different ways to call get_affinity_matrix """
    # 1. (No existing affinity, no passed radius, no self.radius, input_type = data)
    X = np.random.uniform(size = (10,10))
    dist_mat = distance_matrix(X, radius = 1/X.shape[1])
    A2 = affinity_matrix(dist_mat, neighbors_radius = 1/X.shape[1])
    Geometry = geom.Geometry(X, input_type = 'data')
    ## Return default Affinity
    A = Geometry.get_affinity_matrix()
    assert_array_almost_equal(A.todense(), A2.todense(), almost_equal_decimals)
    
    # 2. (No existing affinity, no passed radius, no self.radius, input_type = distance)
    X = np.random.uniform(size = (10,10))
    dist_mat = distance_matrix(X, radius = 1/X.shape[1])
    A2 = affinity_matrix(dist_mat, neighbors_radius = 1/X.shape[1])
    Geometry = geom.Geometry(dist_mat, input_type = 'distance')
    ## Return default
    A = Geometry.get_affinity_matrix()
    assert_array_almost_equal(A.todense(), A2.todense(), almost_equal_decimals)
    
    # 3. (No existing affinity, no passed radius, existing self.radius, input_type = data)
    X = np.random.uniform(size = (10,10))
    radius = 1
    dist_mat = distance_matrix(X, radius = radius)
    A2 = affinity_matrix(dist_mat, neighbors_radius = radius)
    Geometry = geom.Geometry(X, input_type = 'data', neighborhood_radius = radius,
                            affinity_radius = radius)
    ## Return with existing radius
    A = Geometry.get_affinity_matrix()
    assert_array_almost_equal(A.todense(), A2.todense(), almost_equal_decimals)
    
    # 4. (No existing affinity, no passed radius, existing self.radius, input_type = distance)
    X = np.random.uniform(size = (10,10))
    radius = 1
    dist_mat = distance_matrix(X, radius = radius)
    A2 = affinity_matrix(dist_mat, neighbors_radius = radius)
    Geometry = geom.Geometry(dist_mat, input_type = 'distance', neighborhood_radius = radius,
                            affinity_radius = radius)
    ## Return with existing radius
    A = Geometry.get_affinity_matrix()
    assert_array_almost_equal(A.todense(), A2.todense(), almost_equal_decimals)
    
    # 5. (No existing affinity, passed radius, no existing self.radius, input_type = data)
    X = np.random.uniform(size = (10,10))
    radius = 1
    dist_mat = distance_matrix(X, radius = radius)
    A2 = affinity_matrix(dist_mat, neighbors_radius = 2)
    Geometry = geom.Geometry(X, input_type = 'data', neighborhood_radius = radius)
    ## Return passed radius affinity, set radius to passed
    A = Geometry.get_affinity_matrix(affinity_radius=2)
    assert_array_almost_equal(A.todense(), A2.todense(), almost_equal_decimals)
    
    # 6. (No existing affinity, passed radius, no existing self.radius, input_type = distance)
    X = np.random.uniform(size = (10,10))
    radius = 1
    dist_mat = distance_matrix(X, radius = radius)
    A2 = affinity_matrix(dist_mat, neighbors_radius = 2)
    Geometry = geom.Geometry(dist_mat, input_type = 'distance', neighborhood_radius = radius)
    ## Return passed radius affinity, set radius to passed
    A = Geometry.get_affinity_matrix(affinity_radius=2)
    assert_array_almost_equal(A.todense(), A2.todense(), almost_equal_decimals)
    
    # 7. (No existing affinity, passed radius equal to existing self.radius, input_type = data)
    X = np.random.uniform(size = (10,10))
    radius = 1
    dist_mat = distance_matrix(X, radius = radius)
    A2 = affinity_matrix(dist_mat, neighbors_radius = radius)
    Geometry = geom.Geometry(X, input_type = 'data', neighborhood_radius = radius, 
                            affinity_radius = radius)
    ## Return self.radius affinity 
    A = Geometry.get_affinity_matrix(affinity_radius=radius)
    assert_array_almost_equal(A.todense(), A2.todense(), almost_equal_decimals)
    
    # 8. (No existing affinity, passed radius equal to existing self.radius, input_type = distance)
    X = np.random.uniform(size = (10,10))
    radius = 1
    dist_mat = distance_matrix(X, radius = radius)
    A2 = affinity_matrix(dist_mat, neighbors_radius = radius)
    Geometry = geom.Geometry(dist_mat, input_type = 'distance', neighborhood_radius = radius, 
                            affinity_radius = radius)
    ## Return self.radius affinity 
    A = Geometry.get_affinity_matrix(affinity_radius=radius)
    assert_array_almost_equal(A.todense(), A2.todense(), almost_equal_decimals)
    
    # 9. (No existing affinity, passed radius not equal to existing self.radius, input_type = data)
    X = np.random.uniform(size = (10,10))
    radius = 1
    radius2 = 2
    dist_mat = distance_matrix(X, radius = radius)
    A2 = affinity_matrix(dist_mat, neighbors_radius = radius2)
    Geometry = geom.Geometry(X, input_type = 'data', neighborhood_radius = radius, 
                            affinity_radius = radius)
    ## Return passed radius affinity, set radius to passed    
    A = Geometry.get_affinity_matrix(affinity_radius=radius2)
    assert_array_almost_equal(A.todense(), A2.todense(), almost_equal_decimals)
    assert(Geometry.affinity_radius == radius2)
    
    # 10. (No existing affinity, passed radius not equal to existing self.radius, input_type = distance)
    X = np.random.uniform(size = (10,10))
    radius = 1
    radius2 = 2
    dist_mat = distance_matrix(X, radius = radius)
    A2 = affinity_matrix(dist_mat, neighbors_radius = radius2)
    Geometry = geom.Geometry(dist_mat, input_type = 'distance', neighborhood_radius = radius, 
                            affinity_radius = radius)
    ## Return passed radius affinity, set radius to passed    
    A = Geometry.get_affinity_matrix(affinity_radius=radius2)
    assert_array_almost_equal(A.todense(), A2.todense(), almost_equal_decimals)
    assert(Geometry.affinity_radius == radius2)
    
    # 11. (Existing affinity, no passed radius, no self.radius, input_type = data)
    X = np.random.uniform(size = (10,10))
    radius = 1
    dist_mat = distance_matrix(X, radius = radius)
    A2 = affinity_matrix(dist_mat, neighbors_radius = radius)
    Geometry = geom.Geometry(X, input_type = 'data')
    Geometry.assign_affinity_matrix(A2)
    ## Return existing affinity
    A = Geometry.get_affinity_matrix()
    assert_array_almost_equal(A.todense(), A2.todense(), almost_equal_decimals)

    # 12. (Existing affinity, no passed radius, no self.radius, input_type = distance)
    X = np.random.uniform(size = (10,10))
    radius = 1
    dist_mat = distance_matrix(X, radius = radius)
    A2 = affinity_matrix(dist_mat, neighbors_radius = radius)
    Geometry = geom.Geometry(dist_mat, input_type = 'distance')
    Geometry.assign_affinity_matrix(A2)
    ## Return existing affinity
    A = Geometry.get_affinity_matrix()
    assert_array_almost_equal(A.todense(), A2.todense(), almost_equal_decimals)

    # 13. (Existing affinity, no passed radius, no self.radius, input_type = affinity)
    X = np.random.uniform(size = (10,10))
    radius = 1
    dist_mat = distance_matrix(X, radius = radius)
    A2 = affinity_matrix(dist_mat, neighbors_radius = radius)
    Geometry = geom.Geometry(A2, input_type = 'affinity')
    ### Return existing affinity
    A = Geometry.get_affinity_matrix()
    assert_array_almost_equal(A.todense(), A2.todense(), almost_equal_decimals)

    # 14. (Existing affinity, no passed radius, existing self.radius, input_type = data)
    X = np.random.uniform(size = (10,10))
    radius = 1
    dist_mat = distance_matrix(X, radius = radius)
    A2 = affinity_matrix(dist_mat, neighbors_radius = radius)
    Geometry = geom.Geometry(X, input_type = 'data', neighborhood_radius = radius,
                            affinity_radius = radius)
    Geometry.assign_affinity_matrix(A2)
    ### Return existing affinity
    A = Geometry.get_affinity_matrix()
    assert_array_almost_equal(A.todense(), A2.todense(), almost_equal_decimals)

    # 15. (Existing affinity, no passed radius, existing self.radius, input_type = distance)
    X = np.random.uniform(size = (10,10))
    radius = 1
    dist_mat = distance_matrix(X, radius = radius)
    A2 = affinity_matrix(dist_mat, neighbors_radius = radius)
    Geometry = geom.Geometry(dist_mat, input_type = 'distance', neighborhood_radius = radius,
                            affinity_radius = radius)
    Geometry.assign_affinity_matrix(A2)
    ### Return existing affinity
    A = Geometry.get_affinity_matrix()
    assert_array_almost_equal(A.todense(), A2.todense(), almost_equal_decimals)

    # 16. (Existing affinity, no passed radius, existing self.radius, input_type = affinity)
    X = np.random.uniform(size = (10,10))
    radius = 1
    dist_mat = distance_matrix(X, radius = radius)
    A2 = affinity_matrix(dist_mat, neighbors_radius = radius)
    Geometry = geom.Geometry(A2, input_type = 'affinity', neighborhood_radius = radius,
                            affinity_radius = radius)
    ## Return existing affinity
    A = Geometry.get_affinity_matrix()
    assert_array_almost_equal(A.todense(), A2.todense(), almost_equal_decimals)

    # 17. (Existing affinity, passed radius, no self.radius, input_type = data)
    X = np.random.uniform(size = (10,10))
    radius = 1
    dist_mat = distance_matrix(X, radius = radius)
    A2 = affinity_matrix(dist_mat, neighbors_radius = 2)
    Geometry = geom.Geometry(X, input_type = 'data', neighborhood_radius = radius)
    Geometry.assign_affinity_matrix(A2)
    ## Compute with passed radius, set self.radius to passed radius
    A2 = affinity_matrix(dist_mat, neighbors_radius = radius)
    A = Geometry.get_affinity_matrix(affinity_radius=radius)
    assert_array_almost_equal(A.todense(), A2.todense(), almost_equal_decimals)

    # 18. (Existing affinity, passed radius, no self.radius, input_type = distance)
    X = np.random.uniform(size = (10,10))
    radius = 1
    dist_mat = distance_matrix(X, radius = radius)
    A2 = affinity_matrix(dist_mat, neighbors_radius = 2)
    Geometry = geom.Geometry(dist_mat, input_type = 'distance', neighborhood_radius = radius)
    Geometry.assign_affinity_matrix(A2)
    ## Compute with passed radius, set self.radius to passed radius
    A2 = affinity_matrix(dist_mat, neighbors_radius = radius)
    A = Geometry.get_affinity_matrix(affinity_radius=radius)
    assert_array_almost_equal(A.todense(), A2.todense(), almost_equal_decimals)

    # 19. (Existing affinity, passed radius, no self.radius, input_type = affinity)
    X = np.random.uniform(size = (10,10))
    radius = 1
    dist_mat = distance_matrix(X, radius = radius)
    A2 = affinity_matrix(dist_mat, neighbors_radius = 2)
    Geometry = geom.Geometry(A2, input_type = 'affinity', neighborhood_radius = radius)
    ## Raise error, unknown existing radius, passed radius but input type affinity
    msg = ("Input_method was passed as affinity."
           "Requested radius was not equal to self.affinity_radius."
           "Affinity Matrix cannot be recalculated.")
    assert_raise_message(ValueError, msg, Geometry.get_affinity_matrix,
                        affinity_radius=radius)

    # 20. (Existing affinity, passed radius equal to self.radius, input_type = data)
    X = np.random.uniform(size = (10,10))
    radius = 1
    dist_mat = distance_matrix(X, radius = radius)
    A2 = affinity_matrix(dist_mat, neighbors_radius = radius)
    Geometry = geom.Geometry(X, input_type = 'data', neighborhood_radius = radius,
                            affinity_radius = radius)
    Geometry.assign_affinity_matrix(A2)
    ## Return existing affinity
    A = Geometry.get_affinity_matrix(affinity_radius=radius)
    assert_array_almost_equal(A.todense(), A2.todense(), almost_equal_decimals)

    # 21. (Existing affinity, passed radius equal to self.radius, input_type = distance)
    X = np.random.uniform(size = (10,10))
    radius = 1
    dist_mat = distance_matrix(X, radius = radius)
    A2 = affinity_matrix(dist_mat, neighbors_radius = radius)
    Geometry = geom.Geometry(dist_mat, input_type = 'distance', neighborhood_radius = radius,
                            affinity_radius = radius)
    Geometry.assign_affinity_matrix(A2)
    ## Return existing affinity
    A = Geometry.get_affinity_matrix(affinity_radius=radius)
    assert_array_almost_equal(A.todense(), A2.todense(), almost_equal_decimals)

    # 22. (Existing affinity, passed radius equal to self.radius, input_type = affinity)
    X = np.random.uniform(size = (10,10))
    radius = 1
    dist_mat = distance_matrix(X, radius = radius)
    A2 = affinity_matrix(dist_mat, neighbors_radius = radius)
    Geometry = geom.Geometry(A2, input_type = 'affinity', neighborhood_radius = radius,
                            affinity_radius = radius)
    ## Return existing affinity
    A = Geometry.get_affinity_matrix(affinity_radius=radius)
    assert_array_almost_equal(A.todense(), A2.todense(), almost_equal_decimals)

    # 23. (Existing affinity, passed radius not equal to self.radius, input_type = data)
    X = np.random.uniform(size = (10,10))
    radius = 1
    dist_mat = distance_matrix(X, radius = radius)
    A2 = affinity_matrix(dist_mat, neighbors_radius = radius)
    Geometry = geom.Geometry(X, input_type = 'data', neighborhood_radius = radius,
                            affinity_radius = radius)
    Geometry.assign_affinity_matrix(A2)
    ## Re-calculate with passed radius, set self.radius to passed radius
    A2 = affinity_matrix(dist_mat, neighbors_radius = 2)
    A = Geometry.get_affinity_matrix(affinity_radius=2)
    assert_array_almost_equal(A.todense(), A2.todense(), almost_equal_decimals)
    assert(Geometry.affinity_radius == 2)

    # 24. (Existing affinity, passed radius not equal to self.radius, input_type = distance)
    X = np.random.uniform(size = (10,10))
    radius = 1
    dist_mat = distance_matrix(X, radius = radius)
    A2 = affinity_matrix(dist_mat, neighbors_radius = radius)
    Geometry = geom.Geometry(dist_mat, input_type = 'distance', neighborhood_radius = radius,
                            affinity_radius = radius)
    Geometry.assign_affinity_matrix(A2)
    ## Re-calculate with passed radius, set self.radius to passed radius
    A2 = affinity_matrix(dist_mat, neighbors_radius=2)
    A = Geometry.get_affinity_matrix(affinity_radius=2)
    assert_array_almost_equal(A.todense(), A2.todense(), almost_equal_decimals)
    assert(Geometry.affinity_radius == 2)

    # 25. (Existing affinity, passed radius not equal to self.radius, input_type = affinity)
    X = np.random.uniform(size = (10,10))
    radius = 1
    dist_mat = distance_matrix(X, radius = radius)
    A2 = affinity_matrix(dist_mat, neighbors_radius = 2)
    Geometry = geom.Geometry(A2, input_type = 'affinity', neighborhood_radius = radius, 
                            affinity_radius = 2)
    ## Raise error, passed affinity re-calculateion not supported. 
    msg = ("Input_method was passed as affinity."
           "Requested radius was not equal to self.affinity_radius."
           "Affinity Matrix cannot be recalculated.")
    assert_raise_message(ValueError, msg, Geometry.get_affinity_matrix,
                        affinity_radius=radius)
Esempio n. 7
0
def test_get_distance_matrix(almost_equal_decimals = 5):
    """ test different ways to call get_distance_matrix """
    # 1. (Input type = Affinity)
    X = np.random.uniform(size = (10,10))
    Geometry = geom.Geometry(X, input_type = 'affinity')
    ## Raise error Affinity matrix passed
    msg = ( "input_method was passed as affinity. " "Distance matrix cannot be computed.")
    assert_raise_message(ValueError, msg, Geometry.get_distance_matrix)
        
    # 2. (No existing distance, no passed radius, no self.radius, input_type = Data)
    X = np.random.uniform(size = (10,10))
    Geometry = geom.Geometry(X, input_type = 'data')    
    ## Calculate with default radius
    distance_mat = Geometry.get_distance_matrix()
    distance_mat2 = distance_matrix(X, radius = 1/X.shape[1])
    assert_array_almost_equal(distance_mat.todense(), distance_mat2.todense(), almost_equal_decimals)
    
    # 3. (No existing distance, no passed radius, existing self.radius, input_type = Data)
    X = np.random.uniform(size = (10,10))
    Geometry = geom.Geometry(X, input_type = 'data', neighborhood_radius = 1)    
    ## Calculate with existing radius
    distance_mat = Geometry.get_distance_matrix()
    distance_mat2 = distance_matrix(X, radius = 1)
    assert_array_almost_equal(distance_mat.todense(), distance_mat2.todense(), almost_equal_decimals)
   
    # 4. (No existing distance, passed radius, no existing self.radius, input_type = Data)
    X = np.random.uniform(size = (10,10))
    Geometry = geom.Geometry(X, input_type = 'data')    
    ## Set current radius to passed, calcualte with passed radius
    distance_mat = Geometry.get_distance_matrix(neighborhood_radius = 1)
    distance_mat2 = distance_matrix(X, radius = 1)
    assert_array_almost_equal(distance_mat.todense(), distance_mat2.todense(), almost_equal_decimals)
    assert(Geometry.neighborhood_radius == 1)
    
    # 5. (No existing distance, passed radius equal to existing self.radius, input_type = Data)
    X = np.random.uniform(size = (10,10))
    Geometry = geom.Geometry(X, input_type = 'data', neighborhood_radius = 1)    
    ## Calculate with passed radius
    distance_mat = Geometry.get_distance_matrix(neighborhood_radius = 1)
    distance_mat2 = distance_matrix(X, radius = 1)
    assert_array_almost_equal(distance_mat.todense(), distance_mat2.todense(), almost_equal_decimals)
    
    # 6. (No existing distance, passed radius not equal to existing self.radius, input_type = Data)
    X = np.random.uniform(size = (10,10))
    Geometry = geom.Geometry(X, input_type = 'data', neighborhood_radius = 1)    
    ## Calculate with passed radius, set new self.radius
    distance_mat = Geometry.get_distance_matrix(neighborhood_radius = 2)
    distance_mat2 = distance_matrix(X, radius = 2)
    assert_array_almost_equal(distance_mat.todense(), distance_mat2.todense(), almost_equal_decimals)
    assert(Geometry.neighborhood_radius == 2)
    
    # 7. (Existing distance, no passed radius, no self.radius, input_type = data)
    X = np.random.uniform(size = (10,10))
    Geometry = geom.Geometry(X, input_type = 'data')    
    distance_mat2 = distance_matrix(X, radius = 1)
    Geometry.assign_distance_matrix(distance_mat2)
    ## Return existing distance matrix
    distance_mat = Geometry.get_distance_matrix()
    assert_array_almost_equal(distance_mat.todense(), distance_mat2.todense(), almost_equal_decimals)

    # 8. (Existing distance, no passed radius, no self.radius, input_type = distance)
    X = np.random.uniform(size = (10,10))
    distance_mat2 = distance_matrix(X, radius = 1)
    Geometry = geom.Geometry(distance_mat2, input_type = 'distance')    
    ## Return existing distance matrix
    distance_mat = Geometry.get_distance_matrix()
    assert_array_almost_equal(distance_mat.todense(), distance_mat2.todense(), almost_equal_decimals)

    # 9. (Existing distance, no passed radius, existing self.radius, input_type = data)
    X = np.random.uniform(size = (10,10))
    Geometry = geom.Geometry(X, input_type = 'data', neighborhood_radius = 1)    
    distance_mat2 = distance_matrix(X, radius = 1)
    Geometry.assign_distance_matrix(distance_mat2)
    ## Return existing distance Matrix
    distance_mat = Geometry.get_distance_matrix()
    assert_array_almost_equal(distance_mat.todense(), distance_mat2.todense(), almost_equal_decimals)

    # 10. (Existing distance, no passed radius, existing self.radius, input_type = distance)
    X = np.random.uniform(size = (10,10))
    distance_mat2 = distance_matrix(X, radius = 1)
    Geometry = geom.Geometry(distance_mat2, input_type = 'distance', neighborhood_radius = 1)    
    ## Return existing distance matrix 
    distance_mat = Geometry.get_distance_matrix()
    assert_array_almost_equal(distance_mat.todense(), distance_mat2.todense(), almost_equal_decimals)

    # 11. (Existing distance, passed radius, no existing self.radius, input_type = data)
    X = np.random.uniform(size = (10,10))
    distance_mat2 = distance_matrix(X, radius = 1)
    Geometry = geom.Geometry(X, input_type = 'data')    
    Geometry.assign_distance_matrix(distance_mat2)
    ## Re-calculate with passed radius, set self.radius to passed radius
    distance_mat = Geometry.get_distance_matrix(neighborhood_radius=3)
    distance_mat2 = distance_matrix(X, radius = 3)
    assert_array_almost_equal(distance_mat.todense(), distance_mat2.todense(), almost_equal_decimals)
    assert(Geometry.neighborhood_radius == 3)

    # 12. (Existing distance, passed radius, no existing self.radius, input_type = distance)
    X = np.random.uniform(size = (10,10))
    distance_mat2 = distance_matrix(X, radius = 1)
    Geometry = geom.Geometry(distance_mat2, input_type = 'distance')    
    ## Raise error, can't re-calculate 
    msg = ("input_method was passed as distance."
           "requested radius not equal to self.neighborhood_radius."
           "distance matrix cannot be re-calculated.")
    assert_raise_message(ValueError, msg, Geometry.get_distance_matrix, neighborhood_radius=3)

    # 13. (Existing distance, passed radius equal to existing self.radius, input_type = data)
    X = np.random.uniform(size = (10,10))
    distance_mat2 = distance_matrix(X, radius = 1)
    Geometry = geom.Geometry(X, input_type = 'data', neighborhood_radius=1)    
    Geometry.assign_distance_matrix(distance_mat2)
    ## Return existing distance matrix
    distance_mat = Geometry.get_distance_matrix(neighborhood_radius=1)
    assert_array_almost_equal(distance_mat.todense(), distance_mat2.todense(), almost_equal_decimals)

    # 14. (Existing distance, passed radius equal to existing self.radius, input_type = distance)
    X = np.random.uniform(size = (10,10))
    distance_mat2 = distance_matrix(X, radius = 1)
    Geometry = geom.Geometry(distance_mat2, input_type = 'distance', neighborhood_radius = 1)    
    ## Return existing distance matrix
    distance_mat = Geometry.get_distance_matrix(neighborhood_radius=1)
    assert_array_almost_equal(distance_mat.todense(), distance_mat2.todense(), almost_equal_decimals)

    # 15. (Existing distance, passed radius not equal to existing self.radius, input_type = data)
    X = np.random.uniform(size = (10,10))
    distance_mat2 = distance_matrix(X, radius = 1)
    Geometry = geom.Geometry(X, input_type = 'data', neighborhood_radius=1)    
    Geometry.assign_distance_matrix(distance_mat2)
    ## Recompute with passed radius, set self.radius to passed radius
    distance_mat2 = distance_matrix(X, radius = 3)
    distance_mat = Geometry.get_distance_matrix(neighborhood_radius=3)
    assert_array_almost_equal(distance_mat.todense(), distance_mat2.todense(), almost_equal_decimals)
    assert(Geometry.neighborhood_radius == 3)

    # 16. (Existing distance, passed radius not equal to existing self.radius, input_type = distance)
    X = np.random.uniform(size = (10,10))
    distance_mat2 = distance_matrix(X, radius = 1)
    Geometry = geom.Geometry(distance_mat2, input_type = 'distance', neighborhood_radius = 1)    
    ## Raise error, can't recalculate
    msg = ("input_method was passed as distance."
           "requested radius not equal to self.neighborhood_radius."
           "distance matrix cannot be re-calculated.")
    assert_raise_message(ValueError, msg, Geometry.get_distance_matrix, neighborhood_radius=3)
Esempio n. 8
0
def time_function(X, method, radius, d = None, random_state = None):
    if method == 'distance_cy':
        t0 = time.time()
        dmat = distance_matrix(X, method = 'cyflann', radius = radius)
        t1 = time.time()
        return (t1 - t0)
    if method == 'distance_py':
        path_to_flann = '/homes/jmcq/flann-1.8.4-src/src/python'
        sys.path.insert(0, path_to_flann)
        import pyflann as pyf
        flindex = pyf.FLANN()
        flparams = flindex.build_index(X, algorithm = 'kmeans', target_precision = 0.9)
        t0 = time.time()
        dmat = distance_matrix(X, method = 'pyflann', radius = radius, flindex = flindex)
        t1 = time.time()
        return (t1 - t0)
    if method == 'distance_brute':
        t0 = time.time()
        dmat = distance_matrix(X, method = 'brute', radius = radius)
        t1 = time.time()
        return (t1 - t0)
    elif method == 'spectral':
        from Mmani.embedding.spectral_embedding import SpectralEmbedding
        t0 = time.time()
        SE = SpectralEmbedding(n_components=d, eigen_solver='amg',random_state=random_state,
                                neighborhood_radius=radius, distance_method='cyflann',
                                input_type='data',laplacian_type='geometric')
        embedding = SE.fit_transform(X)
        t1 = time.time()
        return (t1 - t0)
    elif method == 'isomap':
        from Mmani.embedding.isomap import Isomap
        t0 = time.time()
        try:
            isomap = Isomap(n_components=d, eigen_solver='amg',random_state=random_state,
                            neighborhood_radius=radius, distance_method='cyflann',
                            input_type='data')
            embedding = isomap.fit_transform(X)
        except:
            isomap = Isomap(n_components=d, eigen_solver='arpack',random_state=random_state,
                            neighborhood_radius=radius, distance_method='cyflann',
                            input_type='data')
            embedding = isomap.fit_transform(X)        
        t1 = time.time()
        return (t1 - t0)
    elif method == 'ltsa':
        from Mmani.embedding.ltsa import LTSA
        t0 = time.time()
        ltsa = LTSA(n_components=d, eigen_solver='amg',random_state=random_state,
                    neighborhood_radius=radius, distance_method='cyflann',
                    input_type='data')
        embedding = ltsa.fit_transform(X)
        t1 = time.time()
        return (t1 - t0)
    elif method == 'lle':
        from Mmani.embedding.locally_linear import LocallyLinearEmbedding
        t0 = time.time()
        lle = LocallyLinearEmbedding(n_components=d, eigen_solver='amg',random_state=random_state,
                                    neighborhood_radius=radius, distance_method='cyflann',
                                    input_type='data')
        embedding = lle.fit_transform(X)
        t1 = time.time()
        return (t1 - t0)
    else:
        raise ValueError('benchmark method: ' + str(method) + ' not found.')
        return None
Esempio n. 9
0
import numpy as np
from scipy import sparse
from sklearn.datasets.samples_generator import make_swiss_roll

rng = np.random.RandomState(123)
ns = 3000
X, t = make_swiss_roll( ns, noise = 0.0, random_state = rng)
X = np.asarray( X, order="C" )
nf = 750 
rad0 = 2.5
dim = 2

rad = rad0/ns**(1./(dim+6))  #check the scaling
n_noisef = nf - 3
noise_rad_frac = 0.1
noiserad = rad/np.sqrt(n_noisef) * noise_rad_frac
Xnoise = rng.rand(ns, n_noisef) * noiserad
X = np.hstack((X, Xnoise))
rad = rad*(1+noise_rad_frac) # add a fraction for noisy dimensions

from Mmani.geometry.distance import distance_matrix
dmat = distance_matrix(X, method = 'cython', radius = rad)