Python utils.parse_game_result() Examples

The following are 21 code examples of utils.parse_game_result(). 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 utils , or try the search function .
Example #1
Source File: prepare_bigquery.py    From training with Apache License 2.0 5 votes vote down vote up
def extract_game_data(gcs_path, root_node):
    props = root_node.properties
    komi = float(sgf_wrapper.sgf_prop(props.get('KM')))
    result = sgf_wrapper.sgf_prop(props.get('RE', ''))
    board_size = int(sgf_wrapper.sgf_prop(props.get('SZ')))
    value = utils.parse_game_result(result)
    was_resign = '+R' in result

    filename = os.path.basename(gcs_path)
    filename_no_ext, _ = os.path.splitext(filename)
    # BigQuery's TIMESTAMP() takes in unix millis.
    completion_millis = 1000 * int(filename_no_ext.split('-')[0])
    worker_id = filename_no_ext.split('-')[-1]
    model_num = shipname.detect_model_num(props.get('PW')[0])
    sgf_url = gcs_path
    first_comment_node_lines = root_node.next.properties['C'][0].split('\n')
    # in-place edit to comment node so that first move's comment looks
    # the same as all the other moves.
    root_node.next.properties['C'][0] = '\n'.join(first_comment_node_lines[1:])
    resign_threshold = float(first_comment_node_lines[0].split()[-1])

    return {
        'worker_id': worker_id,
        'completed_time': completion_millis,
        'board_size': board_size,
        'model_num': model_num,
        'result_str': result,
        'value': value,
        'was_resign': was_resign,
        'sgf_url': sgf_url,
        'resign_threshold': resign_threshold,
    } 
Example #2
Source File: sgf_wrapper.py    From multilabel-image-classification-tensorflow with MIT License 5 votes vote down vote up
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: utils_test.py    From multilabel-image-classification-tensorflow with MIT License 5 votes vote down vote up
def test_parse_game_result(self):
    self.assertEqual(utils.parse_game_result('B+3.5'), go.BLACK)
    self.assertEqual(utils.parse_game_result('W+T'), go.WHITE)
    self.assertEqual(utils.parse_game_result('Void'), 0) 
Example #4
Source File: sgf_wrapper.py    From g-tensorflow-models with Apache License 2.0 5 votes vote down vote up
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: utils_test.py    From g-tensorflow-models with Apache License 2.0 5 votes vote down vote up
def test_parse_game_result(self):
    self.assertEqual(utils.parse_game_result('B+3.5'), go.BLACK)
    self.assertEqual(utils.parse_game_result('W+T'), go.WHITE)
    self.assertEqual(utils.parse_game_result('Void'), 0) 
Example #6
Source File: selfplay.py    From AlphaGOZero-python-tensorflow with MIT License 5 votes vote down vote up
def extract_moves(final_positions):
    winning_moves = []
    losing_moves = []
    for final_position in final_positions:
        positions_w_context = utils.take_n(
            strategies.POLICY_CUTOFF_DEPTH,
            sgf_wrapper.replay_position(final_position))
        winner = utils.parse_game_result(final_position.result())
        for pwc in positions_w_context:
            if pwc.position.to_play == winner:
                winning_moves.append(pwc)
            else:
                losing_moves.append(pwc)
    return (load_data_sets.DataSet.from_positions_w_context(winning_moves),
            load_data_sets.DataSet.from_positions_w_context(losing_moves)) 
Example #7
Source File: selfplay.py    From AlphaGOZero-python-tensorflow with MIT License 5 votes vote down vote up
def get_winrate(final_positions):
    black_win = [utils.parse_game_result(pos.result()) == go.BLACK
                 for pos in final_positions]
    return sum(black_win) / len(black_win) 
Example #8
Source File: test_utils.py    From AlphaGOZero-python-tensorflow with MIT License 5 votes vote down vote up
def test_parse_game_result(self):
        self.assertEqual(utils.parse_game_result('B+3.5'), go.BLACK)
        self.assertEqual(utils.parse_game_result('W+T'), go.WHITE)
        self.assertEqual(utils.parse_game_result('Void'), None) 
