00001 import roslib; roslib.load_manifest('semantic_model_web_interface')
00002
00003 from . import app, site_root, DB_NAME
00004 import db
00005 import flask as f
00006 import semanticmodel.diff_generation as dg
00007 import logging
00008 from logging import config
00009 from os import path
00010 import datetime
00011 import time
00012
00013 config.fileConfig(path.join(site_root, 'logging.conf'))
00014 log = logging.getLogger('semantic_model_web.views')
00015
00016 COLORS = ['black', 'grey', 'white', 'red', 'yellow', 'green', 'blue']
00017
00018 @app.route('/')
00019 def redirect_list_runs():
00020 return f.redirect('/runs')
00021
00022 def extract_run_id(r):
00023 return int(r[0][3:])
00024
00025 @app.route('/runs/')
00026 def list_runs():
00027 return f.render_template('runs.html', runs=sorted(db.list_runs(),
00028 key=extract_run_id))
00029
00030 @app.route('/runs/<run>/')
00031 def clusters(run):
00032 global colors
00033 coll = db.get_cluster_collection(run)
00034 other_runs = sorted([r for r in db.list_runs() if r[0] != run],
00035 key=extract_run_id)
00036 ids = sorted([m['id'] for m in coll.query(db.filter_query({}), True)])
00037 return f.render_template('run.html',
00038 run=run,
00039 ids=ids,
00040 other_runs=other_runs,
00041 info=db.run_info(run),
00042 colors=COLORS)
00043
00044 @app.route('/runs/<run>/clusters/<int:cluster_id>/')
00045 def cluster(cluster_id, run):
00046 path = db.image_file_loc(cluster_id, run)
00047 coll = db.get_cluster_collection(run)
00048 meta = coll.find_one({'id': cluster_id}, True)
00049 return f.render_template('cluster.html', img_path=path, meta=meta,
00050 next=db.next_id(coll.coll, cluster_id),
00051 prev=db.prev_id(coll.coll, cluster_id))
00052
00053 @app.route('/runs/<run>/clusters/<int:cluster_id>/matches/')
00054 def matches(cluster_id, run):
00055 path = db.image_file_loc(cluster_id, run)
00056 coll = db.get_cluster_collection(run)
00057 image_coll = db.get_image_collection(run)
00058 (cluster, meta) = coll.find_one({'id': cluster_id})
00059 dt = time.localtime(meta['creation_time'])
00060 other_runs = sorted([r for r in db.list_runs() if r[0] != run],
00061 key=extract_run_id)
00062
00063
00064
00065
00066 matches = []
00067 for (other_run, other_bagfile) in other_runs:
00068 other_coll = db.get_cluster_collection(other_run)
00069 other_image_coll = db.get_image_collection(other_run)
00070 other_cluster = dg.find_matching_cluster(cluster, meta, other_coll)
00071 if other_cluster:
00072 other_path = db.image_file_loc(other_cluster[1]['id'], other_run)
00073 T = time.localtime(other_cluster[1]['creation_time'])
00074 matches.append((other_run,
00075 other_cluster[1]['id'],
00076 True,
00077 other_path,
00078 T.tm_year,
00079 T.tm_mon,
00080 T.tm_mday,
00081 T.tm_hour,
00082 T.tm_min,
00083 T.tm_sec))
00084 else:
00085 nonmatch = dg.nearest_nonmatch(meta, other_image_coll)
00086 if nonmatch:
00087 other_id = nonmatch['cluster_id']
00088 other_path = db.image_file_loc(other_id, other_run)
00089 matches.append((other_run, other_id, False, other_path, None))
00090 else:
00091 matches.append((other_run, 'None', False, None, None))
00092
00093 return f.render_template('matches.html',
00094 meta=meta,
00095 info=db.run_info(run),
00096 run=run,
00097 dt=dt,
00098 matches=matches,
00099 path=path,
00100 next=db.next_id(coll.coll, cluster_id),
00101 prev=db.prev_id(coll.coll, cluster_id))
00102
00103 @app.route('/runs/<run>/diffs/<other_run>/')
00104 def diffs(run, other_run):
00105 diffs = dg.generate_diffs(run, other_run, DB_NAME)
00106 matches = [(x, db.image_file_loc(x, run),
00107 y, db.image_file_loc(y, other_run))
00108 for x, y in diffs['matches']]
00109 nonmatches = [(x, db.image_file_loc(x, run),
00110 y, db.image_file_loc(y, other_run))
00111 for x, y in diffs['nonmatches']]
00112 return f.render_template('diffs.html',
00113 matches=matches,
00114 nonmatches=nonmatches)
00115
00116 @app.route('/runs/<run>/query_results/')
00117 def query_results(run):
00118 q = db.semantic_query(f.request.args)
00119 coll = db.get_cluster_collection(run)
00120 cluster_ids = [d['id'] for d in coll.query(db.filter_query(q), True)]
00121 clusters = [(id, db.image_file_loc(id, run)) for id in cluster_ids]
00122 if 'query_only' in f.request.args:
00123 return "Query: {0}".format(q)
00124 else:
00125 return f.render_template('query_results.html',
00126 run=run, clusters=clusters)
00127
00128 @app.route('/runs/<run>/query_form/')
00129 def query_form(run):
00130 return f.render_template('query_form.html', run=run)
00131
00132
00133 @app.route('/runs/<run>/colors/<color>/')
00134 def colors(run, color):
00135 coll = db.get_cluster_collection(run)
00136 blobs = coll.query(db.filter_query({'color' : color}), True)
00137 pairs = []
00138 for blob in blobs:
00139 path = db.image_file_loc(blob['id'], run)
00140 pairs.append((blob['id'], path))
00141 return f.render_template('color.html',
00142 run=run,
00143 color=color,
00144 pairs=pairs)
00145