def distance(self, y0, y1):
        """
            Compare Y1(s) with best matching pixel in Y2(s_n) 
            where s_s \in (the neighbourhood of s)
        """

        Y1 = y0.resize(self.size).get_values()
        Y2 = y1.resize(self.size).get_values()
        
        Y2_unrolled = self.fs.image2unrolledneighbors(Y2)
        Y1_repeated = self.fs.image2repeated(Y1)
        assert_allclose(Y2_unrolled.shape, Y1_repeated.shape) 
        
        diff1 = np.abs(Y2_unrolled - Y1_repeated)
        myres = np.mean(np.min(diff1, axis=1))
        
        if False:
            # old method, equivalent
            neighbor_indices_flat = self.fs.neighbor_indices_flat
            nchannels = Y1.shape[2]
            nsensel = Y1[:, :, 0].size 
            best = np.zeros((nsensel, Y1.shape[2])) 
            for c in range(nchannels):
                y1_flat = Y1[:, :, c].astype(np.int16).flat 
                y2_flat = Y2[:, :, c].astype(np.int16).flat 
                for k in range(nsensel):
                    a = y1_flat[k].astype(np.float)
                    b = y2_flat[neighbor_indices_flat[k]]
                    diff = np.abs(a - b) 
                    best[k, c] = np.min(diff) 
            res = np.mean(best)  # /self.maxval_distance_neighborhood_bestmatch
            assert_allclose(res, myres)
    
        return myres
Esempio n. 2
0
    def distance(self, y0, y1):
        """
            Compare Y1(s) with best matching pixel in Y2(s_n) 
            where s_s \in (the neighbourhood of s)
        """

        Y1 = y0.resize(self.size).get_values()
        Y2 = y1.resize(self.size).get_values()

        Y2_unrolled = self.fs.image2unrolledneighbors(Y2)
        Y1_repeated = self.fs.image2repeated(Y1)
        assert_allclose(Y2_unrolled.shape, Y1_repeated.shape)

        diff1 = np.abs(Y2_unrolled - Y1_repeated)
        myres = np.mean(np.min(diff1, axis=1))

        if False:
            # old method, equivalent
            neighbor_indices_flat = self.fs.neighbor_indices_flat
            nchannels = Y1.shape[2]
            nsensel = Y1[:, :, 0].size
            best = np.zeros((nsensel, Y1.shape[2]))
            for c in range(nchannels):
                y1_flat = Y1[:, :, c].astype(np.int16).flat
                y2_flat = Y2[:, :, c].astype(np.int16).flat
                for k in range(nsensel):
                    a = y1_flat[k].astype(np.float)
                    b = y2_flat[neighbor_indices_flat[k]]
                    diff = np.abs(a - b)
                    best[k, c] = np.min(diff)
            res = np.mean(best)  # /self.maxval_distance_neighborhood_bestmatch
            assert_allclose(res, myres)

        return myres
def plan_reducer_test2():
    
    labels = [0, 1, 2, 3]
    
    same = np.eye(4, dtype='bool')
    inverse = np.zeros((4, 4), dtype='bool')
    commute = np.ones((4, 4), dtype='bool')
    
    inverse[0, 3] = inverse[3, 0] = 1
    inverse[1, 2] = inverse[2, 1] = 1
    
    
    pr = PlanReducer.from_matrices(labels, commute, inverse, same)

    expected = []
    for x in itertools.permutations(labels):
        expected.append((x, ()))
        
    expected.append(((0, 0, 0, 0, 2, 2, 3),
                    (0, 0, 0, 2, 2)))
    
    expected.append(((2, 2, 0, 2, 2),
                     (0, 2, 2, 2, 2)))

    expected.append(((3, 2), (2, 3)))
        
    for a, b in expected:
        a = tuple(a)
        b = tuple(b)
        b2 = pr.get_canonical(a)
        assert_allclose(b, b2) 
Esempio n. 4
0
def plan_reducer_test2():

    labels = [0, 1, 2, 3]

    same = np.eye(4, dtype='bool')
    inverse = np.zeros((4, 4), dtype='bool')
    commute = np.ones((4, 4), dtype='bool')

    inverse[0, 3] = inverse[3, 0] = 1
    inverse[1, 2] = inverse[2, 1] = 1

    pr = PlanReducer.from_matrices(labels, commute, inverse, same)

    expected = []
    for x in itertools.permutations(labels):
        expected.append((x, ()))

    expected.append(((0, 0, 0, 0, 2, 2, 3), (0, 0, 0, 2, 2)))

    expected.append(((2, 2, 0, 2, 2), (0, 2, 2, 2, 2)))

    expected.append(((3, 2), (2, 3)))

    for a, b in expected:
        a = tuple(a)
        b = tuple(b)
        b2 = pr.get_canonical(a)
        assert_allclose(b, b2)
Esempio n. 5
0
    def belongs_ts(self, bv):
        ''' 
            Checks that a vector *vx* belongs to the tangent space
            at the given point *base*.

        '''
        bvp = self.project_ts(bv)
        assert_allclose(bv[1], bvp[1], atol=self.atol_distance)
