# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# # Analysis - exp6
#
# A wythoff stumbler hyper-parameter search.

# +
import os
import csv
import numpy as np
import torch as th

from glob import glob
from pprint import pprint

import matplotlib.pyplot as plt
# %matplotlib inline

# +
# Find the files
path = "/Users/type/Code/azad/data/wythoff/exp6/"
results_path = "/Users/type/Code/azad/data/wythoff/"

files = sorted(glob(os.path.join(path, "run_*")))
files[:10]

# +
# Process 'em
table = []
for fi in files:
    # Extract row_code
    row_code = int(os.path.split(fi)[-1].split('_')[-1].split('.')[0])
    
    # Load data
    model = th.load(fi)
    row = (
        row_code, 
        model['gamma'], 
        model['epsilon'], 
        model['learning_rate_stumbler'],
        model['stumbler_score']
    ) 
    table.append(row)
    
pprint(table[:20])


# +
# Sort by score
def get_score(row):
    return row[-1]

table = sorted(table, key=get_score, reverse=True)
pprint(table[:20])

# +
# Save the sorted results table
f_name = os.path.join(results_path, "exp6_ranked.csv")
head = ["row_code", "gamma", "epsilon", "learning_rate", "score"]

with open(f_name, "w") as csv_file:
    writer = csv.writer(csv_file, delimiter=',')
    writer.writerow(head)
    
    for row in table:
        writer.writerow(row)
# -

# # Plots

# ## Dist. of scores

# +
scores = [row[4] for row in table]

plt.figure(figsize=(3, 2))
_ = plt.hist(scores, color='black', bins=20)
_ = plt.tight_layout()
_ = plt.xlim(0, 1)
_ = plt.ylabel("Counts")
_ = plt.xlabel("Score")
# -

# ## Scores as a fn of other parameters

learning_rates = [row[3] for row in table]

plt.figure(figsize=(2.5, 2.5))
_ = plt.scatter(learning_rates, scores, color='black', alpha=0.1, s=2)
_ = plt.tight_layout()
_ = plt.xlabel("Learning rate")
_ = plt.ylabel("Final optimal play")
_ = plt.ylim(0, 1)

epsilons = [row[2] for row in table]

plt.figure(figsize=(2.5, 2.5))
_ = plt.scatter(epsilons, scores, color='black', alpha=0.1, s=2)
_ = plt.tight_layout()
_ = plt.xlabel("Epsilon")
_ = plt.ylabel("Final optimal play")
_ = plt.ylim(0, 1)

gammas = [row[1] for row in table]

plt.figure(figsize=(2.5, 2.5))
_ = plt.scatter(gammas, scores, color='black', alpha=0.1, s=2)
_ = plt.tight_layout()
_ = plt.xlabel("Gamma")
_ = plt.ylabel("Final optimal play")
_ = plt.ylim(0, 1)



StarcoderdataJupyterScriptsDedupFiltered