def main(_): with open(FLAGS.hq_replay_set) as f: replay_list = sorted(json.load(f)) race_vs_race = os.path.basename(FLAGS.hq_replay_set).split('.')[0] global_feature_vec_path = os.path.join(FLAGS.parsed_replay_path, 'GlobalFeatureVector', race_vs_race) races = set(race_vs_race.split('_vs_')) for race in races: path = os.path.join(global_feature_vec_path, race) if not os.path.isdir(path): os.makedirs(path) pbar = tqdm(total=len(replay_list), desc='#Replay') for replay_path, replay_info_path in replay_list: with open(replay_info_path) as f: info = json.load(f) info = Parse(info['info'], sc_pb.ResponseReplayInfo()) replay_name = os.path.basename(replay_path) for player_info in info.player_info: race = sc_pb.Race.Name(player_info.player_info.race_actual) player_id = player_info.player_info.player_id reward = player_info.player_result.result replay_player_path = os.path.join( race_vs_race, race, '{}@{}'.format(player_id, replay_name)) parse_replay(replay_player_path, reward, race, race if len(races) == 1 else list(races - {race})[0]) pbar.update()
def sample_action(replay_path, action_path, sampled_path): replay_info = os.path.join(FLAGS.infos_path, replay_path) if not os.path.isfile(replay_info): return with open(replay_info) as f: info = json.load(f) result = [] proto = Parse(info['info'], sc_pb.ResponseReplayInfo()) for p in proto.player_info: player_id = p.player_info.player_id race = sc_common.Race.Name(p.player_info.race_actual) action_file = os.path.join(action_path, race, '{}@{}'.format(player_id, replay_path)) if not os.path.isfile(action_file): return result.append(sample_action_from_player(action_file)) assert len(result) == 2 sampled_actions = sorted(set(result[0]) | set(result[1])) with open(os.path.join(sampled_path, replay_path), 'w') as f: json.dump(sampled_actions, f)
def sample_frames(replay_path, action_path, sampled_frame_path): replay_info = os.path.join(FLAGS.infos_path, replay_path) if not os.path.isfile(replay_info): return with open(replay_info) as f: info = json.load(f) result = [] proto = Parse(info['info'], sc_pb.ResponseReplayInfo()) for p in proto.player_info: # Sample actions taken by each player player_id = p.player_info.player_id race = common_pb.Race.Name(p.player_info.race_actual) action_file = os.path.join(action_path, race, '{}@{}'.format(player_id, replay_path)) if not os.path.isfile( action_file ): # Skip replays where actions haven't been extracted yet print('Unable to locate', action_file) return result.append(sample_frames_from_player(action_file) ) # Get the frames where each player took a macro action assert len(result) == 2 sampled_actions = sorted( set(result[0]) | set(result[1]) ) # Collect all frames where either player took a macro action with open(os.path.join(sampled_frame_path, replay_path), 'w') as f: json.dump(sampled_actions, f)
def main(_): if not os.path.isdir(FLAGS.save_path): os.makedirs(FLAGS.save_path) replay_infos = glob.glob(os.path.join(FLAGS.infos_path, '*.SC2Replay')) run_config = run_configs.get() with run_config.start() as controller: ping = controller.ping() result = {} pbar = tqdm(total=len(replay_infos), desc='#Replay') for info_path in replay_infos: with open(info_path) as f: info = json.load(f) proto = Parse(info['info'], sc_pb.ResponseReplayInfo()) if valid_replay(proto, ping): players_info = proto.player_info races = '_vs_'.join(sorted(sc_pb.Race.Name(player_info.player_info.race_actual) for player_info in players_info)) if races not in result: result[races] = [] result[races].append((info['path'], info_path)) pbar.update() for k, v in result.items(): with open(os.path.join(FLAGS.save_path, k+'.json'), 'w') as f: json.dump(v, f)
def main(argv): with open(FLAGS.hq_replay_set) as f: replay_list = sorted(json.load(f)) race_vs_race = os.path.basename(FLAGS.hq_replay_set).split('.')[0] global_feature_path = os.path.join(FLAGS.parsed_replay_path, 'GlobalFeatures', race_vs_race) for race in set(race_vs_race.split('_vs_')): path = os.path.join(global_feature_path, race) if not os.path.isdir(path): os.makedirs(path) pbar = tqdm(total=len(replay_list), desc='#Replay') for replay_path, replay_info_path in replay_list: # Parse all replays with open(replay_info_path) as f: info = json.load(f) info = Parse(info['info'], sc_pb.ResponseReplayInfo()) replay_name = os.path.basename(replay_path) sampled_frame_path = os.path.join(FLAGS.parsed_replay_path, 'SampledFrames', race_vs_race, replay_name) for player_info in info.player_info: # Parse replay from each players point of view race = common_pb.Race.Name(player_info.player_info.race_actual) player_id = player_info.player_info.player_id reward = player_info.player_result.result replay_player_path = os.path.join(race_vs_race, race, '{}@{}'.format(player_id, replay_name)) parse_replay(replay_player_path, sampled_frame_path, reward) pbar.update()
def __call__(self, line): replay_path, replay_info_path = line with open(replay_info_path) as f: info = json.load(f) info = Parse(info['info'], sc_pb.ResponseReplayInfo()) replay_name = os.path.basename(replay_path) sampled_action_path = os.path.join(FLAGS.parsed_replay_path, 'SampledActions', self.race_vs_race, replay_name) for player_info in info.player_info: race = sc_pb.Race.Name(player_info.player_info.race_actual) player_id = player_info.player_info.player_id reward = 2 - player_info.player_result.result replay_player_path = os.path.join(self.race_vs_race, race, '{}@{}'.format(player_id, replay_name)) parse_replay(replay_player_path, sampled_action_path, reward, race, race if len(self.races) == 1 else list(self.races - {race})[0], self.stats[race])
def main(): np.random.seed(FLAGS.seed) ratio = np.asarray([float(i) for i in FLAGS.ratio.split(':')]) ratio /= np.sum(ratio) if not os.path.isdir(FLAGS.save_path): os.makedirs(FLAGS.save_path) with open(FLAGS.hq_replay_set) as f: replays_list = json.load(f) result = [] race_vs_race = os.path.basename(FLAGS.hq_replay_set).split('.')[0] for replay_path, info_path in replays_list: replay_path_dict = {} replay_name = os.path.basename(replay_path) ## Parsed Replay for race in set(race_vs_race.split('_vs_')): replay_path_dict[race] = [] with open(info_path) as f: info = json.load(f) proto = Parse(info['info'], sc_pb.ResponseReplayInfo()) for p in proto.player_info: player_id = p.player_info.player_id race = sc_pb.Race.Name(p.player_info.race_actual) parsed_replays_info = {} ## Global Feature global_path = os.path.join(FLAGS.parsed_replay_path, 'GlobalFeatureVector', race_vs_race, race, '{}@{}.npz'.format(player_id, replay_name)) if os.path.isfile(os.path.join(FLAGS.root, global_path)): parsed_replays_info['global_path'] = global_path ## Spatial Feature spatial_path_S = os.path.join(FLAGS.parsed_replay_path, 'SpatialFeatureTensor', race_vs_race, race, '{}@{}@S.npz'.format(player_id, replay_name)) if os.path.isfile(os.path.join(FLAGS.root, spatial_path_S)): parsed_replays_info['spatial_path_S'] = spatial_path_S spatial_path_G = os.path.join(FLAGS.parsed_replay_path, 'SpatialFeatureTensor', race_vs_race, race, '{}@{}@G.npz'.format(player_id, replay_name)) if os.path.isfile(os.path.join(FLAGS.root, spatial_path_G)): parsed_replays_info['spatial_path_G'] = spatial_path_G replay_path_dict[race].append(parsed_replays_info) result.append(replay_path_dict) FLAGS.save_path = os.path.join(FLAGS.save_path, race_vs_race) if not os.path.isdir(FLAGS.save_path): os.makedirs(FLAGS.save_path) train_end = int(len(result)*ratio[0]) val_end = int(len(result)*(ratio[0]+ratio[1])) np.random.shuffle(result) save(result[:train_end], 'train', FLAGS.save_path) save(result[train_end:val_end], 'val', FLAGS.save_path) save(result[val_end:], 'test', FLAGS.save_path)