Fix tests

This commit is contained in:
2023-05-22 11:15:19 +02:00
parent b6fc9096a1
commit c10bf27a16
4 changed files with 112 additions and 70 deletions

View File

@@ -15,19 +15,17 @@ def get_input(message="", is_test=False):
class Manage: class Manage:
def __init__(self, summary): def __init__(self, summary):
self.summary = summary self.summary = summary
self.cmd = SimpleNamespace(
quit="q", relist="r", delete="d", hide="h", excel="e"
)
def manage_results(self): def process_file(self, num, command, path):
"""Manage results showed in the summary
return True if excel file is created False otherwise
"""
def process_file(num, command, path):
num = int(num) num = int(num)
name = self.summary.data_filtered[num]["file"] name = self.summary.data_filtered[num]["file"]
file_name_result = os.path.join(path, name) file_name_result = os.path.join(path, name)
verb1, verb2 = ( verb1, verb2 = (
("delete", "Deleting") ("delete", "Deleting")
if command == cmd.delete if command == self.cmd.delete
else ( else (
"hide", "hide",
"Hiding", "Hiding",
@@ -40,7 +38,7 @@ class Manage:
confirm = get_input(message=conf_message) confirm = get_input(message=conf_message)
if confirm == "y": if confirm == "y":
print(TextColor.YELLOW + f"{verb2} {file_name_result}") print(TextColor.YELLOW + f"{verb2} {file_name_result}")
if command == cmd.delete: if command == self.cmd.delete:
os.unlink(file_name_result) os.unlink(file_name_result)
else: else:
os.rename( os.rename(
@@ -51,12 +49,14 @@ class Manage:
get_input(message="Press enter to continue") get_input(message="Press enter to continue")
self.summary.list_results() self.summary.list_results()
cmd = SimpleNamespace( def manage_results(self):
quit="q", relist="r", delete="d", hide="h", excel="e" """Manage results showed in the summary
) return True if excel file is created False otherwise
"""
message = ( message = (
TextColor.ENDC TextColor.ENDC
+ f"Choose option {str(cmd).replace('namespace', '')}: " + f"Choose option {str(self.cmd).replace('namespace', '')}: "
) )
path = ( path = (
Folders.hidden_results if self.summary.hidden else Folders.results Folders.hidden_results if self.summary.hidden else Folders.results
@@ -65,37 +65,58 @@ class Manage:
max_value = len(self.summary.data_filtered) max_value = len(self.summary.data_filtered)
while True: while True:
match get_input(message=message).split(): match get_input(message=message).split():
case [cmd.relist]: case [self.cmd.relist]:
self.summary.list_results() self.summary.list_results()
case [cmd.quit]: case [self.cmd.quit]:
if book is not None: if book is not None:
book.close() book.close()
return True return True
return False return False
case [cmd.hide, num] if num.isdigit() and int(num) < max_value: case [self.cmd.hide, num] if num.isdigit() and int(
num
) < max_value:
if self.summary.hidden: if self.summary.hidden:
print("Already hidden") print("Already hidden")
else: else:
process_file(num, path=path, command=cmd.hide) self.process_file(
case [cmd.delete, num] if num.isdigit() and int( num, path=path, command=self.cmd.hide
)
case [self.cmd.delete, num] if num.isdigit() and int(
num num
) < max_value: ) < max_value:
process_file(num=num, path=path, command=cmd.delete) self.process_file(
case [cmd.excel, num] if num.isdigit() and int( num=num, path=path, command=self.cmd.delete
)
case [self.cmd.excel, num] if num.isdigit() and int(
num num
) < max_value: ) < max_value:
# Add to excel file result #num # Add to excel file result #num
book = self.add_to_excel(num, path, book)
case [num] if num.isdigit() and int(num) < max_value:
# Report the result #num
self.report(num, path)
case _:
print("Invalid option. Try again!")
def report(self, num, path):
num = int(num)
file_name_result = os.path.join(
path, self.summary.data_filtered[num]["file"]
)
try:
rep = Report(file_name_result, compare=self.summary.compare)
rep.report()
except ValueError as e:
print(e)
def add_to_excel(self, num, path, book):
num = int(num) num = int(num)
file_name_result = os.path.join( file_name_result = os.path.join(
path, self.summary.data_filtered[num]["file"] path, self.summary.data_filtered[num]["file"]
) )
if book is None: if book is None:
file_name = os.path.join( file_name = os.path.join(Folders.excel, Files.be_list_excel)
Folders.excel, Files.be_list_excel book = xlsxwriter.Workbook(file_name, {"nan_inf_to_errors": True})
)
book = xlsxwriter.Workbook(
file_name, {"nan_inf_to_errors": True}
)
excel = Excel( excel = Excel(
file_name=file_name_result, file_name=file_name_result,
book=book, book=book,
@@ -103,18 +124,4 @@ class Manage:
) )
excel.report() excel.report()
print(f"Added {file_name_result} to {Files.be_list_excel}") print(f"Added {file_name_result} to {Files.be_list_excel}")
case [num] if num.isdigit() and int(num) < max_value: return book
# Report the result #num
num = int(num)
file_name_result = os.path.join(
path, self.summary.data_filtered[num]["file"]
)
try:
rep = Report(
file_name_result, compare=self.summary.compare
)
rep.report()
except ValueError as e:
print(e)
case _:
print("Invalid option. Try again!")

View File

@@ -66,6 +66,27 @@ class ReportTest(TestBase):
self.assertEqual(res, Symbols.better_best) self.assertEqual(res, Symbols.better_best)
res = report._compute_status("balloons", 1.0) res = report._compute_status("balloons", 1.0)
self.assertEqual(res, Symbols.better_best) self.assertEqual(res, Symbols.better_best)
report = Report(file_name=file_name)
with patch(self.output, new=StringIO()):
report.report()
res = report._compute_status("balloons", 0.99)
self.assertEqual(res, Symbols.upward_arrow)
report.margin = 0.9
res = report._compute_status("balloons", 0.99)
self.assertEqual(res, Symbols.cross)
def test_reportbase_compute_status(self):
with patch.multiple(BaseReport, __abstractmethods__=set()):
file_name = os.path.join(
"results",
"results_accuracy_STree_iMac27_2021-09-30_11:42:07_0.json",
)
temp = BaseReport(file_name)
temp.compare = False
temp._compare_totals = {}
temp.score_name = "f1"
res = temp._compute_status("balloons", 0.99)
self.assertEqual(res, " ")
def test_report_file_not_found(self): def test_report_file_not_found(self):
with self.assertRaises(FileNotFoundError): with self.assertRaises(FileNotFoundError):

View File

@@ -143,6 +143,12 @@ class BeListTest(TestBase):
self.assertEqual(stderr.getvalue(), "") self.assertEqual(stderr.getvalue(), "")
self.check_output_file(stdout, "be_list_hidden") self.check_output_file(stdout, "be_list_hidden")
@patch("benchmark.Manager.get_input", side_effect=iter(["0", "q"]))
def test_be_list_compare(self, input_data):
stdout, stderr = self.execute_script("be_list", ["--compare"])
self.assertEqual(stderr.getvalue(), "")
self.check_output_file(stdout, "be_list_compare_fault")
def test_be_no_env(self): def test_be_no_env(self):
path = os.getcwd() path = os.getcwd()
os.chdir("..") os.chdir("..")

View File

@@ -0,0 +1,8 @@
 # Date File Score Time(h) Title
=== ========== =============================================================== ======== ======= ============================================
 0 2022-04-20 results_accuracy_ODTE_Galgo_2022-04-20_10:52:20_0.json 0.04341 6.275 Gridsearched hyperparams v022.1b random_init
 1 2022-01-14 results_accuracy_RandomForest_iMac27_2022-01-14_12:39:30_0.json 0.03627 0.076 Test default paramters with RandomForest
 2 2021-11-01 results_accuracy_STree_macbook-pro_2021-11-01_19:17:07_0.json 0.03790 1.143 default B
 3 2021-10-27 results_accuracy_STree_iMac27_2021-10-27_09:40:40_0.json 0.04158 0.943 default A
 4 2021-09-30 results_accuracy_STree_iMac27_2021-09-30_11:42:07_0.json 0.04544 0.173 With gridsearched hyperparameters
results/best_results_accuracy_ODTE.json does not exist