def write_similarity_matrix(A, out, more=False, images=None): dtu.safe_pickle_dump(A, out + '.pickle') # rgb = rgb_zoom(scale(A), 8) rgb = scale(A) rgb = dtu.d8_image_zoom_linear(rgb, 8) dtu.write_image_as_jpg(rgb, out + '.jpg') if more: r = Report() n = A.shape[0] for i in range(n): f = r.figure() ignore = 10 Ai = A[i, :].copy() for i2 in range(i - ignore, i + ignore): if 0 <= i2 < n: Ai[i2] = np.inf jbest = np.argmin(Ai) with f.plot('i-%d' % i) as pylab: pylab.plot(A[i, :], '-*') if images: f.data_rgb(str(i), dtu.bgr_from_rgb(images[i])) f.data_rgb(str(jbest), dtu.bgr_from_rgb(images[jbest])) r.to_html(out + '.html')
def render_page(view2result, outdir, page_id): def iterate_views(): for view in views: yield view, view2result[view.id] # first compute max value mean_max = max(map(lambda x: numpy.max(x[1].mean), iterate_views())) var_max = max(map(lambda x: numpy.max(x[1].var), iterate_views())) n = Report(page_id) f = n.figure(cols=3) for view, stats in iterate_views(): nv = n.node(view.id) add_scaled(nv, 'mean', stats.mean, max_value=mean_max) add_scaled(nv, 'var', stats.var, max_value=var_max) #add_scaled(nv, 'min', stats.min) #add_scaled(nv, 'max', stats.max) for view in views: what = 'mean' #for what, view in prod(['mean', 'var'], views): f.sub('%s/%s' % (view.id, what), caption='%s (%s)' % (view.desc, what)) output_file = os.path.join(outdir, '%s.html' % n.id) resources_dir = os.path.join(outdir, 'images') print "Writing to %s" % output_file n.to_html(output_file, resources_dir=resources_dir)
def set_goal_observations(self, goal): self.goal = self.obs2ui(goal) self.a_pred = [a.predict(self.goal) for a in self.actions_i] r = Report('set_goal_observations') self.report(r) r.to_html('set_goal_observations.html')
def set_goal_observations(self, goal): self.goal = self.obs2ui(goal) self.a_pred = [a.predict(self.goal) for a in self.actions_i] r = Report('set_goal_observations') self.report(r) r.to_html('set_goal_observations.html')
def go(): ieee_fonts_zoom3(pylab) r = Report() algos = [InvMult2.ALGO_UNIFORM, InvMult2.ALGO_VAN_DER_CORPUT] for algo in algos: InvMult2.ALGO = algo InvPlus2.ALGO = algo print('Using algorithm %s ' % algo) with r.subsection(algo) as r2: # first F = parse_poset('dimensionless') R = F dp = InvMult2(F, (R, R)) ns = [3, 4, 5, 6, 10, 15] axis = (0.0, 6.0, 0.0, 6.0) with r2.subsection('invmult2') as rr: go1(rr, ns, dp, plot_nominal_invmult, axis) # second axis = (0.0, 1.2, 0.0, 1.2) dp = InvPlus2(F, (R, R)) with r2.subsection('invplus2') as rr: go1(rr, ns, dp, plot_nominal_invplus, axis) fn = 'out-plot_approximations/report.html' print('writing to %s' % fn) r.to_html(fn)
def go(): ieee_fonts_zoom3(pylab) r = Report() algos = [InvMult2.ALGO_UNIFORM, InvMult2.ALGO_VAN_DER_CORPUT] for algo in algos: InvMult2.ALGO = algo InvPlus2.ALGO = algo print("Using algorithm %s " % algo) with r.subsection(algo) as r2: # first F = parse_poset("dimensionless") R = F dp = InvMult2(F, (R, R)) ns = [3, 4, 5, 6, 10, 15] axis = (0.0, 6.0, 0.0, 6.0) with r2.subsection("invmult2") as rr: go1(rr, ns, dp, plot_nominal_invmult, axis) # second axis = (0.0, 1.2, 0.0, 1.2) dp = InvPlus2(F, (R, R)) with r2.subsection("invplus2") as rr: go1(rr, ns, dp, plot_nominal_invplus, axis) fn = "out-plot_approximations/report.html" print("writing to %s" % fn) r.to_html(fn)
def main(): parser = OptionParser() parser.add_option("--outdir", type="string", help='Directory containing data') (options, args) = parser.parse_args() #@UnusedVariable assert not args variables = os.path.join(options.outdir, 'variables.pickle.part') data = pickle.load(open(variables, 'rb')) d = OpenStruct(**data) d.R = d.correlation d.num_ref, d.num_sensels = d.R.shape assert d.num_ref <= d.num_sensels d.imshape = (100, 100) # XXX d.toimg = lambda x : x.reshape(d.imshape) r = Report('calibrator_analysis') r.add_child(new_analysis(data)) r.add_child(correlation_embedding_report(R=data['correlation'], num_eig=6)) r.add_child(show_some_correlations(d, num=20)) filename = os.path.join(options.outdir, 'supersensels.html') print("Writing to %r" % filename) r.to_html(filename)
def save_graph(self): """ Saves a copy of the progress so far """ r = Report(self.id_dds) outdir = "out/cover-progress/%s/" % self.id_dds self.draw_graph(r) filename = os.path.join(outdir, "graphs.html") logger.info("Writing to %r" % filename) r.to_html(filename, write_pickle=True)
def save_graph(self): """ Saves a copy of the progress so far """ r = Report(self.id_dds) outdir = 'out/cover-progress/%s/' % self.id_dds self.draw_graph(r) filename = os.path.join(outdir, 'graphs.html') logger.info('Writing to %r' % filename) r.to_html(filename)
def save_graph(self): """ Saves a copy of the progress so far """ r = Report(self.id_dds) outdir = 'out/cover-progress/%s/' % self.id_dds self.draw_graph(r) filename = os.path.join(outdir, 'graphs.html') logger.info('Writing to %r' % filename) r.to_html(filename, write_pickle=True)
def create_report(data, image, outdir): r = Report('%s_stats' % image) xcorr = data['results'] lags = data['lags'] T = lags * (1.0 / 60) * 1000 mean_xcorr = numpy.mean(xcorr, axis=0) min_xcorr = numpy.min(xcorr, axis=0) max_xcorr = numpy.max(xcorr, axis=0) with r.data_pylab('some') as pylab: for i in range(0, 1000, 50): pylab.plot(T, xcorr[i, :], 'x-', label='%d' % i) pylab.axis([T[0], T[-1], -0.5, 1]) pylab.xlabel('delay (ms)') pylab.ylabel('autocorrelation') pylab.legend() with r.data_pylab('mean_xcorr') as pylab: pylab.plot(T, mean_xcorr, 'x-') pylab.plot([T[0], T[-1]], [0, 0], 'k-') pylab.plot([0, 0], [-0.5, 1], 'k-') pylab.axis([T[0], T[-1], -0.5, 1.1]) pylab.xlabel('delay (ms)') pylab.ylabel('autocorrelation') with r.data_pylab('various') as pylab: pylab.plot(T, mean_xcorr, 'gx-', label='mean') pylab.plot(T, min_xcorr, 'bx-', label='min') pylab.plot(T, max_xcorr, 'rx-', label='max') pylab.plot([T[0], T[-1]], [0, 0], 'k-') pylab.plot([0, 0], [-0.5, 1], 'k-') pylab.axis([T[0], T[-1], -0.5, 1.1]) pylab.xlabel('delay (ms)') pylab.ylabel('autocorrelation') pylab.legend() f = r.figure() f.sub('some', caption='Autocorrelation of some receptors') f.sub('mean_xcorr', caption='Mean autocorrelation') f.sub('various', caption='Mean,min,max') filename = os.path.join(outdir, r.id + '.html') resources = os.path.join(outdir, 'images') print 'Writing to %s' % filename r.to_html(filename, resources) return r
def __init__(self): report = Report('id', caption='env1d') self.N = 1000 self.res = 10 x = np.linspace(0, 10, self.N * self.res) self.E = scipy.convolve(np.random.ranf(len(x)), np.ones(self.res * 20) / self.res * 20, mode='same') plot_env(report, x, self.E) self.commands = [-2.5, 2.0] self.n_sampels = [0, 0] self.sensels = [30, 31] self.state = self.N / 2 self.plot_y = False self.plot_e = False self.size = 60 self.area = 9 self.s = range(self.size) self.clean() lsize = 20 sensor_noise = 0 actuator_noise = 0 self.run_learning(lsize, actuator_noise=actuator_noise, sensor_noise=sensor_noise) report.text('info0', ('Learning size: \t\t%g \nActuator noise: \t%g ' + '\nSensor noise: \t\t%g') % (lsize, actuator_noise, sensor_noise)) report.text('commands', str(self.commands)) self.summarize(report, 0) self.state = self.N / 2 self.clean() lsize = 100 sensor_noise = 0 actuator_noise = 2 self.run_learning(lsize, actuator_noise=actuator_noise, sensor_noise=sensor_noise) report.text('info1', ('Learning size: \t\t%g \nActuator noise: \t%g ' + '\nSensor noise: \t\t%g') % (lsize, actuator_noise, sensor_noise)) self.summarize(report, 1) self.state = self.N / 2 self.clean() # lsize = 1000 sensor_noise = 2 actuator_noise = 0 self.run_learning(lsize, actuator_noise=actuator_noise, sensor_noise=sensor_noise) report.text('info2', ('Learning size: \t\t%g \nActuator noise: \t%g ' + '\nSensor noise: \t\t%g') % (lsize, actuator_noise, sensor_noise)) self.summarize(report, 2) report.to_html('env1d.html')
def plot_different_solutions(libname, ndpname, query, out, upper=None): if not os.path.exists(out): os.makedirs(out) library = get_test_library(libname) #library.use_cache_dir(os.path.join(out, 'cache')) context = Context() ndp = library.load_ndp(ndpname, context) context = library._generate_context_with_hooks() ndp_labelled = get_labelled_version(ndp) dp0 = ndp_labelled.get_dp() if upper is not None: _, dpU = get_dp_bounds(dp0, nl=1, nu=upper) dp = dpU else: dp = dp0 M = dp.get_imp_space() with open(os.path.join(out, 'ndp.txt'), 'w') as f: f.write(ndp.repr_long()) with open(os.path.join(out, 'M.txt'), 'w') as f: f.write(M.repr_long()) with open(os.path.join(out, 'dp.txt'), 'w') as f: f.write(dp.repr_long()) with open(os.path.join(out, 'dp0.txt'), 'w') as f: f.write(dp0.repr_long()) f = convert_string_query(ndp=ndp, query=query, context=context) report = Report() res = dp.solve(f) print('num solutions: %s' % len(res.minimals)) for ri, r in enumerate(res.minimals): ms = dp.get_implementations_f_r(f, r) for j, m in enumerate(ms): imp_dict = get_imp_as_recursive_dict(M, m) print imp_dict images_paths = library.get_images_paths() gv = GetValues(ndp=ndp, imp_dict=imp_dict, nu=upper, nl=1) gg = gvgen_from_ndp(ndp=ndp, style=STYLE_GREENREDSYM, images_paths=images_paths, plotting_info=gv) with report.subsection('%s-%s' % (ri, j)) as rr: gg_figure(rr, 'figure', gg, do_png=True, do_pdf=False, do_svg=False, do_dot=False) fn = os.path.join(out, 'solutions.html') print('writing to %s' % fn) report.to_html(fn)
def main(): cp = ClientProcess() cp.config_stimulus_xml(example_stim_xml) position = [0.5, 0.5, 0.5] linear_velocity_body = [0, 0, 0] angular_velocity_body = [0, 0, 0] r = Report('am-I-crazy-test') f = r.figure('varying theta', shape=(3, 3)) f2 = r.figure('varying x', shape=(3, 3)) f3 = r.figure('varying y', shape=(3, 3)) desc = lambda position, theta: 'At x: %.2f, y: %.2f, z: %.2f, theta: %d deg' % \ (position[0], position[1], position[2], numpy.degrees(theta)) idm = lambda position, theta, t: "%s-x:%.2f,y:%.2f,z:%.2f,th:%.3f" % (t, position[0], position[1], position[2], theta) for theta in numpy.linspace(0, 2 * numpy.pi, 16): position = [0.5, 0.5, 0.5] attitude = rotz(theta) res = cp.render(position, attitude, linear_velocity_body, angular_velocity_body) lum = res['luminance'] id = idm(position, theta, 'theta') r.data_rgb(id, plot_luminance(lum)) f.sub(id, desc(position, theta)) for x in numpy.linspace(0, 1, 20): position = [x, 0, 0.1] theta = 0 res = cp.render(position, attitude, linear_velocity_body, angular_velocity_body) id = idm(position, theta, 'x') r.data_rgb(id, plot_luminance(res['luminance'])) f2.sub(id, desc(position, theta)) for y in numpy.linspace(0, 1, 20): position = [0, y, 0.1] theta = 0 res = cp.render(position, attitude, linear_velocity_body, angular_velocity_body) id = idm(position, theta, 'y') r.data_rgb(id, plot_luminance(res['luminance'])) f3.sub(id, desc(position, theta)) filename = 'demo_pipe_rotation_experimenting.html' print "Writing to %s" % filename r.to_html(filename) cp.close()
def test_coords1(): vl = np.array([ 0, 1, 0, ]) va = np.array([np.deg2rad(20), 0, 0]) vel = { 'F': vl, 'FL': vl + va, 'FR': vl - va, 'B': (-vl), 'BL': (-vl + va), 'BR': (-vl - va), } def make_motion(v): A = se2.algebra_from_vector(v) Q = SE2.group_from_algebra(A) return Q motions = dictmap(make_motion, vel) print motions for k, v in motions.items(): print(' - %s: %s -> %s' % (k, vel[k], SE2.friendly(v))) names = sorted(vel.keys()) def commuting(a, b): q1 = motions[a] q2 = motions[b] return SE2.distance(SE2.multiply(q1, q2), SE2.multiply(q2, q1)) def same(a, b): q1 = motions[a] q2 = motions[b] return SE2.distance(q1, q2) def anti(a, b): q1 = motions[a] q2 = motions[b] return SE2.distance(q1, SE2.inverse(q2)) cD = construct_matrix_iterators((names, names), commuting) aD = construct_matrix_iterators((names, names), anti) D = construct_matrix_iterators((names, names), same) r = Report('test_coords1') r.table('D', data=D, cols=names, rows=names, fmt='%f') r.table('aD', data=aD, cols=names, rows=names, fmt='%f') r.table('cD', data=cD, cols=names, rows=names, fmt='%f') r.to_html('out/test_coords1/test_coords1.html')
def create_report(self): report = Report('OnlinePlanning') report.text('summary', 'Result report for online planning') # Plot images for job in self.plots['line_graph_mean']: graph_errorbar(report, self.all_stats, job['x_axis'], job['function'], job['categorize']) filename = '/home/adam/public_html/testrep.html' report.to_html(filename)
def create_report(self): report = Report('OnlinePlanning') report.text('summary', 'Result report for online planning') # Plot images for job in self.plots['line_graph_mean']: graph_errorbar(report, self.all_stats, job['x_axis'], job['function'], job['categorize']) filename = '/home/adam/public_html/testrep.html' report.to_html(filename)
def test_imp_dict_1(id_ndp, ndp): if '_inf' in id_ndp: # infinite return try: ndp.check_fully_connected() except NotConnected: print('Skipping test_imp_dict_1 because %r not connected.' % id_ndp) return ndp_labeled = get_labelled_version(ndp) dp0 = ndp_labeled.get_dp() F = dp0.get_fun_space() I = dp0.get_imp_space() # print ndp_labeled.repr_long() # print dp0.repr_long() print('I: %s' % I.repr_long()) f = list(F.get_minimal_elements())[0] try: ur = dp0.solve(f) except NotSolvableNeedsApprox: return imp_dict = None for r in ur.minimals: imps = dp0.get_implementations_f_r(f, r) for imp in imps: I.belongs(imp) context = {} imp_dict = get_imp_as_recursive_dict(I, imp) print('imp_dict: {}'.format(imp_dict)) artifact = ndp_make(ndp, imp_dict, context) print('artifact: {}'.format(artifact)) # Let's just do it with one if imp_dict is not None: gv = GetValues(ndp=ndp, imp_dict=imp_dict, nu=None, nl=None) images_paths = [] # library.get_images_paths() from mcdp_report.gdc import STYLE_GREENREDSYM gg = gvgen_from_ndp(ndp=ndp, style=STYLE_GREENREDSYM, images_paths=images_paths, plotting_info=gv) from reprep import Report from mcdp_report.gg_utils import gg_figure report = Report() gg_figure(report, 'figure', gg, do_png=True, do_pdf=False, do_svg=False, do_dot=False) fn = os.path.join('out', 'test_imp_dict_1', '%s.html' % id_ndp) print('written to %s' % fn) report.to_html(fn)
def test_imp_dict_1(id_ndp, ndp): if '_inf' in id_ndp: # infinite return try: ndp.check_fully_connected() except NotConnected: print('Skipping test_imp_dict_1 because %r not connected.' % id_ndp) return ndp_labeled = get_labelled_version(ndp) dp0 = ndp_labeled.get_dp() F = dp0.get_fun_space() I = dp0.get_imp_space() # print ndp_labeled.repr_long() # print dp0.repr_long() print('I: %s' % I.repr_long()) f = list(F.get_minimal_elements())[0] try: ur = dp0.solve(f) except NotSolvableNeedsApprox: return imp_dict = None for r in ur.minimals: imps = dp0.get_implementations_f_r(f, r) for imp in imps: I.belongs(imp) context = {} imp_dict = get_imp_as_recursive_dict(I, imp) print('imp_dict: {}'.format(imp_dict)) artifact = ndp_make(ndp, imp_dict, context) print('artifact: {}'.format(artifact)) # Let's just do it with one if imp_dict is not None: gv = GetValues(ndp=ndp, imp_dict=imp_dict, nu=None, nl=None) # images_paths = [] # library.get_images_paths() from mcdp_report.gdc import STYLE_GREENREDSYM gg = gvgen_from_ndp(ndp=ndp, style=STYLE_GREENREDSYM, plotting_info=gv) from reprep import Report from mcdp_report.gg_utils import gg_figure report = Report() gg_figure(report, 'figure', gg, do_png=True, do_pdf=False, do_svg=False, do_dot=False) fn = os.path.join('out', 'test_imp_dict_1', '%s.html' % id_ndp) print('written to %s' % fn) report.to_html(fn)
def test_coords1(): vl = np.array([0, 1, 0, ]) va = np.array([np.deg2rad(20), 0, 0]) vel = { 'F': vl, 'FL': vl + va, 'FR': vl - va, 'B': (-vl), 'BL': (-vl + va), 'BR': (-vl - va), } def make_motion(v): A = se2.algebra_from_vector(v) Q = SE2.group_from_algebra(A) return Q motions = dictmap(make_motion, vel) print motions for k, v in motions.items(): print(' - %s: %s -> %s' % (k, vel[k], SE2.friendly(v))) names = sorted(vel.keys()) def commuting(a, b): q1 = motions[a] q2 = motions[b] return SE2.distance(SE2.multiply(q1, q2), SE2.multiply(q2, q1)) def same(a, b): q1 = motions[a] q2 = motions[b] return SE2.distance(q1, q2) def anti(a, b): q1 = motions[a] q2 = motions[b] return SE2.distance(q1, SE2.inverse(q2)) cD = construct_matrix_iterators((names, names), commuting) aD = construct_matrix_iterators((names, names), anti) D = construct_matrix_iterators((names, names), same) r = Report('test_coords1') r.table('D', data=D, cols=names, rows=names, fmt='%f') r.table('aD', data=aD, cols=names, rows=names, fmt='%f') r.table('cD', data=cD, cols=names, rows=names, fmt='%f') r.to_html('out/test_coords1/test_coords1.html')
def main(): N = 100 num_svds = 8 radius_deg = 180 kernels = [identity, linear01_sat, pow3_sat, pow7_sat] # kernels = [linear01_sat, pow3_sat, pow7_sat] r = Report('eig analysis') # warps_desc = ", ".join(['%.2f' % x for x in warps]) caption = """ This figure shows that on S^1 things can be warped easily. The initial distribution of {N} points, with radius {radius_deg}. """.format(**locals()) f = r.figure(caption=caption) mime = 'application/pdf' figsize = (4, 3) with r.data_pylab('kernels', mime=mime, figsize=figsize) as pylab: for kernel in kernels: x = np.linspace(-1, +1, 256) y = kernel(x) pylab.plot(x, y, label=kernel.__name__) pylab.axis([-1, 1, -1, 1]) pylab.xlabel('Cosine between orientations') pylab.ylabel('Correlation') pylab.legend(loc='lower right') r.last().add_to(f, caption='Correlation kernels') for ndim in [2, 3]: S = get_distribution(ndim, N, radius_deg) C = cosines_from_directions(S) D = distances_from_directions(S) assert np.degrees(D.max()) <= 2 * radius_deg with r.data_pylab('svds%d' % ndim, mime=mime, figsize=figsize) as pylab: for kernel in kernels: Cw = kernel(C) # TODO: # Cw = cos(kernel(D)) s = svds(Cw, num_svds) pylab.semilogy(s, 'x-', label=kernel.__name__) pylab.legend(loc='center right') r.last().add_to( f, caption='Singular value for different kernels (ndim=%d)' % ndim) filename = 'cbc_demos/kernels.html' print("Writing to %r." % filename) r.to_html(filename)
def main(): N = 100 num_svds = 8 radius_deg = 180 kernels = [identity, linear01_sat, pow3_sat, pow7_sat] # kernels = [linear01_sat, pow3_sat, pow7_sat] r = Report('eig analysis') # warps_desc = ", ".join(['%.2f' % x for x in warps]) caption = """ This figure shows that on S^1 things can be warped easily. The initial distribution of {N} points, with radius {radius_deg}. """.format(**locals()) f = r.figure(caption=caption) mime = 'application/pdf' figsize = (4, 3) with r.data_pylab('kernels', mime=mime, figsize=figsize) as pylab: for kernel in kernels: x = np.linspace(-1, +1, 256) y = kernel(x) pylab.plot(x, y, label=kernel.__name__) pylab.axis([-1, 1, -1, 1]) pylab.xlabel('Cosine between orientations') pylab.ylabel('Correlation') pylab.legend(loc='lower right') r.last().add_to(f, caption='Correlation kernels') for ndim in [2, 3]: S = get_distribution(ndim, N, radius_deg) C = cosines_from_directions(S) D = distances_from_directions(S) assert np.degrees(D.max()) <= 2 * radius_deg with r.data_pylab('svds%d' % ndim, mime=mime, figsize=figsize) as pylab: for kernel in kernels: Cw = kernel(C) # TODO: # Cw = cos(kernel(D)) s = svds(Cw, num_svds) pylab.semilogy(s, 'x-', label=kernel.__name__) pylab.legend(loc='center right') r.last().add_to(f, caption='Singular value for different kernels (ndim=%d)' % ndim) filename = 'cbc_demos/kernels.html' print("Writing to %r." % filename) r.to_html(filename)
def display_current_results(learner, name, dirname, iteration): dds = learner.summarize(prefix=name) r = Report('%s-it%s' % (name, iteration)) r.text('summary', 'Iteration: %s' % iteration) base = '%s-current.html' % (name) filename = os.path.join(dirname, 'iterations', base) # TODO: add file f = '/opt/EPD/7.3/lib/python2.7/site-packages/PIL/Images/lena.jpg' lena = imread(f) image = UncertainImage(lena) dds.display(r, image) logger.info('Writing to %r.' % filename) r.to_html(filename)
def display_current_results(learner, name, dirname, iteration): dds = learner.summarize(prefix=name) r = Report('%s-it%s' % (name, iteration)) r.text('summary', 'Iteration: %s' % iteration) base = '%s-current.html' % (name) filename = os.path.join(dirname, 'iterations', base) # TODO: add file f = '/opt/EPD/7.3/lib/python2.7/site-packages/PIL/Images/lena.jpg' lena = imread(f) image = UncertainImage(lena) dds.display(r, image) logger.info('Writing to %r.' % filename) r.to_html(filename)
def all_demos(argv): # @UnusedVariable if len(argv) == 0: which = DemoStorage.demos.keys() else: which = argv print(DemoStorage.demos.keys()) r = Report("reprep_demos") for id_f in which: demof = DemoStorage.demos[id_f] ri = r.section(nid="%s" % demof.__name__, caption=demof.__doc__) ri.text("source", inspect.getsource(demof)) with ri.subsection("output") as sub: demof(sub) r.to_html("reprep_demos_out/index.html")
def main(): data = yaml.load(sys.stdin) r = Report('plot_activities') with r.data_pylab('activities') as pylab: for activity, stats in data.items(): ndays = max(stats.keys()) + 1 accum = np.zeros(ndays) for day, amount in stats.items(): accum[day] = amount x = range(ndays) pylab.plot(x, accum, label=activity) pylab.legend() r.to_html('out/plots.html')
def go_(model_name, queries, result_like, what_to_plot_res, what_to_plot_fun, fn): lib = MCDPLibrary() lib.add_search_dir('.') ndp = lib.load_ndp(model_name) data = solve_queries(ndp, queries, result_like) r = Report() plot_all_directions(r, queries=data['queries'], results=data['results'], what_to_plot_res=what_to_plot_res, what_to_plot_fun=what_to_plot_fun) print('writing to %r' % fn) r.to_html(fn)
def create_report(outdir, combination_id, saccades): r = Report(combination_id) stats = 'Combination %r has %d saccades' % (combination_id, len(saccades)) r.text('stats', stats) desc = "" #r.add_child(create_report_subset(combination_id,desc, saccades)) #r.add_child(create_report_randomness(combination_id, desc, saccades)) r.add_child(create_report_axis_angle(combination_id, desc, saccades)) rd = os.path.join(outdir, 'images') out = os.path.join(outdir, 'combinations', '%s.html' % combination_id) print('Writing to %r' % out) r.to_html(out, resources_dir=rd)
def all_demos(argv): #@UnusedVariable if len(argv) == 0: which = DemoStorage.demos.keys() else: which = argv print(DemoStorage.demos.keys()) r = Report('reprep_demos') for id_f in which: demof = DemoStorage.demos[id_f] ri = r.section(nid='%s' % demof.__name__, caption=demof.__doc__) ri.text('source', inspect.getsource(demof)) with ri.subsection('output') as sub: demof(sub) r.to_html('reprep_demos_out/index.html')
def test_consistency_uncertainty(): print 'here' pass contracts.disable_all() symdds = 'sym-dpchain1-120' print('instancing dds %s' % symdds) dds = get_conftools_discdds().instance(symdds) shape = dds.get_shape() d1f = dds.actions[0].get_diffeo2d_forward() d1b = dds.actions[0].get_diffeo2d_backward() fb = Diffeomorphism2D.compose(d1f, d1b) bf = Diffeomorphism2D.compose(d1b, d1f) identity = Diffeomorphism2D.identity(shape) print Diffeomorphism2D.distance_L2_infow(d1f, identity) print Diffeomorphism2D.distance_L2_infow(d1b, identity) print Diffeomorphism2D.distance_L2_infow(fb, identity) print Diffeomorphism2D.distance_L2_infow(bf, identity) action = dds.actions[0] action2 = consistency_based_uncertainty(action, None) r = Report(symdds) r.text('symdds', symdds) with r.subsection('action') as sub: action.display(sub) with r.subsection('action2') as sub: action2.display(sub) # # with r.subsection('misc') as sub: # d = d1f.get_discretized_diffeo() # f = sub.figure() # f.array_as_image('d0', d[:, :, 0]) # f.array_as_image('d1', d[:, :, 1]) # # with r.subsection('d1f') as sub: # d1f.display(sub) # with r.subsection('d1b') as sub: # d1b.display(sub) # # with r.subsection('fb') as sub: # fb.display(sub) # with r.subsection('bf') as sub: # bf.display(sub) r.to_html('test_consistency_uncertainty.html')
def go_(model_name, queries, result_like, what_to_plot_res, what_to_plot_fun, fn): lib = MCDPLibrary() lib.add_search_dir('.') ndp = lib.load_ndp(model_name) data = solve_queries(ndp, queries, result_like) r = Report() plot_all_directions(r, queries=data['queries'], results=data['results'], what_to_plot_res=what_to_plot_res, what_to_plot_fun=what_to_plot_fun) print('writing to %r' % fn) r.to_html(fn)
def make_report(learners): print('make_report(learners) in diffeomorphism2d_continuous is used') for i, name in enumerate(learners): # init report report = Report(learners[i]) learner = pickle.load(open(name)) diffeo = learner.estimators[0].summarize() learner.estimators[0].show_areas(report, diffeo.d) cmd = learner.command_list[0] # pdb.set_trace() report.text('learner' + str(i), name) report.text('cmd' + str(i), str(cmd)) diffeo.display(report, nbins=500) # Save report report.to_html(learners[i] + '.html')
def make_report(learners): print('make_report(learners) in diffeomorphism2d_continuous is used') for i, name in enumerate(learners): # init report report = Report(learners[i]) learner = pickle.load(open(name)) diffeo = learner.estimators[0].summarize() learner.estimators[0].show_areas(report, diffeo.d) cmd = learner.command_list[0] # pdb.set_trace() report.text('learner' + str(i), name) report.text('cmd' + str(i), str(cmd)) diffeo.display(report, nbins=500) # Save report report.to_html(learners[i] + '.html')
def creation_suite(fid, f): # @UnusedVariable shape = [50, 50] D = diffeomorphism_from_function(shape, f) from reprep import Report name = f.__name__ r = Report(name) fig = r.figure() M = 1 n1 = 10 n2 = 100 bx = [-.99, +.99] # depends on fmod by = bx params = dict(figsize=(3, 3)) def common_settings(pylab): pylab.axis('equal') pylab.axis((-M, M, -M, M)) turn_all_axes_off(pylab) with fig.plot('grid1', **params) as pylab: curved = CurvedPylab(pylab, f) plot_grid(curved, n1=n1, n2=n2, bx=bx, by=by, hcol='k-', vcol='k-') common_settings(pylab) with fig.plot('grid2', caption="different colors", **params) as pylab: plot_grid(curved, n1=n1, n2=n2, bx=bx, by=by, hcol='r-', vcol='b-') common_settings(pylab) with fig.plot('grid3', caption="smiley", **params) as pylab: plot_grid(curved, n1=n1, n2=n2, bx=bx, by=by, hcol='r-', vcol='b-') common_settings(pylab) plot_smiley(curved) with fig.plot('grid4', caption="smiley", **params) as pylab: plot_grid(curved, n1=n1, n2=n2, bx=bx, by=by, hcol='k-', vcol='k-') common_settings(pylab) plot_smiley(curved, '0.5') rgb = diffeomorphism_to_rgb(D) r.data_rgb('diffeomorphism_rgb', rgb).add_to(fig) filename = 'out/diffeo_creation_suite/%s.html' % name print('Writing to %r.' % filename) r.to_html(filename)
def creation_suite(fid, f): # @UnusedVariable shape = [50, 50] D = diffeomorphism_from_function(shape, f) from reprep import Report name = f.__name__ r = Report(name) fig = r.figure() M = 1 n1 = 10 n2 = 100 bx = [-.99, +.99] # depends on fmod by = bx params = dict(figsize=(3, 3)) def common_settings(pylab): pylab.axis('equal') pylab.axis((-M, M, -M, M)) turn_all_axes_off(pylab) with fig.plot('grid1', **params) as pylab: curved = CurvedPylab(pylab, f) plot_grid(curved, n1=n1, n2=n2, bx=bx, by=by, hcol='k-', vcol='k-') common_settings(pylab) with fig.plot('grid2', caption="different colors", **params) as pylab: plot_grid(curved, n1=n1, n2=n2, bx=bx, by=by, hcol='r-', vcol='b-') common_settings(pylab) with fig.plot('grid3', caption="smiley", **params) as pylab: plot_grid(curved, n1=n1, n2=n2, bx=bx, by=by, hcol='r-', vcol='b-') common_settings(pylab) plot_smiley(curved) with fig.plot('grid4', caption="smiley", **params) as pylab: plot_grid(curved, n1=n1, n2=n2, bx=bx, by=by, hcol='k-', vcol='k-') common_settings(pylab) plot_smiley(curved, '0.5') rgb = diffeomorphism_to_rgb(D) r.data_rgb('diffeomorphism_rgb', rgb).add_to(fig) filename = 'out/diffeo_creation_suite/%s.html' % name print('Writing to %r.' % filename) r.to_html(filename)
def go(lib): combinations = { "capacity": (np.linspace(50, 3000, 10), "Wh"), "missions": ( 1000, "[]"), } result_like = dict(maintenance="dimensionless", cost="USD", mass='kg') what_to_plot_res = result_like what_to_plot_fun = dict(capacity="Wh", missions="[]") ndp = lib.load_ndp('batteries') data = solve_combinations(ndp, combinations, result_like) r = Report() plot_all_directions(r, queries=data['queries'], results=data['results'], what_to_plot_res=what_to_plot_res, what_to_plot_fun=what_to_plot_fun) r.to_html('out/batteries-c1.html')
def go2(lib): model_name = 'batteries_squash' combinations = { "capacity": (np.linspace(50, 3000, 10), "Wh"), "missions": (1000, "[]"), } result_like = dict(cost="USD", mass='kg') what_to_plot_res = result_like what_to_plot_fun = dict(capacity="Wh", missions="[]") ndp = lib.load_ndp(model_name) data = solve_combinations(ndp, combinations, result_like) r = Report() plot_all_directions(r, queries=data['queries'], results=data['results'], what_to_plot_res=what_to_plot_res, what_to_plot_fun=what_to_plot_fun) r.to_html('out/batteries_squash-c2.html')
def go(): model_name = 'droneC' queries = [] def add(q): queries.append(q) n = 10 endurance = np.linspace(1, 20, n) payload = np.linspace(5, 50, n) for endurance, payload in zip(endurance, payload): q = { "num_missions": (1000, "[]"), "extra_power": (5, "W"), "extra_payload": (payload, "g"), "endurance": (endurance, "minutes"), } add(q) result_like = dict(total_cost="CHF", total_mass='kg') what_to_plot_res = result_like what_to_plot_fun = dict(extra_payload="g", endurance="minutes") librarian = Librarian() librarian.find_libraries('..') lib = librarian.load_library('droneC_cost_v1') ndp = lib.load_ndp(model_name) data = solve_queries(ndp, queries, result_like) r = Report() plot_all_directions(r, queries=data['queries'], results=data['results'], what_to_plot_res=what_to_plot_res, what_to_plot_fun=what_to_plot_fun) fn = 'out/droneC_c1.html' print('writing to %r' % fn) r.to_html(fn)
def main(): theta = get_uniform_directions(360, 180) D = distances_from_angles(theta) def identity(x): return x def exp1(x): return np.exp(-x) def exp2(x): return np.exp(-5 * x) def exp3(x): return np.exp(-x * x) def p1(x): return np.cos(2 * np.pi - x) functions = [identity, exp1, exp2, exp3, p1] r = Report() for function in functions: name = function.__name__ section = r.section(name) f = section.figure() M = function(D) sigma = 0.0001 D1 = np.maximum(0, D + sigma * np.random.randn(*D.shape)) M1 = function(D1) with f.plot('svd') as pylab: plot_matrix_svd(pylab, M) with f.plot('svd1', caption='Perturbed') as pylab: plot_matrix_svd(pylab, M1) out = 'check_rank.html' print('Writing to %r' % out) r.to_html(out)
def main(): parser = OptionParser() parser.add_option("--file", help='Pickle file') parser.add_option("--outdir") (options, args) = parser.parse_args() #@UnusedVariable assert not args data = pickle.load(open(options.file, 'rb')) select = range(181) for x in ['y_cov', 'y_dot_cov', 'y_dot_sign_cov']: data[x] = data[x][select, :][:, select] check('array[NxN]', data[x]) n = data['y_cov'].shape[0] #theta = numpy.linspace(0, numpy.pi * 2, n) theta = numpy.linspace(0, numpy.pi, n) d = OpenStruct(**data) # groundtruth d.theta = theta d.S = create_s_from_theta(d.theta) d.C = get_cosine_matrix_from_s(d.S) d.D = get_distance_matrix_from_cosine(d.C) r = Report('calibrator_analysis') #r.add_child(simple_plots(d)) #r.add_child(ground_truth_plots(d)) #r.add_child(hist_plots(d)) r.add_child(iterations_plots(d)) filename = os.path.join(options.outdir, 'calib_1d_stats_plots.html') print("Writing to %r" % filename) r.to_html(filename)
def create_and_write_report(flydra_db, sample, image_name): view_start = 'saccades_view_start_%s' % image_name view_stop = 'saccades_view_stop_%s' % image_name view_rstop = 'saccades_view_rstop_%s' % image_name db = FlydraDB(flydra_db, False) saccades = db.get_saccades(sample) values_start = db.get_table(sample, view_start) values_stop = db.get_table(sample, view_stop) values_rstop = db.get_table(sample, view_rstop) r = Report(sample) for i in range(len(saccades)): ri = r.node("saccade-%04d" % i) ri.data_rgb('start', plot_luminance(values_start[i]['value'])) ri.data_rgb('stop', plot_luminance(values_stop[i]['value'])) ri.data_rgb('rstop', plot_luminance(values_rstop[i]['value'])) f = ri.figure(shape=(1, 3)) f.sub('start', 'At saccade start') f.sub('stop', 'At saccade stop') #f.sub('rstop', '(random stop)') db.release_table(saccades) db.release_table(values_start) db.release_table(values_stop) db.release_table(values_rstop) filename = "%s/out/saccade_view_show/%s_%s.html" % (flydra_db, image_name, sample) print "Writing to %s" % filename r.to_html(filename)
def main(): parser = OptionParser() parser.add_option("--outdir", type="string", help='Directory containing data') (options, args) = parser.parse_args() #@UnusedVariable assert not args variables = os.path.join(options.outdir, 'results.pickle.part') data = pickle.load(open(variables, 'rb')) d = OpenStruct(**data) d.P_inv = get_information_matrix(d.P) d.Gn = normalize_G(d.G, d.P_inv) d.Q_inv = numpy.linalg.pinv(d.Q) d.Gnn = normalize_input(d.Gn, d.Q_inv) r = Report('boot') r.add_child(cov_plots(d)) r.add_child(basic_plots(d)) dir = os.path.join(options.outdir, 'plots') filename = os.path.join(dir, 'index.html') print("Writing report to %r." % filename) r.to_html(filename, resources_dir=dir) data = { 'Gnn': d.Gnn, 'Gn': d.Gn, 'G': d.G, 'Q': d.Q, 'Q_inv': d.Q_inv, 'P_inv': d.P_inv } filename = os.path.join(options.outdir, 'tensors.pickle') print("Writing tensors to %r." % filename) with open(filename, 'wb') as f: pickle.dump(data, f)
def go(): fn = 'out/actuation_c1.html' model_name = 'actuation' queries = [] def add(q): queries.append(q) n = 10 lifts = np.linspace(0, 10.0, n) for lift, in zip(lifts): q = { "lift": (lift, "N"), } add(q) result_like = dict(power="W", cost='$') what_to_plot_res = result_like what_to_plot_fun = dict(lift="N") lib = MCDPLibrary() lib.add_search_dir('.') ndp = lib.load_ndp(model_name) data = solve_queries(ndp, queries, result_like) r = Report() plot_all_directions(r, queries=data['queries'], results=data['results'], what_to_plot_res=what_to_plot_res, what_to_plot_fun=what_to_plot_fun) print('writing to %r' % fn) r.to_html(fn)
def go(): fn = 'out/actuation_c1.html' model_name = 'actuation' queries = [] def add(q): queries.append(q) n = 10 lifts = np.linspace(0, 10.0, n) for lift, in zip(lifts): q = { "lift": (lift, "N"), } add(q) result_like = dict(power="W", cost='$') what_to_plot_res = result_like what_to_plot_fun = dict(lift="N") lib = MCDPLibrary() lib.add_search_dir('.') ndp = lib.load_ndp(model_name) data = solve_queries(ndp, queries, result_like) r = Report() plot_all_directions(r, queries=data['queries'], results=data['results'], what_to_plot_res=what_to_plot_res, what_to_plot_fun=what_to_plot_fun) print('writing to %r' % fn) r.to_html(fn)
def estimation(fid, f): # @UnusedVariable shape = [50, 50] diffeo = diffeomorphism_from_function(shape, f) K = 50 epsilon = 1 de = DiffeomorphismEstimator([0.2, 0.2], MATCH_CONTINUOUS) for y0, y1 in generate_input(shape, K, diffeo, epsilon=epsilon): de.update(y0, y1) diff2d = de.summarize() diffeo_learned = diff2d.d from reprep import Report name = f.__name__ r = Report(name) fig = r.figure(cols=4) diffeo_learned_rgb = diffeomorphism_to_rgb_cont(diffeo_learned) diffeo_rgb = diffeomorphism_to_rgb_cont(diffeo) r.data_rgb('diffeo_rgb', diffeo_rgb).add_to(fig) r.data_rgb('diffeo_learned_rgb', diffeo_learned_rgb).add_to(fig) L = r.data('diffeo_learned_uncertainty', diff2d.variance) L.display('scale').add_to(fig, caption='uncertainty') r.data('last_y0', y0).display('scale').add_to(fig, caption='last y0') r.data('last_y1', y1).display('scale').add_to(fig, caption='last y1') cs = [(0, 25), (10, 25), (25, 25), (25, 5)] for c in cs: M25 = de.get_similarity(c) r.data('cell-%s-%s' % c, M25).display('scale').add_to(fig, caption='Example similarity field') filename = 'out/diffeo_estimation_suite/%s.html' % name print('Writing to %r.' % filename) r.to_html(filename)
def main(): if not vehicles_has_cairo: logger.error('This program cannot be run if Cairo is not installed.') return from vehicles_cairo import (cairo_plot_rectangle) from vehicles_cairo import cairo_plot_circle, cairo_set_axis, show_grid parser = OptionParser(usage=usage) parser.disable_interspersed_args() parser.add_option("--outdir", "-o", default='.', help="output directory [%default]") # parser.add_option("--outdir", "-o", default='.', # help="additional config directory [%default]") (options, args) = parser.parse_args() if args: raise Exception() width = 400 height = 400 r = Report('skins_demo') f = r.figure(cols=3) import cairo VehiclesConfig.make_sure_loaded() skins = get_conftools_skins().keys() logger.info('Skins: %s' % skins) for id_skin in skins: skin = get_conftools_skins().instance(id_skin) with f.data_file(id_skin, MIME_PNG) as filename: surf = cairo.ImageSurface( cairo.FORMAT_ARGB32, # @UndefinedVariable width, height) cr = cairo.Context(surf) # @UndefinedVariable cairo_plot_rectangle(cr, 0, 0, width, height, fill_color=[1, 1, 1]) cairo_set_axis(cr, width, height, [-2, +2, -2, +2]) cairo_plot_circle(cr, center=[0, 0], radius=1, facecolor=None, edgecolor=(1, 0, 0), width=0.1) show_grid(cr, bx=[-3, +3], by=[-3, +3], spacing=1.0) if skin.njoints_required() > 1: logger.warning('Skipping skin %r' % id_skin) continue try: skin.draw(cr, joints=[]) except Exception: logger.error('Error for skin %r' % id_skin) raise surf.write_to_png(filename) # Output to PNG filename = os.path.join(options.outdir, 'index.html') logger.info('Writing to %r.' % filename) r.to_html(filename)
def detect_angvel(argv): pdb.set_trace() np.seterr(all='raise') parser = LenientOptionParser(usage=usage) parser.add_option("--db", help="FlydraDB directory.") parser.add_option( "--version", default='smooth', type='string', help="[=%default] Which version of the 'rows' table to use.") parser.add_option("--saccades_table_version", default='angvel', type='string', help='[=%default] Version of output saccades tables.') parser.add_option("--nocache", help="Ignores already computed results.", default=False, action="store_true") parser.add_option("--out", help="Output directory for graphic representation.") parser.add_option("--fps", help="Recording framerate") fps = parser.parse_args(args='--fps', type='float') # detection parameters dt = 1.0 / 60 # XXX: read from file warnings.warn('Using fixed dt = %s.' % dt) parser.add_option("--deltaT_inner_sec", default=4 * dt, type='float', help="Inner interval [= %default]") parser.add_option("--deltaT_outer_sec", default=10 * dt, type='float', help="Outer interval [= %default]") parser.add_option("--min_amplitude_deg", default=25, type='float', help="Minimum saccade amplitude (deg) [= %default]") parser.add_option("--min_linear_velocity", default=0.1, type='float', help="Minimum linear velocity when" " saccading (m/s) [= %default]") parser.add_option("--max_linear_acceleration", default=20, type='float', help="Maximum linear acceleration when saccading " "(m/s^2) [= %default]") parser.add_option("--max_angular_velocity", default=8000, type='float', help="Maximum angular velocity when saccading" " (deg/s) [= %default]") parser.add_option("--max_orientation_dispersion_deg", default=15, type='float', help="Maximum dispersion (deg) [= %default]") parser.add_option("--minimum_interval_sec", default=10 * dt, type='float', help="Minimum interval between saccades. [= %default]") (options, args) = parser.parse_args(argv) if not options.db: raise Exception('No flydra DB directory specified.') if args: raise Exception('Spurious arguments') # Create processed string processed = get_computed_string('angvel_saccade_detector', __version__) rows_table_name = NamingConventions.ROWS_TABLE rows_table_version = options.version saccades_table_name = NamingConventions.SACCADES_TABLE # annotations_table_name = 'annotated' saccades_table_version = options.saccades_table_version with safe_flydra_db_open(options.db) as db: samples = db.list_samples() for i, sample in enumerate(samples): already_has = db.has_table(sample, saccades_table_name, saccades_table_version) if options.nocache and already_has: msg = ('Sample %r already has table %s:%s; skipping.' % (sample, saccades_table_name, saccades_table_version)) logger.info(msg) continue if not db.has_table(sample, rows_table_name, rows_table_version): msg = ('Sample %r does not have table %s:%s.' % (sample, rows_table_name, rows_table_version)) raise Exception(msg) with db.safe_get_table(sample, rows_table_name, rows_table_version) as rows: rows = np.array(rows[:]) params = {} # saccades, annotated data = angvel_saccade_detect(rows, **params) saccades = data['saccades'] for saccade in saccades: check_saccade_is_well_formed(saccade) DT = rows['timestamp'][-1] - rows['timestamp'][0] logger.info( "%4d/%d %s: %6d saccades for %6d rows (%6g saccades/s)" % (i, len(samples), sample, len(saccades), len(rows), DT / len(saccades))) if True: db.set_table(sample=sample, table=saccades_table_name, data=saccades, version=saccades_table_version) db.set_attr( sample, 'saccades_%s_processed' % saccades_table_version, processed) if options.out is not None: outdir = os.path.join(options.out, 'angvel_sac_detect') if not os.path.exists(outdir): os.makedirs(outdir) resources = os.path.join(outdir, 'images') filename = os.path.join(outdir, '%s.html' % sample) r = Report() chunks = enumerate_chunks(len(rows), max_chunk_size=300) for i, select in enumerate(chunks): rows_i = rows[select] ri = plot_angvel_saccade_detect_results(rows_i) ri.nid = 'chunk_%s' % i r.add_child(ri) logger.info('Writing to %r.' % filename) r.to_html(filename, resources_dir=resources)
file1 = 'data/GOPR_groundtruth_orig.mat' file2 = 'data/GOPR_groundtruth.mat' orig_size = (1944, 2592) target_size = (1080, 1920) data1 = scipy.io.loadmat(file1) data2 = {} for k in ['X', 'Y', 'Z']: data2[k] = resize(data1[k], target_size).astype('float32') data2['S'] = get_S(data2) #data2['before_resizing'] = data1 scipy.io.savemat(file2, data2, oned_as='row') r = Report() def go(name, data): f = r.figure(name) for a in ['X', 'Y', 'Z']: print name, a, data[a].shape f.data(a, data[a]).display('posneg').add_to(f) go('data1', data1) go('data2', data2) r.to_html('data/resizing/index.html')
def invmult2_check3(): F = parse_poset('dimensionless') R1 = parse_poset('dimensionless') R2 = parse_poset('dimensionless') im = InvMult2(F, (R1, R2)) InvMult2.ALGO = InvMult2.ALGO_VAN_DER_CORPUT R = im.get_res_space() UR = UpperSets(R) # ns = [1, 2, 3, 4, 10, 15] # ns = [1, 5, 10, 15, 25, 50, 61, 100] ns = [1, 2, 3, 4, 5, 10] resL = [] resU = [] f0 = 1.0 for n in ns: dpU = im.get_upper_bound(n) dpL = im.get_lower_bound(n) urL = dpL.solve(f0) print urL print '%r' % urL.minimals check_minimal(urL.minimals, R) urU = dpU.solve(f0) check_minimal(urU.minimals, R) UR.belongs(urL) UR.belongs(urU) resL.append(urL) resU.append(urU) def plot_upper(pylab, ur, markers): points = np.array(list(ur.minimals)) eps = np.finfo(float).eps points = np.maximum(points, eps) points = np.minimum(points, 20) pylab.plot(points[:, 0], points[:, 1], markers) r = Report() f = r.figure() for n, ur in zip(ns, resL): caption = str(ur) with f.plot('resL-%d' % n, caption=caption) as pylab: for n0, ur0 in zip(ns, resL): if n0 == n: continue plot_upper(pylab, ur0, 'kx') plot_upper(pylab, ur, 'o') pylab.axis((-0.1, 10.1, -0.1, 10.1)) f = r.figure() for n, ur in zip(ns, resU): with f.plot('resU-%d' % n) as pylab: for n0, ur0 in zip(ns, resU): if n0 == n: continue plot_upper(pylab, ur0, 'kx') plot_upper(pylab, ur, 'o') pylab.axis((-0.1, 10.1, -0.1, 10.1)) fn = 'out/invmult2_check3.html' print('writing to %s' % fn) r.to_html(fn) for urU in resU: for x, y in urU.minimals: prod = x * y if prod > f0: continue else: assert_allclose(x * y, f0) # , (x, y, f0, x * y) for urL in resL: for x, y in urL.minimals: x = float(x) y = float(y) assert x * y <= f0, (x, y, f0, x * y) # check resU is DECREASING for i in range(len(resU) - 1): ur0 = resU[i] ur1 = resU[i + 1] try: UR.check_leq(ur1, ur0) except NotLeq: print('ur[%s]: %s ' % (i, UR.format(ur0))) print('ur[%s]: %s ' % (i + 1, UR.format(ur1))) raise Exception('resU is not DECREASING') # check resL is INCREASING for i in range(len(resU) - 1): ur0 = resL[i] ur1 = resL[i + 1] try: UR.check_leq(ur0, ur1) except NotLeq: print 'resL is not INCREASING' print('ur[%s]: %s x' % (i, UR.format(ur0))) print('ur[%s]: %s x ' % (i + 1, UR.format(ur1))) raise raise Exception('resL is not INCREASING') for ur0, ur1 in zip(resL, resU): UR.check_leq(ur0, ur1)
def opt_basic_1_long(): libnames = ['actuation'] library = get_test_library2(libnames) outdir = 'out/opt_basic_1' library.use_cache_dir(os.path.join(outdir, 'cache')) options = library.list_spec(SPEC_MODELS) options.remove('RigidBodyAssignID') options.remove('YoubotBaseBattery') options.remove('YoubotBaseComputer') options.remove('AdafruitDCHatCore') # XXX: this should not be case-sensitive # options.remove('raspberryPI2') # does not compile # options.remove('DaguChassis') options.remove('IRobotCreate') options.remove('YoubotBase') options.remove('duckiebot1') options.remove('duckiebot1_flatten') print('libraries: %s' % libnames) print('options: %s' % options) initial_string = """ mcdp { provides motion [`Motion] assign_id = instance abstract `RigidBodyAssignID add_budget = instance abstract mcdp { provides budget1 [USD] provides budget2 [USD] provides budget3 [USD] provides budget4 [USD] provides budget5 [USD] provides budget6 [USD] requires budget [USD] required budget >= ( provided budget1 + provided budget2 + provided budget3 + provided budget4 + provided budget5 + provided budget6 ) } requires budget >= budget required by add_budget requires ac [`AC_charging] } """ poset = library.parse_poset flabels, F0s, f0s = zip(*(('motion', poset('`Motion'), ('rb1', 0.1, 600.0)), )) rlabels, R0s, r0s = zip(*( ('ac', poset('`AC_Charging'), (('AC_power', 'TypeA', 'v110', 'f50', 200.0), 3 * 3600.0)), ('budget', poset('USD'), 1000.0), )) initial = library.parse_ndp(initial_string) opt = Optimization(library=library, options=options, flabels=flabels, F0s=F0s, f0s=f0s, rlabels=rlabels, R0s=R0s, r0s=r0s, initial=initial) i = 0 maxit = 500 out_draw_tree = os.path.join(outdir, 'optim_tree') if os.path.exists(out_draw_tree): shutil.rmtree(out_draw_tree) while not opt.is_done() and i <= maxit: opt.step() opt.print_status() opt.draw_tree(out_draw_tree) i += 1 opt.print_status() r = Report() for i, state in enumerate(opt.done): ndp = state.get_current_ndp() plot_ndp(r, 'done%d' % i, ndp, library) for i, state in enumerate(opt.states): ndp = state.get_current_ndp() with r.subsection('open%d' % i) as s: plot_ndp(s, 'open%d' % i, ndp, library) msg = 'ur: %s' % state.ur msg += '\n num_connection_options: %s' % state.num_connection_options s.text('info', msg) for i, state in enumerate(opt.abandoned): with r.subsection('abandoned%d' % i) as s: ndp = state.get_current_ndp() plot_ndp(s, 'abandoned%d' % i, ndp, library) msg = getattr(state, 'msg', '(no message)') msg += '\n ur: %s' % state.ur msg += '\n num_connection_options: %s' % state.num_connection_options s.text('info', msg) fn = os.path.join(outdir, 'opt_basic_1.html') print(fn) r.to_html(fn)
def invmult2_check2(): F = parse_poset('m') R1 = parse_poset('m') R2 = parse_poset('m') im = InvPlus2(F, (R1, R2)) R = im.get_res_space() UR = UpperSets(R) ns = [1, 2, 3, 4, 10, 15] resL = [] resU = [] f0 = 1.0 for n in ns: dpU = im.get_upper_bound(n) dpL = im.get_lower_bound(n) urL = dpL.solve(f0) urU = dpU.solve(f0) UR.belongs(urL) UR.belongs(urU) resL.append(urL) resU.append(urU) r = Report() f = r.figure() for n, ur in zip(ns, resL): with f.plot('resL-%d' % n) as pylab: for n0, ur0 in zip(ns, resL): if n0 == n: continue points = np.array(list(ur0.minimals)) pylab.plot(points[:, 0], points[:, 1], 'kx') points = np.array(list(ur.minimals)) pylab.plot(points[:, 0], points[:, 1], 'o') pylab.axis((-0.1, 1.1, -0.1, 1.1)) f = r.figure() for n, ur in zip(ns, resU): with f.plot('resU-%d' % n) as pylab: for n0, ur0 in zip(ns, resU): if n0 == n: continue points = np.array(list(ur0.minimals)) pylab.plot(points[:, 0], points[:, 1], 'kx') points = np.array(list(ur.minimals)) pylab.plot(points[:, 0], points[:, 1], 'o') pylab.axis((-0.1, 1.1, -0.1, 1.1)) fn = 'out/invmult2_check2.html' print('writing to %s' % fn) r.to_html(fn) for urU in resU: for x, y in urU.minimals: assert x + y >= f0, (x, y, f0) for urL in resL: for x, y in urL.minimals: assert x + y <= f0, (x, y, f0) # check resU is DECREASING for i in range(len(resU) - 1): ur0 = resU[i] ur1 = resU[i + 1] try: UR.check_leq(ur1, ur0) except NotLeq: print('ur[%s]: %s ' % (i, UR.format(ur0))) print('ur[%s]: %s ' % (i + 1, UR.format(ur1))) raise Exception() # check resL is INCREASING for i in range(len(resU) - 1): ur0 = resL[i] ur1 = resL[i + 1] try: UR.check_leq(ur0, ur1) except NotLeq: print 'resL is not INCREASING' print('ur[%s]: %s x' % (i, UR.format(ur0))) print('ur[%s]: %s x ' % (i + 1, UR.format(ur1))) raise raise Exception('resL is not INCREASING') for ur0, ur1 in zip(resL, resU): UR.check_leq(ur0, ur1)
def solve_main( logger, config_dirs, maindir, cache_dir, model_name, lower, upper, out_dir, max_steps, query_strings, intervals, _exp_advanced, expect_nres, imp, expect_nimp, plot, do_movie, # expect_res=None, expect_res, # @UnusedVariable make): if out_dir is None: out = solve_get_output_dir(prefix='out/out') else: out = out_dir logger.info('Using output dir %r' % out) librarian = Librarian() logger.info('Looking for libraries in %s...' % config_dirs) for e in config_dirs: librarian.find_libraries(e) logger.info('Found %d libraries.' % len(librarian.get_libraries())) library = librarian.get_library_by_dir(maindir) if cache_dir is not None: library.use_cache_dir(cache_dir) ndp = library.load_ndp(model_name) basename = model_name if make or (plot and imp): ndp_labelled = get_labelled_version(ndp) else: ndp_labelled = ndp basename, dp = solve_get_dp_from_ndp(basename=basename, ndp=ndp_labelled, lower=lower, upper=upper) F = dp.get_fun_space() R = dp.get_res_space() UR = UpperSets(R) query = " ".join(query_strings) c = library.parse_constant(query) tu = get_types_universe() try: tu.check_leq(c.unit, F) except NotLeq as e: msg = 'The value given cannot be converted to functionality space.' raise_wrapped(UserError, e, msg, unit=c.unit, F=F, compact=True) fg = express_value_in_isomorphic_space(c.unit, c.value, F) logger.info('query: %s' % F.format(fg)) tracer = Tracer(logger=logger) res, trace = solve_meat_solve_ftor(tracer, ndp, dp, fg, intervals, max_steps, _exp_advanced) nres = len(res.minimals) if expect_nres is not None: if nres != expect_nres: msg = 'Found wrong number of resources' raise_desc(ExpectationsNotMet, msg, expect_nres=expect_nres, nres=nres) if imp: M = dp.get_imp_space() nimplementations = 0 for r in res.minimals: ms = dp.get_implementations_f_r(fg, r) nimplementations += len(ms) s = 'r = %s ' % R.format(r) for j, m in enumerate(ms): # print('m = %s' % str(m)) s += "\n implementation %d of %d: m = %s " % (j + 1, len(ms), M.format(m)) if make: imp_dict = get_imp_as_recursive_dict( M, m) # , ignore_hidden=False) print('imp dict: %r' % imp_dict) context = {} artifact = ndp_make(ndp, imp_dict, context) print('artifact: %s' % artifact) tracer.log(s) if expect_nimp is not None: if expect_nimp != nimplementations: msg = 'Found wrong number of implementations' raise_desc(ExpectationsNotMet, msg, expect_nimp=expect_nimp, nimplementations=nimplementations) # if expect_res is not None: # value = interpret_string(expect_res) # tracer.log('value: %s' % value) # res_expected = value.value # tu = get_types_universe() # # If it's a tuple of two elements, then we assume it's upper/lower bounds # if isinstance(value.unit, PosetProduct): # subs = value.unit.subs # assert len(subs) == 2, subs # # lower_UR_expected, upper_UR_expected = subs # lower_res_expected, upper_res_expected = value.value # # lower_bound = tu.get_embedding(lower_UR_expected, UR)[0](lower_res_expected) # upper_bound = tu.get_embedding(upper_UR_expected, UR)[0](upper_res_expected) # # tracer.log('lower: %s <= %s' % (UR.format(lower_bound), UR.format(res))) # tracer.log('upper: %s <= %s' % (UR.format(upper_bound), UR.format(res))) # # UR.check_leq(lower_bound, res) # UR.check_leq(res, upper_bound) # else: # # only one element: equality # UR_expected = value.unit # tu.check_leq(UR_expected, UR) # A_to_B, _B_to_A = tu.get_embedding(UR_expected, UR) # # res_expected_f = A_to_B(res_expected) # try: # UR.check_equal(res, res_expected_f) # except NotEqual as e: # raise_wrapped(ExpectationsNotMet, e, 'res is different', # res=res, res_expected=res_expected, compact=True) if plot: r = Report() if _exp_advanced: from mcdp_report.generic_report_utils import generic_report generic_report(r, dp, trace, annotation=None, axis0=(0, 0, 0, 0)) else: f = r.figure() from mcdp_report.generic_report_utils import generic_plot generic_plot(f, space=UR, value=res) from mcdp_report.generic_report_utils import generic_report_trace generic_report_trace(r, ndp, dp, trace, out, do_movie=do_movie) out_html = os.path.join(out, 'report.html') logger.info('writing to %r' % out_html) r.to_html(out_html) if plot and imp: from mcdp_report_ndp_tests.test1 import GetValues from mcdp_report.gg_ndp import gvgen_from_ndp from mcdp_report.gdc import STYLE_GREENREDSYM from mcdp_report.gg_utils import gg_figure M = dp.get_imp_space() report_solutions = Report() for i, r in enumerate(res.minimals): ms = dp.get_implementations_f_r(fg, r) for j, m in enumerate(ms): imp_dict = get_imp_as_recursive_dict(M, m) images_paths = library.get_images_paths() image_source = ImagesFromPaths(images_paths) gv = GetValues(ndp=ndp, imp_dict=imp_dict, nu=upper, nl=1) setattr(ndp, '_hack_force_enclose', True) with report_solutions.subsection('sol-%s-%s' % (i, j)) as rr: # Left right gg = gvgen_from_ndp(ndp=ndp, style=STYLE_GREENREDSYM, image_source=image_source, plotting_info=gv, direction='LR') gg_figure(rr, 'figure', gg, do_png=True, do_pdf=True, do_svg=False, do_dot=False) # Top-bottom gg = gvgen_from_ndp(ndp=ndp, style=STYLE_GREENREDSYM, image_source=image_source, plotting_info=gv, direction='TB') gg_figure(rr, 'figure2', gg, do_png=True, do_pdf=True, do_svg=False, do_dot=False) out_html = os.path.join(out, 'report_solutions.html') logger.info('writing to %r' % out_html) report_solutions.to_html(out_html)
def main(): if not vehicles_has_cairo: logger.error('This program cannot be run if Cairo is not installed.') return from vehicles_cairo import (vehicles_cairo_display_pdf, vehicles_cairo_display_png, vehicles_cairo_display_svg) parser = OptionParser(usage=usage) parser.disable_interspersed_args() parser.add_option("--vehicle", default='d_SE2_rb_v-rf360', help="ID vehicle [%default].") parser.add_option("--world", default='stochastic_box_10', help="ID world [%default].") parser.add_option("-n", default=1, type='int', help="number of simulations [%default].") parser.add_option("--outdir", "-o", default='display_demo', help="output directory [%default]") parser.add_option("--figsize", default=10, type='float', help="figsize (inches) [%default]") parser.add_option("-z", "--zoom", default=0, type='float', help="zoom in meters; 0 for full view [%default]") parser.add_option("-g", "--grid", default=1, type='float', help="grid size in meters; 0 for no grid [%default]") parser.add_option("--cairo", default=False, action='store_true') parser.add_option("--seed", default=None, type='int') (options, args) = parser.parse_args() if args: raise Exception() id_vehicle = options.vehicle id_world = options.world logger.info('id_vehicle: %s' % id_vehicle) logger.info(' id_world: %s' % id_world) if options.seed is None: options.seed = np.random.randint(1000000) np.random.seed(seed=options.seed) logger.info('Using seed %s (your lucky number is %s)' % (options.seed, np.random.randint(1000))) vehicle = VehiclesConfig.vehicles.instance( id_vehicle) # @UndefinedVariable world = VehiclesConfig.worlds.instance(id_world) # @UndefinedVariable simulation = VehicleSimulation(vehicle, world) from reprep import Report, MIME_PDF basename = 'display-%s-%s' % (id_vehicle, id_world) r = Report(basename) r.text('seed', 'Seed = %s' % options.seed) for i in range(options.n): sec = r.node('simulation%d' % i) f = sec.figure() simulation.new_episode() simulation.compute_observations() sim_state = simulation.to_yaml() plot_params = dict(grid=options.grid, zoom=options.zoom, show_sensor_data=True) # with f.plot('start', figsize=(options.figsize, # options.figsize)) as pylab: # display_all(pylab, sim_state, **plot_params) with f.data_file('start_cairo_png', MIME_PNG) as filename: vehicles_cairo_display_png(filename, width=800, height=800, sim_state=sim_state, **plot_params) with f.data_file('start_cairo_pdf', MIME_PDF) as filename: vehicles_cairo_display_pdf(filename, width=800, height=800, sim_state=sim_state, **plot_params) with f.data_file('start_cairo_svg', MIME_SVG) as filename: vehicles_cairo_display_svg(filename, width=800, height=800, sim_state=sim_state, **plot_params) filename = os.path.join(options.outdir, 'index.html') logger.info('Writing to %r.' % filename) r.to_html(filename)
def plot_different_solutions(libname, ndpname, query, out, upper=None): if not os.path.exists(out): os.makedirs(out) library = get_test_library(libname) #library.use_cache_dir(os.path.join(out, 'cache')) context = Context() ndp = library.load_ndp(ndpname, context) context = library._generate_context_with_hooks() ndp_labelled = get_labelled_version(ndp) dp0 = ndp_labelled.get_dp() if upper is not None: _, dpU = get_dp_bounds(dp0, nl=1, nu=upper) dp = dpU else: dp = dp0 M = dp.get_imp_space() with open(os.path.join(out, 'ndp.txt'), 'w') as f: f.write(ndp.repr_long()) with open(os.path.join(out, 'M.txt'), 'w') as f: f.write(M.repr_long()) with open(os.path.join(out, 'dp.txt'), 'w') as f: f.write(dp.repr_long()) with open(os.path.join(out, 'dp0.txt'), 'w') as f: f.write(dp0.repr_long()) f = convert_string_query(ndp=ndp, query=query, context=context) report = Report() res = dp.solve(f) print('num solutions: %s' % len(res.minimals)) for ri, r in enumerate(res.minimals): ms = dp.get_implementations_f_r(f, r) for j, m in enumerate(ms): imp_dict = get_imp_as_recursive_dict(M, m) logger.info(imp_dict) images_paths = library.get_images_paths() image_source = ImagesFromPaths(images_paths) gv = GetValues(ndp=ndp, imp_dict=imp_dict, nu=upper, nl=1) gg = gvgen_from_ndp(ndp=ndp, style=STYLE_GREENREDSYM, image_source=image_source, plotting_info=gv) with report.subsection('%s-%s' % (ri, j)) as rr: gg_figure(rr, 'figure', gg, do_png=True, do_pdf=False, do_svg=False, do_dot=False) fn = os.path.join(out, 'solutions.html') print('writing to %s' % fn) report.to_html(fn)
def __init__(self): report = Report('id', caption='env1d') self.N = 1000 self.res = 10 x = np.linspace(0, 10, self.N * self.res) self.E = scipy.convolve(np.random.ranf(len(x)), np.ones(self.res * 20) / self.res * 20, mode='same') plot_env(report, x, self.E) self.commands = [-2.5, 2.0] self.n_sampels = [0, 0] self.sensels = [30, 31] self.state = self.N / 2 self.plot_y = False self.plot_e = False self.size = 60 self.area = 9 self.s = range(self.size) self.clean() lsize = 20 sensor_noise = 0 actuator_noise = 0 self.run_learning(lsize, actuator_noise=actuator_noise, sensor_noise=sensor_noise) report.text('info0', ('Learning size: \t\t%g \nActuator noise: \t%g ' + '\nSensor noise: \t\t%g') % (lsize, actuator_noise, sensor_noise)) report.text('commands', str(self.commands)) self.summarize(report, 0) self.state = self.N / 2 self.clean() lsize = 100 sensor_noise = 0 actuator_noise = 2 self.run_learning(lsize, actuator_noise=actuator_noise, sensor_noise=sensor_noise) report.text('info1', ('Learning size: \t\t%g \nActuator noise: \t%g ' + '\nSensor noise: \t\t%g') % (lsize, actuator_noise, sensor_noise)) self.summarize(report, 1) self.state = self.N / 2 self.clean() # lsize = 1000 sensor_noise = 2 actuator_noise = 0 self.run_learning(lsize, actuator_noise=actuator_noise, sensor_noise=sensor_noise) report.text('info2', ('Learning size: \t\t%g \nActuator noise: \t%g ' + '\nSensor noise: \t\t%g') % (lsize, actuator_noise, sensor_noise)) self.summarize(report, 2) report.to_html('env1d.html')
x = np.linspace(0, vmax, 200) # pdb.set_trace() # Sn = np.abs(Sn) f = report.figure(cols=3) with f.plot(name, caption=name) as pylab: cax = pylab.imshow(np.clip(Sn, vmin, vmax), interpolation='nearest') pylab.colorbar(cax) with f.plot('hist' + name, caption=name) as pylab: cax = pylab.hist(Sn.flatten(), bins=(vmax - vmin) * 2, range=[vmin, vmax], normed=True) # pylab.plot(x, pdf(x), color='red') with f.plot('histexpr' + name, caption=name) as pylab: cax = pylab.hist(np.abs(Sn.flatten()), bins=(vmax - vmin) * 2, range=[0, vmax], normed=True) fpdf = np.array([pdf([t]) + pdf([-t]) for t in x]) pylab.plot(x, fpdf, color='red') # Save report filename = '/home/adam/public_html/testrep.html' report.to_html(filename)