def test_distance_norm_weighted1():

    shape = (5, 3)
    one = np.ones(shape)
    sure = one
    zero = 0 * one

    L2 = DistanceNorm(2)
    L1 = DistanceNorm(1)
    L2w = DistanceNormWeighted(2)
    L1w = DistanceNormWeighted(1)

    cases = [
        (0.0, L2, UncertainImage(one, sure), UncertainImage(one, sure)),
        (1.0, L2, UncertainImage(zero, sure), UncertainImage(one, sure)),
        (1.0, L1, UncertainImage(zero, sure), UncertainImage(one, sure)),
        (1.0, L2w, UncertainImage(zero, sure), UncertainImage(one, sure)),
        (1.0, L1w, UncertainImage(zero, sure), UncertainImage(one, sure)),
        (0.5, L1, UncertainImage(zero, sure), UncertainImage(one * 0.5, sure)),
        (0.5, L2, UncertainImage(zero, sure), UncertainImage(one * 0.5, sure)),
        (0.5, L1w, UncertainImage(zero, sure), UncertainImage(one * 0.5,
                                                              sure)),
        (0.5, L2w, UncertainImage(zero, sure), UncertainImage(one * 0.5,
                                                              sure)),
    ]

    v1 = one * 0.5
    u1 = one
    v2 = v1.copy()
    u2 = u1.copy()
    # Modify random pixels, and put the uncertainty at zero
    for _ in range(5):
        i = np.random.randint(v1.size - 1)
        v2.flat[i] = np.random.rand()
        u2.flat[i] = 0
    # then the distane should still be 0
    cases.append((0, L1w, UncertainImage(v1, u1), UncertainImage(v2, u2)))
    cases.append((0, L2w, UncertainImage(v1, u1), UncertainImage(v2, u2)))

    for expected, d, y0, y1 in cases:
        distance = d.distance(y0, y1)
        try:
            assert_allclose(distance, expected)
        except:
            print(' distance: %s' % d)
            print('       y0: %s' % y0)
            print('       y1: %s' % y1)
            print(' expected: %s' % expected)
            print(' obtained: %s' % distance)
            raise
def test_distance_norm_weighted2():
    
    shape = (5, 3)
    one = np.ones(shape)
    zero = 0 * one

    L2w = DistanceNormWeighted(2)
    L1w = DistanceNormWeighted(1)

    r = np.random.rand(*shape)
    
    # two equal but with strange uncertainty
    y1 = UncertainImage(r, np.round(np.random.rand(*shape)))
    y2 = UncertainImage(r, np.round(np.random.rand(*shape)))
    
    assert_allclose(L2w.distance(y1, y2), 0)
    assert_allclose(L1w.distance(y1, y2), 0)
    
    
    # two completely different but with strange uncertainty
    y1 = UncertainImage(one, np.round(np.random.rand(*shape)))
    y2 = UncertainImage(zero, np.round(np.random.rand(*shape)))
    
    assert_allclose(L2w.distance(y1, y2), 1)
    assert_allclose(L1w.distance(y1, y2), 1)
Esempio n. 8
0
def compute_dist_stats(config, id_distance, id_stream, delta):
    distance = config.distances.instance(id_distance)
    stream = config.streams.instance(id_stream)
    it = stream.read_all()
    results = []
    for logitem in iterate_testcases(it, delta):
        assert_allclose(len(logitem.u), delta)
        y0 = UncertainImage(logitem.y0)
        y1 = UncertainImage(logitem.y1)
        d = distance.distance(y0, y1)
        results.append(d)

    logger.info('%s: found %d of %d steps in %s' %
                (id_distance, len(results), delta, id_stream))
    return results
Esempio n. 9
0
def compute_dist_stats(config, id_distance, id_stream, delta):
    distance = config.distances.instance(id_distance)
    stream = config.streams.instance(id_stream)
    it = stream.read_all()
    results = []
    for logitem in iterate_testcases(it, delta):
        assert_allclose(len(logitem.u), delta)
        y0 = UncertainImage(logitem.y0)
        y1 = UncertainImage(logitem.y1)
        d = distance.distance(y0, y1)
        results.append(d)
        
    logger.info('%s: found %d of %d steps in %s' % 
                (id_distance, len(results), delta, id_stream))
    return results
