Exemple #1
0
 def test_control_training_models_for_5_models_and_2_pretrained_models(
         self):
     mock_object = model_developement.Training_Process_Functionality(
         folder_customisation="_test_folder")
     mock_models_dict = {}
     [
         mock_models_dict.update({f"{index}": np.random.rand(5)})
         for index in range(5)
     ]
     for index in range(2):
         temp_model_file_path = os.path.join("saved_models_test_folder",
                                             f"model_{index}.h5")
         report_file_path = os.path.join("saved_reports_test_folder",
                                         f"report_{index}.csv")
         with open(temp_model_file_path, "w+") as model_file:
             model_file.close()
         with open(report_file_path, "w+") as report_file:
             report_file.close()
     expected_model_quantity = 4
     expected_starting_id = "1"
     expected_final_id = "4"
     mock_mod_control = mock_object.control_training_models(
         all_models=mock_models_dict)
     self.assertEqual(len(mock_mod_control), expected_model_quantity)
     self.assertEqual(
         list(mock_mod_control.keys())[0], expected_starting_id)
     self.assertEqual(list(mock_mod_control.keys())[-1], expected_final_id)
     shutil.rmtree("saved_models_test_folder")
     shutil.rmtree("saved_reports_test_folder")
Exemple #2
0
 def test_delete_last_files_for_folders_of_2_models_and_2_reports(self):
     mock_object = model_developement.Training_Process_Functionality(
         folder_customisation="_test_folder")
     for index in range(3):
         temp_model_file_path = os.path.join("saved_models_test_folder",
                                             f"model_{index}.h5")
         report_file_path = os.path.join("saved_reports_test_folder",
                                         f"report_{index}.csv")
         with open(temp_model_file_path, "w+") as model_file:
             model_file.close()
         with open(report_file_path, "w+") as report_file:
             report_file.close()
     expected_before = 3
     expected_after = 2
     self.assertEqual(len(os.listdir("saved_models_test_folder")),
                      expected_before)
     self.assertEqual(len(os.listdir("saved_reports_test_folder")),
                      expected_before)
     mock_object.delete_last_files()
     self.assertEqual(len(os.listdir("saved_models_test_folder")),
                      expected_after)
     self.assertEqual(len(os.listdir("saved_reports_test_folder")),
                      expected_after)
     shutil.rmtree("saved_models_test_folder")
     shutil.rmtree("saved_reports_test_folder")
Exemple #3
0
 def test_collect_file_ids_for_valid_report_name(self):
     mock_object = model_developement.Training_Process_Functionality(
         folder_customisation="_test_folder")
     valid_file_name = 'report_101.cvs'  # 7 chars before the ID and 4 chars after the ID
     valid_file = open(
         os.path.join("saved_reports_test_folder", valid_file_name), 'w')
     valid_file.close()
     expected = ["101"]
     self.assertEqual(mock_object.collect_file_ids(dir_to_check="reports"),
                      expected)
     shutil.rmtree("saved_models_test_folder")
     shutil.rmtree("saved_reports_test_folder")
Exemple #4
0
 def test_collect_file_ids_for_invalid_model_name(self):
     mock_object = model_developement.Training_Process_Functionality(
         folder_customisation="_test_folder")
     invalid_file_name = 'the_model_101.h5'  # other than 6 chars before the IDa and 3 chars after the ID
     invalid_file = open(
         os.path.join("saved_models_test_folder", invalid_file_name), 'w')
     invalid_file.close()
     expected = ["101"]
     self.assertNotEqual(
         mock_object.collect_file_ids(dir_to_check="models"), expected)
     shutil.rmtree("saved_models_test_folder")
     shutil.rmtree("saved_reports_test_folder")
Exemple #5
0
 def test_consider_report_id_only_for_valid_and_invalid_element(self):
     mock_object = model_developement.Training_Process_Functionality()
     valid_element = "report_101.csv"
     invalid_element = "reports_101.csv"
     expected = "101"
     self.assertEqual(
         mock_object.consider_report_id_only(element=valid_element),
         expected)
     self.assertNotEqual(
         mock_object.consider_report_id_only(element=invalid_element),
         expected)
     shutil.rmtree("saved_models")
     shutil.rmtree("saved_reports")
Exemple #6
0
 def test_control_dir_to_check_parameter_for_valid_and_invalid_param(self):
     mock_object = model_developement.Training_Process_Functionality()
     valid_element = ["models", "reports"]
     invalid_element = "other"
     self.assertIsNone(
         mock_object.control_dir_to_check_parameter(
             given_param=valid_element[0]))
     self.assertIsNone(
         mock_object.control_dir_to_check_parameter(
             given_param=valid_element[1]))
     with self.assertRaises(ValueError):
         mock_object.control_dir_to_check_parameter(
             given_param=invalid_element)
     shutil.rmtree("saved_models")
     shutil.rmtree("saved_reports")
Exemple #7
0
 def test_sort_given_dir_files_for_model_files(self):
     mock_object = model_developement.Training_Process_Functionality(
         folder_customisation="_test_folder")
     for index in range(3):
         temp_model_file_path = os.path.join("saved_models_test_folder",
                                             f"model_{index}.h5")
         with open(temp_model_file_path, "w+") as model_file:
             model_file.close()
     expected_models = ["model_0.h5", "model_2.h5"]
     sorted_directory = mock_object.sort_given_dir_files(
         dir_to_check="models")
     self.assertEqual(sorted_directory[0], expected_models[0])
     self.assertEqual(sorted_directory[-1], expected_models[-1])
     shutil.rmtree("saved_models_test_folder")
     shutil.rmtree("saved_reports_test_folder")
Exemple #8
0
 def test_find_common_ids_for_3_models_and_3_files_but_only_2_common_IDs(
         self):
     mock_object = model_developement.Training_Process_Functionality(
         folder_customisation="_test_folder")
     for index in range(3):  # create files
         temp_model_file_path = os.path.join("saved_models_test_folder",
                                             f"model_{index + 1}.h5")
         temp_report_file_path = os.path.join("saved_reports_test_folder",
                                              f"report_{index}.csv")
         with open(temp_model_file_path, "w+") as model_file:
             model_file.close()
         with open(temp_report_file_path, "w+") as report_file:
             report_file.close()
     expected = ["1", "2"]
     self.assertListEqual(mock_object.find_common_ids(), expected)
     shutil.rmtree("saved_models_test_folder")
     shutil.rmtree("saved_reports_test_folder")
Exemple #9
0
 def test_keep_only_common_ids_for_3_models_and_3_files_but_only_2_common_IDs(
         self):
     mock_object = model_developement.Training_Process_Functionality(
         folder_customisation="_test_folder")
     for index in range(3):
         temp_model_file_path = os.path.join("saved_models_test_folder",
                                             f"model_{index + 1}.h5")
         temp_report_file_path = os.path.join("saved_reports_test_folder",
                                              f"report_{index}.csv")
         with open(temp_model_file_path, "w+") as model_file:
             model_file.close()
         with open(temp_report_file_path, "w+") as report_file:
             report_file.close()
     expected_models = ["model_1.h5", "model_2.h5"]
     expected_reports = ["report_1.csv", "report_2.csv"]
     mock_object.keep_only_common_ids()
     self.assertListEqual(os.listdir("saved_models_test_folder"),
                          expected_models)
     self.assertListEqual(os.listdir("saved_reports_test_folder"),
                          expected_reports)
     shutil.rmtree("saved_models_test_folder")
     shutil.rmtree("saved_reports_test_folder")