From ca630c80f217414b3b85214f5f89dcf63239ac85 Mon Sep 17 00:00:00 2001 From: andrew Date: Fri, 24 Nov 2023 18:20:55 -0500 Subject: [PATCH] Initial step for PGN uploading 1. Create the interface code to allow for it. 2. Create pgn_util to strip and compress PGNs --- Client/pgn_util.py | 78 ++++++++++++++++++++++++ Engines/Torch.json | 15 +++-- OpenBench/config.py | 2 + OpenBench/models.py | 6 +- OpenBench/workloads/create_workload.py | 2 + OpenBench/workloads/verify_workload.py | 6 ++ Templates/OpenBench/create_workload.html | 9 +++ Templates/OpenBench/test.html | 1 + 8 files changed, 113 insertions(+), 6 deletions(-) create mode 100644 Client/pgn_util.py diff --git a/Client/pgn_util.py b/Client/pgn_util.py new file mode 100644 index 0000000..52c80ea --- /dev/null +++ b/Client/pgn_util.py @@ -0,0 +1,78 @@ +# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # +# # +# OpenBench is a chess engine testing framework by Andrew Grant. # +# # +# # +# OpenBench is free software: you can redistribute it and/or modify # +# it under the terms of the GNU General Public License as published by # +# the Free Software Foundation, either version 3 of the License, or # +# (at your option) any later version. # +# # +# OpenBench is distributed in the hope that it will be useful, # +# but WITHOUT ANY WARRANTY; without even the implied warranty of # +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # +# GNU General Public License for more details. # +# # +# You should have received a copy of the GNU General Public License # +# along with this program. If not, see . # +# # +# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # + +import bz2 +import re +import sys + +def pgn_iterator(fname): + with open(fname) as pgn: + while True: + headers = pgn_list_to_headers(iter(lambda: pgn.readline().rstrip(), '')) + move_list = ' '.join(iter(lambda: pgn.readline().rstrip(), '')) + if not headers or not move_list: + break + yield (headers, move_list) + +def pgn_list_to_headers(lines): + # PGN Format: [
""] + return { f.split()[0][1:] : re.search(r'"([^"]*)"', f).group(1) for f in lines } + +def pgn_strip_headers(headers): + # PGN Format: [
""] + desired = [ 'White', 'Black', 'Result', 'PlyCount', 'FEN', 'TimeControl' ] + return '\n'.join('[%s "%s"]' % (f, headers[f]) for f in desired) + +def pgn_strip_movelist(move_text, compact): + + if not compact: # Captures Score Depth/SelDepth Time Nodes + comment_regex = r'([+-]?M?\d+(?:\.\d+)? \d+/\d+ \d+ \d+)[^}]*' + + else: # Captures Score and nothing else + comment_regex = r'([+-]?M?\d+(?:\.\d+)?) \d+/\d+ \d+ \d+[^}]*' + + # Captures the Move and Comment, discarding extra commentary and move numbers + one_ply_regex = re.compile(r'\s*(?:\d+\. )?([a-zA-Z0-9+=#-]+) {%s}' % (comment_regex)) + + # Captures the trailing game result + result_regex = re.compile(r'\s*(1-0|0-1|1/2-1/2|\*)') + + stripped = '' # Add each: {} + for move, comment in one_ply_regex.findall(move_text): + stripped += '%s {%s} ' % (move, comment) + + # PGNs expect trailing game result text + return stripped + result_regex.search(move_text).group(1) + +def strip_entire_pgn(file_name, compact=True): + + stripped = '' + for header_dict, move_text in pgn_iterator(sys.argv[1]): + stripped += pgn_strip_headers(header_dict) + '\n' + stripped += pgn_strip_movelist(move_text, compact) + '\n' + + return stripped + + +if __name__ == '__main__': + + text = strip_entire_pgn(sys.argv[1], compact=True) + + compressed = bz2.compress(text.encode()) diff --git a/Engines/Torch.json b/Engines/Torch.json index a9f5a53..3167646 100644 --- a/Engines/Torch.json +++ b/Engines/Torch.json @@ -29,20 +29,23 @@ "both_options" : "Threads=1 Hash=64 Minimal=true", "both_time_control" : "60.0+0.6", "workload_size" : 8, - "test_bounds" : "[0.00, 2.25]" + "test_bounds" : "[0.00, 2.25]", + "upload_pgns" : "COMPACT" }, "SMP STC" : { "both_options" : "Threads=6 Hash=128 Minimal=true", "both_time_control" : "10.0+0.1", - "workload_size" : 64 + "workload_size" : 64, + "upload_pgns" : "COMPACT" }, "SMP LTC" : { "both_options" : "Threads=15 Hash=256 Minimal=true", "both_time_control" : "30.0+0.3", "workload_size" : 16, - "test_bounds" : "[0.50, 2.50]" + "test_bounds" : "[0.50, 2.50]", + "upload_pgns" : "COMPACT" }, "STC Simplification" : { @@ -56,7 +59,8 @@ "both_options" : "Threads=1 Hash=64 Minimal=true", "both_time_control" : "60.0+0.6", "workload_size" : 8, - "test_bounds" : "[-1.75, 0.25]" + "test_bounds" : "[-1.75, 0.25]", + "upload_pgns" : "COMPACT" }, "STC Regression" : { @@ -72,7 +76,8 @@ "both_time_control" : "60.0+0.6", "workload_size" : 8, "book_name" : "8moves_v3.epd", - "test_max_games" : 40000 + "test_max_games" : 40000, + "upload_pgns" : "COMPACT" }, "STC Fixed Games" : { diff --git a/OpenBench/config.py b/OpenBench/config.py index e24665c..6725b6b 100644 --- a/OpenBench/config.py +++ b/OpenBench/config.py @@ -126,6 +126,7 @@ def verify_engine_test_preset(test_preset): 'test_max_games', 'book_name', + 'upload_pgns', 'priority', 'throughput', 'workload_size', @@ -159,6 +160,7 @@ def verify_engine_tune_preset(tune_preset): 'spsa_pairs_per', 'book_name', + 'upload_pgns', 'priority', 'throughput', 'syzygy_wdl', diff --git a/OpenBench/models.py b/OpenBench/models.py index 669bfbb..974d8dc 100644 --- a/OpenBench/models.py +++ b/OpenBench/models.py @@ -90,7 +90,8 @@ class Result(Model): class Test(Model): # Misc information - author = CharField(max_length=64) + author = CharField(max_length=64) + upload_pgns = CharField(max_length=16, default='FALSE') # Opening book settings book_name = CharField(max_length=32) @@ -194,3 +195,6 @@ class Network(Model): def __str__(self): return '[{}] {} ({})'.format(self.engine, self.name, self.sha256) + +class PGN(Model): + pass diff --git a/OpenBench/workloads/create_workload.py b/OpenBench/workloads/create_workload.py index 6db0cee..8e7bcad 100644 --- a/OpenBench/workloads/create_workload.py +++ b/OpenBench/workloads/create_workload.py @@ -105,6 +105,7 @@ def create_new_test(request): test = Test() test.author = request.user.username test.book_name = request.POST['book_name'] + test.upload_pgns = request.POST['upload_pgns'] test.dev = get_engine(*dev_info) test.dev_repo = request.POST['dev_repo'] @@ -169,6 +170,7 @@ def create_new_tune(request): test = Test() test.author = request.user.username test.book_name = request.POST['book_name'] + test.upload_pgns = request.POST['upload_pgns'] test.dev = test.base = get_engine(*dev_info) test.dev_repo = test.base_repo = request.POST['dev_repo'] diff --git a/OpenBench/workloads/verify_workload.py b/OpenBench/workloads/verify_workload.py index 0e984ab..e1c8cbe 100644 --- a/OpenBench/workloads/verify_workload.py +++ b/OpenBench/workloads/verify_workload.py @@ -82,6 +82,7 @@ def verify_test_creation(errors, request): # Verify everything about the Test Settings (verify_configuration , 'book_name', 'Book', 'books'), + (verify_upload_pgns , 'upload_pgns', 'Upload PGNs'), (verify_test_mode , 'test_mode'), (verify_sprt_bounds , 'test_bounds'), (verify_sprt_conf , 'test_confidence'), @@ -124,6 +125,7 @@ def verify_tune_creation(errors, request): # Verify everything about the Test Settings (verify_configuration , 'book_name', 'Book', 'books'), + (verify_upload_pgns , 'upload_pgns', 'Upload PGNs'), # Verify everything about the General Settings (verify_integer , 'priority', 'Priority'), @@ -270,6 +272,10 @@ def verify_spsa_distribution_type(errors, request, field, field_name): try: assert request.POST[field] in candidates except: errors.append('%s must be in %s' % (field_name, ', '.join(candidates))) +def verify_upload_pgns(errors, request, field, field_name): + try: request.POST[field] in ['FALSE', 'COMPACT', 'VERBOSE'] + except: errors.append('"%s" must be FALSE, COMPACT, or VERBOSE' % (field_name)) + def collect_github_info(errors, request, field): diff --git a/Templates/OpenBench/create_workload.html b/Templates/OpenBench/create_workload.html index dcc4a9e..2a7a2ac 100644 --- a/Templates/OpenBench/create_workload.html +++ b/Templates/OpenBench/create_workload.html @@ -186,6 +186,15 @@ +
+ + +
+
diff --git a/Templates/OpenBench/test.html b/Templates/OpenBench/test.html index 0f34b9d..05d947b 100644 --- a/Templates/OpenBench/test.html +++ b/Templates/OpenBench/test.html @@ -41,6 +41,7 @@ Base Options{{test.base_options}} Base Time{{test.base_time_control}} Opening Book{{test.book_name}} + Upload PGNs{{test.upload_pgns}} Syzygy WDL{{test.syzygy_wdl}} Syzygy ADJ.{{test.syzygy_adj}} Win ADJ.{{test.win_adj}}