generated from bokulich-lab/q2-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e28e9e8
commit 90f0d4d
Showing
10 changed files
with
483 additions
and
746 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import argparse | ||
import os | ||
|
||
from q2_ritme.evaluate_all_experiments import ( | ||
best_trial_name, | ||
compare_trials, | ||
get_all_exp_analyses, | ||
) | ||
|
||
|
||
def parse_args(): | ||
parser = argparse.ArgumentParser( | ||
description="Post-run evaluation over all experiments." | ||
) | ||
parser.add_argument( | ||
"--model_path", | ||
type=str, | ||
default="experiments/models", | ||
help="Path where the models are stored.", | ||
) | ||
parser.add_argument( | ||
"--overall_comparison_output", | ||
type=str, | ||
default=None, | ||
help="Output path for the overall comparison. If not provided, it defaults to " | ||
"a 'compare_all' directory inside the base path.", | ||
) | ||
parser.add_argument( | ||
"--ls_model_types", | ||
type=str, | ||
nargs="+", | ||
default=["nn", "xgb", "linreg", "rf"], | ||
help="List of model types to evaluate. Separate each model type with a space.", | ||
) | ||
return parser.parse_args() | ||
|
||
|
||
def main(): | ||
args = parse_args() | ||
|
||
# Use the provided arguments | ||
model_path = args.model_path | ||
overall_comparison_output = args.overall_comparison_output or os.path.join( | ||
model_path, "compare_all" | ||
) | ||
ls_model_types = args.ls_model_types | ||
|
||
# Ensure the overall comparison output directory exists | ||
os.makedirs(overall_comparison_output, exist_ok=True) | ||
|
||
# Find best trial over all experiments for each model type | ||
best_trials_overall = {} | ||
for model in ls_model_types: | ||
# read all ExperimentAnalysis objects from this directory | ||
experiment_dir = f"{model_path}/*/{model}" | ||
analyses_ls = get_all_exp_analyses(experiment_dir) | ||
|
||
# identify best trial from all analyses of this model type | ||
best_trials_overall[model] = best_trial_name( | ||
analyses_ls, "rmse_val", mode="min" | ||
) | ||
|
||
compare_trials(best_trials_overall, model_path, overall_comparison_output) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{ | ||
"experiment_tag": "test_synthetic", | ||
"host_id": "host_id", | ||
"ls_model_types": [ | ||
"nn", | ||
"xgb", | ||
"linreg", | ||
"rf" | ||
], | ||
"mlflow_tracking_uri": "mlruns", | ||
"models_to_evaluate_separately": [ | ||
"xgb", | ||
"nn" | ||
], | ||
"path_to_ft": null, | ||
"path_to_md": null, | ||
"seed_data": 12, | ||
"seed_model": 12, | ||
"target": "age_days", | ||
"train_size": 0.8 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
import argparse | ||
import json | ||
import os | ||
import shutil | ||
|
||
import pandas as pd | ||
|
||
from q2_ritme.evaluate_models import ( | ||
aggregate_best_models_metrics_and_configs, | ||
get_predictions, | ||
plot_best_models_comparison, | ||
plot_model_training_over_iterations, | ||
plot_rmse_over_experiments, | ||
plot_rmse_over_time, | ||
retrieve_best_models, | ||
) | ||
from q2_ritme.process_data import load_n_split_data | ||
from q2_ritme.tune_models import run_all_trials | ||
|
||
|
||
def parse_args(): | ||
parser = argparse.ArgumentParser(description="Run configuration.") | ||
parser.add_argument( | ||
"--config", | ||
type=str, | ||
required=True, | ||
help="Path to the run configuration JSON file.", | ||
) | ||
return parser.parse_args() | ||
|
||
|
||
def main(config_path): | ||
with open(config_path, "r") as f: | ||
config = json.load(f) | ||
|
||
# ! Define needed paths | ||
base_path = os.path.join("experiments", "models") | ||
|
||
exp_comparison_output = os.path.join(base_path, config["experiment_tag"]) | ||
if os.path.exists(exp_comparison_output): | ||
raise ValueError( | ||
f"This experiment tag already exists: {config['experiment_tag']}." | ||
"Please use another one." | ||
) | ||
|
||
# todo: flag mlflow runs also with experiment tag somehow | ||
path_mlflow = os.path.join("experiments", config["mlflow_tracking_uri"]) | ||
path_exp = os.path.join(base_path, config["experiment_tag"]) | ||
|
||
# ! Load and split data | ||
train_val, test = load_n_split_data( | ||
config["path_to_md"], | ||
config["path_to_ft"], | ||
config["host_id"], | ||
config["target"], | ||
config["train_size"], | ||
config["seed_data"], | ||
) | ||
|
||
# ! Run all experiments | ||
result_dic = run_all_trials( | ||
train_val, | ||
config["target"], | ||
config["host_id"], | ||
config["seed_data"], | ||
config["seed_model"], | ||
path_mlflow, | ||
path_exp, | ||
model_types=config["ls_model_types"], | ||
fully_reproducible=False, | ||
) | ||
|
||
# ! Save run config | ||
config_output_path = os.path.join(exp_comparison_output, "run_config.json") | ||
shutil.copy(config_path, config_output_path) | ||
|
||
# ! Evaluate best models of this experiment | ||
# Eval1: train_val vs. test -> performance | ||
best_model_dic = retrieve_best_models(result_dic) | ||
non_features = [config["target"], config["host_id"]] | ||
features = [x for x in train_val if x not in non_features] | ||
|
||
preds_dic = {} | ||
for model_type, tmodel in best_model_dic.items(): | ||
train_pred = get_predictions( | ||
train_val, tmodel, config["target"], features, "train" | ||
) | ||
test_pred = get_predictions(test, tmodel, config["target"], features, "test") | ||
all_pred = pd.concat([train_pred, test_pred]) | ||
|
||
# Save all predictions to model file | ||
path2save = os.path.join(tmodel.path, "predictions.csv") | ||
all_pred.to_csv(path2save, index=True) | ||
preds_dic[model_type] = all_pred | ||
|
||
plot_rmse_over_experiments(preds_dic, exp_comparison_output) | ||
|
||
plot_rmse_over_time(preds_dic, config["ls_model_types"], exp_comparison_output) | ||
|
||
# Eval2: train vs. val -> performance and config | ||
metrics_all, best_configs = aggregate_best_models_metrics_and_configs(result_dic) | ||
|
||
plot_best_models_comparison(metrics_all, exp_comparison_output) | ||
|
||
best_configs.to_csv( | ||
os.path.join(exp_comparison_output, "best_trial_config.csv"), index=True | ||
) | ||
|
||
# ! Evaluate one model over training iterations | ||
for m in config["models_to_evaluate_separately"]: | ||
plot_model_training_over_iterations( | ||
m, result_dic, labels=["data_transform"], save_loc=exp_comparison_output | ||
) | ||
|
||
|
||
if __name__ == "__main__": | ||
args = parse_args() | ||
main(args.config) |
Oops, something went wrong.