def create_olfaction_Txy(positions, fder, distances):
    '''
    positions: 2 x n  vector
    f_der, distances: n x n
    
    returns Tx, Ty,   each n x n
    '''
    require_shape((2, gt(0)), positions)
    n = positions.shape[1]
    require_shape((n, n), fder)
    
    Tx = ndarray((n, n))
    Ty = ndarray((n, n))

    for i in range(n):
        rep = tile(positions[:, i], (n, 1)).transpose()
        require_shape((2, n), rep)
        diff = positions - rep
        
        Tx[i, :] = diff[0, :]
        Ty[i, :] = diff[1, :]
        
    # we add eps because otherwise 0/0 = NAN
    eps = 0.0001
    Tx = (Tx / (distances + eps)) * fder
    Ty = (Ty / (distances + eps)) * fder
    
    return Tx, Ty
Example #2
0
def loop_convolve(x, filter):
    ''' Wraps around x and convolves with the filter '''
    require_shape((gt(0),), x)
    n = x.shape[0]
    a = zeros(shape=3 * n) 
    a[0:n] = x
    a[n:(2 * n)] = x
    a[2 * n:(3 * n)] = x
    af = convolve(a, filter, mode='same')
    require_shape((3 * n,), af)
    result = af[(n):(2 * n)]
    return result
def create_distance_matrix(positions):
    ''' Returns the corresponding distance matrix 
        Position: 2 x n vector '''
    require_shape((2, gt(0)), positions)
    
    n = positions.shape[1]
    results = ndarray(shape=(n, n))

    for i in range(n):
        rep = tile(positions[:, i], (n, 1)).transpose()
        require_shape((2, n), rep)
        diff = positions - rep
        square(diff)
        distances = sqrt(sum(square(diff), axis=0))
        require_shape((n,), distances)
        results[i, :] = distances

    return results
def create_olfaction_Ttheta(positions, fder):
    '''
    positions: 2 x n  vector
    f_der: n x n
    '''
    require_shape((2, gt(0)), positions)
    n = positions.shape[1]
    require_shape((n, n), fder)
    
    results = ndarray(shape=(n, n))

    for i in range(n):
        J = array([ [0, -1], [1, 0]])
        Js = dot(J, positions[:, i])
        
        results[i, :] = dot(positions.transpose(), Js)

    results = results * fder # it IS element by element
    return results
def create_report_fields(result, report_id=None):
    report = Node(report_id)
    
    fields = [('x_y', result.lattice_x_y, result.fields_x_y),
              ('x_theta', result.lattice_x_theta, result.fields_x_theta),
              ('theta_y', result.lattice_theta_y, result.fields_theta_y) ]

    fig_fields = report.figure('fields', caption='Inner products', shape=(3, 3))
    # Compute inner product w/ right direction
    fig_inner = report.figure(id='inner', caption='Inner products')

    fig_success = report.figure(id='success', caption='Success')
 
    
    for field_name, lattice, field in fields:
        field = array(field)
        average = numpy.mean(field, 0)
        #print "List  shape: %s " % str(field.shape)
        #print "Mean shape: %s " % str(average.shape)
        require_shape((gt(0), gt(0), 3), average)

        for i, cmd_name in [(0, 'vx'), (1, 'vy'), (2, 'vtheta')]:
            image_name = '%s-%s' % (field_name, cmd_name)

            fi = average[:, :, i].squeeze()
            require_shape((gt(0), gt(0)), fi)
            
            max_value = abs(fi).max()
            # let's allow a matrix with all 0
            if max_value == 0:
                max_value = 1

            report.data(image_name, fi)
            fig_fields.sub(image_name, display='posneg', max_value=max_value,
                           caption=field_name)
    
    def lattice2pose(lattice):
        def rbs2pose(rbs):
            p = rbs.get_2d_position()
            theta = rbs.get_2d_orientation()
            return array([p[0, 0], p[1, 0], theta])
        return map(lambda x: map(rbs2pose, x), lattice)
        
    for field_name, lattice, field in fields:
        lattice = array(lattice2pose(lattice))
        inners = []
        for commands in field:
            print "lattice shape", lattice.shape
            #print "commands shape", commands.shape
            inner = numpy.sum(lattice * commands, axis=2) 
            inners.append(inner)
        
        average_inner = numpy.mean(array(inners), 0)
        require_shape((gt(0), gt(0)), average_inner)
        
        success = numpy.mean(array(inners) > 0, 0) # XXX should be < 0
        require_shape((gt(0), gt(0)), success)
            
        image_name = 'inner-%s' % (field_name)
        
        max_value = abs(fi).max()
        # let's allow a matrix with all 0
        if max_value == 0:
            max_value = 1
        
        report.data(image_name, average_inner)
        fig_inner.sub(image_name, display='posneg',
                      max_value=max_value, caption=field_name)


        image_name = 'success-%s' % (field_name)
        
        report.data(image_name, success)
        fig_success.sub(image_name, display='success')


    return report