def print_minimum(self):
        self.info('print_minimum:')
        connections = self.compute_connections(only_close_enough=False)
        
        self.show_connections(connections, 'all of them')    
    
        nodes1 = list(self.tree1.G.nodes())
        nodes2 = list(self.tree2.G.nodes())
        
        # TODO: do not compute all of this
        Db = construct_matrix_iterators((nodes1, nodes2),
                                       lambda n1, n2: self.distance(n1, n2))
        Dp = construct_matrix_iterators((nodes1, nodes2),
                                       lambda n1, n2: self.distance_prediction(n1, n2))
        Db_min, m = md_argmin(Db)
        n1 = nodes1[m[0]]                    
        n2 = nodes2[m[1]]
        assert_allclose(Db_min, self.distance(n1, n2)) 
        self.info('Minimum distance_branch: %g between %s and %s' % 
                  (Db_min, self.tree1.node_friendly(n1),
                            self.tree2.node_friendly(n2)))

        Dp_min, m = md_argmin(Dp)
        n1 = nodes1[m[0]]                    
        n2 = nodes2[m[1]]
        assert_allclose(Dp_min, self.distance_prediction(n1, n2))
        self.info('Minimum distance_prediction: %g between %s and %s' % 
                  (Dp_min, self.tree1.node_friendly(n1),
                           self.tree2.node_friendly(n2)))
    def print_minimum(self):
        self.info('print_minimum:')
        connections = self.compute_connections(only_close_enough=False)

        self.show_connections(connections, 'all of them')

        nodes1 = list(self.tree1.G.nodes())
        nodes2 = list(self.tree2.G.nodes())

        # TODO: do not compute all of this
        Db = construct_matrix_iterators((nodes1, nodes2),
                                        lambda n1, n2: self.distance(n1, n2))
        Dp = construct_matrix_iterators(
            (nodes1, nodes2), lambda n1, n2: self.distance_prediction(n1, n2))
        Db_min, m = md_argmin(Db)
        n1 = nodes1[m[0]]
        n2 = nodes2[m[1]]
        assert_allclose(Db_min, self.distance(n1, n2))
        self.info('Minimum distance_branch: %g between %s and %s' %
                  (Db_min, self.tree1.node_friendly(n1),
                   self.tree2.node_friendly(n2)))

        Dp_min, m = md_argmin(Dp)
        n1 = nodes1[m[0]]
        n2 = nodes2[m[1]]
        assert_allclose(Dp_min, self.distance_prediction(n1, n2))
        self.info('Minimum distance_prediction: %g between %s and %s' %
                  (Dp_min, self.tree1.node_friendly(n1),
                   self.tree2.node_friendly(n2)))
Beispiel #3
0
def plot_3d_graph(pylab, G, plan2point, plan2color, edges2color=None):
    if edges2color is None:
        edges2color = lambda n1, n2: [0, 0, 0]  #@UnusedVariable

    cm = matplotlib.cm.get_cmap('RdYlBu')
    import mpl_toolkits.mplot3d.axes3d as plot3
    fig = pylab.gcf()
    ax = plot3.Axes3D(fig)
    for pi, pj, ec in get_edges_points_and_color(G, plan2point, edges2color):
        coords = np.vstack((pi, pj)).T
        ax.plot3D(xs=coords[0],
                  ys=coords[1],
                  zs=coords[2],
                  linestyle='-',
                  color=ec)
    n = len(G)
    P = np.array(map(plan2point, G)).T
    assert_allclose(P.shape, (3, n))
    color = map(plan2color, G)

    patches = ax.scatter3D(P[0], P[1], P[2], s=40, c=color, cmap=cm)
    try:
        fig.colorbar(patches)
    except TypeError as e:
        logger.error('Cannot use colorbar')
        logger.exception(e)
def md_argmin(a):
    """ Returns the value and the index coordinate of a multidimensional array """
    min_value = np.min(a)
    index_flat = np.argmin(a)
    ij = np.unravel_index(index_flat, a.shape)
    ij = tuple(ij)
    assert_allclose(a[ij], min_value)
    return a[ij], ij
def md_argmin(a):
    """ Returns the value and the index coordinate of a multidimensional array """
    min_value = np.min(a)
    index_flat = np.argmin(a)
    ij = np.unravel_index(index_flat, a.shape)
    ij = tuple(ij)
    assert_allclose(a[ij], min_value)
    return a[ij], ij
def get_nodes_distance_matrix(G, nodelist, weight_field=None):
    n = len(nodelist)
    D = floyd_warshall_numpy(G, nodelist, weight=weight_field)
    assert_allclose(D.shape, (n, n))
    assert_allclose(np.isfinite(D), True)
    assert np.all(D >= 0)
    # there was some strange bug. Keep this even if you don't understand why
    D = np.array(D, dtype='float64')
    return D
Beispiel #7
0
def get_nodes_distance_matrix(G, nodelist, weight_field=None):
    n = len(nodelist)
    D = floyd_warshall_numpy(G, nodelist, weight=weight_field)
    assert_allclose(D.shape, (n, n))
    assert_allclose(np.isfinite(D), True)
    assert np.all(D >= 0)
    # there was some strange bug. Keep this even if you don't understand why
    D = np.array(D, dtype='float64')
    return D
