def test_handicap_load(self): input_sgf = ( "(;GM[1]FF[4]CA[UTF-8]AP[CGoban:3]ST[2]RU[Chinese]SZ[19]HA[2]KM[0.50]TM[600]OT[5x30 byo-yomi]PW[kneh]PB[ayabot003]WR[4k]BR[6k]DT[2021-01-04]PC[The KGS Go Server at http://www.gokgs.com/]C[ayabot003 [6k\\" "]: GTP Engine for ayabot003 (black): Aya version 7.85x]RE[W+Resign];B[pd]BL[599.647];B[dp]BL[599.477];W[pp]WL[597.432];B[cd]BL[598.896];W[ed]WL[595.78];B[ec]BL[598.558])" ) root = KaTrainSGF.parse_sgf(input_sgf) game = Game(MockKaTrain(force_package_config=True), MockEngine(), move_tree=root) assert 0 == len(game.root.placements) root2 = KaTrainSGF.parse_sgf("(;GM[1]FF[4]SZ[19]HA[2];)") game2 = Game(MockKaTrain(force_package_config=True), MockEngine(), move_tree=root2) assert 2 == len(game2.root.placements)
def load_sgf_from_clipboard(self): clipboard = Clipboard.paste() if not clipboard: self.controls.set_status("Ctrl-V pressed but clipboard is empty.", STATUS_INFO) return url_match = re.match(r"(?P<url>https?://[^\s]+)", clipboard) if url_match: self.log("Recognized url: " + url_match.group(), OUTPUT_INFO) http = urllib3.PoolManager() response = http.request("GET", url_match.group()) clipboard = response.data.decode("utf-8") try: move_tree = KaTrainSGF.parse_sgf(clipboard) except Exception as exc: self.controls.set_status( i18n._("Failed to import from clipboard").format( error=exc, contents=clipboard[:50]), STATUS_INFO) return move_tree.nodes_in_tree[-1].analyze( self.engine, analyze_fast=False) # speed up result for looking at end of game self._do_new_game(move_tree=move_tree, analyze_fast=True) self("redo", 9999) self.log("Imported game from clipboard.", OUTPUT_INFO)
def load_sgf_file(self, file, fast=False, rewind=False): try: move_tree = KaTrainSGF.parse_file(file) except ParseError as e: self.log(i18n._("Failed to load SGF").format(error=e), OUTPUT_ERROR) return self._do_new_game(move_tree=move_tree, analyze_fast=fast) if not rewind: self.game.redo(999)
def test_foxwq(): for sgf in ["data/fox sgf error.sgf", "data/fox sgf works.sgf"]: file = os.path.join(os.path.dirname(__file__), sgf) move_tree = KaTrainSGF.parse_file(file) katrain = KaTrainBase(force_package_config=True, debug_level=0) game = Game(katrain, MagicMock(), move_tree) assert [] == move_tree.placements assert [] == game.root.placements while game.current_node.children: assert 1 == len(game.current_node.children) game.redo(1)
def load_sgf_file(self, file, fast=False, rewind=True): if self.contributing: return try: file = os.path.abspath(file) move_tree = KaTrainSGF.parse_file(file) except (ParseError, FileNotFoundError) as e: self.log(i18n._("Failed to load SGF").format(error=e), OUTPUT_ERROR) return self._do_new_game(move_tree=move_tree, analyze_fast=fast, sgf_filename=file) if not rewind: self.game.redo(999)
def readfile(*args): files = popup_contents.filesel.selection self.fileselect_popup.dismiss() try: move_tree = KaTrainSGF.parse_file(files[0]) except ParseError as e: self.log( i18n._("Failed to load SGF").format(error=e), OUTPUT_ERROR) return self._do_new_game(move_tree=move_tree, analyze_fast=popup_contents.fast.active) if not popup_contents.rewind.active: self.game.redo(999)
def load_sgf_from_clipboard(self): clipboard = Clipboard.paste() if not clipboard: self.controls.set_status(f"Ctrl-V pressed but clipboard is empty.", STATUS_INFO) return try: move_tree = KaTrainSGF.parse_sgf(clipboard) except Exception as exc: self.controls.set_status( i18n._("Failed to import from clipboard").format(error=exc, contents=clipboard[:50]), STATUS_INFO ) return move_tree.nodes_in_tree[-1].analyze( self.engine, analyze_fast=False ) # speed up result for looking at end of game self._do_new_game(move_tree=move_tree, analyze_fast=True) self("redo", 999) self.log("Imported game from clipboard.", OUTPUT_INFO)
def readfile(*_args): files = popup_contents.filesel.selection self.fileselect_popup.dismiss() path, file = os.path.split(files[0]) settings_path = self.config("general/sgf_load") if path != settings_path: self.log(f"Updating sgf load path default to {path}", OUTPUT_INFO) self._config["general"]["sgf_load"] = path self.save_config("general") try: move_tree = KaTrainSGF.parse_file(files[0]) except ParseError as e: self.log( i18n._("Failed to load SGF").format(error=e), OUTPUT_ERROR) return self._do_new_game(move_tree=move_tree, analyze_fast=popup_contents.fast.active) if not popup_contents.rewind.active: self.game.redo(999)
from rank_utils import rank_game def format_rank(rank): if rank <= 0: return f"{1-rank:5.1f}d" else: return f"{rank:5.1f}k" if __name__ == "__main__": kt = KaTrainBase(force_package_config=True) e_config = kt.config("engine") e_config["max_visits"] = e_config[ "fast_visits"] = 1 # since it's just policy anyway engine = KataGoEngine(kt, e_config) for filename in glob.glob("sgf_ogs/*.sgf"): game = Game(kt, engine, move_tree=KaTrainSGF.parse_file(filename)) size = game.board_size len_segment = 80 ranks = rank_game(game, len_segment) if not ranks: continue print("* File name: {0:s}".format(filename)) for start, end, rank in ranks: print( f"\tMove quality for moves {start:3d} to {end:3d}\tB: {format_rank(rank['B'])}\tW: {format_rank(rank['W'])}" )
} return results katrain = KaTrainBase(force_package_config=True, debug_level=0) combined_settings = {**katrain.config("engine"), **settings} engine = KataGoEngine(katrain, {**katrain.config("engine"), **settings}) thresholds = katrain.config("trainer/eval_thresholds") games = [] n = 0 for sgf in os.listdir("sgftest/"): if sgf.lower().endswith("sgf"): print(sgf) with open(os.path.join("sgftest", sgf)) as f: move_tree = KaTrainSGF.parse_sgf(f.read()) games.append( Game(katrain, engine, move_tree=move_tree, analyze_fast=False)) n += 1 if n >= 30000: # small test=3 break while not engine.is_idle(): print( f"waiting for engine to finish...{engine.queries_remaining()} queries left" ) time.sleep(0.5) engine.shutdown(finish=None) reports = [] for game in games: