def display(self, report, discdds=None): report.text('summary', 'Testcase: %s\nPlan: %s' % (self.id_tc, self.true_plan)) report.data('id_tc', self.id_tc) report.data('id_discdds', self.id_discdds) report.data('true_plan', self.true_plan) def zoom(rgb): """ Enlarge image so that pixels are distinguishable even though the brows makes them smooth. """ return rgb_zoom(rgb, K=8) f = report.figure(cols=4) f.data_rgb('y0_rgb', zoom(self.y0.get_rgb()), caption='$y_0$ (rgb)') f.data_rgb('y1_rgb', zoom(self.y1.get_rgb()), caption='$y_1$ (rgb)') from diffeoplan.library.distances.distance_norm import DistanceNorm d = DistanceNorm(2) if discdds is None: discdds = get_conftools_discdds().instance(self.id_discdds) y1p = discdds.predict(self.y0, self.true_plan) e_y0_y1_field = d.error_field(self.y1, self.y0) e_y1p_y1_field = d.error_field(self.y1, y1p) e_max = float(max(e_y0_y1_field.max(), e_y1p_y1_field.max())) f.data_rgb('e_y0_y1', zoom(scale(e_y0_y1_field, max_value=e_max)), caption="Discrepancy between $y_0$ and $y_1$.") f = report.figure('prediction_model', cols=4, caption="This is the prediction according to the " "learned model." "") f.data_rgb('y1p_rgb', zoom(y1p.get_rgb()), caption="$p^\star \cdot y_0$") f.data_rgb('y1p_rgb_u', zoom(y1p.get_rgb_uncertain()), caption="Uncertainty") f.data_rgb('e_y1p_y1', zoom(scale(e_y1p_y1_field, max_value=e_max)), caption="Discrepancy between $y_1$ and $p^\star " "\cdot y_0$.")
def diffeo_to_rgb_norm(D, max_value=None): stats = diffeo_stats(D) return scale(stats.norm, min_value=0, max_value=max_value, min_color=[1, 1, 1], max_color=[0, 0, 1])
def report_alldata(alldata): r = Report() y = alldata["observations"] yr = scale(y) f = r.figure() f.data_rgb("y", yr) return r
def display(self, report): if not self.initialized(): report.text('notice', 'Cannot display() because not initialized.') return report.text('estimator', type(self).__name__) report.data('num_samples', self.num_samples) score = self._get_score() flattening = self.flat_structure.flattening min_score = flattening.flat2rect(np.min(score, axis=1)) max_score = flattening.flat2rect(np.max(score, axis=1)) caption = 'minimum and maximum score per pixel across neighbors' f = report.figure('scores', caption=caption) f.data('min_score', min_score).display('scale') f.data('max_score', max_score).display('scale') f = report.figure(cols=4) def make_best(x): return x == np.min(x, axis=1) max_d = int(np.ceil(np.hypot(np.floor(self.area[0] / 2.0), np.floor(self.area[1] / 2.0)))) safe_d = int(np.floor(np.min(self.area) / 2.0)) def plot_safe(pylab): plot_vertical_line(pylab, safe_d, 'g--') plot_vertical_line(pylab, max_d, 'r--') if self.shape[0] <= 64: as_grid = self.make_grid(score) f.data_rgb('grid', rgb_zoom(scale(as_grid), 4)) else: report.text('warn', 'grid visualization not done because too big') distance_to_border = distance_to_border_for_best(self.flat_structure, score) distance_to_center = distance_from_center_for_best(self.flat_structure, score) bdist_scale = dict(min_value=0, max_value=max_d, max_color=[0, 1, 0]) d = report.data('distance_to_border', distance_to_border).display('scale', **bdist_scale) d.add_to(f, caption='Distance to border of best guess, white=0, green=%d' % max_d) cdist_scale = dict(min_value=0, max_value=max_d, max_color=[1, 0, 0]) d = report.data('distance_to_center', distance_to_center).display('scale', **cdist_scale) d.add_to(f, caption='Distance to center of best guess, white=0, red=%d' % max_d) bins = np.array(range(max_d + 2)) # values will be integers (0, 1, 2, ...), center bins appropriately bins = bins - 0.5 with f.plot('distance_to_border_hist') as pylab: pylab.hist(distance_to_border.flat, bins) caption = 'Red line: on border; Green line (red-1): safe to be here.' with f.plot('distance_to_center_hist', caption=caption) as pylab: pylab.hist(distance_to_center.flat, bins) plot_safe(pylab)
def draw_significance_figure(pylab, xlabels, ylabels, P): def format(p): return 1 if p else 0 # x = numpy.array(values_to_strings(P, format)) rgb = scale(P * 1.0, max_color=[0, 1, 0]) #print x #print rgb draw_matrix_and_labels(pylab, xlabels, ylabels, rgb)
def add_scaled(report, id, x, **kwargs): n = report.data(id, x) n.data_rgb('retina', add_reflines(scale(values2retina(x), min_value=0, **kwargs))) #with n.data_pylab('plot') as pylab: # pylab.plot(x, '.') return n
def display(self, report, discdds=None): report.text('summary', 'Testcase: %s\nPlan: %s' % (self.id_tc, self.true_plan)) report.data('id_tc', self.id_tc) report.data('id_discdds', self.id_discdds) report.data('true_plan', self.true_plan) def zoom(rgb): """ Enlarge image so that pixels are distinguishable even though the brows makes them smooth. """ return rgb_zoom(rgb, K=8) f = report.figure(cols=4) f.data_rgb('y0_rgb', zoom(self.y0.get_rgb()), caption='$y_0$ (rgb)') f.data_rgb('y1_rgb', zoom(self.y1.get_rgb()), caption='$y_1$ (rgb)') from diffeoplan.library.distances.distance_norm import DistanceNorm d = DistanceNorm(2) if discdds is None: discdds = get_conftools_discdds().instance(self.id_discdds) y1p = discdds.predict(self.y0, self.true_plan) e_y0_y1_field = d.error_field(self.y1, self.y0) e_y1p_y1_field = d.error_field(self.y1, y1p) e_max = float(max(e_y0_y1_field.max(), e_y1p_y1_field.max())) f.data_rgb('e_y0_y1', zoom(scale(e_y0_y1_field, max_value=e_max)), caption="Discrepancy between $y_0$ and $y_1$.") f = report.figure('prediction_model', cols=4, caption="This is the prediction according to the " "learned model.""") f.data_rgb('y1p_rgb', zoom(y1p.get_rgb()), caption="$p^\star \cdot y_0$") f.data_rgb('y1p_rgb_u', zoom(y1p.get_rgb_uncertain()), caption="Uncertainty") f.data_rgb('e_y1p_y1', zoom(scale(e_y1p_y1_field, max_value=e_max)), caption="Discrepancy between $y_1$ and $p^\star " "\cdot y_0$.")
def write_image(name, values): time_us = timestamp * 1000 * 1000 dirname2 = os.path.join(dirname, name) if not os.path.exists(dirname2): os.makedirs(dirname2) filename = os.path.join(dirname2, 'ms%08d.png' % time_us) if np.nanmin(values) < 0: rgb = posneg(values) else: rgb = scale(values) rgb = rgb_zoom(rgb, K=4) imwrite(rgb, filename) logger.debug('print to %r' % filename)
def report_nmap_distances(nmap, nmap_distances): r = Report() timestamps = [] alldists = [] allobs = [] poses = [] for i, (timestamp, (y, distances, pose)) in enumerate(nmap_distances): timestamps.append(timestamp) alldists.append(distances) allobs.append(y) poses.append(pose) timestamp = np.array(timestamps) alldists = np.vstack(alldists).T print ('ntimestamps: %s' % len(timestamps)) nwaypoints, nmoments = alldists.shape print('nmoments: %s' % nmoments) print('nwaypoints: %s' % nwaypoints) assert len(timestamps) == nmoments f = r.figure(cols=1) GREEN = [0, 1, 0] RED = [1, 0, 0] alldists_rgb1 = scale(alldists, min_color=GREEN, max_color=RED) f.data_rgb('alldists', alldists_rgb1) alldists_rgb2 = filter_colormap(alldists, cmap='jet', min_value=None, max_value=None, nan_color=[1, 0.6, 0.6], inf_color=[0.6, 1, 0.6], flat_color=[0.5, 0.5, 0.5]) f.data_rgb('alldists2', alldists_rgb2) f.data_rgb('observations', _nmapobslist_to_rgb(allobs)) waypoints = range(0, nwaypoints, 5) with f.plot('distances') as pylab: for a, i in enumerate(waypoints): d = alldists[i, :] d = d - np.min(d) d = d / np.max(d) x = timestamps y = d + a * 1.1 pylab.plot(x, y, 'k-') return r
def get_rgb(field, colors, **kwargs): ''' Returns a dict with values 'rgb' (rgb in [0,1]), 'colorbar', etc.. ''' properties = {} if colors == 'posneg': rgb = posneg(field, properties=properties, **kwargs) / 255.0 elif colors == 'scale': rgb = scale(field, properties=properties, **kwargs) / 255.0 elif colors == 'cmap': rgb = filter_colormap(field, properties=properties, **kwargs) / 255.0 else: raise ValueError('No known colors %r. ' % colors) properties['rgb'] = rgb return properties
def visualize_result(id_tc, id_algo, stats): """ Returns a report """ result = stats['result'] r = Report('%s-%s' % (id_tc, id_algo)) # tc = config.testcases.instance(id_tc) # discdds = config.discdds.instance(tc.id_discdds) algo = stats['algo'] tc = stats['tc'] discdds = algo.get_dds() tc.display(r.section('testcase'), discdds=discdds) if not result.success: r.text('warning', 'Plannning unsuccesful') else: rsol = r.section('solution') rsol.text('plan', 'Plan: %s' % str(result.plan)) y0 = tc.y0 y1 = tc.y1 y1plan = discdds.predict(y0, result.plan) mismatch = np.abs(y1.get_values() - y1plan.get_values()).sum(axis=2) f = rsol.figure(cols=4) zoom = lambda x: rgb_zoom(x, 8) f.data_rgb('y1plan', zoom(y1plan.get_rgb()), caption='plan prediction (certain)') f.data_rgb('y1plan_certain', zoom(y1plan.get_rgb_uncertain()), caption='certainty of prediction') f.data_rgb('mismatch', zoom(scale(mismatch)), caption='Mismatch value pixel by pixel ' '(zero for synthetic testcases...)') algo.plan_report(r.section('planning'), result, tc) extra = result.extra write_log_lines(r, extra) return r
def visualize_result(config, id_tc, id_algo, stats): """ Returns a report """ set_current_config(config) result = stats["result"] r = Report("%s-%s" % (id_tc, id_algo)) # tc = config.testcases.instance(id_tc) # discdds = config.discdds.instance(tc.id_discdds) algo = stats["algo"] tc = stats["tc"] discdds = algo.get_dds() tc.display(r.section("testcase"), discdds=discdds) if not result.success: r.text("warning", "Plannning unsuccesful") else: rsol = r.section("solution") rsol.text("plan", "Plan: %s" % str(result.plan)) y0 = tc.y0 y1 = tc.y1 y1plan = discdds.predict(y0, result.plan) mismatch = np.abs(y1.get_values() - y1plan.get_values()).sum(axis=2) f = rsol.figure(cols=4) zoom = lambda x: rgb_zoom(x, 8) f.data_rgb("y1plan", zoom(y1plan.get_rgb()), caption="plan prediction (certain)") f.data_rgb("y1plan_certain", zoom(y1plan.get_rgb_uncertain()), caption="certainty of prediction") f.data_rgb( "mismatch", zoom(scale(mismatch)), caption="Mismatch value pixel by pixel " "(zero for synthetic testcases...)", ) algo.plan_report(r.section("planning"), result, tc) extra = result.extra write_log_lines(r, extra) return r
def report_for_one(r, events, coords): f = r.figure(caption='For coordinate %s' % str(coords)) h = aer_histogram(events).astype('float') h[coords[0], coords[1]] = np.nan one = get_for_one(events, coords) min_f = 20.0 max_f = 5000.0 min_d = 1.0 / max_f max_d = 1.0 / min_f f.data_rgb('where', scale(h)) if False: with f.plot('frequency') as pylab: fbins = np.linspace(min_f, max_f, 1000) pylab.hist(one['frequency'], bins=fbins) pylab.xlabel('1/interval (Hz)') with f.plot('delta') as pylab: dbins = (1.0 / fbins)[::-1] pylab.hist(one['delta'], bins=dbins) ticks = np.linspace(min_d, max_d, 10) labels = ['%d' % (x * 1000000) for x in ticks] pylab.xticks(ticks, labels) pylab.xlabel('interval (microseconds)') time = one['timestamp'] time = time - time[0] with f.plot('history') as pylab: plus = one['sign'] == 1 minus = one['sign'] == -1 ones = np.ones(one.size) pylab.plot(time[plus], 1 * ones[plus], 'rs') pylab.plot(time[minus], -1 * ones[minus], 'bs') t0 = time[0] delta = 0.01 pylab.axis((t0, t0 + delta, -1.2, 1.2))
def _nmapobs_to_rgb(m): print m.shape m = m.T rgb = scale(m, min_value=0, max_value=1, nan_color=[0.6, 1, 0.6]) return rgb_zoom(rgb, 4)
def info2rgb(x): return scale(x, max_value=1, min_value=0, min_color=[1, 0, 0], max_color=[0, 1, 0])
def main(): sigma_deg = 6 kernel1 = get_contrast_kernel(sigma_deg=sigma_deg, eyes_interact=True) kernel2 = get_contrast_kernel(sigma_deg=sigma_deg, eyes_interact=False) # better kernel1 = kernel1.astype('float32') kernel2 = kernel2.astype('float32') meany = Expectation() ex1 = Expectation() ex2 = Expectation() cp = ClientProcess() cp.config_use_white_arena() cp.config_stimulus_xml(example_stim_xml) #position = [0.15, 0.5, 0.25] position = [0.35, 0.5, 0.25] linear_velocity_body = [0, 0, 0] angular_velocity_body = [0, 0, 0] #from flydra_render.contrast import intrinsic_contrast from fast_contrast import intrinsic_contrast #@UnresolvedImport N = 360 pb = progress_bar('Computing contrast', N) orientation = numpy.linspace(0, 2 * numpy.pi, N) for i, theta in enumerate(orientation): attitude = rotz(theta) pb.update(i) res = cp.render(position, attitude, linear_velocity_body, angular_velocity_body) y = numpy.array(res['luminance']).astype('float32') meany.update(y) #y = numpy.random.rand(1398) c1 = intrinsic_contrast(y, kernel1) c2 = intrinsic_contrast(y, kernel2) ex1.update(c1) ex2.update(c2) r = Report() r.data_rgb('meany', scale(values2retina(meany.get_value()))) r.data_rgb('mean1', plot_contrast(ex1.get_value())) r.data_rgb('mean2', plot_contrast(ex2.get_value())) r.data_rgb('one-y', (plot_luminance(y))) r.data_rgb('one-c1', plot_contrast(c1)) r.data_rgb('one-c2', plot_contrast(c2)) r.data_rgb('kernel', scale(values2retina(kernel2[100, :]))) f = r.figure(shape=(2, 3)) f.sub('one-y', 'One random image') f.sub('one-c1', 'Contrast of random image') f.sub('one-c2', 'Contrast of random image') f.sub('meany', 'Mean luminance') f.sub('mean1', 'Mean over %s samples' % N) f.sub('mean2', 'Mean over %s samples' % N) f.sub('kernel') filename = 'compute_contrast_demo.html' print("Writing on %s" % filename) r.to_html(filename)
def display(self, report): if not self.initialized(): report.text('notice', 'Cannot display() because not initialized.') return report.text('estimator', type(self).__name__) report.data('num_samples', self.num_samples) score = self._get_score() flattening = self.flat_structure.flattening min_score = flattening.flat2rect(np.min(score, axis=1)) max_score = flattening.flat2rect(np.max(score, axis=1)) caption = 'minimum and maximum score per pixel across neighbors' f = report.figure('scores', caption=caption) f.data('min_score', min_score).display('scale') f.data('max_score', max_score).display('scale') f = report.figure(cols=4) def make_best(x): return x == np.min(x, axis=1) max_d = int( np.ceil( np.hypot(np.floor(self.area[0] / 2.0), np.floor(self.area[1] / 2.0)))) safe_d = int(np.floor(np.min(self.area) / 2.0)) def plot_safe(pylab): plot_vertical_line(pylab, safe_d, 'g--') plot_vertical_line(pylab, max_d, 'r--') if self.shape[0] <= 64: as_grid = self.make_grid(score) f.data_rgb('grid', rgb_zoom(scale(as_grid), 4)) else: report.text('warn', 'grid visualization not done because too big') distance_to_border = distance_to_border_for_best( self.flat_structure, score) distance_to_center = distance_from_center_for_best( self.flat_structure, score) bdist_scale = dict(min_value=0, max_value=max_d, max_color=[0, 1, 0]) d = report.data('distance_to_border', distance_to_border).display('scale', **bdist_scale) d.add_to( f, caption='Distance to border of best guess, white=0, green=%d' % max_d) cdist_scale = dict(min_value=0, max_value=max_d, max_color=[1, 0, 0]) d = report.data('distance_to_center', distance_to_center).display('scale', **cdist_scale) d.add_to(f, caption='Distance to center of best guess, white=0, red=%d' % max_d) bins = np.array(range(max_d + 2)) # values will be integers (0, 1, 2, ...), center bins appropriately bins = bins - 0.5 with f.plot('distance_to_border_hist') as pylab: pylab.hist(distance_to_border.flat, bins) caption = 'Red line: on border; Green line (red-1): safe to be here.' with f.plot('distance_to_center_hist', caption=caption) as pylab: pylab.hist(distance_to_center.flat, bins) plot_safe(pylab)
def find_background(video, whens, debug=False, tmp_path=None): if not os.path.exists(video): msg = 'Filename does not exist: %s' % friendly_path(video) raise ValueError(msg) id_video = splitext(basename(video))[0] if tmp_path is None: tmp_path = join(dirname(video), 'find_background-%s' % id_video) if not exists(tmp_path): os.makedirs(tmp_path) @contract(rgb='array[HxWx3](uint8)') def write_debug(rgb, id_image): if debug: f = join(tmp_path, '%s.png' % id_image) # print('Writing %s' % f) imwrite(rgb, f) frames = [] for i, when in enumerate(whens): frame = get_frame(video, when) write_debug(frame, 'frame-%s-t%4.2f' % (i, when)) frames.append(frame.astype('float32')) n = len(frames) errs = {} for i, j in product(range(n), range(n)): if i == j: continue diff = np.abs(frames[i] - frames[j]) errs[(i, j)] = np.mean(diff, axis=2) write_debug(scale(errs[(i, j)]), 'errs-%s-%s' % (i, j)) mm = np.dstack(tuple(errs.values())) s = np.mean(mm, axis=2) write_debug(scale(s), 's') # print('computing moving stuff') moving = [] bgshade = [] for i in range(n): its = np.dstack([errs[(i, j)] for j in range(n) if i != j]) moving_i = np.min(its, axis=2) moving.append(moving_i) bgshade_i = assemble_transp(frames[i], 1.0 / (moving_i + 10)) bgshade.append(bgshade_i) write_debug(scale(moving_i), 'moving-%d' % i) write_debug(bgshade_i, 'bgshade-%d' % i) avg = TransparentImageAverage() for i in range(n): avg.update(bgshade[i]) bg_rgb = avg.get_rgb() write_debug(bg_rgb, 'background') return {'background': bg_rgb}
def publish_ros_sensels(self, obs): from reprep import scale obs2d = reshape_smart(obs) obs2d_image = scale(obs2d) ros_image = numpy_to_imgmsg(obs2d_image, stamp=None) self.pub_sensels_image.publish(ros_image)