def check_ex16c_r(dp): # from mocdp.dp.dp_loop import SimpleLoop # funsp = dp.get_fun_space() # assert isinstance(dp, SimpleLoop) # Payload2ET dp1 = dp.dp1.dp1 payload1 = 10.0 payload2 = 12.0 # bot = dp1.get._fun_space().get_bottom() res1 = dp1.solve(payload1) res2 = dp1.solve(payload2) r = Report() caption = 'Two curves for each payload' with r.plot('p1', caption=caption) as pylab: plot_upset_minima(pylab, res1) plot_upset_minima(pylab, res2) axis = pylab.axis() plot_upset_R2(pylab, res1, axis, color_shadow=[0.5, 0.5, 0.5]) plot_upset_R2(pylab, res2, axis, color_shadow=[0.7, 0.7, 0.6]) pylab.xlabel('time') pylab.ylabel('energy') import numpy as np payloads = np.linspace(0, 100, 100) payloads_ = [] payload2payload = dp.dp1 for p in payloads: res = payload2payload.solve(p) assert len(res.minimals) == 1 pp = list(res.minimals)[0] payloads_.append(pp) with r.plot('p2') as pylab: pylab.plot(payloads, payloads, 'k--') pylab.plot(payloads, payloads_, 'r.') pylab.xlabel('payload (g)') pylab.ylabel('payload (g)') pylab.xlim(0, max(payloads)) # pylab.ylim(0, max(payloads)) return r
def plot(self, name, **args): name = normalize(name) r = Report() a = r.plot('plot', mime=MIME_PNG, **args) # XXX: better way? yield a.__enter__() a.__exit__(None, None, None) data_node = r.children[0] rgb = data_node.get_rgb() self.bitmap(name, rgb)
def testPlotCaption(self): r = Report("test") with r.plot("ciao", caption="my caption") as pylab: pylab.plot([0, 1], [0, 1], "-k")
def testPlot(self): r = Report("test") with r.plot("ciao") as pylab: pylab.plot([0, 1], [0, 1], "-k")
def main(): np.seterr(all='warn') n = 100 z = np.linspace(-1, 1, n) z_order = np.array(range(n)) alpha = 0.1 base = 0.3 noise_eff = 0.05 noise_est = noise_eff f_L = lambda z: np.exp(-np.abs(+1 - np.maximum(z, 0)) / alpha) + base f_R = lambda z: np.exp(-np.abs(-1 - np.minimum(z, 0)) / alpha) + base rate_L0 = f_L(z) rate_R0 = f_R(z) simulate_L = lambda: f_L(z) + np.random.uniform(-1, 1, n) * noise_eff simulate_R = lambda: f_R(z) + np.random.uniform(-1, 1, n) * noise_eff rate_L = simulate_L() rate_R = simulate_R() T = 100 ord1 = np.zeros((n, T)) for k in range(T): ord1[:, k] = scale_score(simulate_L()) order_L_sim = np.ndarray(n, fit_dtype) for i in range(n): order_L_sim[i]['mean'] = np.mean(ord1[i, :]) l, u = np.percentile(ord1[i, :], [5, 95]) order_L_sim[i]['upper'] = u order_L_sim[i]['lower'] = l rate_L_est = np.ndarray(n, fit_dtype) rate_L_est['upper'] = rate_L + noise_est rate_L_est['lower'] = rate_L - noise_est rate_R_est = np.ndarray(n, fit_dtype) rate_R_est['upper'] = rate_R + noise_est rate_R_est['lower'] = rate_R - noise_est # estimate according to naive procedure z_naive = estimate_stimulus_naive(rate_L, rate_R) res = estimate_stimulus(rate_L_est, rate_R_est) L_order = res.L_order R_order = res.R_order scale_rate = max(rate_L.max(), rate_R.max()) * 1.2 cL = 'r' cR = 'b' r = Report() f = r.figure(cols=3) with r.plot('noiseless') as pylab: pylab.plot(z, rate_L0, '%s-' % cL) pylab.plot(z, rate_R0, '%s-' % cR) pylab.axis((-1, 1, 0.0, scale_rate)) r.last().add_to(f, caption='noiseless rates') with r.plot('observed_rates') as pylab: pylab.plot(z, rate_R0, '%s-' % cR) pylab.plot(z, rate_L0, '%s-' % cL) plot_rate_bars(pylab, z, rate_L_est, '%s' % cL) plot_rate_bars(pylab, z, rate_R_est, '%s' % cR) pylab.axis((-1, 1, 0.0, scale_rate)) r.last().add_to(f, caption='true_rates') with r.plot('M') as pylab: pylab.plot(z, rate_L0, '%s-' % cL) pylab.plot(z, rate_R0, '%s-' % cR) pylab.axis((-1, 1, 0.0, scale_rate)) with r.plot('z_naive') as pylab: pylab.plot(z_naive, rate_L, '%s.' % cL) pylab.plot(z_naive, rate_R, '%s.' % cR) pylab.axis((-1, 1, 0.0, scale_rate)) r.last().add_to(f, caption='Stimulus estimated in naive way.') with r.plot('simulated_order_stats') as pylab: pylab.plot([0, 0], [n, n], 'k-') pylab.plot([0, n], [n, 0], 'k-') pylab.plot(z_order, order_L_sim['mean'], '%s.' % cL) plot_rate_bars(pylab, z_order, order_L_sim, '%s' % cL) pylab.axis((0, n, -n / 10, n * 1.1)) pylab.axis('equal') r.last().add_to(f, caption='Orders as found by simulation') with r.plot('estimated_order') as pylab: pylab.plot(z, L_order['mean'], '%s.' % cL) pylab.plot(z, R_order['mean'], '%s.' % cR) pylab.axis((-1, 1, -n / 2, n * 3 / 2)) r.last().add_to(f, caption='estimated_order') with r.plot('estimated_order_order') as pylab: pylab.plot([0, 0], [n, n], 'k-') plot_rate_bars(pylab, z_order, L_order, '%s' % cL) plot_rate_bars(pylab, z_order, R_order, '%s' % cR) pylab.plot([0, 0], [n, n], 'k-') pylab.axis((0, n, -n / 10, n * 1.1)) pylab.axis('equal') r.last().add_to(f, caption='estimated_order_order') with r.plot('estimated_order_bar') as pylab: pylab.plot(z, L_order['mean'], '%s.' % cL) pylab.plot(z, R_order['mean'], '%s.' % cR) plot_rate_bars(pylab, z, L_order, '%s' % cL) plot_rate_bars(pylab, z, R_order, '%s' % cR) pylab.axis((-1, 1, -n / 2, n * 3 / 2)) r.last().add_to(f, caption='estimated_order_bar') with r.plot('estimated_order_both') as pylab: pylab.plot([0, 0], [n, n], 'g-') pylab.plot(z_order, res.order['mean'], 'k.') plot_rate_bars(pylab, z_order, res.order, 'k') pylab.axis((0, n, -n / 10, n * 1.1)) pylab.axis('equal') r.last().add_to(f, caption='estimated_order_order') with r.plot('z_better') as pylab: pylab.plot(res.z['mean'], rate_L, '%s.' % cL) pylab.plot(res.z['mean'], rate_R, '%s.' % cR) pylab.axis((-1, 1, 0.0, scale_rate)) r.last().add_to(f, caption='Stimulus estimated in a better way.') filename = 'order_estimation.html' print('Writing to %r.' % filename) r.to_html(filename)
def report(res): r = Report() dataL = res['dataL'] dataU = res['dataU'] what_to_plot_res = dict(total_cost="USD", total_mass='kg') what_to_plot_fun = dict(endurance="hour", extra_payload="g") queries = dataL['queries'] endurance = [q['endurance'] for q in queries] def get_value(data, field): for res in data['results']: a = to_numpy_array({field: 'kg'}, res) if len(a): a = min(a[field]) else: a = None yield a from matplotlib import pylab ieee_fonts_zoom3(pylab) markers = dict(markeredgecolor='none', markerfacecolor='black', markersize=6, marker='o') LOWER2 = dict(color='orange', linewidth=4, linestyle='-', clip_on=False) UPPER2 = dict(color='purple', linewidth=4, linestyle='-', clip_on=False) LOWER2.update(markers) UPPER2.update(markers) color_resources = '#700000' color_functions = '#007000' fig = dict(figsize=(4.5, 4)) with r.plot('total_mass', **fig) as pylab: ieee_spines_zoom3(pylab) total_massL = np.array(list(get_value(dataL, 'total_mass'))) total_massU = np.array(list(get_value(dataU, 'total_mass'))) print endurance print total_massL, total_massU pylab.plot(endurance, total_massL, **LOWER2) pylab.plot(endurance, total_massU, **UPPER2) set_axis_colors(pylab, color_functions, color_resources) pylab.xlabel('endurance [hours]') pylab.ylabel('total_mass [kg]') return r print('Plotting lower') with r.subsection('lower') as rL: plot_all_directions(rL, queries=dataL['queries'], results=dataL['results'], what_to_plot_res=what_to_plot_res, what_to_plot_fun=what_to_plot_fun) print('Plotting upper') with r.subsection('upper') as rU: plot_all_directions(rU, queries=dataU['queries'], results=dataU['results'], what_to_plot_res=what_to_plot_res, what_to_plot_fun=what_to_plot_fun) return r
def testPlotCaption(self): r = Report('test') with r.plot('ciao', caption='my caption') as pylab: pylab.plot([0, 1], [0, 1], '-k')
def testPlot(self): r = Report('test') with r.plot('ciao') as pylab: pylab.plot([0, 1], [0, 1], '-k')
def create_report1(data, model_name): all_min_throughput = [] all_resolution = [] all_num_solutions = [] all_min_budget = [] all_min_power = [] for query, query_results in zip(data['queries'], data['results']): resolution = query['resolution'] min_throughput = query['min_throughput'] num_solutions = len(query_results) if num_solutions == 0: min_power = np.nan min_budget = np.nan else: min_power = np.min([_['power'] for _ in query_results]) min_budget = np.min([_['budget'] for _ in query_results]) all_min_throughput.append(min_throughput) all_resolution.append(resolution) all_num_solutions.append(num_solutions) all_min_power.append(min_power) all_min_budget.append(min_budget) all_min_throughput = np.array(all_min_throughput) all_resolution = np.array(all_resolution) all_num_solutions = np.array(all_num_solutions) all_min_power = np.array(all_min_power) all_min_budget = np.array(all_min_budget) one_solution = all_num_solutions == 1 multiple_solutions = all_num_solutions > 1 is_not_feasible = all_num_solutions == 0 is_feasible = all_num_solutions > 0 from matplotlib import pylab ieee_fonts_zoom3(pylab) fig = dict(figsize=(4.5, 4)) popt = dict(clip_on=False) def do_axes(pylab): pylab.xlabel('resolution [pixels/deg]') pylab.ylabel('min_throughput [Hz]') set_axis_colors(pylab, color_functions, color_functions) r = Report(model_name) with r.plot('feasibility', **fig) as pylab: ieee_spines_zoom3(pylab) pylab.plot(all_resolution[one_solution], all_min_throughput[one_solution], 'k.', markersize=4, **popt) pylab.plot(all_resolution[multiple_solutions], all_min_throughput[multiple_solutions], 'ms', markersize=4, **popt) pylab.plot(all_resolution[is_not_feasible], all_min_throughput[is_not_feasible], 'r.', markersize=0.5, **popt) do_axes(pylab) with r.plot('power', **fig) as pylab: ieee_spines_zoom3(pylab) x = all_resolution y = all_min_throughput z = all_min_power plot_field(pylab, x, y, z, cmap='afmhot') pylab.title('power', color=color_resources) do_axes(pylab) with r.plot('budget', **fig) as pylab: ieee_spines_zoom3(pylab) x = all_resolution y = all_min_throughput z = all_min_budget plot_field(pylab, x, y, z, cmap='afmhot') pylab.title('budget', color=color_resources) do_axes(pylab) with r.plot('budget2', **fig) as pylab: ieee_spines_zoom3(pylab) unique_budgets = np.unique(all_min_budget[is_feasible]) markers = ['b.', 'g.', 'm.', 'y.'] for i, ub in enumerate(unique_budgets): which = all_min_budget == ub pylab.plot(all_resolution[which], all_min_throughput[which], markers[i], markersize=4, **popt) pylab.plot(all_resolution[is_not_feasible], all_min_throughput[is_not_feasible], 'r.', markersize=0.5, **popt) do_axes(pylab) r.text('about_budget', '%s = %s' % (unique_budgets, markers)) r.text( 'misc', 'min_power: %s W - %s W' % (np.min( all_min_power[is_feasible]), np.max(all_min_power[is_feasible]))) return r