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_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_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
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_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
def analyze_olfaction_covariance(covariance, receptors): ''' Covariance: n x n covariance matrix. Positions: list of n positions ''' positions = [pose.get_2d_position() for pose, sens in receptors] #@UnusedVariable positions = array(positions).transpose().squeeze() require_shape(square_shape(), covariance) n = covariance.shape[0] require_shape((2, n), positions) distances = create_distance_matrix(positions) correlation = cov2corr(covariance) flat_distances = distances.reshape(n * n) flat_correlation = correlation.reshape(n * n) # let's fit a polynomial deg = 4 poly = polyfit(flat_distances, flat_correlation, deg=deg) knots = linspace(min(flat_distances), max(flat_distances), 2000) poly_int = polyval(poly, knots) poly_fder = polyder(poly) fder = polyval(poly_fder, distances) Ttheta = create_olfaction_Ttheta(positions, fder) Tx, Ty = create_olfaction_Txy(positions, fder, distances) report = Node('olfaction-theory') report.data('flat_distances', flat_distances) report.data('flat_correlation', flat_correlation) with report.data_file('dist_vs_corr', 'image/png') as filename: pylab.figure() pylab.plot(flat_distances, flat_correlation, '.') pylab.plot(knots, poly_int, 'r-') pylab.xlabel('distance') pylab.ylabel('correlation') pylab.title('Correlation vs distance') pylab.legend(['data', 'interpolation deg = %s' % deg]) pylab.savefig(filename) pylab.close() with report.data('fder', fder).data_file('fder', 'image/png') as filename: pylab.figure() pylab.plot(knots, polyval(poly_fder, knots), 'r-') pylab.title('f der') pylab.savefig(filename) pylab.close() report.data('distances', distances) report.data('correlation', correlation) report.data('covariance', covariance) report.data('f', polyval(poly, distances)) f = report.figure(id='cor-vs-distnace', caption='Estimated kernels', shape=(3, 3)) f.sub('dist_vs_corr') f.sub('fder') f.sub('f', display='scale') f.sub('distances', display='scale') f.sub('correlation', display='posneg') f.sub('covariance', display='posneg') T = numpy.zeros(shape=(3, Tx.shape[0], Tx.shape[1])) T[0, :, :] = Tx T[1, :, :] = Ty T[2, :, :] = Ttheta T_report = create_report_figure_tensors(T, report_id='tensors', caption="Predicted learned tensors") report.add_child(T_report) return report