mirror of
https://github.com/Doctorado-ML/benchmark.git
synced 2025-08-16 16:05:54 +00:00
Begin refactor arguments
This commit is contained in:
147
benchmark/Arguments.py
Normal file
147
benchmark/Arguments.py
Normal file
@@ -0,0 +1,147 @@
|
|||||||
|
import argparse
|
||||||
|
from .Experiments import Models
|
||||||
|
from .Utils import Files
|
||||||
|
|
||||||
|
ALL_METRICS = (
|
||||||
|
"accuracy",
|
||||||
|
"f1-macro",
|
||||||
|
"f1-micro",
|
||||||
|
"f1-weighted",
|
||||||
|
"roc-auc-ovr",
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class EnvData:
|
||||||
|
@staticmethod
|
||||||
|
def load():
|
||||||
|
args = {}
|
||||||
|
with open(Files.dot_env) as f:
|
||||||
|
for line in f.read().splitlines():
|
||||||
|
if line == "" or line.startswith("#"):
|
||||||
|
continue
|
||||||
|
key, value = line.split("=")
|
||||||
|
args[key] = value
|
||||||
|
return args
|
||||||
|
|
||||||
|
|
||||||
|
class EnvDefault(argparse.Action):
|
||||||
|
# Thanks to https://stackoverflow.com/users/445507/russell-heilling
|
||||||
|
def __init__(self, envvar, required=True, default=None, **kwargs):
|
||||||
|
self._args = EnvData.load()
|
||||||
|
default = self._args[envvar]
|
||||||
|
required = False
|
||||||
|
super(EnvDefault, self).__init__(
|
||||||
|
default=default, required=required, **kwargs
|
||||||
|
)
|
||||||
|
|
||||||
|
def __call__(self, parser, namespace, values, option_string=None):
|
||||||
|
setattr(namespace, self.dest, values)
|
||||||
|
|
||||||
|
|
||||||
|
class Arguments:
|
||||||
|
def __init__(self):
|
||||||
|
self.ap = argparse.ArgumentParser()
|
||||||
|
models_data = Models.define_models(random_state=0)
|
||||||
|
models = "{" + ", ".join(models_data) + "}"
|
||||||
|
self.parameters = {
|
||||||
|
"best": [
|
||||||
|
("-b", "--best"),
|
||||||
|
{
|
||||||
|
"type": str,
|
||||||
|
"required": False,
|
||||||
|
"help": "best results of models",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
"color": [],
|
||||||
|
"compare": [
|
||||||
|
("-c", "--compare"),
|
||||||
|
{
|
||||||
|
"type": bool,
|
||||||
|
"required": False,
|
||||||
|
"help": "Compare accuracy with best results",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
"dataset": [],
|
||||||
|
"excel": [
|
||||||
|
("-x", "--excel"),
|
||||||
|
{
|
||||||
|
"type": bool,
|
||||||
|
"required": False,
|
||||||
|
"default": False,
|
||||||
|
"help": "Generate Excel File",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
"file": [
|
||||||
|
("-f", "--file"),
|
||||||
|
{"type": str, "required": False, "help": "Result file"},
|
||||||
|
],
|
||||||
|
"grid": [
|
||||||
|
("-g", "--grid"),
|
||||||
|
{
|
||||||
|
"type": str,
|
||||||
|
"required": False,
|
||||||
|
"help": "grid results of model",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
"grid_paramfile": [],
|
||||||
|
"hidden": [],
|
||||||
|
"hyperparameters": [],
|
||||||
|
"key": [],
|
||||||
|
"lose": [],
|
||||||
|
"model": [
|
||||||
|
("-m", "--model"),
|
||||||
|
{
|
||||||
|
"type": str,
|
||||||
|
"required": True,
|
||||||
|
"choices": list(models_data),
|
||||||
|
"help": f"model name: {models}",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
"model1": [],
|
||||||
|
"model2": [],
|
||||||
|
"nan": [],
|
||||||
|
"number": [],
|
||||||
|
"n_folds": [],
|
||||||
|
"paramfile": [],
|
||||||
|
"platform": [],
|
||||||
|
"quiet": [],
|
||||||
|
"report": [],
|
||||||
|
"score": [
|
||||||
|
("-s", "--score"),
|
||||||
|
{
|
||||||
|
"action": EnvDefault,
|
||||||
|
"envvar": "score",
|
||||||
|
"type": str,
|
||||||
|
"required": True,
|
||||||
|
"choices": ALL_METRICS,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
"sql": [
|
||||||
|
("-q", "--sql"),
|
||||||
|
{"type": bool, "required": False, "help": "Generate SQL File"},
|
||||||
|
],
|
||||||
|
"stratified": [],
|
||||||
|
"tex_output": [
|
||||||
|
("-t", "--tex-output"),
|
||||||
|
{
|
||||||
|
"type": bool,
|
||||||
|
"required": False,
|
||||||
|
"default": False,
|
||||||
|
"help": "Generate Tex file with the table",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
"title": [],
|
||||||
|
"win": [],
|
||||||
|
}
|
||||||
|
|
||||||
|
def xset(self, *arg_name, **kwargs):
|
||||||
|
print("parameters", arg_name[0])
|
||||||
|
names, default = self.parameters[arg_name[0]]
|
||||||
|
self.ap.add_argument(
|
||||||
|
*names,
|
||||||
|
**{**default, **kwargs},
|
||||||
|
)
|
||||||
|
return self
|
||||||
|
|
||||||
|
def parse(self):
|
||||||
|
return self.ap.parse_args()
|
@@ -13,7 +13,8 @@ from sklearn.model_selection import (
|
|||||||
GridSearchCV,
|
GridSearchCV,
|
||||||
cross_validate,
|
cross_validate,
|
||||||
)
|
)
|
||||||
from .Utils import Folders, Files, EnvData
|
from .Utils import Folders, Files
|
||||||
|
from .Arguments import EnvData
|
||||||
from .Models import Models
|
from .Models import Models
|
||||||
|
|
||||||
|
|
||||||
|
@@ -1,6 +1,5 @@
|
|||||||
import os
|
import os
|
||||||
import subprocess
|
import subprocess
|
||||||
import argparse
|
|
||||||
|
|
||||||
BEST_ACCURACY_STREE = 40.282203
|
BEST_ACCURACY_STREE = 40.282203
|
||||||
ALL_METRICS = (
|
ALL_METRICS = (
|
||||||
@@ -132,33 +131,6 @@ class Symbols:
|
|||||||
better_best = black_star
|
better_best = black_star
|
||||||
|
|
||||||
|
|
||||||
class EnvData:
|
|
||||||
@staticmethod
|
|
||||||
def load():
|
|
||||||
args = {}
|
|
||||||
with open(Files.dot_env) as f:
|
|
||||||
for line in f.read().splitlines():
|
|
||||||
if line == "" or line.startswith("#"):
|
|
||||||
continue
|
|
||||||
key, value = line.split("=")
|
|
||||||
args[key] = value
|
|
||||||
return args
|
|
||||||
|
|
||||||
|
|
||||||
class EnvDefault(argparse.Action):
|
|
||||||
# Thanks to https://stackoverflow.com/users/445507/russell-heilling
|
|
||||||
def __init__(self, envvar, required=True, default=None, **kwargs):
|
|
||||||
self._args = EnvData.load()
|
|
||||||
default = self._args[envvar]
|
|
||||||
required = False
|
|
||||||
super(EnvDefault, self).__init__(
|
|
||||||
default=default, required=required, **kwargs
|
|
||||||
)
|
|
||||||
|
|
||||||
def __call__(self, parser, namespace, values, option_string=None):
|
|
||||||
setattr(namespace, self.dest, values)
|
|
||||||
|
|
||||||
|
|
||||||
class TextColor:
|
class TextColor:
|
||||||
BLUE = "\033[94m"
|
BLUE = "\033[94m"
|
||||||
CYAN = "\033[96m"
|
CYAN = "\033[96m"
|
||||||
|
@@ -1,6 +1,6 @@
|
|||||||
from .Experiments import Experiment, Datasets, DatasetsSurcov, DatasetsTanveer
|
from .Experiments import Experiment, Datasets, DatasetsSurcov, DatasetsTanveer
|
||||||
from .Results import Report, Summary
|
from .Results import Report, Summary
|
||||||
from .Utils import EnvDefault
|
from .Arguments import EnvDefault
|
||||||
|
|
||||||
__author__ = "Ricardo Montañana Gómez"
|
__author__ = "Ricardo Montañana Gómez"
|
||||||
__copyright__ = "Copyright 2020-2022, Ricardo Montañana Gómez"
|
__copyright__ = "Copyright 2020-2022, Ricardo Montañana Gómez"
|
||||||
|
@@ -1,47 +1,19 @@
|
|||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
from benchmark.Results import Benchmark
|
from benchmark.Results import Benchmark
|
||||||
from benchmark.Utils import ALL_METRICS, Files, EnvDefault
|
from benchmark.Utils import Files
|
||||||
import argparse
|
from benchmark.Arguments import Arguments
|
||||||
|
|
||||||
|
|
||||||
def parse_arguments():
|
arguments = Arguments()
|
||||||
ap = argparse.ArgumentParser()
|
arguments.xset("score").xset("excel").xset("tex_output")
|
||||||
ap.add_argument(
|
ar = arguments.parse()
|
||||||
"-s",
|
benchmark = Benchmark(score=ar.score, visualize=True)
|
||||||
"--score",
|
|
||||||
action=EnvDefault,
|
|
||||||
envvar="score",
|
|
||||||
type=str,
|
|
||||||
required=True,
|
|
||||||
choices=ALL_METRICS,
|
|
||||||
help="score name {accuracy, f1_macro, ...}",
|
|
||||||
)
|
|
||||||
ap.add_argument(
|
|
||||||
"-x",
|
|
||||||
"--excel",
|
|
||||||
type=bool,
|
|
||||||
required=False,
|
|
||||||
help="Generate Excel File",
|
|
||||||
)
|
|
||||||
ap.add_argument(
|
|
||||||
"-t",
|
|
||||||
"--tex-output",
|
|
||||||
type=bool,
|
|
||||||
required=False,
|
|
||||||
default=False,
|
|
||||||
)
|
|
||||||
args = ap.parse_args()
|
|
||||||
return (args.score, args.excel, args.tex_output)
|
|
||||||
|
|
||||||
|
|
||||||
(score, excel, tex_output) = parse_arguments()
|
|
||||||
benchmark = Benchmark(score=score, visualize=True)
|
|
||||||
benchmark.compile_results()
|
benchmark.compile_results()
|
||||||
benchmark.save_results()
|
benchmark.save_results()
|
||||||
benchmark.report(tex_output)
|
benchmark.report(ar.tex_output)
|
||||||
benchmark.exreport()
|
benchmark.exreport()
|
||||||
if excel:
|
if ar.excel:
|
||||||
benchmark.excel()
|
benchmark.excel()
|
||||||
Files.open(benchmark.get_excel_file_name())
|
Files.open(benchmark.get_excel_file_name())
|
||||||
if tex_output:
|
if ar.tex_output:
|
||||||
print(f"File {benchmark.get_tex_file()} generated")
|
print(f"File {benchmark.get_tex_file()} generated")
|
||||||
|
@@ -1,29 +1,17 @@
|
|||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
import argparse
|
|
||||||
import json
|
import json
|
||||||
from benchmark.Results import Summary
|
from benchmark.Results import Summary
|
||||||
from benchmark.Utils import EnvDefault, ALL_METRICS
|
from benchmark.Utils import ALL_METRICS, Arguments
|
||||||
|
|
||||||
|
|
||||||
def parse_arguments():
|
arguments = Arguments()
|
||||||
ap = argparse.ArgumentParser()
|
metrics = list(ALL_METRICS)
|
||||||
ap.add_argument(
|
metrics.append("all")
|
||||||
"-s",
|
arguments.xset("score", choices=metrics)
|
||||||
"--score",
|
args = arguments.parse()
|
||||||
type=str,
|
|
||||||
action=EnvDefault,
|
|
||||||
envvar="score",
|
|
||||||
required=True,
|
|
||||||
choices=ALL_METRICS,
|
|
||||||
help="score name {accuracy, f1-macro, f1-weighted, roc-auc-ovr}",
|
|
||||||
)
|
|
||||||
args = ap.parse_args()
|
|
||||||
return (args.score,)
|
|
||||||
|
|
||||||
|
|
||||||
(score,) = parse_arguments()
|
metrics = ALL_METRICS if args.score == "all" else [args.score]
|
||||||
|
|
||||||
metrics = ALL_METRICS if score == "all" else [score]
|
|
||||||
|
|
||||||
summary = Summary()
|
summary = Summary()
|
||||||
summary.acquire()
|
summary.acquire()
|
||||||
|
@@ -1,9 +1,12 @@
|
|||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
import argparse
|
|
||||||
import numpy as np
|
import numpy as np
|
||||||
from benchmark.Experiments import Datasets
|
from benchmark.Experiments import Datasets
|
||||||
from benchmark.Results import Report, Excel, SQL, ReportBest
|
from benchmark.Results import Report, Excel, SQL, ReportBest
|
||||||
from benchmark.Utils import ALL_METRICS, Files, TextColor, EnvDefault
|
from benchmark.Utils import (
|
||||||
|
Files,
|
||||||
|
TextColor,
|
||||||
|
)
|
||||||
|
from benchmark.Arguments import Arguments
|
||||||
|
|
||||||
|
|
||||||
"""Build report on screen of a result file, optionally generate excel and sql
|
"""Build report on screen of a result file, optionally generate excel and sql
|
||||||
@@ -12,83 +15,6 @@ If no argument is set, displays the datasets and its characteristics
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
def parse_arguments():
|
|
||||||
ap = argparse.ArgumentParser()
|
|
||||||
ap.add_argument(
|
|
||||||
"-f",
|
|
||||||
"--file",
|
|
||||||
type=str,
|
|
||||||
required=False,
|
|
||||||
help="Result file",
|
|
||||||
)
|
|
||||||
ap.add_argument(
|
|
||||||
"-x",
|
|
||||||
"--excel",
|
|
||||||
type=bool,
|
|
||||||
required=False,
|
|
||||||
help="Generate Excel file",
|
|
||||||
)
|
|
||||||
ap.add_argument(
|
|
||||||
"-q",
|
|
||||||
"--sql",
|
|
||||||
type=bool,
|
|
||||||
required=False,
|
|
||||||
help="Generate sql file",
|
|
||||||
)
|
|
||||||
ap.add_argument(
|
|
||||||
"-c",
|
|
||||||
"--compare",
|
|
||||||
type=bool,
|
|
||||||
required=False,
|
|
||||||
help="Compare accuracy with best results",
|
|
||||||
)
|
|
||||||
ap.add_argument(
|
|
||||||
"-b",
|
|
||||||
"--best",
|
|
||||||
type=str,
|
|
||||||
required=False,
|
|
||||||
help="best results of models",
|
|
||||||
)
|
|
||||||
ap.add_argument(
|
|
||||||
"-g",
|
|
||||||
"--grid",
|
|
||||||
type=str,
|
|
||||||
required=False,
|
|
||||||
help="grid results of model",
|
|
||||||
)
|
|
||||||
ap.add_argument(
|
|
||||||
"-m",
|
|
||||||
"--model",
|
|
||||||
action=EnvDefault,
|
|
||||||
envvar="model",
|
|
||||||
type=str,
|
|
||||||
required=True,
|
|
||||||
help="model name",
|
|
||||||
)
|
|
||||||
ap.add_argument(
|
|
||||||
"-s",
|
|
||||||
"--score",
|
|
||||||
action=EnvDefault,
|
|
||||||
envvar="score",
|
|
||||||
type=str,
|
|
||||||
required=True,
|
|
||||||
choices=ALL_METRICS,
|
|
||||||
help="score name {accuracy, f1_macro, ...}",
|
|
||||||
)
|
|
||||||
args = ap.parse_args()
|
|
||||||
|
|
||||||
return (
|
|
||||||
args.file,
|
|
||||||
args.excel,
|
|
||||||
args.sql,
|
|
||||||
args.compare,
|
|
||||||
args.best,
|
|
||||||
args.grid,
|
|
||||||
args.score,
|
|
||||||
args.model,
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def default_report():
|
def default_report():
|
||||||
sets = Datasets()
|
sets = Datasets()
|
||||||
color_line = TextColor.LINE1
|
color_line = TextColor.LINE1
|
||||||
@@ -116,22 +42,26 @@ def default_report():
|
|||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
(file, excel, sql, compare, best, grid, score, model) = parse_arguments()
|
arguments = Arguments()
|
||||||
if grid:
|
arguments.xset("file").xset("excel").xset("sql").xset("compare")
|
||||||
best = False
|
arguments.xset("best").xset("grid").xset("model").xset("score")
|
||||||
if file is None and best is None:
|
args = arguments.parse()
|
||||||
|
|
||||||
|
if args.grid:
|
||||||
|
args.best = False
|
||||||
|
if args.file is None and args.best is None:
|
||||||
default_report()
|
default_report()
|
||||||
else:
|
else:
|
||||||
if best is not None or grid is not None:
|
if args.best is not None or args.grid is not None:
|
||||||
report = ReportBest(score, model, best, grid)
|
report = ReportBest(args.score, args.model, args.best, args.grid)
|
||||||
report.report()
|
report.report()
|
||||||
else:
|
else:
|
||||||
report = Report(file, compare)
|
report = Report(args.file, args.compare)
|
||||||
report.report()
|
report.report()
|
||||||
if excel:
|
if args.excel:
|
||||||
excel = Excel(file, compare)
|
excel = Excel(args.file, args.compare)
|
||||||
excel.report()
|
excel.report()
|
||||||
Files.open(excel.get_file_name())
|
Files.open(excel.get_file_name())
|
||||||
if sql:
|
if args.sql:
|
||||||
sql = SQL(file)
|
sql = SQL(args.file)
|
||||||
sql.report()
|
sql.report()
|
||||||
|
@@ -266,3 +266,8 @@ class UtilTest(TestBase):
|
|||||||
self.assertEqual(TextColor.ENDC, "\033[0m")
|
self.assertEqual(TextColor.ENDC, "\033[0m")
|
||||||
self.assertEqual(TextColor.BOLD, "\033[1m")
|
self.assertEqual(TextColor.BOLD, "\033[1m")
|
||||||
self.assertEqual(TextColor.UNDERLINE, "\033[4m")
|
self.assertEqual(TextColor.UNDERLINE, "\033[4m")
|
||||||
|
|
||||||
|
def test_Arguments(self):
|
||||||
|
arguments = Arguments()
|
||||||
|
arg_list = ["score", "excel", "tex_output"]
|
||||||
|
arguments.set_arguments(arg_list)
|
||||||
|
Reference in New Issue
Block a user