def get_modules(samples, path_py, path_cy, ignore_error=False): samples = samples.split(",") samples = list(map(int, samples)) samples.sort() if path_py != "": module_name_py = path_py.split("/")[-1].replace(".py", "") module_path_py = "//".join(path_py.split("/")[:-1]) spec_py = spec_from_file_location(module_name_py, path_py) module_py = module_from_spec(spec_py) sys.path.insert(0, module_path_py) spec_py.loader.exec_module(module_py) timer_py = Timer(module_py, spec_py) timer_py(samples, ignore_error) if path_cy != "": module_name_cy = path_cy.split("/")[-1].replace( ".cp36-win_amd64.pyd", "") module_path_cy = "//".join(path_cy.split("/")[:-1]) spec_cy = spec_from_file_location(module_name_cy, path_cy) module_cy = module_from_spec(spec_cy) sys.path.insert(0, module_path_cy) spec_cy.loader.exec_module(module_cy) timer_cy = Timer(module_cy, spec_cy) timer_cy(samples, ignore_error)
def post(self): checked_request = self.__check_request(request.form) timer = Timer.find(checked_request['id']) if not timer: abort(404) res = datetime.strptime(checked_request['end_at'], '%Y-%m-%d %H:%M:%S') - timer.start_at [tmp_minutes, seconds] = divmod(res.total_seconds(), 60) [hours, minutes] = divmod(tmp_minutes, 60) result_time = '%02d:%02d:%02d' % (hours, minutes, seconds) with Timer.transaction(): timer.update(end_at=checked_request['end_at'], result_time=result_time) # raking update session = Timer._get_session() sum_label = func.sec_to_time( func.sum(func.time_to_sec(Timer.result_time))).label('sum') ranking_data = (session.query(Timer, sum_label).filter( Timer.end_at != None, Timer.spot_id == timer.spot.id).group_by( Timer.user_id).order_by(sum_label.desc()).all()) n = 0 ranking = [] res = {} for row in ranking_data: n += 1 if row[0].user.id == g.user.id: res['rank'] = n res['user_name'] = row[0].user.name res['spot_name'] = row[0].spot.name res['result_time'] = result_time ranking.append({ 'rank': n, 'user': { 'id': row[0].user.id, 'name': row[0].user.name }, 'sum': str(row.sum) }) [lng, lat] = re.findall('[0-9]*\.[0-9]*', timer.spot.latlng) doc = g.mongo.focus rank = doc.ranking rank.update({'spot_id': timer.spot.id}, { '$set': { 'data': ranking, 'spot': { 'name': timer.spot.name, 'lat': lat, 'lng': lng, } } }, upsert=True, multi=False) # end ranking update return jsonify(status=200, message='ok', request=request.form, response=res)
def post(self): checked_request = self.__check_request(request.form) timer = Timer.find(checked_request['id']) if not timer: abort(404) res = datetime.strptime(checked_request['end_at'],'%Y-%m-%d %H:%M:%S') - timer.start_at [tmp_minutes, seconds] = divmod(res.total_seconds(), 60) [hours, minutes] = divmod(tmp_minutes,60) result_time = '%02d:%02d:%02d' % (hours, minutes, seconds) with Timer.transaction(): timer.update( end_at=checked_request['end_at'], result_time=result_time ) # raking update session = Timer._get_session() sum_label = func.sec_to_time(func.sum(func.time_to_sec(Timer.result_time))).label('sum') ranking_data = (session .query(Timer, sum_label) .filter(Timer.end_at!=None, Timer.spot_id==timer.spot.id) .group_by(Timer.user_id) .order_by(sum_label.desc()) .all()) n = 0 ranking = [] res={} for row in ranking_data: n += 1; if row[0].user.id == g.user.id: res['rank'] = n res['user_name'] = row[0].user.name res['spot_name'] = row[0].spot.name res['result_time'] = result_time ranking.append( { 'rank':n, 'user':{ 'id':row[0].user.id, 'name':row[0].user.name }, 'sum':str(row.sum) } ) [lng,lat] = re.findall('[0-9]*\.[0-9]*', timer.spot.latlng) doc = g.mongo.focus rank = doc.ranking rank.update( {'spot_id': timer.spot.id}, {'$set':{'data':ranking,'spot':{'name':timer.spot.name, 'lat':lat, 'lng':lng, } }}, upsert=True, multi=False) # end ranking update return jsonify(status=200, message='ok', request=request.form, response=res)
def index(self): session = Timer._get_session() timer = session.query(Timer).filter(Timer.user_id == g.user.id, Timer.end_at != None).order_by( Timer.created_at.desc()).all() res = [] for row in timer: res.append({ 'id': row.id, 'result_time': row.result_time, 'spot': { 'id': row.spot_id, 'name': row.spot.name, 'foursquare_id': row.spot.forsquare_id, 'latlng': row.spot.latlng }, 'created_at': row.created_at.strftime("%Y年%m月%d日 %H時%M分~") }) return jsonify(status=200, message="ok", request=request.form, response=res)
def get(self, id): session = Timer._get_session() spot = Spot.find(id) if not spot: abort(500) res = [] f = g.mongo.focus print(f) rank = f.ranking return jsonify(status=200, message='ok', request={'id':id}, response=rank.find_one({'spot_id':int(id)},{"_id":0}))
def post(self): checked_request = self.__check_request(request.form) if not Spot.find_by(forsquare_id=checked_request['foursquare_id']): checked_request = self.__re_check_request(request.form) spot = Spot(forsquare_id=checked_request['foursquare_id'], name=checked_request['_location'], latlng='POINT(' + checked_request['lng'] + ' ' + checked_request['lat'] + ")") spot.insert() spot = Spot.find_by(forsquare_id=checked_request['foursquare_id']) with Timer.transaction(): timer = Timer(user_id=g.user.id, spot_id=spot.id, start_at=checked_request["start_at"]) timer.insert() return jsonify(status=200, message='ok', request=request.form, response={'timer_id': timer.id})
def post(self): checked_request = self.__check_request(request.form) if not Spot.find_by(forsquare_id=checked_request['foursquare_id']): checked_request = self.__re_check_request(request.form) spot = Spot( forsquare_id=checked_request['foursquare_id'], name=checked_request['_location'], latlng= 'POINT('+ checked_request['lng'] +' '+ checked_request['lat'] + ")" ) spot.insert() spot = Spot.find_by(forsquare_id=checked_request['foursquare_id']) with Timer.transaction(): timer = Timer(user_id=g.user.id, spot_id=spot.id, start_at=checked_request["start_at"] ) timer.insert() return jsonify(status=200, message='ok', request=request.form, response={'timer_id':timer.id} )
def get(self, id): session = Timer._get_session() spot = Spot.find(id) if not spot: abort(500) res = [] f = g.mongo.focus print(f) rank = f.ranking return jsonify(status=200, message='ok', request={'id': id}, response=rank.find_one({'spot_id': int(id)}, {"_id": 0}))
def index(self): session = Timer._get_session() timer = session.query(Timer).filter(Timer.user_id==g.user.id,Timer.end_at!=None).order_by(Timer.created_at.desc()).all() res = [] for row in timer: res.append( {'id':row.id, 'result_time': row.result_time, 'spot':{ 'id':row.spot_id, 'name':row.spot.name, 'foursquare_id':row.spot.forsquare_id, 'latlng':row.spot.latlng }, 'created_at': row.created_at.strftime("%Y年%m月%d日 %H時%M分~") } ) return jsonify(status=200, message="ok",request=request.form, response=res)
def _timer_initialize(self, complexity): self._timer = Timer(complexity['timer'], self._nightmare_mode, self._view.timer_update, self._game_over) self._timer.start()
class Game(object): TEST = {'rows': 3, 'columns': 4, 'bombs': 1, 'timer': 5} EASY = {'rows': 9, 'columns': 9, 'bombs': 10, 'timer': 20} NORMAL = {'rows': 16, 'columns': 16, 'bombs': 40, 'timer': 120} HARD = {'rows': 16, 'columns': 30, 'bombs': 99, 'timer': 220} def __init__(self): self._model = None self._timer = None self._view = None self._is_game_over = False self._nightmare_mode = False self._closed_cells = 0 self._flags_count = 0 def start(self, complexity): self._model = Field(complexity) self._game_initialize() self._view_initialize(complexity) self._timer_initialize(complexity) self._view.ShowDialog() def _game_initialize(self): self._closed_cells = self._model.size - self._model.bombs self._flags_count = self._model.bombs def _view_initialize(self, complexity): self._view = GameWindow(complexity['rows'], complexity['columns']) self._view.add_handler_new_game_buttons(self._new_game) self._view.set_cell_click_handler(self._mouse_button_down) self._view.set_flags_counter(self._flags_count) def _timer_initialize(self, complexity): self._timer = Timer(complexity['timer'], self._nightmare_mode, self._view.timer_update, self._game_over) self._timer.start() def _mouse_button_down(self, cell, args): if not cell.is_checked: if args.Button == MouseButtons.Right: self._right_click(cell) elif args.Button == MouseButtons.Left: self._left_click(cell) def _right_click(self, cell): if cell.Text != 'F': if self._flags_count: cell.set_value('F') self._flags_count -= 1 else: cell.set_value('') self._flags_count += 1 self._view.set_flags_counter(self._flags_count) def _left_click(self, cell): self._closed_cells -= 1 value = self._model[cell.y][cell.x] if self._model.is_bomb(cell.y, cell.x): self._game_over() else: cell.change_view(value, False) if value == '0': self._check_neighboring_cells(cell) if not self._closed_cells: self._game_over() def _new_game(self, sender, event_args): self._hide_previus_game(sender) self._game_reset(sender) if 'Easy' in event_args.Text: self.start(Game.EASY) elif 'Normal' in event_args.Text: self.start(Game.NORMAL) elif 'Hard' in event_args.Text: self.start(Game.HARD) def _hide_previus_game(self, game_window): game_window.Hide() game_window.Close() def _game_reset(self, game_window): self._is_game_over = False self._nightmare_mode = game_window.checkBox.Checked def _game_over(self): self._is_game_over = True if not self._closed_cells: self._view.set_final_message('You Win!') self._disabled_cells() else: self._view.set_final_message('You Lose') self._show_and_activated_all_bombs() self._timer.stop_timer() def _check_neighboring_cells(self, cell): for dy in (-1, 0, 1): y = cell.y + dy if not (0 <= y < self._model.rows): continue for dx in (-1, 0, 1): x = cell.x + dx if not (0 <= x < self._model.columns): continue neighbro_cell = self._view[y][x] if not neighbro_cell.is_checked: neighbro_cell.programmable_mouse_down() def _show_and_activated_all_bombs(self): for row in self._view: for cell in row: if self._model.is_bomb(cell.y, cell.x): if not cell.is_checked: cell.change_view(self._model[cell.y][cell.x], True) cell.Enabled = False def _disabled_cells(self): for row in self._view: for cell in row: if cell.Enabled: cell.Enabled = False
def train(): total_timer = Timer() train_timer = Timer() load_timer = Timer() max_epoch = 30 epoch_step = int(cfg.train_num//cfg.batch_size) t = 1 for epoch in range(1, max_epoch + 1): print('-'*25, 'epoch', epoch,'/',str(max_epoch), '-'*25) t_loss = 0 ll_loss = 0 r_loss = 0 c_loss = 0 for step in range(1, epoch_step + 1): t = t + 1 total_timer.tic() load_timer.tic() images, labels, imnm, num_boxes, imsize = data.get() # load_timer.toc() feed_dict = {input_: images, get_boxes: labels[..., 1:5][:, ::-1, :], get_classes: labels[..., 0].reshape((cfg.batch_size, -1))[:, ::-1] } if cfg.cnt_branch: _, g_step_, tt_loss, cl_loss, cn_loss, re_loss, lr_ = sess.run( [train_op, global_step, total_loss, cls_loss, cnt_loss, reg_loss, lr], feed_dict = feed_dict) else: _, g_step_, tt_loss, cl_loss, re_loss, lr_ = sess.run( [train_op, global_step, total_loss, cls_loss, reg_loss, lr], feed_dict = feed_dict) total_timer.toc() if g_step_%50 ==0: sys.stdout.write('\r>> ' + 'iters '+str(g_step_)+str('/')+str(epoch_step*max_epoch)+' loss '+str(tt_loss) + ' ') sys.stdout.flush() summary_str = sess.run(summary_op, feed_dict = feed_dict) train_total_summary = tf.Summary(value=[ tf.Summary.Value(tag="config/learning rate", simple_value=lr_), tf.Summary.Value(tag="train/classification/focal_loss", simple_value=cfg.class_weight*cl_loss), tf.Summary.Value(tag="train/classification/cnt_loss", simple_value=cfg.cnt_weight*cn_loss), # tf.Summary.Value(tag="train/p_nm", simple_value=p_nm_), tf.Summary.Value(tag="train/regress_loss", simple_value=cfg.regress_weight*re_loss), # tf.Summary.Value(tag="train/clone_loss", simple_value=cfg.class_weight*cl_loss + cfg.regress_weight*re_loss + cfg.cnt_weight*cn_loss), # tf.Summary.Value(tag="train/l2_loss", simple_value=l2_loss), tf.Summary.Value(tag="train/total_loss", simple_value=tt_loss) ]) print('curent speed: ', total_timer.diff, 'remain time: ', total_timer.remain(g_step_, epoch_step*max_epoch)) summary_writer.add_summary(summary_str, g_step_) summary_writer.add_summary(train_total_summary, g_step_) if g_step_%10000 == 0: print('saving checkpoint') saver.save(sess, cfg.ckecpoint_file + '/model.ckpt', g_step_) total_timer.toc() sys.stdout.write('\n') print('>> mean loss', t_loss) print('curent speed: ', total_timer.average_time, 'remain time: ', total_timer.remain(g_step_, epoch_step*max_epoch)) print('saving checkpoint') saver.save(sess, cfg.ckecpoint_file + '/model.ckpt', g_step_)
print('epoch', epoch) g_list = tf.global_variables() sess = tf.Session() summary_writer = tf.summary.FileWriter(cfg.ckecpoint_file, sess.graph) sess.run(tf.global_variables_initializer()) if restore_path is not None: print('Restoring weights from: ' + restore_path) restorer = tf.train.Saver(g_list) restorer.restore(sess, restore_path) if __name__ == '__main__': val_timer = Timer() data = pascal_voc('test', False, cfg.test_img_path, cfg.test_label_path, cfg.test_img_txt, False) val_pred = [] # saved_pred = {} gt_dict = {} val_rloss = 0 val_closs = 0 val_cnt_loss = 0 for val_step in range(1, cfg.test_num + 1): val_timer.tic() images, labels, imnm, num_boxes, imsize = data.get() feed_dict = { input_: images, get_boxes: labels[..., 1:5][:, ::-1, :], get_classes: labels[..., 0].reshape((cfg.batch_size, -1))[:, ::-1]
def __init__(self): self.players = {} self.pairings = {} self.round_num = 0 self.rounds = 0 self.timer = Timer()
class TournamentOrganizer(object): def __init__(self): self.players = {} self.pairings = {} self.round_num = 0 self.rounds = 0 self.timer = Timer() def add_player(self, name, user): if user.id in [player.user.id for player in self.players.values()]: raise TournamentException(user.name + '#' + str(user.id) + ' has already been added') elif user.name in self.players: existing_player = self.players[user.name] self.players.pop(user.name) new_tag = existing_player.name + '#' + str(existing_player.user.id) existing_player.name = new_tag self.players[new_tag] = existing_player tag = name + '#' + str(user.id) self.players[tag] = Player(tag, user) else: self.players[user.name] = Player(user.name, user) def remove_player(self, name): if name in self.players: um.save_user(self.players[name].user) del self.players[name] else: raise TournamentException(name + ' has not been added yet') def record_win(self, winner, record): if winner in self.pairings: if winner in self.players: self.players[winner].record_win(record) opponent = self.pairings[winner] if not opponent == None and opponent in self.pairings: if opponent in self.players: self.players[opponent].record_loss(record) del self.pairings[opponent] del self.pairings[winner] else: raise TournamentException('Win not recorded, pairing for player ' + winner + ' does not exist') def record_loss(self, loser, record): if loser in self.pairings.keys(): if loser in self.players: self.players[loser].record_loss(record) opponent = self.pairings[loser] if not opponent == None and opponent in self.pairings: if opponent in self.players: self.players[opponent].record_win(record) del self.pairings[opponent] del self.pairings[loser] else: raise TournamentException('Win not recorded, pairing for player ' + winner + ' does not exist') def record_draw(self, player, record): if player in self.pairings: if not self.pairings[player] == None and self.pairings[player] in self.pairings: player_wins, opp_wins, draws = record if player in self.players: self.players[player].record_draw(record) opponent = self.pairings[player] if opponent in self.players: self.players[opponent].record_draw((opp_wins, player_wins, draws)) del self.pairings[player] del self.pairings[opponent] else: raise TournamentException('Draw not recorded, player ' + player + ' has a bye and cannot draw') else: raise TournamentException('Draw not recorded, pairing for player ' + player + ' does not exist') def sorted_players(self, method='by_rank'): if method == 'by_rank': players = self.players.values() return [player.name for player in sorted(players)] elif method == 'by_name': return [player for player in sorted(self.players.keys())] elif method == 'random': players = self.players.keys() random.shuffle(players) return players def make_pair(self, p1, p2): self.pairings[p1] = p2 if p2 != None: self.pairings[p2] = p1 def make_restrictions(self): restrictions = [] for p1 in self.players.keys(): for p2 in self.players[p1].opponents: restrictions.append((p1, p2.name)) return restrictions def make_pairings(self): if to.round_num == 0 and len(to.players) < 4: raise TournamentException('Can\'t start tournament, not enough players') if len(self.pairings) > 0: raise TournamentException('Can\'t make pairings, round still in progress') restrictions = self.make_restrictions() restrictions.append(None) unpaired_count = 2 MAX_BYES = len(to.players) % 2 while unpaired_count > MAX_BYES: self.pairings = {} restrictions = restrictions[:-1] unpaired = self.sorted_players() + [ None ] unpaired_count = len(unpaired) - 1 while len(unpaired) > 1 and not unpaired[0] == None: p1 = unpaired[0] unpaired = unpaired[1:] restricted = [] paired = False while not paired and len(unpaired) > 0: p2 = unpaired[0] unpaired = unpaired[1:] if (p1, p2) in restrictions: restricted.append(p2) else: self.make_pair(p1, p2) paired = True if p2 == None: unpaired_count -= 1 else: unpaired_count -= 2 unpaired = restricted + unpaired def lock_pairings(self): if self.round_num == 0: self.rounds = int(math.ceil(math.log(len(self.players), 2))) self.round_num += 1 self.timer.set(3000) self.timer.start() for p1, p2 in self.pairings.items(): if not p2 == None: self.players[p1].opponents.add(self.players[p2]) self.players[p2].opponents.add(self.players[p1]) def game_end(self): for name, player in self.players.items(): um.save_user(player.user) def reset(self): self.players = {} self.pairings = {} self.round_num = 0 self.rounds = 0 self.timer.reset()