def create_report_var(name, mean, cov, inf, minimum=None, maximum=None): report = Node(id=name) report.data("covariance", cov) report.data("information", inf) node_mean = report.data("mean", mean) with node_mean.data_file("bounds", "image/png") as filename: e = 3 * sqrt(cov.diagonal()) pylab.ioff() f = pylab.figure() x = range(len(mean)) pylab.errorbar(x, mean, yerr=e) if minimum is not None: pylab.plot(x, minimum, "b-") if maximum is not None: pylab.plot(x, maximum, "b-") pylab.savefig(filename) pylab.close(f) f = report.figure(name) f.sub("mean/bounds", caption="Mean") f.sub("covariance", caption="Covariance", display="posneg") f.sub("information", caption="Information", display="posneg") return report
class Test(unittest.TestCase): def setUp(self): # n1 -- n2 -- n3 -- n4 # \_ n3bis self.n1 = Node("n1") self.n3bis = self.n1.data("n3", None) self.n2 = self.n1.data("n2", None) self.n3 = self.n2.data("n3", None) self.n4 = self.n3.data("n4", None) self.all = [self.n1, self.n2, self.n3, self.n4, self.n3bis] def test_normal(self): """ Testing normal cases of resolve_url """ self.assertEqual(self.n1, self.n2.resolve_url("..")) self.assertEqual(self.n1, self.n3.resolve_url("../..")) self.assertEqual(self.n1, self.n4.resolve_url("../../..")) self.assertEqual(self.n2, self.n4.resolve_url("../../../n2")) self.assertEqual(self.n2, self.n1.resolve_url("n2")) self.assertEqual(self.n3, self.n1.resolve_url("n2/n3")) def test_smart(self): """ Testing smart cases of resolve_url """ self.assertEqual(self.n1.resolve_url("n3"), self.n3bis) self.assertEqual(self.n2.resolve_url("n3"), self.n3) self.assertEqual(self.n3.resolve_url("n3"), self.n3) self.assertEqual(self.n2.resolve_url("n1/n3"), self.n3bis) self.assertEqual(self.n3.resolve_url("n1/n3"), self.n3bis) self.assertEqual(self.n4.resolve_url("n4"), self.n4) self.assertEqual(self.n3.resolve_url("n4"), self.n4) self.assertEqual(self.n2.resolve_url("n4"), self.n4) self.assertEqual(self.n1.resolve_url("n4"), self.n4) for n in self.all: self.assertEqual(n.resolve_url("n4"), self.n4) def test_relative(self): for A in self.all: for B in self.all: if A == B: continue url = A.get_relative_url(B) self.assertEqual(A.resolve_url_dumb(url), B) def test_not_existent(self): """ Testing that we warn if not existent """ self.assertRaises(NotExistent, self.n1.resolve_url, "x") def test_not_found(self): self.assertRaises(NotExistent, self.n1.resolve_url, "..") def test_well_formed(self): """ Testing that we can recognize invalid urls """ self.assertRaises(InvalidURL, self.n1.resolve_url, "") self.assertRaises(InvalidURL, self.n2.resolve_url, "//")
class Test(unittest.TestCase): def setUp(self): # n1 -- n2 -- n3 -- n4 # \_ n3bis self.n1 = Node("n1") self.n3bis = self.n1.data("n3", None) self.n2 = self.n1.data("n2", None) self.n3 = self.n2.data("n3", None) self.n4 = self.n3.data("n4", None) self.all = [self.n1, self.n2, self.n3, self.n4, self.n3bis] def test_normal(self): """ Testing normal cases of resolve_url """ self.assertEqual(self.n1, self.n2.resolve_url("..")) self.assertEqual(self.n1, self.n3.resolve_url("../..")) self.assertEqual(self.n1, self.n4.resolve_url("../../..")) self.assertEqual(self.n2, self.n4.resolve_url("../../../n2")) self.assertEqual(self.n2, self.n1.resolve_url("n2")) self.assertEqual(self.n3, self.n1.resolve_url("n2/n3")) def test_smart(self): """ Testing smart cases of resolve_url """ self.assertEqual(self.n1.resolve_url("n3"), self.n3bis) self.assertEqual(self.n2.resolve_url("n3"), self.n3) self.assertEqual(self.n3.resolve_url("n3"), self.n3) self.assertEqual(self.n2.resolve_url("n1/n3"), self.n3bis) self.assertEqual(self.n3.resolve_url("n1/n3"), self.n3bis) self.assertEqual(self.n4.resolve_url("n4"), self.n4) self.assertEqual(self.n3.resolve_url("n4"), self.n4) self.assertEqual(self.n2.resolve_url("n4"), self.n4) self.assertEqual(self.n1.resolve_url("n4"), self.n4) for n in self.all: self.assertEqual(n.resolve_url("n4"), self.n4) def test_relative(self): for A in self.all: for B in self.all: if A == B: continue url = A.get_relative_url(B) self.assertEqual(A.resolve_url_dumb(url), B) def test_not_existent(self): """ Testing that we warn if not existent """ self.assertRaises(NotExistent, self.n1.resolve_url, "x") def test_not_found(self): self.assertRaises(NotExistent, self.n1.resolve_url, "..") def test_well_formed(self): """ Testing that we can recognize invalid urls """ self.assertRaises(InvalidURL, self.n1.resolve_url, "") self.assertRaises(InvalidURL, self.n2.resolve_url, "//")
def testImage(self): C = numpy.random.rand(50, 50) information = pinv(C) T = numpy.random.rand(50, 50, 3) report = Node("rangefinder") report.data("Tx", T[:, :, 0]) report.data("Ty", T[:, :, 1]) report.data("Tz", T[:, :, 2]) cov = report.data("covariance", C) report.data("information", information) pylab = get_pylab_instance() with cov.data_file("plot", MIME_PNG) as f: pylab.figure() pylab.plot(C) pylab.savefig(f) pylab.close() report.table("results", cols=["One", "Two"], data=[[1, 2], [3, 4]]) f = report.figure(caption="Covariance and information matrix", cols=3) f.sub("covariance", "should default to plot") f.sub("covariance/plot", "Same as <-") f = report.figure("Tensors", cols=3) f.sub("Tx", display="posneg") f.sub("Ty", display="posneg") f.sub("Tz", display="posneg") self.node_serialization_ok(report)
def testImage(self): C = numpy.random.rand(50, 50) information = pinv(C) T = numpy.random.rand(50, 50, 3) report = Node('rangefinder') report.data('Tx', T[:, :, 0]) report.data('Ty', T[:, :, 1]) report.data('Tz', T[:, :, 2]) cov = report.data('covariance', C) report.data('information', information) pylab = get_pylab_instance() with cov.data_file('plot', 'image/png') as f: pylab.figure() pylab.plot(C) pylab.savefig(f) pylab.close() report.table('results', cols=['One', 'Two'], data=[[1, 2], [3, 4]]) f = report.figure(caption='Covariance and information matrix', cols=3) f.sub('covariance', 'should default to plot') f.sub('covariance/plot', 'Same as <-') f = report.figure('Tensors', cols=3) f.sub('Tx', display='posneg') f.sub('Ty', display='posneg') f.sub('Tz', display='posneg') self.node_serialization_ok(report)
def create_report_figure_tensors(T, report_id, same_scale=False, caption=None): report = Node(report_id) f = report.figure('tensors', caption=caption) if same_scale: max_value = abs(T).max() else: max_value = None components = [(0, 'Tx', '$T_x$'), (1, 'Ty', '$T_y$'), (2, 'Ttheta', '$T_{\\theta}$')] for component, slice, caption in components: report.data(slice, T[component, :, :].squeeze()) f.sub(slice, display='posneg', max_value=max_value) return report
def create_report_affine(state, report_id): y_dot_mean = state.result.y_dot_stats.mean y_dot_cov = state.result.y_dot_stats.covariance y_dot_inf = state.result.y_dot_stats.information y_dot_min = state.result.y_dot_stats.minimum y_dot_max = state.result.y_dot_stats.maximum y_mean = state.result.y_stats.mean y_cov = state.result.y_stats.covariance y_inf = state.result.y_stats.information y_min = state.result.y_stats.minimum y_max = state.result.y_stats.maximum u_mean = state.result.u_stats.mean u_inf = state.result.u_stats.information u_cov = state.result.u_stats.covariance N = state.result.N.get_value() report_y_dot = create_report_var( name="y_dot", mean=y_dot_mean, cov=y_dot_cov, inf=y_dot_inf, minimum=y_dot_min, maximum=y_dot_max ) report_y = create_report_var(name="y", mean=y_mean, cov=y_cov, inf=y_inf, minimum=y_min, maximum=y_max) report_u = create_report_var(name="u", mean=u_mean, cov=u_cov, inf=u_inf) T = state.result.T.get_value() report_T = create_report_figure_tensors(T, report_id="T", caption="Learned T") Nreport = Node("n-report") with Nreport.data("N", N).data_file("N", "image/png") as filename: pylab.figure() for i in range(N.shape[1]): v = N[:, i].squeeze() pylab.plot(v) pylab.savefig(filename) pylab.close() f = Nreport.figure("nfig") f.sub("N/N") bT = state.result.bT.get_value() report_bT = create_report_figure_tensors(bT, report_id="bT", caption="Learned bT") bTn = state.result.bTn.get_value() report_bTn = create_report_figure_tensors(bTn, report_id="bTn", caption="Learned bTn") node = Node(id=report_id, children=[report_y, report_y_dot, report_u, report_T, Nreport, report_bT, report_bTn]) return node
def create_report_covariance(state, report_id): covariance = state.result.cov_sensels report = Node(report_id) report.data('covariance', covariance) report.data('correlation', cov2corr(covariance)) report.data('information', pinv(covariance, rcond=1e-2)) f = report.figure('matrices') f.sub('covariance', caption='Covariance matrix', display='posneg') f.sub('correlation', caption='Correlation matrix', display='posneg') f.sub('information', caption='Information matrix', display='posneg') return report
def main(): def find_in_path(path, pattern): for root, dirs, files in os.walk(path): #@UnusedVariable for f in files: if fnmatch.fnmatch(f, pattern): yield os.path.join(root, f) dir = 'Bicocca_2009-02-25a/out/camera_bgds_predict/gray_GI_DI/' files = list(find_in_path(dir, '*.??.pickle*')) if not files: raise Exception('No files found.') for f in files: print 'Loading {0}'.format(f) data = my_pickle_load(f) basename = os.path.splitext(f)[0] out_html = basename + '/report.html' #out_pickle = basename + '/report.pickle' if not os.path.exists(basename): os.makedirs(basename) logdir = data['logdir'] time = data['time'] y = data['y'] y_dot = data['y_dot'] y_dot_pred = data['y_dot_pred'] y_dot_s = data['y_dot_s'] y_dot_pred_s = data['y_dot_pred_s'] report = Node('{logdir}-{time:.2f}'.format(logdir=logdir, time=time)) prod = -numpy.minimum(0, y_dot_s * y_dot_pred_s) report.data('y', y) report.data('y_dot', y_dot) report.data('y_dot_pred', y_dot_pred) report.data('y_dot_s', y_dot_s) report.data('y_dot_pred_s', y_dot_pred_s) report.data('prod', prod) f = report.figure('display') f.sub('y', display='rgb') f.sub('y_dot', display='posneg') f.sub('y_dot_pred', display='posneg') f.sub('y_dot_s', display='posneg') f.sub('y_dot_pred_s', display='posneg') f.sub('prod', display='scale') zones = [ ('ZoneA', range(95, 181), range(55, 140)), ('ZoneB', range(80, 111), range(480, 510)), ('ZoneC', range(50, 181), range(330, 460)) ] for zone in zones: name, y, x = zone zr = report.node(name) zf = zr.figure() for var in ['y', 'y_dot', 'y_dot_pred', 'y_dot_s', 'y_dot_pred_s', 'prod']: zd = data[var][y, :][:, x] zr.data(var, zd) if var == 'y': disp = {'display': 'rgb'} elif var == 'prod': disp = {'display': 'scale'} else: disp = {'display': 'posneg'} zf.sub(var, **disp) outdir = basename + '/' + name id = "%s:%.2f:%s" % (logdir, time, name) print id if not os.path.exists(outdir): os.makedirs(outdir) create_latex_frag(os.path.join(outdir, 'conf.tex'), outdir, zr, id, True) create_latex_frag(os.path.join(outdir, 'report.tex'), outdir, zr, id, False) print 'Writing on %s' % out_html node_to_html_document(report, out_html)
def analyze_olfaction_covariance(covariance, receptors): ''' Covariance: n x n covariance matrix. Positions: list of n positions ''' positions = [pose.get_2d_position() for pose, sens in receptors] #@UnusedVariable positions = array(positions).transpose().squeeze() require_shape(square_shape(), covariance) n = covariance.shape[0] require_shape((2, n), positions) distances = create_distance_matrix(positions) correlation = cov2corr(covariance) flat_distances = distances.reshape(n * n) flat_correlation = correlation.reshape(n * n) # let's fit a polynomial deg = 4 poly = polyfit(flat_distances, flat_correlation, deg=deg) knots = linspace(min(flat_distances), max(flat_distances), 2000) poly_int = polyval(poly, knots) poly_fder = polyder(poly) fder = polyval(poly_fder, distances) Ttheta = create_olfaction_Ttheta(positions, fder) Tx, Ty = create_olfaction_Txy(positions, fder, distances) report = Node('olfaction-theory') report.data('flat_distances', flat_distances) report.data('flat_correlation', flat_correlation) with report.data_file('dist_vs_corr', 'image/png') as filename: pylab.figure() pylab.plot(flat_distances, flat_correlation, '.') pylab.plot(knots, poly_int, 'r-') pylab.xlabel('distance') pylab.ylabel('correlation') pylab.title('Correlation vs distance') pylab.legend(['data', 'interpolation deg = %s' % deg]) pylab.savefig(filename) pylab.close() with report.data('fder', fder).data_file('fder', 'image/png') as filename: pylab.figure() pylab.plot(knots, polyval(poly_fder, knots), 'r-') pylab.title('f der') pylab.savefig(filename) pylab.close() report.data('distances', distances) report.data('correlation', correlation) report.data('covariance', covariance) report.data('f', polyval(poly, distances)) f = report.figure(id='cor-vs-distnace', caption='Estimated kernels', shape=(3, 3)) f.sub('dist_vs_corr') f.sub('fder') f.sub('f', display='scale') f.sub('distances', display='scale') f.sub('correlation', display='posneg') f.sub('covariance', display='posneg') T = numpy.zeros(shape=(3, Tx.shape[0], Tx.shape[1])) T[0, :, :] = Tx T[1, :, :] = Ty T[2, :, :] = Ttheta T_report = create_report_figure_tensors(T, report_id='tensors', caption="Predicted learned tensors") report.add_child(T_report) return report