2022-01-11 19:15:58 +01:00
|
|
|
import pickle
|
|
|
|
|
import datetime
|
2022-01-26 23:22:43 +01:00
|
|
|
from config.types import Config
|
2022-01-29 06:41:40 +01:00
|
|
|
from typing import Optional
|
2022-01-11 19:15:58 +01:00
|
|
|
import os
|
|
|
|
|
import warnings
|
2022-01-29 06:41:40 +01:00
|
|
|
from training.types import PipelineOutcome
|
2022-01-11 19:15:58 +01:00
|
|
|
|
2022-01-29 06:41:40 +01:00
|
|
|
def save_models(pipeline_outcome: PipelineOutcome, config: Config) -> None:
|
2022-01-12 23:10:18 +01:00
|
|
|
dict_for_pickle = dict()
|
2022-01-23 18:37:43 +01:00
|
|
|
dict_for_pickle['config'] = config
|
2022-01-29 06:41:40 +01:00
|
|
|
dict_for_pickle['pipeline_outcome'] = pipeline_outcome
|
2022-01-11 19:15:58 +01:00
|
|
|
|
|
|
|
|
date_string = datetime.datetime.now().strftime("%Y-%m-%d-%H-%M")
|
|
|
|
|
|
|
|
|
|
if not os.path.exists('output/models'):
|
|
|
|
|
warnings.warn("No folder exists, creating one.")
|
|
|
|
|
os.makedirs('output/models')
|
|
|
|
|
|
2022-01-12 23:10:18 +01:00
|
|
|
pickle.dump( dict_for_pickle, open( "output/models/{}.p".format(date_string), "wb" ) )
|
2022-01-11 19:15:58 +01:00
|
|
|
|
|
|
|
|
|
2022-01-29 06:41:40 +01:00
|
|
|
def load_models(file_name: Optional[str]) -> tuple[PipelineOutcome, Config]:
|
2022-01-11 19:15:58 +01:00
|
|
|
|
|
|
|
|
if file_name is None:
|
|
|
|
|
warnings.warn("No file name provided, will load latest models and configurations.")
|
|
|
|
|
files_in_directory:list = os.listdir('output/models')
|
|
|
|
|
|
|
|
|
|
assert len(files_in_directory) > 0, "No models found in output/models."
|
|
|
|
|
file_name = sorted(files_in_directory)[-1]
|
|
|
|
|
|
2022-01-14 10:34:28 +01:00
|
|
|
packacked_dict = pickle.load( open( "output/models/{}".format(file_name), "rb" ) )
|
2022-01-11 19:15:58 +01:00
|
|
|
|
2022-01-23 18:37:43 +01:00
|
|
|
config = packacked_dict.pop("config", None)
|
2022-01-29 06:41:40 +01:00
|
|
|
pipeline_outcome = packacked_dict.pop("pipeline_outcome", None)
|
2022-01-11 19:15:58 +01:00
|
|
|
|
2022-01-29 06:41:40 +01:00
|
|
|
return pipeline_outcome, config
|
2022-01-11 19:15:58 +01:00
|
|
|
|