def test1(): # test C = np.matrix('1 -1 1 1; 1 1 1 0; 0 1 1 1 ;1 -3 -2 1') (cost, pi, edge_cost) = approxMatch(C) (cy_cost, cy_pi, cy_edge_cost) = fast_approxMatch(C) print C print "cost:", cost, cy_cost-cost print pi, pi-cy_pi print edge_cost, cy_edge_cost - edge_cost D = 2000 np.random.seed(1) C = common.randn((D, D)) import cProfile cProfile.runctx('(cost, pi, edge_cost) = approxMatch(C)', globals(), locals()) cProfile.runctx('(cy_cost0, cy_pi0, cy_edge_cost0) = cy_ApproxMatch(C)', globals(), locals()) (cost, pi, edge_cost) = approxMatch(C) (cy_cost0, cy_pi0, cy_edge_cost0) = cy_ApproxMatch(C) assert np.linalg.norm(cy_cost0-cost) == 0 assert np.linalg.norm(pi-cy_pi0) == 0 assert np.linalg.norm(cy_edge_cost0 - edge_cost) == 0 X = np.array([[ 0.17034787, -1.11852005, 2.3723583 ], [ 0.40587496, -0.71610151, 0.24853477], [ 0.28721089, -1.62157422, 0.33806607], [ 0.88027416, 0.30368357, -1.15908568], [-0.50893908, -0.61650787, -1.10849268]]) Y = np.array([[ 0.49316125, -1.6459511 , 0.03514289], [ 0.16211477, 0.41796482, -0.19066103], [-0.68095143, 0.48752118, 1.08161025], [-0.44147856, 0.43943179, 1.05625251]]) GX = np.array([[0, 1, 1, 0, 1], [0, 1, 1, 1, 0], [0, 1, 0, 1, 1], [0, 1, 1, 0, 0], [1, 0, 0, 0, 1]]) GY = np.array([[0, 1, 1, 0], [0, 1, 0, 0], [1, 0, 1, 1], [0, 0, 0, 1]]) options = common.Struct() options.weight_type = 'dist' options.K = 1 # K is not really 1 here, but a non-zero value is required. options.alpha = 0.3 options.normalize_projections = 1 W, U, Z = makeWeights(options, X, Y, GX, GY) print 'W shape: ', W.shape print W
def fast_approxMatch(C): if isinstance(C, np.matrix): C = np.array(C, dtype=np.double) return cy_ApproxMatch(C)