def errors (shape, radius = 20.0, trials = 100): 'Plot shape fitting errors for multiple MC trials' errors = [] for i in range(trials): errors.append(fit(shape, radius)) def safe (e): e = abs(e) if e < 1: return e else: return 1 r_errors = array([safe(e[0]) for e in errors]) xy_errors = array([safe(sqrt(e[2]**2 + e[2]**2)) for e in errors]) r_mean = exp(sum(log(r_errors)) / trials) xy_mean = exp(sum(log(xy_errors)) / trials) if main.at_top(): pyplot.figure() pyplot.loglog(r_errors, xy_errors, 'r.') pyplot.loglog([r_mean],[xy_mean], 'k+') pyplot.xlabel('radius error (px)') pyplot.ylabel('position error (px)') pyplot.title('Precision of %g pixel %s markers (%i trials)' % (2 * radius, shape.capitalize(), trials)) pyplot.savefig('%s-%g-error.png' % (shape, radius)) pyplot.savefig('%s-%g-error.pdf' % (shape, radius)) pyplot.show() return r_mean,xy_mean
def search (self, query): self.add_new_results(query) # return new + old results results = list(self.db_cursor.execute( ''' select tweets.tweet_id, tweets.from_user_id, tweets.to_user_id, tweets.geo, tweets.created_at, tweets.text from search_results join tweets on search_results.tweet_id = tweets.tweet_id where query = (?) order by tweets.tweet_id ''', (query,))) if main.at_top(): for result in results: text = results[-1] print '\n%s' % text else: return results
def layers (offset = 4, *positions): 'Plots grid with offset positions' positions = set(int(p) for p in positions) positions.add(0) radius = 3 * 12 R = range(radius+1) def marked (p,x,y): return (x + offset * y) % 12 == p pyplot.figure() #pyplot.axis('equal') pyplot.title('offset = %i, positions = %s' % (offset, list(positions))) for r in R: pyplot.plot([0,radius],[r,r], color = (0.9,)*3) pyplot.plot([r,r],[0,radius], color = (0.9,)*3) for p in positions: XY = [(x,y) for x in R for y in R if marked(p,x,y)] X = [x for x,y in XY] Y = [y for x,y in XY] pyplot.plot(X,Y, marker = '.', linestyle = 'none') pyplot.xlim(0,radius) pyplot.ylim(0,radius) if main.at_top(): pyplot.show()
def search (query): 'searches twitter & caches result in local database' print query results = searcher.search(query) if main.at_top(): for t in results: print '-' * 80 print t print '\nsearch returned %i results' % len(results) else: return results
def ranges (shape, min_rad = 1.0, max_rad = 20.0, steps = 100): 'Plot sampled fitting error vs radius' L0 = log(min_rad) L1 = log(max_rad) dL = (L1 - L0) / steps radii = exp(arange(L0, L1, dL)) r_errors = [] xy_errors = [] for radius in radii: r,xy = errors(shape, radius, trials = 1) r_errors.append(r) xy_errors.append(xy) r_errors = array(r_errors) xy_errors = array(xy_errors) if main.at_top(): pyplot.figure() pyplot.loglog(2 * radii, 2 * r_errors, 'k.') pyplot.xlabel('marker size (px)') pyplot.ylabel('size error (px)') pyplot.ylim(1e-3, 1) pyplot.title('%s marker radius precision' % shape.capitalize()) pyplot.savefig('%s-radius-ranges.png' % shape) pyplot.savefig('%s-radius-ranges.pdf' % shape) pyplot.figure() pyplot.loglog(2 * radii, xy_errors, 'k.') pyplot.xlabel('marker size (px)') pyplot.ylabel('position error (px)') pyplot.ylim(1e-3, 1) pyplot.title('%s marker position precision' % shape.capitalize()) pyplot.savefig('%s-position-ranges.png' % shape) pyplot.savefig('%s-position-ranges.pdf' % shape) pyplot.show() return radii, r_errors, xy_errors
def curves (*radii): 'Plot geometric mean fitting error vs radius' radii = [float(r) for r in radii] radii.sort() r_shapes = dict((shape,[]) for shape in shapes) xy_shapes = dict((shape,[]) for shape in shapes) for radius in radii: for shape in shapes: r,xy = errors(shape, radius) r_shapes[shape].append(r) xy_shapes[shape].append(xy) if main.at_top(): pyplot.figure() for s in shapes: pyplot.plot(radii, r_shapes[s], label=s) pyplot.xlabel('marker radius (px)') pyplot.ylabel('mean radius error (px)') pyplot.title('Precision of shapes: %s' % ', '.join(shapes)) pyplot.legend() pyplot.savefig('radius-curves.png') pyplot.savefig('radius-curves.pdf') pyplot.figure() for s in shapes: pyplot.plot(radii, xy_shapes[s], label=s) pyplot.xlabel('marker radius (px)') pyplot.ylabel('position error (px, geometric mean)') pyplot.title('Precision of shapes: %s' % ', '.join(shapes)) pyplot.legend() pyplot.savefig('position-curves.png') pyplot.savefig('position-curves.pdf') pyplot.show()
def fit (shape, radius = 20.0, iters = 10): 'Fit shape to noisy image, display pixel errors' draw = draw_shape[shape] size = int(3 * radius) im = draw(size, radius, 0.5 * size, 0.5 * size) im += 2 * noise_level * randn(*im.shape) truth = array([ log(radius), 0.5 * size, 0.5 * size, ]) sigma = array([ 0.1, 1.0, 1.0, ]) mean = truth + sigma * randn(3) sigma fun = drawing_error(draw,im) def print_mean (): r,x,y = exp(mean[0]),mean[1],mean[2] print 'radius = %g, x = %g, y = %g' % (r,x,y) print 'fitting dot radius of %i x %i image' % im.shape print_mean() optim.nonlinear_minimize(fun, mean, sigma, iters) print_mean() if main.at_top(): fun(mean, True) error = mean - truth print 'error = %s' % error return error