Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce Calculate Elo API for external use. #1742

Merged
merged 1 commit into from
Aug 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions server/fishtest/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ def group_finder(username, request):
config.add_route("api_download_nn", "/api/nn/{id}")
config.add_route("api_get_elo", "/api/get_elo/{id}")
config.add_route("api_actions", "/api/actions")
config.add_route("api_calc_elo", "/api/calc_elo")

config.scan()
return config.make_wsgi_app()
76 changes: 76 additions & 0 deletions server/fishtest/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,82 @@ def get_elo(self):
run["elo"] = a
return run

@view_config(route_name="api_calc_elo")
ppigazzini marked this conversation as resolved.
Show resolved Hide resolved
def calc_elo(self):
self.__t0 = datetime.utcnow()
self.__api = "/api/calc_elo"

W = self.request.params.get("W")
D = self.request.params.get("D")
L = self.request.params.get("L")
LL = self.request.params.get("LL")
LD = self.request.params.get("LD")
DDWL = self.request.params.get("DDWL")
WD = self.request.params.get("WD")
WW = self.request.params.get("WW")
elo0 = self.request.params.get("elo0")
elo1 = self.request.params.get("elo1")
elo_model = self.request.params.get("elo_model", "normalized")

if elo_model not in ["BayesElo", "logistic", "normalized"]:
self.handle_error(
"Valid Elo models are: BayesElo, logistic, and normalized."
)

is_ptnml = all(
peregrineshahin marked this conversation as resolved.
Show resolved Hide resolved
value is not None and value.replace(".", "").replace("-", "").isdigit()
for value in [LL, LD, DDWL, WD, WW, elo0, elo1]
)

is_ptnml = is_ptnml and all(int(value) >= 0 for value in [LL, LD, DDWL, WD, WW])

is_wdl = not is_ptnml and all(
value is not None and value.replace(".", "").replace("-", "").isdigit()
for value in [W, D, L, elo0, elo1]
)

is_wdl = is_wdl and all(int(value) >= 0 for value in [W, D, L])

if not is_ptnml and not is_wdl:
self.handle_error(
"Invalid or missing parameters. Please provide all values as valid numbers."
)

if is_ptnml:
LL = int(LL)
LD = int(LD)
DDWL = int(DDWL)
WD = int(WD)
WW = int(WW)
if (LL + LD + DDWL + WD + WW) * 2 > 2**32:
self.handle_error("Number of games exceeds the limit.")
if LL + LD + DDWL + WD + WW == 0:
self.handle_error("No games to calculate Elo.")
results = {
"pentanomial": [LL, LD, DDWL, WD, WW],
}
if is_wdl:
W = int(W)
D = int(D)
L = int(L)
if W + D + L > 2**32:
self.handle_error("Number of games exceeds the limit.")
if W + D + L == 0:
self.handle_error("No games to calculate Elo.")
results = {
"wins": W,
"draws": D,
"losses": L,
}
elo0 = float(elo0)
elo1 = float(elo1)
alpha = 0.05
beta = 0.05
a = SPRT_elo(
results, alpha=alpha, beta=beta, elo0=elo0, elo1=elo1, elo_model=elo_model
)
return a

@view_config(route_name="api_request_task")
def request_task(self):
self.validate_request("/api/request_task")
Expand Down
Loading