From 4e8c6c94238b591a9a34690798f50c5da3226431 Mon Sep 17 00:00:00 2001 From: rektdie Date: Sun, 15 Mar 2026 22:48:09 +0100 Subject: [PATCH] interpolation with estimated llr and peak locking --- OpenBench/templatetags/mytags.py | 7 +-- OpenBench/utils.py | 71 ++++++++++++++++++++++++++----- OpenBench/views.py | 2 +- Templates/OpenBench/workload.html | 29 ++++++++++++- 4 files changed, 92 insertions(+), 17 deletions(-) diff --git a/OpenBench/templatetags/mytags.py b/OpenBench/templatetags/mytags.py index ec320ec..bd66498 100644 --- a/OpenBench/templatetags/mytags.py +++ b/OpenBench/templatetags/mytags.py @@ -211,9 +211,9 @@ def llr_history_graph(test, width=320, height=112): history = list(OpenBench.utils.get_llr_history(test)) if not history: - history = [[0, 0.0], [max(test.games, 1), test.currentllr]] + history = [[0, 0.0, False], [max(test.games, 1), test.currentllr, False]] elif history[-1][0] != test.games or history[-1][1] != test.currentllr: - history.append([test.games, test.currentllr]) + history.append([test.games, test.currentllr, False]) x_max = max(max(point[0] for point in history), 1) observed = [point[1] for point in history] + [0.0] @@ -247,10 +247,11 @@ def llr_history_graph(test, width=320, height=112): { 'games': x, 'llr': round(y, 4), + 'estimated': estimated, 'x': round(scale_x(x), 2), 'y': round(scale_y(y), 2), } - for x, y in history + for x, y, estimated in history ] y_mid = (y_min + y_max) / 2.0 x_mid = x_max / 2.0 diff --git a/OpenBench/utils.py b/OpenBench/utils.py index 7e530bb..4646c57 100644 --- a/OpenBench/utils.py +++ b/OpenBench/utils.py @@ -136,7 +136,7 @@ def path_join(*args): def llr_history_path(test_id): return os.path.join(MEDIA_ROOT, 'llr_history', '%d.json' % (test_id)) -def get_llr_history(test): +def load_llr_history(test): path = llr_history_path(test.id) if not os.path.exists(path): @@ -149,6 +149,63 @@ def get_llr_history(test): except Exception: return [[0, 0.0]] +def interpolate_llr_history(history, max_points=120): + + if not history: + return [] + + normalized = [] + for games, llr in sorted(history, key=lambda point: point[0]): + point = [int(games), float(llr), False] + if normalized and normalized[-1][0] == point[0]: + normalized[-1] = point + else: + normalized.append(point) + + if not normalized or normalized[0][0] != 0: + normalized.insert(0, [0, 0.0, False]) + + if len(normalized) == 1: + return normalized + + final_games = max(normalized[-1][0], 1) + desired_points = min(max_points, max(2, final_games + 1)) + step = final_games / float(desired_points - 1) + source_index = 0 + estimated = [] + + for index in range(desired_points): + target_games = final_games if index == desired_points - 1 else index * step + + while source_index + 1 < len(normalized) and normalized[source_index + 1][0] < target_games: + source_index += 1 + + left = normalized[source_index] + if source_index + 1 >= len(normalized): + llr = left[1] + else: + right = normalized[source_index + 1] + game_span = right[0] - left[0] + if game_span <= 0: + llr = right[1] + else: + ratio = (target_games - left[0]) / float(game_span) + llr = left[1] + ratio * (right[1] - left[1]) + + display_games = int(round(target_games)) + estimated.append([display_games, round(llr, 4), True]) + + merged = {} + for games, llr, is_estimated in estimated + normalized: + existing = merged.get(games) + if existing is None or (existing[2] and not is_estimated): + merged[games] = [games, llr, is_estimated] + + return [merged[key] for key in sorted(merged.keys())] + +def get_llr_history(test, max_points=120): + return interpolate_llr_history(load_llr_history(test), max_points=max_points) + def extract_option(options, option): match = re.search(r'(?<={0}=")[^"]*'.format(option), options) @@ -526,9 +583,9 @@ def update_test(request, machine): return [{}, { 'stop' : True }][test.finished] -def record_llr_history(test, max_points=120): +def record_llr_history(test): - history = get_llr_history(test) + history = load_llr_history(test) point = [test.games, round(test.currentllr, 4)] if history and history[-1][0] == point[0]: @@ -539,14 +596,6 @@ def record_llr_history(test, max_points=120): if not history or history[0][0] != 0: history.insert(0, [0, 0.0]) - if len(history) > max_points: - keep = [history[0]] - interior = history[1:-1:2] - history = keep + interior + [history[-1]] - - while len(history) > max_points: - history = [history[0]] + history[1:-1:2] + [history[-1]] - path = llr_history_path(test.id) os.makedirs(os.path.dirname(path), exist_ok=True) with open(path, 'w') as fout: diff --git a/OpenBench/views.py b/OpenBench/views.py index 838feb7..92bd0a3 100644 --- a/OpenBench/views.py +++ b/OpenBench/views.py @@ -951,4 +951,4 @@ def api_pgns(request, pgn_id): # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # def buyEthereal(request): - return render(request, 'buyEthereal.html') \ No newline at end of file + return render(request, 'buyEthereal.html') diff --git a/Templates/OpenBench/workload.html b/Templates/OpenBench/workload.html index a9faa7a..116e36c 100644 --- a/Templates/OpenBench/workload.html +++ b/Templates/OpenBench/workload.html @@ -10,6 +10,20 @@ document.querySelectorAll('.llr-history-widget').forEach(function(widget) { var history = JSON.parse(widget.dataset.history || '[]'); if (!history.length) return; + var realPoints = history.filter(function(point) { return !point.estimated; }); + var peakGames = {}; + + for (var r = 1; r < realPoints.length - 1; r++) { + var prev = realPoints[r - 1]; + var curr = realPoints[r]; + var next = realPoints[r + 1]; + var isPeak = curr.llr > prev.llr && curr.llr > next.llr; + var isValley = curr.llr < prev.llr && curr.llr < next.llr; + + if (isPeak || isValley) { + peakGames[curr.games] = true; + } + } var svg = widget.querySelector('.llr-history-graph'); var plot = widget.querySelector('.llr-history-plot'); @@ -30,7 +44,7 @@ hoverPoint.classList.toggle('llr-hover-point-positive', point.llr >= 0); hoverPoint.classList.toggle('llr-hover-point-negative', point.llr < 0); - tooltip.textContent = point.games + ' games | LLR ' + point.llr.toFixed(2); + tooltip.textContent = point.games + ' games | LLR ' + (point.estimated ? '≈' : '') + point.llr.toFixed(2); tooltip.classList.add('active'); var left = clamp((point.x / viewBox.width) * plot.clientWidth + 8, 8, plot.clientWidth - tooltip.offsetWidth - 8); @@ -50,13 +64,24 @@ var localX = ((clientX - rect.left) / rect.width) * viewBox.width; var best = history[0]; var bestDistance = Math.abs(best.x - localX); + var bestPeak = null; + var bestPeakDistance = Infinity; - for (var i = 1; i < history.length; i++) { + for (var i = 0; i < history.length; i++) { var distance = Math.abs(history[i].x - localX); if (distance < bestDistance) { best = history[i]; bestDistance = distance; } + + if (!history[i].estimated && peakGames[history[i].games] && distance < bestPeakDistance) { + bestPeak = history[i]; + bestPeakDistance = distance; + } + } + + if (bestPeak && bestPeakDistance <= 5) { + return bestPeak; } return best;