def app_state(start, goal, vertices=None, edges=None): if vertices is None: vertices = [start, goal] if edges is None: edges = [] return obj(start=start, goal=goal, vertices=vertices, edges=edges, mouse_pos=None, closest=None, origin=None, target=None, optimal_path=None, bidi_path=None, bidi_meeting_point=None, right_bidi_path=None, search_obsolete=True)
if __name__ == '__main__': cmd = monkey.cmd() #debug appname = cmd['config'].split(os.path.sep)[-1].split('.')[0] #debug #cmd #appname = cmd.config.split(os.path.sep)[-1].split('.')[0] #cmd file = '%s%sconf%s%s_pageobject.yml' % (os.path.split( os.path.realpath(__file__))[0], os.path.sep, os.path.sep, appname) if os.path.exists(file): pageobjects = util.readyml(file) Obj = util.obj(pageobjects) Geb = GEB(cmd) Geb.registctrlc() Geb.startadbinstall() Geb.startserver() time.sleep(25) print(Geb.resultdir) if not os.path.exists(Geb.resultdir): os.makedirs(Geb.resultdir) file = open(Geb.resultdir + os.path.sep + 'testreport.html', 'wb') firstrun = 1 secrun = 0 tmp = sys.argv[0] sys.argv = [] sys.argv.append(tmp) unittest.main(
def parse(graph_lines, history_lines): """ Parse commands and return a tuple with the following elements: vertices A ctypes array of vertices in v2f format. edges A ctypes array of vertices in v2f format, ready to pass to GL_LINES. start The vertex corresponding to the starting position, as an index into the vertices array. goal The vertex corresponting to the ending position, as an index into the vertices array. color_history A list of `obj`s where each of them has the attributes: time Time in seconds from start of search. vertex_colors A ctypes array of colors in c3B format, corresponding to the vertices. edge_colors A ctypes array of colors in c3B format, corresponding to the edges. """ # This function makes heavy use of iterators and itertools. # # http://docs.python.org/library/itertools.html vertices = None edges = None start = None goal = None color_history = [obj(time=0.0)] it = ifilter(None, imap(str.strip, graph_lines)) def take_tuples_until(s): return imap(str.split, takewhile((lambda l: l != s), it)) # Vertices. assert it.next() == 'begin vertices' vertices = {id_: (float(x), float(y)) for id_, x, y in take_tuples_until('end vertices')} vert_list = [(id_, x, y) for id_, (x, y) in vertices.iteritems()] vertex_index_by_id = {id_: i for i, (id_, x, y) in enumerate(vert_list)} vertex_buffer = (c_float * (len(vert_list) * 2))( *ichain((x, y) for (id_, x, y) in vert_list)) v = obj(coords_by_id=vertices, flat_list=vert_list, index_by_id=vertex_index_by_id, buffer=vertex_buffer) len_vert_colors = len(vert_list) * 3 color_history[0].vertex_colors = (c_ubyte * len_vert_colors)( *repeat(colors['default'][0], len_vert_colors)) # Edges. assert it.next() == 'begin edges' edges = [frozenset((a, b)) for a, b in take_tuples_until('end edges')] edge_index_by_vertex_ids = {pair:i for i, pair in enumerate(edges)} edge_buffer = (c_float * (len(edges) * 4))( *ichain(ichain((vertices[a], vertices[b]) for a, b in edges))) len_edge_colors = len(edges) * 6 color_history[0].edge_colors = (c_ubyte * len_edge_colors)( *repeat(colors['default'][0], len_edge_colors)) for rest in it: if rest.strip(): print "Got unexpected line", repr(rest) for line in history_lines: line = line.strip() if not line: continue cmd, rest = line.split(None, 1) args = rest.split() if cmd == 'start': assert len(args) == 1 start = vertices[args[0]] elif cmd == 'goal': assert len(args) == 1 goal = vertices[args[0]] elif cmd == 'step': assert len(args) == 1 timestamp = float(args[0]) color_history.append( obj(time=timestamp, vertex_colors=clone_array(color_history[-1].vertex_colors), edge_colors=clone_array(color_history[-1].edge_colors))) elif cmd == 'vertex_color': id_, color_name = args index = vertex_index_by_id[id_] color_history[-1].vertex_colors[index*3:index*3+3] = \ colors[color_name] elif cmd == 'edge_color': a, b, color_name = args index = edge_index_by_vertex_ids[frozenset((a, b))] color_history[-1].edge_colors[index*6:index*6+6] = \ colors[color_name] * 2 else: raise RuntimeError("Unknown command:", cmd) assert None not in [vertex_buffer, edge_buffer, start, goal] return v, edge_buffer, start, goal, color_history
def train(self, X, Y, max_k=100, nsamp=100, lamb=None, q='kl', mode=3, rerun=True, min_recall_per_class=0.5): print('##### START #####') Itemset.clear_db() prep_db(X, Y) # Allow specify lamb to a certain number by users if type(lamb) == str or lamb is None: samp = self.sample_from_each_label(set(Itemset.labels), 100, set(), mode) if lamb == 'max': lamb = np.max([Rule.quality([r], metric=q) for r in samp]) elif lamb == 'mean': lamb = np.mean([Rule.quality([r], metric=q) for r in samp]) else: lamb = 0 print('lamb:', lamb) greed = GreedyDiv([], lamb) U_all = [] labels_samp = set(Itemset.labels) while len(self) < max_k and len(labels_samp) > 0: if mode == 0: samps = [] for label in labels_samp: _, samp = sample_rn(nsamp, label) samp = [Rule(s, label) for s in list(samp)] # Very time-consuming samps.extend(samp) U = set(samps) else: covered = set([t for r in self.rules for t in r.trans()]) U = self.sample_from_each_label(labels_samp, nsamp, covered, mode) print('nsamp (after):', len(U)) if len(U) == 0: break U_all.extend(U) # Greedy greed.update_univ(U) r = greed.greedy_once() # Termination criteria. Also check zero sampling above. if self.enough(r): # Include at least one rule per class, except default class. labels_samp.remove(r.label) print('remove label:', r.label) else: # Print quality vs. dispersion q, d = obj(self.rules, lamb, sep=True) qr, dr = obj(self.rules + [r], lamb, sep=True) print('inc q vs. d: {}, {}'.format(qr - q, dr - d)) self.add(r) if np.abs(recall(self.rules)[r.label] - 1.0) < 1e-8: labels_samp.remove(r.label) print('#{} '.format(len(self.rules)), end='') printRules([r]) # Consecutive greedy over all sampels if rerun: greed.clear() greed.update_univ(list(set(U_all))) rules = greed.greedy(len(self.rules)) if obj(rules, lamb) > obj(self.rules, lamb): print('Full greedy wins: {} > {}'.format( obj(rules, lamb), obj(self.rules, lamb))) self.reset(rules) default = self.set_default() print('default:', default) self.build() print('precision: ', precision(self).items()) print('recall (coverage): ', recall(self.rules).items()) print('ave disp: ', dispersion(self.rules, average=True)) print('##### END #####')
def on_key_press(k, *etc): ch.send(obj(type='key', key=k))
def on_mouse_release(x, y, button, *etc): ch.send(obj(type='release', pos=vec2(x, y), button=button))
def on_mouse_drag(x, y, *etc): aps.mouse_pos = vec2(x, y) ch.send(obj(type='motion', pos=vec2(x, y)))
def on_mouse_press(x, y, button, *etc): ch.send(obj(type='press', pos=vec2(x, y), button=button))