Example #9
Source File: test_utils.py    From AlphaGOZero-python-tensorflow with MIT License 5 votes vote down vote up
def test_parse_game_result(self):
        self.assertEqual(utils.parse_game_result('B+3.5'), go.BLACK)
        self.assertEqual(utils.parse_game_result('W+T'), go.WHITE)
        self.assertEqual(utils.parse_game_result('Void'), None) 
Example #10
Source File: sgf_wrapper.py    From training with Apache License 2.0 5 votes vote down vote up
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)
    """
    root_node = get_sgf_root_node(sgf_contents)
    props = root_node.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(komi=komi)
    current_node = root_node
    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 #11
Source File: test_utils.py    From training with Apache License 2.0 5 votes vote down vote up
def test_parse_game_result(self):
        self.assertEqual(go.BLACK, utils.parse_game_result('B+3.5'))
        self.assertEqual(go.WHITE, utils.parse_game_result('W+T'))
        self.assertEqual(0, utils.parse_game_result('Void')) 
Example #12
Source File: training_curve.py    From training_results_v0.5 with Apache License 2.0 5 votes vote down vote up
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 
Example #13
Source File: sgf_wrapper.py    From Gun-Detector with Apache License 2.0 5 votes vote down vote up
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 #14
Source File: utils_test.py    From Gun-Detector with Apache License 2.0 5 votes vote down vote up
def test_parse_game_result(self):
    self.assertEqual(utils.parse_game_result('B+3.5'), go.BLACK)
    self.assertEqual(utils.parse_game_result('W+T'), go.WHITE)
    self.assertEqual(utils.parse_game_result('Void'), 0) 
Example #15
Source File: selfplay.py    From alphago_demo with Apache License 2.0 5 votes vote down vote up
def extract_moves(final_positions):
    winning_moves = []
    losing_moves = []
    for final_position in final_positions:
        positions_w_context = utils.take_n(
            strategies.POLICY_CUTOFF_DEPTH,
            sgf_wrapper.replay_position(final_position))
        winner = utils.parse_game_result(final_position.result())
        for pwc in positions_w_context:
            if pwc.position.to_play == winner:
                winning_moves.append(pwc)
            else:
                losing_moves.append(pwc)
    return (load_data_sets.DataSet.from_positions_w_context(winning_moves),
            load_data_sets.DataSet.from_positions_w_context(losing_moves)) 
Example #16
Source File: selfplay.py    From alphago_demo with Apache License 2.0 5 votes vote down vote up
def get_winrate(final_positions):
    black_win = [utils.parse_game_result(pos.result()) == go.BLACK
                 for pos in final_positions]
    return sum(black_win) / len(black_win) 
Example #17
Source File: test_utils.py    From alphago_demo with Apache License 2.0 5 votes vote down vote up
def test_parse_game_result(self):
        self.assertEqual(utils.parse_game_result('B+3.5'), go.BLACK)
        self.assertEqual(utils.parse_game_result('W+T'), go.WHITE)
        self.assertEqual(utils.parse_game_result('Void'), None) 
Example #18
Source File: test_utils.py    From training_results_v0.5 with Apache License 2.0 5 votes vote down vote up
def test_parse_game_result(self):
        self.assertEqual(utils.parse_game_result('B+3.5'), go.BLACK)
        self.assertEqual(utils.parse_game_result('W+T'), go.WHITE)
        self.assertEqual(utils.parse_game_result('Void'), 0) 
Example #19
Source File: training_curve.py    From training_results_v0.5 with Apache License 2.0 5 votes vote down vote up
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 
Example #20
Source File: sgf_wrapper.py    From training_results_v0.5 with Apache License 2.0 5 votes vote down vote up
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 #21
Source File: test_utils.py    From training_results_v0.5 with Apache License 2.0 5 votes vote down vote up
def test_parse_game_result(self):
        self.assertEqual(utils.parse_game_result('B+3.5'), go.BLACK)
        self.assertEqual(utils.parse_game_result('W+T'), go.WHITE)
        self.assertEqual(utils.parse_game_result('Void'), 0)