Esempio n. 1
0
def sample_positions_from_games(sgf_files, num_positions=1):
    pos_data = []
    move_data = []
    result_data = []
    move_idxs = []

    fail_count = 0
    for path in tqdm(sgf_files, desc="loading sgfs", unit="games"):
        try:
            positions, moves, results = oneoff_utils.parse_sgf(path)
        except KeyboardInterrupt:
            raise
        except Exception as e:
            print("Parse exception:", e)
            fail_count += 1
            continue

        # add entire game
        if num_positions == -1:
            pos_data.extend(positions)
            move_data.extend(moves)
            move_idxs.extend(range(len(positions)))
            result_data.extend(results)
        else:
            for idx in np.random.choice(len(positions), num_positions):
                pos_data.append(positions[idx])
                move_data.append(moves[idx])
                result_data.append(results[idx])
                move_idxs.append(idx)
    print("Sampled {} positions, failed to parse {} files".format(
        len(pos_data), fail_count))
    return pos_data, move_data, result_data, move_idxs
def subsample():
    """Sample num_positions postions from each game in sgf_dir

    Usage:
        python3 sharp_positions.py subsample --num_positions 10 --sgf_dir data/s

    NOTE(sethtroisi): see link for a script to truncate SGFs at move number
        https://github.com/sethtroisi/go-scripts
    """

    sgf_files = oneoff_utils.find_and_filter_sgf_files(FLAGS.sgf_dir, None,
                                                       None)

    with open(FLAG.collection, 'w') as collection:
        fails = 0
        for path in tqdm(sorted(sgf_files)):
            try:
                positions, moves, results = oneoff_utils.parse_sgf(path)
            except KeyboardInterrupt:
                raise
            except Exception as e:
                fails += 1
                print("Fail {}, while parsing {}: {}".format(fails, path, e))
                continue

            moves = len(positions)
            indexes = random.sample(range(10, moves), FLAGS.num_positions)
            for index in sorted(indexes):
                collection.write('{}, {}\n'.format(path, index))
Esempio n. 3
0
def positions_from_sgfs(sgf_files):
    # sgf_replay doesn't like empty sgf file
    data = [("empty", go.Position(komi=7.5))]
    for sgf in sgf_files:
        sgf_name = os.path.basename(sgf).replace(".sgf", "")
        positions, moves, _ = oneoff_utils.parse_sgf(sgf)
        final = positions[-1].play_move(moves[-1])
        data.append((sgf_name, final))
    return data
Esempio n. 4
0
def positions_from_sgfs(sgf_files, include_empty_position=True):
    data = []
    if include_empty:
        # sgf_replay doesn't like SGFs with no moves played.
        # Add the empty position for analysis manually.
        data.append(("empty", go.Position(komi=7.5)))
    for sgf in sgf_files:
        sgf_name = os.path.basename(sgf).replace(".sgf", "")
        positions, moves, _ = oneoff_utils.parse_sgf(sgf)
        final = positions[-1].play_move(moves[-1])
        data.append((sgf_name, final))
    return data