Skip to content

Commit

Permalink
dumped best params into serialisable and fixed optuna to version 2.3.0 (
Browse files Browse the repository at this point in the history
  • Loading branch information
ghiander authored Mar 15, 2023
1 parent 18473a3 commit c365aed
Show file tree
Hide file tree
Showing 8 changed files with 449 additions and 239 deletions.
13 changes: 13 additions & 0 deletions data/optuna_fitting/03-optuna_deltag_fitting_best_params.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"gd": -0.9076664181509553,
"ga": -16.13143317348325,
"g0": 1.8842791231644282,
"gs": 0.046698073586512054,
"gr": -3.6429426011852524,
"gpi1": -1.6022604035599135,
"gpi2": -1.1740853831959548,
"gi": 4.999560447432514,
"F": 0.5143359987215719,
"expd": 0.5041243951898653,
"expa": 0.3436880638589098
}
10 changes: 5 additions & 5 deletions notebooks/gerber_deltag_validation.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"import os\n",
"import sys\n",
"import bz2\n",
"import json\n",
"import mpld3\n",
"import pandas as pd\n",
"import _pickle as cPickle\n",
Expand Down Expand Up @@ -72,11 +73,10 @@
"input_data = bz2.BZ2File(data_filepath, 'rb')\n",
"input_data = cPickle.load(input_data)\n",
"\n",
"# Open the study\n",
"study_filepath = os.path.join(optuna_path, config.OPTUNA_DELTAG_STUDY_FILENAME)\n",
"study = bz2.BZ2File(study_filepath, 'rb')\n",
"study = cPickle.load(study)\n",
"best_params = study.best_trial.params\n",
"# Open the parameters\n",
"params_filepath = os.path.join(optuna_path, config.OPTUNA_DELTAG_PARAMETERS_FILENAME)\n",
"with open(params_filepath) as f:\n",
" best_params = json.load(f)\n",
"best_params"
]
},
Expand Down
11 changes: 5 additions & 6 deletions notebooks/guthrie_deltag_validation.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"import os\n",
"import sys\n",
"import bz2\n",
"import json\n",
"import mpld3\n",
"import pandas as pd\n",
"import _pickle as cPickle\n",
Expand Down Expand Up @@ -64,14 +65,12 @@
"sys.path.insert(0, opt_path)\n",
"import config\n",
"\n",
"# Open the study\n",
"# Open the parameters\n",
"data_path = os.path.abspath(os.path.join(os.getcwd(), '..', config.DATA_PATH))\n",
"optuna_path = os.path.join(data_path, config.OPTUNA_DIRNAME)\n",
"study_filepath = os.path.join(optuna_path, config.OPTUNA_DELTAG_STUDY_FILENAME)\n",
"study = bz2.BZ2File(study_filepath, 'rb')\n",
"study = cPickle.load(study)\n",
"\n",
"best_params = study.best_trial.params\n",
"params_filepath = os.path.join(optuna_path, config.OPTUNA_DELTAG_PARAMETERS_FILENAME)\n",
"with open(params_filepath) as f:\n",
" best_params = json.load(f)\n",
"best_params"
]
},
Expand Down
10 changes: 7 additions & 3 deletions optimisation/02-deltag_fitting.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Parameter fitting of Jazzy using Optuna and sklearn."""
# optimisation/02-deltag_fitting.py
import config
from helpers import dump_parameters_to_json
from helpers import load_data_configuration
from helpers import run_optimisation
from sklearn.metrics import mean_absolute_error
Expand Down Expand Up @@ -106,10 +107,13 @@ def deltag_objective(trial):
return loss


# load input/ouput configuration
input_data, study_filepath = load_data_configuration(
config.OPTUNA_DELTAG_STUDY_FILENAME
# load input/output configuration
input_data, study_filepath, params_filepath = load_data_configuration(
config.OPTUNA_DELTAG_STUDY_FILENAME, config.OPTUNA_DELTAG_PARAMETERS_FILENAME
)

# run optimisation
study = run_optimisation(deltag_objective, study_filepath, verbose=config.VERBOSE)

# dump best parameters into json
dump_parameters_to_json(study_filepath, params_filepath)
1 change: 1 addition & 0 deletions optimisation/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
ORIGINAL_DATA_FILENAME = "03-free_energies_gerber_curated.txt"
PRECALCULATED_DATA_FILENAME = "01-precalculated_mol_data.pbz2"
OPTUNA_DELTAG_STUDY_FILENAME = "02-optuna_deltag_fitting_study.pbz2"
OPTUNA_DELTAG_PARAMETERS_FILENAME = "03-optuna_deltag_fitting_best_params.json"

# logic configuration
MINIMISATION_METHOD = "MMFF94"
Expand Down
28 changes: 24 additions & 4 deletions optimisation/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,20 @@
# optimisation/helpers.py
import _pickle as cpickle
import bz2
import json
import os

import config
import optuna
from optuna.samplers import TPESampler


def load_data_configuration(study_filename):
def load_data_configuration(study_filename, params_filename):
"""Abstraction for input and output loading and configuration setting.
The method only accepts the name of the output study file, whilst
the rest of the parameters must be defined in a config file.
The method only accepts the name of the output study and best parameters
files, whilst the rest of the parameters must be defined in a config file.
"""
# load the input
data_path = os.path.abspath(os.path.join(os.getcwd(), "..", config.DATA_PATH))
Expand All @@ -24,7 +26,8 @@ def load_data_configuration(study_filename):

# configure output
study_filepath = os.path.join(optuna_path, study_filename)
return input_data, study_filepath
params_filepath = os.path.join(optuna_path, params_filename)
return input_data, study_filepath, params_filepath


def run_optimisation(objective, study_filepath, verbose=False):
Expand Down Expand Up @@ -80,3 +83,20 @@ def early_stopping_opt(study, trial):
# write the results out
with bz2.BZ2File(study_filepath, "w") as f:
cpickle.dump(study, f)


def dump_parameters_to_json(study_filepath, params_filepath):
"""Helper for dumping the study best parameters.
Accepts the path to a study file, unpickles it,
and dumps the best parameters into a serialisable.
"""
# read the parameters
with bz2.BZ2File(study_filepath, "rb") as f:
study = cpickle.load(f)
best_params = study.best_trial.params

# dump the parameters
with open(params_filepath, "w") as f:
json.dump(best_params, f, indent=4)
Loading

0 comments on commit c365aed

Please sign in to comment.