def test_distance_norm_weighted1():
    
    shape = (5, 3)
    one = np.ones(shape)
    sure = one
    zero = 0 * one
    
    L2 = DistanceNorm(2)
    L1 = DistanceNorm(1)
    L2w = DistanceNormWeighted(2)
    L1w = DistanceNormWeighted(1)
    
    cases = [
         (0.0, L2, UncertainImage(one, sure), UncertainImage(one, sure)),
         (1.0, L2, UncertainImage(zero, sure), UncertainImage(one, sure)),
         (1.0, L1, UncertainImage(zero, sure), UncertainImage(one, sure)),
         (1.0, L2w, UncertainImage(zero, sure), UncertainImage(one, sure)),
         (1.0, L1w, UncertainImage(zero, sure), UncertainImage(one, sure)),
         (0.5, L1, UncertainImage(zero, sure), UncertainImage(one * 0.5, sure)),
         (0.5, L2, UncertainImage(zero, sure), UncertainImage(one * 0.5, sure)),
         (0.5, L1w, UncertainImage(zero, sure), UncertainImage(one * 0.5, sure)),
         (0.5, L2w, UncertainImage(zero, sure), UncertainImage(one * 0.5, sure)),
    ]
    
    v1 = one * 0.5
    u1 = one
    v2 = v1.copy()
    u2 = u1.copy()
    # Modify random pixels, and put the uncertainty at zero
    for _ in range(5):
        i = np.random.randint(v1.size - 1)
        v2.flat[i] = np.random.rand()
        u2.flat[i] = 0 
    # then the distane should still be 0
    cases.append((0, L1w, UncertainImage(v1, u1), UncertainImage(v2, u2)))
    cases.append((0, L2w, UncertainImage(v1, u1), UncertainImage(v2, u2)))
    
    for expected, d, y0, y1 in cases:
        distance = d.distance(y0, y1)
        try:
            assert_allclose(distance, expected)
        except:
            print(' distance: %s' % d)
            print('       y0: %s' % y0)
            print('       y1: %s' % y1)
            print(' expected: %s' % expected)
            print(' obtained: %s' % distance) 
            raise
Esempio n. 11
0
def compute_predstats(config, id_discdds, id_stream, delta, id_distances):
    dds = config.discdds.instance(id_discdds)
    stream = config.streams.instance(id_stream)
    distances = dict(map(lambda x: (x, config.distances.instance(x)), id_distances))
    dtype = [(x, 'float32') for x in id_distances]
    
    results = []

    for logitem in iterate_testcases(stream.read_all(), delta):
        assert_allclose(len(logitem.u), delta)
        y0 = UncertainImage(logitem.y0)
        y1 = UncertainImage(logitem.y1)
        py0 = dds.predict(y0, dds.commands_to_indices(logitem.u))
        ds = []
        for name in id_distances:
            d = distances[name].distance(y1, py0)
#            d0 = distances[name].distance(y1, y0)
            ds.append(d)
        
        a = np.array(tuple(ds), dtype=dtype)
        results.append(a)
#    pdb.set_trace()        
    return results
Esempio n. 12
0
def compute_predstats(id_discdds, id_stream, delta, id_distances):
    dds = get_conftools_discdds().instance(id_discdds)
    stream = get_conftools_streams().instance(id_stream)
    distances_library = get_conftools_uncertain_image_distances()
    distances = dict(
        map(lambda x: (x, distances_library.instance(x)), id_distances))
    dtype = [(x, 'float32') for x in id_distances]

    results = []
    for logitem in iterate_testcases(stream.read_all(), delta):
        assert_allclose(len(logitem.u), delta)
        y0 = UncertainImage(logitem.y0)
        y1 = UncertainImage(logitem.y1)
        py0 = dds.predict(y0, dds.commands_to_indices(logitem.u))
        ds = []
        for name in id_distances:
            d = distances[name].distance(y1, py0)
            #  d0 = distances[name].distance(y1, y0)
            ds.append(d)

        a = np.array(tuple(ds), dtype=dtype)
        results.append(a)

    return results
def test_distance_norm_weighted2():

    shape = (5, 3)
    one = np.ones(shape)
    zero = 0 * one

    L2w = DistanceNormWeighted(2)
    L1w = DistanceNormWeighted(1)

    r = np.random.rand(*shape)

    # two equal but with strange uncertainty
    y1 = UncertainImage(r, np.round(np.random.rand(*shape)))
    y2 = UncertainImage(r, np.round(np.random.rand(*shape)))

    assert_allclose(L2w.distance(y1, y2), 0)
    assert_allclose(L1w.distance(y1, y2), 0)

    # two completely different but with strange uncertainty
    y1 = UncertainImage(one, np.round(np.random.rand(*shape)))
    y2 = UncertainImage(zero, np.round(np.random.rand(*shape)))

    assert_allclose(L2w.distance(y1, y2), 1)
    assert_allclose(L1w.distance(y1, y2), 1)
Esempio n. 14
0
 def belongs(self, x):
     # TODO: make this much more efficient
     check('array[NxN],orthogonal', x, N=self.n)
     det = np.linalg.det(x)
     assert_allclose(det, 1, err_msg='I expect the determinant to be +1.')
Esempio n. 15
0
 def belongs(self, x):
     assert_allclose(x.size, self.dimension)
     assert np.all(np.isreal(x)), "Expected real vector"
 def belongs(self, x):
     # TODO: make this much more efficient
     check('array[NxN],orthogonal', x, N=self.n)
     det = np.linalg.det(x)
     assert_allclose(det, 1, err_msg='I expect the determinant to be +1.')
Esempio n. 17
0
 def belongs(self, x):
     assert_allclose(x.size, self.dimension)
     assert np.all(np.isreal(x)), "Expected real vector"
Esempio n. 18
0
def assert_projection(P):
    assert_allclose(P.T, P)
    assert_allclose(np.dot(P, P), P)