def get_final_ownership(gnu_sgf_outputfile, board_size=19): sgffile = open(gnu_sgf_outputfile, 'r') sgfContents = sgffile.read() sgffile.close() sgf = gomill.sgf.Sgf_game.from_string(sgfContents) if sgf.get_size() != board_size: print('boardsize not %d, ignoring' % board_size) return board = GoBoard.GoBoard(board_size) for move in sgf.root.get_setup_stones()[0]: board.applyMove("b", move) for move in sgf.root.get_setup_stones()[1]: board.applyMove("w", move) moveIdx = 0 for it in sgf.main_sequence_iter(): (color, move) = it.get_move() if color != None and move != None: (row, col) = move board.applyMove(color, (row, col)) moveIdx = moveIdx + 1 black_ownership = board.get_final_ownership('b') white_ownership = np.zeros((board_size, board_size)) for i in xrange(len(white_ownership)): for j in xrange(len(white_ownership)): if black_ownership[i][j] == 0: white_ownership[i][j] = 1 else: white_ownership[i][j] = 0 return black_ownership, white_ownership
def walkthroughSgf(sgf_contents, sgf_file_path, sgf_file_name, output_file_path, completed_dir, board_size, ownership): sgf = gomill.sgf.Sgf_game.from_string(sgf_contents) try: if sgf.get_size() != board_size: print('boardsize not %d, ignoring' % board_size) return goBoard = GoBoard.GoBoard(board_size) if sgf.get_handicap() != None and sgf.get_handicap() != 0: print 'handicap not zero, ignoring (' + str( sgf.get_handicap()) + ')' return except: print "Error getting handicap. Ignoring this file" return moveIdx = 0 #here we attempt to use gnugo to finish the game and get the final ownership. #we check the score gnugo gives with the score written in the file, if this score is off #by more than difference_threshold then we skip the file. gnugo is often off by 1 because #of chinese scoring. black_ownership, white_ownership = None, None if ownership: black_ownership, white_ownership = finish_games.finish_sgf_and_get_ownership( sgf_file_path, sgf_file_name, completed_dir, board_size, difference_threshold=6, year_lowerbound=0 ) #set year_lowerbound will ignore all games before given year if black_ownership is None or white_ownership is None: print "Unable to get final ownership for %s" % sgf_file_path return #all samples from this sgf will be written to this file output_file = open(output_file_path, 'wb') for it in sgf.main_sequence_iter(): (color, move) = it.get_move() if color != None and move != None: (row, col) = move addToDataFile(output_file, color, move, goBoard, ownership, black_ownership, white_ownership) try: goBoard.applyMove(color, (row, col)) except: print "exception caught at move %d" % (moveIdx) print "Ignoring the rest of this file" output_file.close() return moveIdx = moveIdx + 1 output_file.close()
def walkthroughSgf(countOnly, datafile, sgfContents): sgf = gomill.sgf.Sgf_game.from_string(sgfContents) # print sgf if sgf.get_size() != 19: print('boardsize not 19, ignoring') return goBoard = GoBoard.GoBoard(19) doneFirstMove = False if sgf.get_handicap() != None and sgf.get_handicap() != 0: #print 'handicap not zero, ignoring (' + str( sgf.get_handicap() ) + ')' #handicappoints = gomill.handicap_layout.handicap_points( sgf.get_handicap(), 19 ) numhandicap = sgf.get_handicap() #print sgf.get_root().get_setup_stones() #sys.exit(-1) #for move in getHandicapPoints( numhandicap ): for set in sgf.get_root().get_setup_stones(): #print set for move in set: #print move goBoard.applyMove('b', move) #sys.exit(-1) # print( 'handicap: ' + str(numhandicap) ) doneFirstMove = True #sys.exit(-1) # first, count number of moves... if countOnly: numMoves = 0 countDoneFirstMove = doneFirstMove for it in sgf.main_sequence_iter(): (color, move) = it.get_move() if color != None and move != None: #(row,col) = move if countDoneFirstMove: numMoves = numMoves + 1 #addToDataFile( datafile, color, move, goBoard ) countDoneFirstMove = True return numMoves #writeFileHeader( datafile, numMoves, 7, 19, 'int', 1 ) moveIdx = 0 for it in sgf.main_sequence_iter(): (color, move) = it.get_move() if color != None and move != None: (row, col) = move if doneFirstMove: addToDataFile(datafile, color, move, goBoard) goBoard.applyMove(color, (row, col)) moveIdx = moveIdx + 1 doneFirstMove = True
def walkthroughSgf( countOnly, datafile, sgfContents ): sgf = gomill.sgf.Sgf_game.from_string( sgfContents ) # print sgf if sgf.get_size() != 19: print( 'boardsize not 19, ignoring' ) return goBoard = GoBoard.GoBoard(19) doneFirstMove = False if sgf.get_handicap() != None and sgf.get_handicap() != 0: #print 'handicap not zero, ignoring (' + str( sgf.get_handicap() ) + ')' #handicappoints = gomill.handicap_layout.handicap_points( sgf.get_handicap(), 19 ) numhandicap = sgf.get_handicap() #print sgf.get_root().get_setup_stones() #sys.exit(-1) #for move in getHandicapPoints( numhandicap ): for set in sgf.get_root().get_setup_stones(): #print set for move in set: #print move goBoard.applyMove( 'b', move ) #sys.exit(-1) # print( 'handicap: ' + str(numhandicap) ) doneFirstMove = True #sys.exit(-1) # first, count number of moves... if countOnly: numMoves = 0 countDoneFirstMove = doneFirstMove for it in sgf.main_sequence_iter(): (color,move) = it.get_move() if color != None and move != None: #(row,col) = move if countDoneFirstMove: numMoves = numMoves + 1 #addToDataFile( datafile, color, move, goBoard ) countDoneFirstMove = True return numMoves #writeFileHeader( datafile, numMoves, 7, 19, 'int', 1 ) moveIdx = 0 for it in sgf.main_sequence_iter(): (color,move) = it.get_move() if color != None and move != None: (row,col) = move if doneFirstMove: addToDataFile( datafile, color, move, goBoard ) goBoard.applyMove( color, (row,col) ) moveIdx = moveIdx + 1 doneFirstMove = True
def walkthroughSgf(datafile, sgfContents): sgf = gomill.sgf.Sgf_game.from_string(sgfContents) # print sgf if sgf.get_size() != 19: print('boardsize not 19, ignoring') return goBoard = GoBoard.GoBoard(19) doneFirstMove = False if sgf.get_handicap() != None and sgf.get_handicap() != 0: #print 'handicap not zero, ignoring (' + str( sgf.get_handicap() ) + ')' #handicappoints = gomill.handicap_layout.handicap_points( sgf.get_handicap(), 19 ) numhandicap = sgf.get_handicap() #print sgf.get_root().get_setup_stones() #sys.exit(-1) #for move in getHandicapPoints( numhandicap ): for set in sgf.get_root().get_setup_stones(): #print set for move in set: #print move goBoard.applyMove('b', move) #sys.exit(-1) # print( 'handicap: ' + str(numhandicap) ) doneFirstMove = True #sys.exit(-1) moveIdx = 0 for it in sgf.main_sequence_iter(): (color, move) = it.get_move() # print( 'color ' + str(color) ) # print( move ) if color != None: if move == None: print("pass") move = (19, 0) (row, col) = move if doneFirstMove and datafile != None: addToDataFile(datafile, color, move, goBoard) #print 'applying move ' + str( moveIdx ) if row < 19: goBoard.applyMove(color, (row, col)) #print goBoard moveIdx = moveIdx + 1 doneFirstMove = True
def walkthroughSgf( sgfContents , sgfFilepath, output_file_path): sgf = gomill.sgf.Sgf_game.from_string( sgfContents ) try: if sgf.get_size() != BOARD_SIZE: print ('boardsize not %d, ignoring' %BOARD_SIZE ) return goBoard = GoBoard.GoBoard(BOARD_SIZE) if sgf.get_handicap() != None and sgf.get_handicap() != 0: print 'handicap not zero, ignoring (' + str( sgf.get_handicap() ) + ')' return except: print "Error getting handicap. Ignoring this file" return moveIdx = 0 #here we attempt to use gnugo to finish the game and get the final ownership. #we check the score gnugo gives with the score written in the file, if this score is off #by more than difference_treshold then we skip the file. gnugo is often off by 1 because #of chinese scoring. black_ownership, white_ownership = None, None if OWNERSHIP_TARGET: black_ownership, white_ownership = finish_games.finish_sgf_and_get_ownership(sgfFilepath, BOARD_SIZE, difference_treshold = 6, year_lowerbound = 0) #set year_lowerbound will ignore all games before given year if black_ownership is None or white_ownership is None: print "Unable to get final ownership for %s" %sgfFilepath return #all samples from this sgf will be written to this file output_file = open(output_file_path , 'wb') for it in sgf.main_sequence_iter(): (color,move) = it.get_move() if color != None and move != None: (row,col) = move addToDataFile( output_file, color, move, goBoard, black_ownership, white_ownership ) try: goBoard.applyMove( color, (row,col) ) except: print "exception caught at move %d" %(moveIdx) print "Ignoring the rest of this file" output_file.close() return moveIdx = moveIdx + 1 output_file.close()
def walkthroughSgf( datafile, sgfContents ): sgf = gomill.sgf.Sgf_game.from_string( sgfContents ) # print sgf if sgf.get_size() != 19: print( 'boardsize not 19, ignoring' ) return goBoard = GoBoard.GoBoard(19) doneFirstMove = False if sgf.get_handicap() != None and sgf.get_handicap() != 0: #print 'handicap not zero, ignoring (' + str( sgf.get_handicap() ) + ')' #handicappoints = gomill.handicap_layout.handicap_points( sgf.get_handicap(), 19 ) numhandicap = sgf.get_handicap() #print sgf.get_root().get_setup_stones() #sys.exit(-1) #for move in getHandicapPoints( numhandicap ): for set in sgf.get_root().get_setup_stones(): #print set for move in set: #print move goBoard.applyMove( 'b', move ) #sys.exit(-1) # print( 'handicap: ' + str(numhandicap) ) doneFirstMove = True #sys.exit(-1) moveIdx = 0 for it in sgf.main_sequence_iter(): (color,move) = it.get_move() # print( 'color ' + str(color) ) # print( move ) if color != None and move != None: (row,col) = move if doneFirstMove and datafile != None: addToDataFile( datafile, color, move, goBoard ) #print 'applying move ' + str( moveIdx ) goBoard.applyMove( color, (row,col) ) #print goBoard moveIdx = moveIdx + 1 doneFirstMove = True
def get_final_ownership(gnu_sgf_outputfile, board_size = 19): sgffile = open(gnu_sgf_outputfile, 'r') sgfContents = sgffile.read() sgffile.close() sgf = gomill.sgf.Sgf_game.from_string( sgfContents ) if sgf.get_size() != board_size: print ('boardsize not %d, ignoring' %board_size ) return board = GoBoard.GoBoard(board_size) for move in sgf.root.get_setup_stones()[0]: board.applyMove("b", move) for move in sgf.root.get_setup_stones()[1]: board.applyMove("w", move) moveIdx = 0 for it in sgf.main_sequence_iter(): (color,move) = it.get_move() if color != None and move != None: (row,col) = move board.applyMove( color, (row,col) ) moveIdx = moveIdx + 1 black_ownership = board.get_final_ownership('b') white_ownership = np.zeros((board_size, board_size)) for i in xrange(len(white_ownership)): for j in xrange(len(white_ownership)): if black_ownership[i][j] == 0: white_ownership[i][j] = 1 else: white_ownership[i][j] = 0 return black_ownership, white_ownership