mirror of
https://github.com/webclinic017/drift.git
synced 2026-08-08 00:27:50 +00:00
refactor(Config): use a Config object instead of dictionary of dictionaries! (#184)
* refactor(Config): use a Config object instead of dictionary of dictionaries! * fix(Config): use default_ensemble_config * fix(Portfolio): fixed portfolio construction
This commit is contained in:
committed by
GitHub
parent
5c4a5b0cf1
commit
e80fffdb65
@@ -1,16 +1,16 @@
|
||||
from reporting.wandb import send_report_to_wandb
|
||||
import pandas as pd
|
||||
from config.preprocess import get_model_name
|
||||
from utils.helpers import weighted_average
|
||||
from config.config import Config
|
||||
|
||||
def report_results(results:pd.DataFrame, all_predictions:pd.DataFrame, model_config:dict, wandb, sweep: bool, project_name:str):
|
||||
def report_results(results:pd.DataFrame, all_predictions:pd.DataFrame, config: Config, wandb, sweep: bool, project_name:str):
|
||||
|
||||
primary_results = results[[column for column in results.columns if 'ensemble' not in column]]
|
||||
ensemble_results = results[[column for column in results.columns if 'ensemble' in column]]
|
||||
|
||||
# Only send the results of the final model to wandb
|
||||
results_to_send = ensemble_results if ensemble_results.shape[1] > 0 else primary_results
|
||||
send_report_to_wandb(results_to_send, wandb, project_name, get_model_name(model_config))
|
||||
send_report_to_wandb(results_to_send, wandb)
|
||||
results.to_csv('output/results.csv')
|
||||
|
||||
primary_weights = all_predictions[[column for column in all_predictions.columns if 'ensemble' not in column]]
|
||||
@@ -29,7 +29,7 @@ def report_results(results:pd.DataFrame, all_predictions:pd.DataFrame, model_con
|
||||
print("Mean Sharpe ratio for Level-1 models: ", round(primary_avg_results.loc['sharpe'], 3))
|
||||
print("Mean Probabilistic Sharpe ratio for Level-1 models: ", round(primary_avg_results.loc['prob_sharpe'].mean(), 3))
|
||||
|
||||
if len(model_config['meta_labeling_models']) > 0:
|
||||
if len(config.meta_labeling_models) > 0:
|
||||
print("Level-2 (Ensemble): Number of samples evaluated: ", ensemble_results.loc['no_of_samples'].sum())
|
||||
print("Mean Sharpe ratio for Level-2 (Ensemble) models: ", round(ensemble_avg_results.loc['sharpe'].mean(), 3))
|
||||
print("Mean Probabilistic Sharpe ratio for Level-2 (Ensemble) models: ", round(ensemble_avg_results.loc['prob_sharpe'].mean(), 3))
|
||||
|
||||
+6
-9
@@ -1,5 +1,6 @@
|
||||
import pickle
|
||||
import datetime
|
||||
from config.config import Config
|
||||
from typing import Optional, Union
|
||||
import os
|
||||
import warnings
|
||||
@@ -7,11 +8,9 @@ from reporting.types import Reporting
|
||||
|
||||
|
||||
|
||||
def save_models(all_models: Reporting.Asset, data_config:dict, training_config:dict, model_config:dict) -> None:
|
||||
def save_models(all_models: Reporting.Asset, config: Config) -> None:
|
||||
dict_for_pickle = dict()
|
||||
dict_for_pickle['training_config'] = training_config
|
||||
dict_for_pickle['data_config'] = data_config
|
||||
dict_for_pickle['model_config'] = model_config
|
||||
dict_for_pickle['config'] = config
|
||||
dict_for_pickle['all_models'] = all_models
|
||||
|
||||
date_string = datetime.datetime.now().strftime("%Y-%m-%d-%H-%M")
|
||||
@@ -23,7 +22,7 @@ def save_models(all_models: Reporting.Asset, data_config:dict, training_config:d
|
||||
pickle.dump( dict_for_pickle, open( "output/models/{}.p".format(date_string), "wb" ) )
|
||||
|
||||
|
||||
def load_models(file_name:Union[str, None]) -> tuple[Reporting.Asset, dict, dict, dict]:
|
||||
def load_models(file_name:Union[str, None]) -> tuple[Reporting.Asset, Config]:
|
||||
|
||||
if file_name is None:
|
||||
warnings.warn("No file name provided, will load latest models and configurations.")
|
||||
@@ -34,10 +33,8 @@ def load_models(file_name:Union[str, None]) -> tuple[Reporting.Asset, dict, dict
|
||||
|
||||
packacked_dict = pickle.load( open( "output/models/{}".format(file_name), "rb" ) )
|
||||
|
||||
data_config = packacked_dict.pop("data_config", None)
|
||||
training_config = packacked_dict.pop("training_config", None)
|
||||
model_config = packacked_dict.pop("model_config", None)
|
||||
config = packacked_dict.pop("config", None)
|
||||
all_models = packacked_dict.pop("all_models", None)
|
||||
|
||||
return all_models, data_config, training_config, model_config
|
||||
return all_models, config
|
||||
|
||||
|
||||
+12
-14
@@ -1,36 +1,34 @@
|
||||
import pandas as pd
|
||||
from config.config import RawConfig
|
||||
from typing import Optional
|
||||
from utils.helpers import weighted_average
|
||||
|
||||
def launch_wandb(project_name:str, default_config:dict, sweep:bool=False):
|
||||
def launch_wandb(project_name:str, default_config: RawConfig, sweep:bool=False) -> Optional[object]:
|
||||
from wandb_setup import get_wandb
|
||||
wandb = get_wandb()
|
||||
if wandb is None:
|
||||
raise Exception("Wandb can not be initalized, the environment variable WANDB_API_KEY is missing (can also use .env file)")
|
||||
|
||||
elif sweep:
|
||||
wandb.init(project=project_name, config = default_config)
|
||||
wandb.init(project=project_name, config = vars(default_config))
|
||||
return wandb
|
||||
else:
|
||||
wandb.init(project=project_name, config = default_config, reinit=True)
|
||||
wandb.init(project=project_name, config = vars(default_config), reinit=True)
|
||||
return wandb
|
||||
|
||||
|
||||
def register_config_with_wandb(wandb: Optional[object], model_config:dict, training_config:dict, data_config:dict):
|
||||
if wandb is None: return model_config, training_config, data_config
|
||||
def override_config_with_wandb_values(wandb: Optional[object], raw_config: RawConfig) -> RawConfig:
|
||||
if wandb is None: return raw_config
|
||||
|
||||
config: dict = wandb.config
|
||||
wandb_config: dict = wandb.config
|
||||
|
||||
for k in training_config:
|
||||
training_config[k] = config[k]
|
||||
for k in model_config:
|
||||
model_config[k] = config[k]
|
||||
for k in data_config:
|
||||
data_config[k] = config[k]
|
||||
config_dict = vars(raw_config)
|
||||
for k in config_dict:
|
||||
config_dict[k] = wandb_config[k]
|
||||
|
||||
return model_config, training_config, data_config
|
||||
return RawConfig(**config_dict)
|
||||
|
||||
def send_report_to_wandb(results: pd.DataFrame, wandb:Optional[object], project_name: str, model_name: str):
|
||||
def send_report_to_wandb(results: pd.DataFrame, wandb:Optional[object]):
|
||||
if wandb is None: return
|
||||
|
||||
run = wandb.run
|
||||
|
||||
Reference in New Issue
Block a user