def main(): events = utils.read_from_json("resources/events.json") languages_with_events = utils.read_from_json( "resources/languages_with_events.json" ) #get_languages_with_events(events) for language in languages: language = language.strip("\n") if language != "ar": continue if language not in language_links: continue event_representations = {} for event_type in events: for event in events[event_type]: if event not in languages_with_events: continue representation, y = make_representation( event_type, language, languages_with_events[event]) event_rep = {'representation': representation, 'y': y} event_representations[event] = event_rep if y == 1: print(language, event_representations[event]) utils.save2json(event_representations, "data/data_%s.json" % language)
def get_languages_with_events(events): languages_with_event_query = """ SELECT DISTINCT ?language WHERE { ?sitelink schema:about %s . ?sitelink schema:inLanguage ?language . ?sitelink schema:isPartOf [ wikibase:wikiGroup "wikipedia" ] . } """ languages_with_events = {} for n, event_type in enumerate(events): for event in events[event_type]: q = "wd:" + event result = utils.query_wikidata(languages_with_event_query % q) if result.empty == True: continue languages_with_events[event] = [ x for x in result["language.value"] ] print('languages with events', len(languages_with_events[event])) utils.save2json(languages_with_events, "resources/languages_with_events_%s.json" % n) return languages_with_events
def get_event_distributions(): for lang in languages: event_dist_per_language = {} event_distribution = Counter() lang = lang.strip() print(10 * " * ", lang) # Just using one of the wds for "event": result = utils.query_wikidata(event_distribution_query % ("wd:Q1656682", lang)) if result.empty == True: continue event_types = result["eventType.value"] values = result["cnt.value"] for et, v in zip(event_types, values): event_distribution[et.split("/")[-1]] = int(v) if event_distribution: normalized_distribution = normalize(event_distribution) event_dist_per_language[lang] = normalized_distribution utils.save2json(event_dist_per_language, "resources/event_distributions_%s.json" % lang) event_dist_per_language = {}
def validate(net, val_dataloader,epoch): net.eval() gts, preds = [], [] for batch_idx, batch in enumerate(val_dataloader): inputs, targets, filename, height, width = batch inputs, targets = inputs.cuda(), targets.cuda() inputs, targets = Variable(inputs, volatile=True), Variable(targets) outputs,weights = net(inputs) #loss = utils.cross_entropy2d(outputs, targets) pred = np.squeeze(outputs.data.max(1)[1].cpu().numpy(), axis=1) gt = targets.data.cpu().numpy() for gt_, pred_ in zip(gt, pred): gts.append(gt_) preds.append(pred_) score, class_iou = metric.scores(gts, preds, n_class=21) for k, v in score.items(): print k, v for i in range(21): print i, class_iou[i] metric_json_path = os.path.join(configs.py_dir,'results/metric' + str(epoch) + '.json') class_iou_json_path = os.path.join(configs.py_dir,'results/class_iu' + str(epoch) + '.json') utils.save2json(score,metric_json_path) utils.save2json(class_iou,class_iou_json_path) return score['Mean IoU :']
def _describe(self): x_dict = dict(train=self.l_train, valid=self.l_valid) self._plot_loss(x_dict) # to json json_file_name = os.path.join(self.config.result_dir, "result.json") save2json(x_dict, json_file_name) # save model self._save_model(epoch=self.config.train.epoch, name="last")
def delet_player(game_id, admin_key, player_id): """ Delet an existent players. Parameters ---------- game_id: int Unique game id. admin_key : str Unique admin key. player_id: int Unique player id. Request body ------------ message: str Ok Errors ------ 400: Game id not exist. 400: Player id not exist. 401: Admin key invalid. """ check_game_id(game_id) check_admin_key(game_id, admin_key) # Read game file file_name = "gameId_{}.json".format(game_id) game = read4json('games/' + file_name) # Check if the player id exist if len(game['players']) < player_id: abort(400, 'Player id not exist') # Delet player and generate a new key for this payer game['players'][player_id]['asigned'] = False game['players'][player_id]['key'] = key_generator() # Save the game file file_name = "{}gameId_{}.json".format(games_path, game_id) save2json(file_name, game) # Read all unasigned players numbers asigned = [x['asigned'] for x in game['players']] numbers = [num for num, val in enumerate(asigned) if not val] out = { 'messag': 'player deleted', 'player ready': game['players num'] - len(numbers), 'missing players': len(numbers) } return jsonify(out)
def get_language_pairs(): ''' Get language links per query language for languages that have the same pages as query language.''' for lang in languages: language_links = {} lang = lang.strip() pairs = Counter() print(10 * " * ", lang) result = utils.query_wikidata(language_pairs_query % (lang, lang)) if result.empty == True: continue l1s = result["l1.value"] values = result["cnt.value"] for l1, v in zip(l1s, values): pairs[l1] = int(v) language_links[lang] = normalize(pairs) utils.save2json(language_links, "resources/language_links_%s.json" % lang)
def get_events_from_type(): events_query = """ SELECT ?subtype ?subtypeLabel ?type ?typeLabel #(COUNT(?x) AS ?cnt) WHERE { ?type wdt:P279 wd:Q1656682 . ?subtype wdt:P31 ?type . SERVICE wikibase:label { bd:serviceParam wikibase:language "en" . } } """ events_in_type = defaultdict(lambda: []) result = utils.query_wikidata(events_query) subtypes = result["subtype.value"] EVENT_type = result["type.value"] for t, st in zip(EVENT_type, subtypes): t = t.split("/")[-1] st = st.split("/")[-1] events_in_type[t].append(st) utils.save2json(events_in_type, "resources/events_in_type.json") return events_in_type
def extract_resources(data): """ Make a resource extraction. Parameter --------- game_id: int Unique game id. player_id: int Unique player id. extraction: int Number of extracted resources. Event return name ----------------- game_general_infomation Return ------ TODO ------ Error: Is not your turn to play. Error: Is not all player ready to play. """ """ # Check if all players are ready to play ready = all([player['asigned'] for player in game['players']]) if not ready: return jsonify({'error': "Missing players to start the game."}) # Check if is the player turn to play if not game['players'][player_id]['can play']: return jsonify({'error': 'Is not your turn to play'}) """ game_id = data['game_id'] player_id = data['player_id'] # Read game file_name = "gameId_{}.json".format(game_id) game = read4json('games/' + file_name) # Save player extraction extraction = { 'extraction': data['extraction'], 'total extraction': -1, 'other extraction': -1, 'penalty': 0, 'gain': -1, 'gain_penalty': -1 } game['players'][player_id]['rounds'].append(extraction) game['players'][player_id]['can play'] = False # Check if round finished can_play = [player['can play'] for player in game['players']] round_end = True not in can_play if round_end: calculate_gain(game) send_rules_message(game) # Save the game file file_name = "{}gameId_{}.json".format(games_path, game_id) save2json(file_name, game) # General info to return general_info = { 'players number': game['players number'], 'players ready': game['players ready'], 'rounds number': game['rounds number'], 'round actual': game['round actual'], 'game id': game['game id'], 'date': game['date'], 'state': game['state'], 'players info': [{ 'id': player['id'], 'asigned': player['asigned'], 'can play': player['can play'] } for player in game['players']] } emit('game_general_information', general_info, room=game_id)
def new_player(game_id): """ Create new player in a existen game. Generate the player credential. Parameter --------- game_id: int Unique game id. Request body ------------ player id: int Unique player id randomly assigned. player key: str Unique player key used for authenticating. Error ----- 400: Game id not exist. 400: All players was assigned. """ check_game_id(game_id) # Read game file_name = "gameId_{}.json".format(game_id) game = read4json('games/' + file_name) # Read all unasigned players numbers asigned = [x['asigned'] for x in game['players']] numbers = [num for num, val in enumerate(asigned) if not val] # All players were assigned if not numbers: abort(400, 'All players was assigned.') # Choice a random number of player player_id = int(random.choice(numbers)) game['players'][player_id]['asigned'] = True # Update the game state ready_players = game["players number"] - (len(numbers) - 1) if ready_players > 0: text = "waiting players ({0}/{1})".format(ready_players, game["players number"]) game['state'] = text else: game['state'] = "play" game['players ready'] = ready_players # Check if all players are asigned. # If true, 'can play' of each play is put true ready = all([player['asigned'] for player in game['players']]) if ready: for player in game['players']: player['can play'] = True # Save the game file file_name = "{}gameId_{}.json".format(games_path, game_id) save2json(file_name, game) out = { 'player id': player_id, 'player key': game['players'][player_id]['key'] } return jsonify(out)
def new_game(): """ Create a new game with a unique game id. Generate the admin credential. Parameters JSON --------------- name: str Game name. players number: int Number of players. rounds number: int Number of rounds. parameters: list [a, b, alpha, e] use to calculate the gain function. rules: list List with the rules used in the game. bots: list List with the bots used in the game. Request body ------------ game id: int Unique game id. admin key: str Unique admin key used for authenticating. TODO ---- * Con los parametros, calcular la funcion de ganancia para cada juego. * Inicializar los bots en el listado de jugadores. """ content = request.json # Create a unique game id # Busco todos los archivos que empizen con game_ files = [i for i in os.listdir(games_path) if i.startswith('gameId_')] if not files: # no existen juegos anteriores game_id = 1 else: max_num = 0 for file_name in files: ext, text = file_name.split('_', maxsplit=1) num, ext = text.split('.') num = int(num) if num > max_num: max_num = num game_id = max_num + 1 # Create the game key = key_generator() game = { 'game id': game_id, 'date': str(datetime.datetime.now()), 'end': False, 'players number': content['players number'], 'players ready': 0, 'rounds number': content['rounds number'], 'round actual': 1, 'state': 'waiting players(0/{0})'.format(content['players number']), 'admin key': key, 'parameters': content['parameters'], 'rules': content['rules'], 'players': [{ 'asigned': False, 'key': key_generator(), 'id': i, 'can play': False, 'rounds': [] } for i in range(content['players number'])] } # Save the game file_name = "{}gameId_{}.json".format(games_path, game_id) save2json(file_name, game) out = {'game id': game_id, 'admin key': key} return jsonify(out), 201