Python sgf.parse() Examples
The following are 17
code examples of sgf.parse().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
sgf
, or try the search function
.

Example #1
Source File: utils.py From Python-Reinforcement-Learning-Projects with MIT License | 6 votes |
def maybe_correct_next(pos, next_node): if (('B' in next_node.properties and not pos.to_play == GOPARAMETERS.BLACK) or ('W' in next_node.properties and not pos.to_play == GOPARAMETERS.WHITE)): pos.flip_playerturn(mutate=True) # def replay_sgf(sgf_contents): # collection = sgf.parse(sgf_contents) # game = collection.children[0] # props = game.root.properties # assert int(sgf_prop(props.get('GM', ['1']))) == 1, "Not a Go SGF!" # # komi = 0 # if props.get('KM') != None: # komi = float(sgf_prop(props.get('KM'))) # result = parse_game_result(sgf_prop(props.get('RE'))) # # pos = BoardState(komi=komi) # current_node = game.root # while pos is not None and current_node.next is not None: # pos = handle_node(pos, current_node) # maybe_correct_next(pos, current_node.next) # next_move = get_next_move(current_node) # yield PositionWithContext(pos, next_move, result) # current_node = current_node.next
Example #2
Source File: sgf_wrapper.py From Gun-Detector with Apache License 2.0 | 5 votes |
def replay_sgf(board_size, sgf_contents): """Wrapper for sgf files. It does NOT return the very final position, as there is no follow up. To get the final position, call pwc.position.play_move(pwc.next_move) on the last PositionWithContext returned. Example usage: with open(filename) as f: for position_w_context in replay_sgf(f.read()): print(position_w_context.position) Args: board_size: the go board size. sgf_contents: the content in sgf. Yields: The go.PositionWithContext instances. """ collection = sgf.parse(sgf_contents) game = collection.children[0] props = game.root.properties assert int(sgf_prop(props.get('GM', ['1']))) == 1, 'Not a Go SGF!' komi = 0 if props.get('KM') is not None: komi = float(sgf_prop(props.get('KM'))) result = utils.parse_game_result(sgf_prop(props.get('RE'))) pos = Position(board_size, komi=komi) current_node = game.root while pos is not None and current_node.next is not None: pos = handle_node(board_size, pos, current_node) maybe_correct_next(pos, current_node.next) next_move = get_next_move(current_node) yield PositionWithContext(pos, next_move, result) current_node = current_node.next
Example #3
Source File: sgf_wrapper.py From multilabel-image-classification-tensorflow with MIT License | 5 votes |
def replay_sgf(board_size, sgf_contents): """Wrapper for sgf files. It does NOT return the very final position, as there is no follow up. To get the final position, call pwc.position.play_move(pwc.next_move) on the last PositionWithContext returned. Example usage: with open(filename) as f: for position_w_context in replay_sgf(f.read()): print(position_w_context.position) Args: board_size: the go board size. sgf_contents: the content in sgf. Yields: The go.PositionWithContext instances. """ collection = sgf.parse(sgf_contents) game = collection.children[0] props = game.root.properties assert int(sgf_prop(props.get('GM', ['1']))) == 1, 'Not a Go SGF!' komi = 0 if props.get('KM') is not None: komi = float(sgf_prop(props.get('KM'))) result = utils.parse_game_result(sgf_prop(props.get('RE'))) pos = Position(board_size, komi=komi) current_node = game.root while pos is not None and current_node.next is not None: pos = handle_node(board_size, pos, current_node) maybe_correct_next(pos, current_node.next) next_move = get_next_move(current_node) yield PositionWithContext(pos, next_move, result) current_node = current_node.next
Example #4
Source File: sgf_wrapper.py From g-tensorflow-models with Apache License 2.0 | 5 votes |
def replay_sgf(board_size, sgf_contents): """Wrapper for sgf files. It does NOT return the very final position, as there is no follow up. To get the final position, call pwc.position.play_move(pwc.next_move) on the last PositionWithContext returned. Example usage: with open(filename) as f: for position_w_context in replay_sgf(f.read()): print(position_w_context.position) Args: board_size: the go board size. sgf_contents: the content in sgf. Yields: The go.PositionWithContext instances. """ collection = sgf.parse(sgf_contents) game = collection.children[0] props = game.root.properties assert int(sgf_prop(props.get('GM', ['1']))) == 1, 'Not a Go SGF!' komi = 0 if props.get('KM') is not None: komi = float(sgf_prop(props.get('KM'))) result = utils.parse_game_result(sgf_prop(props.get('RE'))) pos = Position(board_size, komi=komi) current_node = game.root while pos is not None and current_node.next is not None: pos = handle_node(board_size, pos, current_node) maybe_correct_next(pos, current_node.next) next_move = get_next_move(current_node) yield PositionWithContext(pos, next_move, result) current_node = current_node.next
Example #5
Source File: util.py From AlphaGOZero-python-tensorflow with MIT License | 5 votes |
def sgf_iter_states(sgf_string, include_end=True): """Iterates over (GameState, move, player) tuples in the first game of the given SGF file. Ignores variations - only the main line is returned. The state object is modified in-place, so don't try to, for example, keep track of it through time If include_end is False, the final tuple yielded is the penultimate state, but the state will still be left in the final position at the end of iteration because 'gs' is modified in-place the state. See sgf_to_gamestate """ collection = sgf.parse(sgf_string) game = collection[0] gs = _sgf_init_gamestate(game.root) if game.rest is not None: for node in game.rest: props = node.properties if 'W' in props: move = _parse_sgf_move(props['W'][0]) player = go.WHITE elif 'B' in props: move = _parse_sgf_move(props['B'][0]) player = go.BLACK yield (gs, move, player) # update state to n+1 gs.do_move(move, player) if include_end: yield (gs, None, None)
Example #6
Source File: sgf_wrapper.py From AlphaGOZero-python-tensorflow with MIT License | 5 votes |
def replay_sgf(sgf_contents): ''' Wrapper for sgf files, exposing contents as position_w_context instances with open(filename) as f: for position_w_context in replay_sgf(f.read()): print(position_w_context.position) ''' collection = sgf.parse(sgf_contents) game = collection.children[0] props = game.root.properties assert int(sgf_prop(props.get('GM', ['1']))) == 1, "Not a Go SGF!" komi = 0 if props.get('KM') != None: komi = float(sgf_prop(props.get('KM'))) metadata = GameMetadata( result=sgf_prop(props.get('RE')), handicap=int(sgf_prop(props.get('HA', [0]))), board_size=int(sgf_prop(props.get('SZ', [19])))) go.set_board_size(metadata.board_size) pos = Position(komi=komi) current_node = game.root while pos is not None and current_node is not None: pos = handle_node(pos, current_node) maybe_correct_next(pos, current_node.next) next_move = get_next_move(current_node) yield PositionWithContext(pos, next_move, metadata) current_node = current_node.next
Example #7
Source File: sgf_wrapper.py From AlphaGOZero-python-tensorflow with MIT License | 5 votes |
def replay_sgf(sgf_contents): ''' Wrapper for sgf files, exposing contents as position_w_context instances with open(filename) as f: for position_w_context in replay_sgf(f.read()): print(position_w_context.position) ''' collection = sgf.parse(sgf_contents) game = collection.children[0] props = game.root.properties assert int(sgf_prop(props.get('GM', ['1']))) == 1, "Not a Go SGF!" komi = 0 if props.get('KM') != None: komi = float(sgf_prop(props.get('KM'))) metadata = GameMetadata( result=sgf_prop(props.get('RE')), handicap=int(sgf_prop(props.get('HA', [0]))), board_size=int(sgf_prop(props.get('SZ', [19])))) go.set_board_size(metadata.board_size) pos = Position(komi=komi) current_node = game.root while pos is not None and current_node is not None: pos = handle_node(pos, current_node) maybe_correct_next(pos, current_node.next) next_move = get_next_move(current_node) yield PositionWithContext(pos, next_move, metadata) current_node = current_node.next
Example #8
Source File: sgf_wrapper.py From training with Apache License 2.0 | 5 votes |
def get_sgf_root_node(sgf_contents): collection = sgf.parse(sgf_contents) game = collection.children[0] return game.root
Example #9
Source File: sgf_wrapper.py From MuGo with Apache License 2.0 | 5 votes |
def replay_sgf(sgf_contents): ''' Wrapper for sgf files, exposing contents as position_w_context instances with open(filename) as f: for position_w_context in replay_sgf(f.read()): print(position_w_context.position) ''' collection = sgf.parse(sgf_contents) game = collection.children[0] props = game.root.properties assert int(sgf_prop(props.get('GM', ['1']))) == 1, "Not a Go SGF!" komi = 0 if props.get('KM') != None: komi = float(sgf_prop(props.get('KM'))) metadata = GameMetadata( result=sgf_prop(props.get('RE')), handicap=int(sgf_prop(props.get('HA', [0]))), board_size=int(sgf_prop(props.get('SZ')))) go.set_board_size(metadata.board_size) pos = Position(komi=komi) current_node = game.root while pos is not None and current_node is not None: pos = handle_node(pos, current_node) maybe_correct_next(pos, current_node.next) next_move = get_next_move(current_node) yield PositionWithContext(pos, next_move, metadata) current_node = current_node.next
Example #10
Source File: training_curve.py From training_results_v0.5 with Apache License 2.0 | 5 votes |
def get_sgf_props(sgf_path): with open(sgf_path) as f: sgf_contents = f.read() collection = sgf.parse(sgf_contents) game = collection.children[0] props = game.root.properties return props
Example #11
Source File: sgf_wrapper.py From alphago_demo with Apache License 2.0 | 5 votes |
def replay_sgf(sgf_contents): ''' Wrapper for sgf files, exposing contents as position_w_context instances with open(filename) as f: for position_w_context in replay_sgf(f.read()): print(position_w_context.position) ''' collection = sgf.parse(sgf_contents) game = collection.children[0] props = game.root.properties assert int(sgf_prop(props.get('GM', ['1']))) == 1, "Not a Go SGF!" komi = 0 if props.get('KM') != None: komi = float(sgf_prop(props.get('KM'))) metadata = GameMetadata( result=sgf_prop(props.get('RE')), handicap=int(sgf_prop(props.get('HA', [0]))), board_size=int(sgf_prop(props.get('SZ')))) go.set_board_size(metadata.board_size) pos = Position(komi=komi) current_node = game.root while pos is not None and current_node is not None: pos = handle_node(pos, current_node) maybe_correct_next(pos, current_node.next) next_move = get_next_move(current_node) yield PositionWithContext(pos, next_move, metadata) current_node = current_node.next
Example #12
Source File: sgf_wrapper.py From training_results_v0.5 with Apache License 2.0 | 5 votes |
def replay_sgf(sgf_contents): ''' Wrapper for sgf files, returning go.PositionWithContext instances. It does NOT return the very final position, as there is no follow up. To get the final position, call pwc.position.play_move(pwc.next_move) on the last PositionWithContext returned. Example usage: with open(filename) as f: for position_w_context in replay_sgf(f.read()): print(position_w_context.position) ''' collection = sgf.parse(sgf_contents) game = collection.children[0] props = game.root.properties assert int(sgf_prop(props.get('GM', ['1']))) == 1, "Not a Go SGF!" komi = 0 if props.get('KM') != None: komi = float(sgf_prop(props.get('KM'))) result = utils.parse_game_result(sgf_prop(props.get('RE'))) assert result is not None pos = Position(komi=komi) current_node = game.root while pos is not None and current_node.next is not None: pos = handle_node(pos, current_node) maybe_correct_next(pos, current_node.next) next_move = get_next_move(current_node) yield PositionWithContext(pos, next_move, result) current_node = current_node.next
Example #13
Source File: training_curve.py From training_results_v0.5 with Apache License 2.0 | 5 votes |
def sample_positions_from_games(sgf_files, num_positions=1): pos_data = [] move_data = [] result_data = [] move_idxs = [] fail_count = 0 for i, path in enumerate(tqdm(sgf_files, desc="loading sgfs", unit="games")): try: positions, moves, result, props = parse_sgf(path) except KeyboardInterrupt: raise except: 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([result for i in range(len(positions))]) else: for idx in np.random.choice(len(positions), num_positions): pos_data.append(positions[idx]) move_data.append(moves[idx]) result_data.append(result) 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
Example #14
Source File: training_curve.py From training_results_v0.5 with Apache License 2.0 | 5 votes |
def get_sgf_props(sgf_path): with open(sgf_path) as f: sgf_contents = f.read() collection = sgf.parse(sgf_contents) game = collection.children[0] props = game.root.properties return props
Example #15
Source File: sgf_wrapper.py From training_results_v0.5 with Apache License 2.0 | 5 votes |
def replay_sgf(sgf_contents): ''' Wrapper for sgf files, returning go.PositionWithContext instances. It does NOT return the very final position, as there is no follow up. To get the final position, call pwc.position.play_move(pwc.next_move) on the last PositionWithContext returned. Example usage: with open(filename) as f: for position_w_context in replay_sgf(f.read()): print(position_w_context.position) ''' collection = sgf.parse(sgf_contents) game = collection.children[0] props = game.root.properties assert int(sgf_prop(props.get('GM', ['1']))) == 1, "Not a Go SGF!" komi = 0 if props.get('KM') != None: komi = float(sgf_prop(props.get('KM'))) result = utils.parse_game_result(sgf_prop(props.get('RE'))) assert result is not None pos = Position(komi=komi) current_node = game.root while pos is not None and current_node.next is not None: pos = handle_node(pos, current_node) maybe_correct_next(pos, current_node.next) next_move = get_next_move(current_node) yield PositionWithContext(pos, next_move, result) current_node = current_node.next
Example #16
Source File: training_curve.py From training_results_v0.5 with Apache License 2.0 | 5 votes |
def sample_positions_from_games(sgf_files, num_positions=1): pos_data = [] move_data = [] result_data = [] move_idxs = [] fail_count = 0 for i, path in enumerate(tqdm(sgf_files, desc="loading sgfs", unit="games")): try: positions, moves, result, props = parse_sgf(path) except KeyboardInterrupt: raise except: 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([result for i in range(len(positions))]) else: for idx in np.random.choice(len(positions), num_positions): pos_data.append(positions[idx]) move_data.append(moves[idx]) result_data.append(result) 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
Example #17
Source File: training_curve.py From training_results_v0.5 with Apache License 2.0 | 5 votes |
def parse_sgf(sgf_path): with open(sgf_path) as f: sgf_contents = f.read() collection = sgf.parse(sgf_contents) game = collection.children[0] props = game.root.properties assert int(sgf_prop(props.get('GM', ['1']))) == 1, "Not a Go SGF!" result = utils.parse_game_result(sgf_prop(props.get('RE'))) positions, moves = zip(*[(p.position, p.next_move) for p in sgf_wrapper.replay_sgf(sgf_contents)]) return positions, moves, result, props