# ---
# 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
# ---

# MIT License
#
# Copyright (c) 2021 <NAME> and <NAME>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

# ### For Optuna.version>0.14, some modifications needed.

import os
import numpy as np
import sqlite3
from itertools import zip_longest

########## User defined parameters #########
dblist = [
    "./data-directory/example_NMNIST-H.db",
    "./data-directory/example_UCF101.db",
]

# +
########## User defined parameters #########
name_db = dblist[0]

keys = ["learning_rate", "optimizer", "weight_decay", "dropout", "batch_size", "param_llr_loss", "order_sprt"] 
    # For train_ti_nmnist-h.py.
    # Use this for "./data-directory/example_NMNIST-H.db".
#keys = ["learning_rate", "optimizer", "weight_decay", "dropout", "batch_size", "param_llr_loss", "order_sprt", "beta"] 
    # For train_ti_UCF101.py.
    # Use this for "./data-directory/example_UCF101.db".
#keys = ["learning_rate", "optimizer", "weight_decay", "batch_size", "param_multLam", "order_sprt"] 
    # For train_dre_nmnist-h.py.
#keys = ["learning_rate", "optimizer", "weight_decay", "batch_size", "param_multLam", "order_sprt", "beta"] 
    # For train_dre_UCF101.py.
############################################ end #
nb_params = len(keys)
maxnum_show = 1000

# Get connect cursor
assert os.path.exists(name_db), name_db
con = sqlite3.connect(name_db)
c = con.cursor()

# Get count
count = c.execute("select count(value) from trials").fetchall()[0][0]
raw = c.execute("select * from trials").fetchall()
_count = count

# Get and cleanse trial params 
trials = c.execute("select * from trials order by value asc").fetchall()
trial_params = c.execute("select * from trial_params").fetchall() 

ls_idx = []
for trial in trials:
    if trial[2] == "FAIL":
        continue

    else:
        ls_idx.append([trial[0], trial[3]]) # [int, float] = [trial num, best value]
        if len(ls_idx) == maxnum_show:
            break
            
ls_show = []            
for iter_idx in ls_idx:
    for iter_params in trial_params:
        # Extract tirial params that showed good results
        if iter_params[1] == iter_idx[0]:
            ls_show.append([iter_idx[0], iter_idx[1], iter_params[2], iter_params[3]])

# Show search spaces, and some sanity checks of 'keys'
print("Search Spaces")
assert_set = set()
for i in range(nb_params):
    print(trial_params[i][2:])
    key = trial_params[i][2]
    assert key in keys, "Maybe there is another keyword that should be included in 'keys'. See 'trial_params'."
    if assert_set >= {key}:
        raise ValueError("Maybe there is an irrelevant keyword in 'keys' (or a typo?).")        
    assert_set.add(trial_params[i][2])
    
if not (assert_set >= {trial_params[i+1][2]}):
    raise ValueError("Maybe there are more keywords that should be included in 'keys'. See 'trial_params'.")
    
# Show top parameters
dc = dict()
for key in keys:
    dc[key] = []

for itr_show in ls_show:
    for key in keys:
        if itr_show[-2] == key:
            dc[key].append(itr_show)     
            
    
# Show trial params
###################
print("\nNum trials: {}".format(_count))
print("trial number, best value\n               {}".format(keys))

for i in range(len(dc[keys[0]])):
    for j, key in enumerate(keys):
        result = dc[key]

        if result[i][1] is None:
            continue
        
        if j == 0:
            print("{:4d} {:.7f}".format(result[i][0], result[i][1]), "          ", end="")

        print("{}          ".format(result[i][3]), end="")

    if result[i][1] is not None:            
        print("")
        
# -

# select * from trials
print("raw trial data")
raw

# select * from trials order by value asc
print("All trials")
trials

# select * from trial_params
print("All trial parameters")
trial_params[:]

StarcoderdataJupyterScriptsDedupFiltered