Beispiel #8
0
def plot_2d_graph(pylab, G, plan2point, plan2color, edges2color=None, s=120):
    if edges2color is None:
        edges2color = lambda n1, n2: [0, 0, 0]  #@UnusedVariable

    cm = matplotlib.cm.get_cmap('RdYlBu')

    for pi, pj, ec in get_edges_points_and_color(G, plan2point, edges2color):
        coords = np.vstack((pi, pj)).T
        pylab.plot(coords[0], coords[1], linestyle='-', color=ec, zorder=1)

    n = len(G)
    P = np.array(map(plan2point, G)).T
    assert_allclose(P.shape, (2, n))
    color = map(plan2color, G)
    pylab.scatter(P[0], P[1], s=s, c=color, cmap=cm, zorder=3)
    pylab.axis('equal')
    turn_all_axes_off(pylab)
    pylab.colorbar()
def plot_2d_graph(pylab, G, plan2point, plan2color, edges2color=None, s=120):
    if edges2color is None:
        edges2color = lambda n1, n2: [0, 0, 0] #@UnusedVariable
        
    cm = matplotlib.cm.get_cmap('RdYlBu')
        
    for pi, pj, ec in get_edges_points_and_color(G, plan2point, edges2color):
        coords = np.vstack((pi, pj)).T
        pylab.plot(coords[0], coords[1], linestyle='-', color=ec, zorder=1)
        
    n = len(G)
    P = np.array(map(plan2point, G)).T
    assert_allclose(P.shape, (2, n))
    color = map(plan2color, G)
    pylab.scatter(P[0], P[1], s=s, c=color, cmap=cm, zorder=3)
    pylab.axis('equal')            
    turn_all_axes_off(pylab)
    pylab.colorbar()
    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.get_values()
        Y2 = y1.get_values()
        
        fs = self._get_flat_structure(Y1.shape[:2])
        
        Y2_unrolled = fs.ndvalues2unrolledneighbors(Y2)
        Y1_repeated = fs.ndvalues2repeated(Y1)
        assert_allclose(Y2_unrolled.shape, Y1_repeated.shape) 
        
        diff1 = np.abs(Y2_unrolled - Y1_repeated)
        if diff1.ndim == 3:
            diff1 = np.mean(diff1, axis=2)
            
        D = fs.get_distances()
        
        N, _ = D.shape
        distance_to_closest = np.zeros(N)
        for i in range(N):
            diff_i = diff1[i, :]
            # best matches
            matches, = np.nonzero(diff_i == np.min(diff_i))
            distance_to_matches = D[i, matches]
            distance_to_closest[i] = np.min(distance_to_matches)
            
            if False:
                if i == N / 2:
                    print('i: %s' % i)
                    print('distances[i]: %s' % D[i, :])
                    print('diff1[i]: %s' % diff_i)
                    print('matches: %s' % matches)
                    print('matches dist: %s' % distance_to_matches)
                    print('dist[i]: %s' % distance_to_closest[i])

        myres = np.mean(distance_to_closest)
        return myres
    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.get_values()
        Y2 = y1.get_values()
        
        fs = self._get_flat_structure(Y1.shape[:2])
        
        Y2_unrolled = fs.ndvalues2unrolledneighbors(Y2)
        Y1_repeated = fs.ndvalues2repeated(Y1)
        assert_allclose(Y2_unrolled.shape, Y1_repeated.shape) 
        
        diff1 = np.abs(Y2_unrolled - Y1_repeated)
        if diff1.ndim == 3:
            diff1 = np.mean(diff1, axis=2)
            
        D = fs.get_distances()
        
        N, _ = D.shape
        distance_to_closest = np.zeros(N)
        for i in range(N):
            diff_i = diff1[i, :]
            # best matches
            matches, = np.nonzero(diff_i == np.min(diff_i))
            distance_to_matches = D[i, matches]
            distance_to_closest[i] = np.min(distance_to_matches)
            
            if False:
                if i == N / 2:
                    print('i: %s' % i)
                    print('distances[i]: %s' % D[i, :])
                    print('diff1[i]: %s' % diff_i)
                    print('matches: %s' % matches)
                    print('matches dist: %s' % distance_to_matches)
                    print('dist[i]: %s' % distance_to_closest[i])

        myres = np.mean(distance_to_closest)
        return myres
def test_distance_neighbor_dist2_repeated():
    # A constant pattern
    a = np.ones((100, 100, 3))

    # Let's get some translated slices
    w = 80
    N = 10
    A = []
    for i in range(N):
        A.append(a[i:i + w, 0:0 + w, ...])
    print()
    areas = [0.01, 0.03, 0.05, 0.07]
    Ds = [DistanceNeighborDist((a, a)) for a in areas]
    L2 = DistanceNorm(2)
    for i in range(N):
        y0 = UncertainImage(A[0])
        y1 = UncertainImage(A[i])
        vl2 = L2.distance(y0, y1)
        ds = [x.distance(y0, y1) for x in Ds]
        assert_allclose(ds, 0)  # < this must be zero
        s = ";".join("%1.5f" % x for x in ds)
        print('- step %d L2: %1.5f %s' % (i, vl2, s))
