-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
304 lines (256 loc) · 10.7 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
#
# Copyright (c) 2023 Alex Spataru <https://github.com/alex-spataru>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the 'Software'), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
import os
import json
import time
import shutil
import imageio
import pandas as pd
import config as cfg
from PIL import Image
#-------------------------------------------------------------------------------
# Global parameters
#-------------------------------------------------------------------------------
## Text displayed in the main menu
LOGO_TEXT = '''
__ __ __ __
/ /_ / /____ _ _____ / /__ / /_ ____ _ __
/ __ \ / // __ `// ___// //_// __ \ / __ \ | |/_/
/ /_/ // // /_/ // /__ / ,< / /_/ // /_/ /_> <
/_.___//_/ \__,_/ \___//_/|_|/_.___/ \____//_/|_|
'''
MENU_TEXT = '''
1. Process experimental data
2. Generate model from scratch
3. Re-train exisiting model
4. Compare predictions with experimental data
5. Execute a test vector
6. Run all test cases
7. Delete all generated files
8. Exit
'''
#-------------------------------------------------------------------------------
# Utility functions
#-------------------------------------------------------------------------------
def pause():
"""
Pause execution and wait for the user to press Enter.
"""
print('')
input("Press Enter to continue...")
def clear_screen():
"""
Clear the terminal screen.
"""
os.system('cls' if os.name == 'nt' else 'clear')
def create_gif(image_folder, gif_path, duration=1.0):
"""
@brief Create a GIF from JPG files in a folder.
@param image_folder: str, Path to the folder containing the JPG files.
@param gif_path: str, Path where the GIF will be saved.
@param duration: float, Duration each image will be displayed in the GIF.
"""
# Sort the files to ensure they are in the correct order
skip_frames = 2
image_files = sorted([img for img in os.listdir(image_folder) if img.endswith(".jpg")])[::skip_frames]
# Create a list to hold image data
images = []
# Read each image file and append to images list
scale = 0.5
print(f'-> Reading images in {image_folder}...')
for file_name in image_files:
file_path = os.path.join(image_folder, file_name)
img = Image.open(file_path)
img_resized = img.resize((int(img.width * scale), int(img.height * scale)), Image.LANCZOS)
images.append(img_resized)
# Save images as a GIF
print(f'-> Generating {gif_path}')
images[0].save(gif_path, save_all=True, append_images=images[1:], optimize=True, duration=duration*1000, loop=0)
#-------------------------------------------------------------------------------
# Main menu loop
#-------------------------------------------------------------------------------
def main_menu(selected_config_file):
"""Main menu for user interaction."""
from utils.data_processing import preprocess_data
from utils.model_generator import retrain_models
from utils.model_generator import generate_models
menu_options = {
'1': preprocess_data,
'2': generate_models,
'3': retrain_models,
'4': compare_predictions_with_experimental_data,
'5': execute_test_vector,
'6': predict_all,
'7': delete_generated_files,
'8': exit,
'q': exit
}
config_name = selected_config_file.replace('.json', '')
while True:
clear_screen()
print(LOGO_TEXT)
print(MENU_TEXT)
choice = input(f'({config_name}) Select an option: ')
if choice in menu_options:
print('')
menu_options[choice]()
if choice != 8:
pause()
else:
print('Invalid choice, please try again...')
time.sleep(3)
#-------------------------------------------------------------------------------
# Implementation functions for each option of the main menu
#-------------------------------------------------------------------------------
def compare_predictions_with_experimental_data():
from utils.model_executor import ModelExecutor
from utils.plotting import plot_comparison
"""
This function reads a specific test case file, uses the ModelExecutor class
to make predictions and then plots these predictions for comparison.
"""
test_case = input('Enter the test case CSV filename (without extension): ')
segment_path = os.path.join(cfg.training_data_path, test_case + '.csv')
if os.path.exists(segment_path):
clear_screen()
executor = ModelExecutor()
df = pd.read_csv(segment_path, encoding=cfg.csv_encoding).astype('float32')
predictions = executor.predict(df)
plot_comparison(df, predictions, test_case, False)
else:
print(f'Test case CSV {test_case} not found!')
def execute_test_vector():
from utils.test_vector import parse_def_file
from utils.model_executor import ModelExecutor
from utils.plotting import plot_test_vector
"""
The function reads a user-specified test vector file, makes predictions
using the ModelExecutor class, and then plots the results.
"""
tvname = input('Enter the test vector filename (without extension): ')
tvpath = os.path.join(cfg.test_vectors_path, tvname + '.def')
if os.path.exists(tvpath):
df = parse_def_file(tvpath)
time.sleep(3)
clear_screen()
executor = ModelExecutor()
predictions = executor.predict(df)
plot_test_vector(df, predictions, tvname)
else:
print('Test vector file not found!')
def predict_all():
from utils.model_executor import ModelExecutor
from utils.plotting import plot_comparison
"""
The function iterates through all the test case files in the configured
path, makes predictions using the ModelExecutor class, and then plots
these predictions.
"""
# Plot all test cases
csv_files = [f for f in os.listdir(cfg.test_cases_path) if f.endswith('.csv')]
for case in sorted(csv_files):
csv = os.path.join(cfg.test_cases_path, case)
print(f'-> Running predictions for "{csv}"')
executor = ModelExecutor()
df = pd.read_csv(csv, encoding=cfg.csv_encoding).astype('float32')
predictions = executor.predict(df)
plot_comparison(df, predictions, case, True)
# Create GIF from all test cases
image_folder = cfg.plot_save_path
gif_path = os.path.join(cfg.plot_save_path, "../test_cases.gif")
create_gif(image_folder, gif_path, duration=0.1)
def delete_generated_files():
"""
Cleans up files generated during the model's workflow, such as saved models,
images, and test cases.
"""
paths_to_clean = [
cfg.model_save_path,
cfg.plot_save_path,
cfg.test_cases_path,
cfg.training_data_path
]
ack = input('Are you sure (y/n): ')
if ack == 'y' or ack == 'Y':
for path in paths_to_clean:
if os.path.exists(path):
if os.path.isfile(path):
os.remove(path)
else:
shutil.rmtree(path)
print(f'-> Deleted {path}')
else:
print(f'-> {path} does not exist.')
elif ack != 'n' or ack != 'N':
print('Invalid response...')
#-------------------------------------------------------------------------------
# Configuration loading code
#-------------------------------------------------------------------------------
def list_config_files(directory='cfg'):
"""
List available configuration files in the specified directory.
@param directory: Directory where the JSON configuration files are stored.
@return: A list of available configuration files
"""
config_files = [f for f in os.listdir(directory) if f.endswith('.json')]
named_configs = {}
for config_file in config_files:
with open(os.path.join(directory, config_file), 'r') as f:
data = json.load(f)
name = data.get('name', 'Unnamed')
named_configs[config_file] = name
return named_configs
#-------------------------------------------------------------------------------
# Automatically call main_menu() when script is executed
#-------------------------------------------------------------------------------
if __name__ == '__main__':
# Automatically select the config if only one is available
available_configs = list_config_files()
if len(available_configs) == 1:
selected_config_file = list(available_configs.keys())[0]
print(f'Automatically selected config: {available_configs[selected_config_file]}')
# Multiple config files available, ask for user input
elif len(available_configs) > 1:
# Print logo & availabe configurations
clear_screen()
print(LOGO_TEXT)
for idx, (filename, name) in enumerate(available_configs.items(), 1):
print(f' {idx}. {name}')
# Print exit option
idx = len(available_configs) + 1
print(f' {idx}. Exit\n')
# Validate user response
choice = int(input('Select a configuration: ')) - 1
if choice == len(available_configs):
exit(0)
elif choice < 0 or choice >= len(available_configs):
print('Invalid selection. Exiting program.')
exit(1)
# Load configuration
selected_config_file = list(available_configs.keys())[choice]
# No files found...
else:
print(f'Error: cannot load application config.json file!')
exit(1)
# Load configuration file & show main menu
cfg.load_config(os.path.join('cfg', selected_config_file))
main_menu(selected_config_file)