10
10
import csv
11
11
import excel_sheet_defines
12
12
import glob
13
+ import re
13
14
import sys
14
15
15
16
from string import ascii_uppercase
@@ -25,18 +26,29 @@ def get_log_dirs_planner(dir_path: str, planner_name: str, min_logs=3):
25
26
dirnames = dirnames_underscore
26
27
dirnames .extend (dirnames_dash )
27
28
if len (dirnames ) < min_logs :
28
- exit (f'Too few data entries for { planner_name } planner. Got: \r \n { dirnames } ' )
29
- return dirnames
29
+ exit (f'Too few data entries for { planner_name } planner. Got { len (dirnames )} /{ min_logs } : \r \n { dirnames } ' )
30
+
31
+ # got rid of the dirs that are marked to be ignored
32
+ dirnames_valid = []
33
+ for dirname in dirnames :
34
+ if re .search ('ignore' , dirname , re .IGNORECASE ):
35
+ print (f'Ignoring a directory `{ dirname } ` from including to the overall `{ planner_name } ` planner results' )
36
+ continue
37
+ dirnames_valid .append (dirname )
38
+
39
+ return dirnames_valid
30
40
31
41
32
42
def get_log_result_files (log_dirs : str ):
33
43
result_files = []
34
44
for log_dir in log_dirs :
35
45
results_file = glob .glob (log_dir + '/' + '*results.txt*' )
36
46
if len (results_file ) == 0 :
37
- exit (f'Lack of results file at { log_dir } ' )
47
+ print (f'Lack of results file at `{ log_dir } ` dir, skipping' )
48
+ continue
38
49
elif len (results_file ) > 1 :
39
- exit (f'Multiple results files at { log_dir } ' )
50
+ print (f'Multiple results files at `{ log_dir } ` dir, skipping' )
51
+ continue
40
52
result_files .append (results_file [0 ])
41
53
return result_files
42
54
@@ -53,10 +65,17 @@ def read_results_file(results_file: str):
53
65
for row in csv_result :
54
66
key = str (row [0 ]).lstrip ().rstrip ()
55
67
value_str = str (row [1 ])
56
- if value_str .find ('.' ) != - 1 :
57
- value = float (value_str )
58
- else :
59
- value = int (value_str )
68
+ try :
69
+ if 'nan' in value_str :
70
+ value = None
71
+ print (f"NaN value detected for the key `{ key } ` in the results file `{ results_file } `" )
72
+ elif value_str .find ('.' ) != - 1 :
73
+ value = float (value_str )
74
+ else :
75
+ value = int (value_str )
76
+ except ValueError :
77
+ print (f"Cannot convert a value `{ value_str } ` of a key `{ key } ` to a numeric, skipping `{ results_file } `" )
78
+ return {}
60
79
dict [key ] = value
61
80
return dict
62
81
@@ -168,14 +187,21 @@ def cell_coords_to_sheet_cell_id(row: int, col: int):
168
187
found_col = True
169
188
break
170
189
it_col = it_col + 1
171
- # repeat once again if required (doubling letters)
190
+ # repeat once again if required (doubling letters) - columns A - ZZ cover a sufficient number of cases
172
191
if not found_col :
173
- for c in ascii_uppercase :
174
- if it_col == col_w_offset :
175
- cell_col = 'A' + str (c )
176
- found_col = True
192
+ # first letter of the cell address
193
+ for first in ascii_uppercase :
194
+ # second letter of the cell address
195
+ for second in ascii_uppercase :
196
+ # print(f'[cell_coords_to_sheet_cell_id] row {row}, col {col}, cell row {cell_row} | it col {it_col}, ADDR `{first}{second}`')
197
+ if it_col == col_w_offset :
198
+ cell_col = str (first ) + str (second )
199
+ found_col = True
200
+ break
201
+ it_col = it_col + 1
202
+ # break the outer loop if possible
203
+ if found_col :
177
204
break
178
- it_col = it_col + 1
179
205
if not found_col :
180
206
exit (f"Could not find a valid column ID for ({ row } , { col } ) configuration" )
181
207
@@ -205,9 +231,17 @@ def get_sheet_datacell(planner: str, trial: int, result_key: str, results_total:
205
231
206
232
# counting rows from the start - exemplary key to iterate through metrics
207
233
metric_counter = 0
208
- results_example = results_total [planner ][0 ]
234
+ try :
235
+ results_example = results_total [planner ][0 ]
236
+ except IndexError :
237
+ print (
238
+ f'The planner `{ planner } ` does not seem to contain any keys with metric names. '
239
+ f'Got `{ results_total [planner ]} ` while searching for the metric `{ result_key } ` for the trial `{ trial } `'
240
+ )
241
+ return None
242
+
209
243
if not result_key in results_example .keys ():
210
- exit (f'Cannot proceed as results do not contain that key: { result_key } . Available keys: { results_example .keys ()} ' )
244
+ exit (f'Cannot proceed as results do not contain that key: ` { result_key } ` . Available keys: ` { results_example .keys ()} ` ' )
211
245
212
246
found = False
213
247
for key in results_example :
@@ -251,6 +285,8 @@ def calculate_sheet(wb: Workbook, planner_names: List[str], results_total: Dict,
251
285
planner_trial_last_id = planner_trials - 1
252
286
cell_begin = get_sheet_datacell (planner , 0 , metric , results )
253
287
cell_end = get_sheet_datacell (planner , planner_trial_last_id , metric , results )
288
+ if cell_begin == None or cell_end == None :
289
+ continue
254
290
# fill spreadsheet
255
291
ws [col_header + str (row_metric_start + m_index )] = metric
256
292
ws [col_planner + str (row_start )] = planner
@@ -307,7 +343,7 @@ def calculate_sheet(wb: Workbook, planner_names: List[str], results_total: Dict,
307
343
calculate_sheet (ws , planners , results , 'MEDIAN' )
308
344
309
345
# Prepare name of the output file
310
- output_filename = 'results'
346
+ output_filename = 'results' + '_' + Path ( logs_dir ). name
311
347
for planner in planners :
312
348
output_filename = output_filename + '_' + planner
313
349
output_filename = output_filename .rstrip ('_' ) + '.xlsx'
@@ -322,5 +358,6 @@ def calculate_sheet(wb: Workbook, planner_names: List[str], results_total: Dict,
322
358
# cell will probably return None
323
359
# Ref1: https://itecnote.com/tecnote/python-openpyxl-data_onlytrue-returning-none/
324
360
# Ref2: https://groups.google.com/g/openpyxl-users/c/GbBOnOa8g7Y
325
- print (f'Consider opening the results file and saving it in Excel/LibreOffice Calc (without any modifications).' )
361
+ print (f'Consider opening the results file and saving it with Excel/LibreOffice Calc (without any modifications).' )
326
362
print (f'It will produce cached values based on formulas written (`openpyxl` library is not able to do so).' )
363
+ print (f'This is a necessary step when one wants to use the script that creates a LaTeX table from a spreadsheet' )
0 commit comments