Beispiel #13
0
def ManualMotion(tcname, id_discdds, id_image, planstring):
    # Get a random plan
    config = get_current_config()
    discdds = config.discdds.instance(id_discdds)
    rgb = config.images.instance(id_image)
    shape = discdds.get_shape()
    image1 = resize(rgb, shape[1], shape[0])       
    assert_allclose(image1.shape[:2], shape)

    chars = "abcdefghilmnopqrst"
    char2int = dict([(c, i) for i, c in enumerate(chars)])
    plan = tuple(map(char2int.__getitem__, planstring))
    
    
    # predict the result
    y0 = UncertainImage(image1)
    y1 = discdds.predict(y0, plan)
    
    tc = TestCase(id_tc=tcname, id_discdds=id_discdds,
                  y0=y0, y1=y1, true_plan=plan)

    return tc
Beispiel #14
0
def FromImages(tcname, id_discdds, image1, image2, true_plan=None):
    config = get_current_config()
    discdds = config.discdds.instance(id_discdds)
    shape = discdds.get_shape()

    rgb1 = config.images.instance(image1)
    image1 = resize(rgb1, shape[1], shape[0])       
    assert_allclose(image1.shape[:2], shape)

    rgb2 = config.images.instance(image2)
    image2 = resize(rgb2, shape[1], shape[0])       
    assert_allclose(image2.shape[:2], shape)

    
    # predict the result
    y0 = UncertainImage(image1)
    y1 = UncertainImage(image2)
    
    tc = TestCase(id_tc=tcname, id_discdds=id_discdds,
                  y0=y0, y1=y1, true_plan=true_plan)

    return tc
Beispiel #15
0
def test_distance_neighbor_dist2_repeated():
    # A constant pattern
    a = np.ones((100, 100, 3))

    # Let's get some translated slices
    w = 80
    N = 10
    A = []
    for i in range(N):
        A.append(a[i:i + w, 0:0 + w, ...])
    print()
    areas = [0.01, 0.03, 0.05, 0.07]
    Ds = [DistanceNeighborDist((a, a)) for a in areas]
    L2 = DistanceNorm(2)
    for i in range(N):
        y0 = UncertainImage(A[0])
        y1 = UncertainImage(A[i])
        vl2 = L2.distance(y0, y1)
        ds = [x.distance(y0, y1) for x in Ds]
        assert_allclose(ds, 0)  # < this must be zero
        s = ";".join("%1.5f" % x for x in ds)
        print('- step %d L2: %1.5f %s' % (i, vl2, s))
def plot_3d_graph(pylab, G, plan2point, plan2color, edges2color=None):
    if edges2color is None:
        edges2color = lambda n1, n2: [0, 0, 0] #@UnusedVariable

    cm = matplotlib.cm.get_cmap('RdYlBu')
    import mpl_toolkits.mplot3d.axes3d as plot3
    fig = pylab.gcf()
    ax = plot3.Axes3D(fig)
    for pi, pj, ec in get_edges_points_and_color(G, plan2point, edges2color):
        coords = np.vstack((pi, pj)).T
        ax.plot3D(xs=coords[0], ys=coords[1], zs=coords[2],
                      linestyle='-', color=ec)
    n = len(G)
    P = np.array(map(plan2point, G)).T
    assert_allclose(P.shape, (3, n))
    color = map(plan2color, G)
    
    patches = ax.scatter3D(P[0], P[1], P[2], s=40, c=color, cmap=cm)
    try:                    
        fig.colorbar(patches)
    except TypeError as e:
        logger.error('Cannot use colorbar')
        logger.exception(e)
    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.get_values()
        Y2 = y1.get_values()
        
        fs = self._get_flat_structure(Y1.shape[:2])
        
        Y2_unrolled = fs.image2unrolledneighbors(Y2)
        Y1_repeated = 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 = 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 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.get_values()
        Y2 = y1.get_values()
        
        fs = self._get_flat_structure(Y1.shape[:2])
        
        Y2_unrolled = fs.image2unrolledneighbors(Y2)
        Y1_repeated = 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 = 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 test_distance_neighbor_dist3():
    gs = FlatStructure((100, 100), (3, 3))
    D = gs.get_distances()
    assert_allclose(np.max(D), np.sqrt(2))
def test_distance_neighbor_dist1():
    gs = FlatStructure((100, 100), (1, 1))
    D = gs.get_distances()
    assert_allclose(D, 0)
Beispiel #21
0
def test_distance_neighbor_dist3():
    gs = FlatStructure((100, 100), (3, 3))
    D = gs.get_distances()
    assert_allclose(np.max(D), np.sqrt(2))
Beispiel #22
0
def test_distance_neighbor_dist1():
    gs = FlatStructure((100, 100), (1, 1))
    D = gs.get_distances()
    assert_allclose(D, 0)