-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
510 lines (452 loc) · 17.3 KB
/
main.py
File metadata and controls
510 lines (452 loc) · 17.3 KB
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
# FILE
import config
import custom_func
# MODULE
import pandas as pd
import time
import os
import sys
import signal
from concurrent.futures import ThreadPoolExecutor, as_completed
import warnings
from typing import Tuple
from prettytable import PrettyTable
import queue
import threading
from typing import Any
# [OK]
def signal_handler(sig, frame):
"""Process stop signal"""
config.force_exit = True
print("\nGet Exit signal, Exiting...")
sys.exit(1)
# [OK]
def update_progress(file_name: str, argA: Any, argB: Any = False) -> None:
# [OK]
def update_progressA(file_name: str, process_add: int, data: str) -> None:
"""Update progress data"""
# Update Process
with config.status_data_lock:
now_processed_count = config.status_data_dict[file_name]["processed_count"] + process_add
config.status_data_dict[file_name].update({
"processed_count": now_processed_count,
"data": data
})
# Print
print_progress_table()
# [OK]
def update_progressB(file_name: str, upate_data: dict, enforce: bool) -> None:
# Update Process
with config.status_data_lock:
config.status_data_dict[file_name].update(upate_data)
# Print
print_progress_table(enforce)
if type(argA) == int:
update_progressA(file_name, argA, argB)
elif type(argA) == dict:
update_progressB(file_name, argA, argB)
else:
print(
f"ERROR: UnExcept Exception in update_progress({file_name},{argA},{argB})")
config.force_exit = True
exit(2)
# Check Exit
if config.force_exit:
sys.exit(1)
# [OK]
def update_progress_queue():
"""Update progress data queue"""
while not config.force_exit:
try:
args = config.update_progress_queue.get(timeout=0.5)
if args is None:
break
update_progress(*args)
except queue.Empty:
continue
# [OK]
def clear_screen():
"""Clean Bash Screen in different platform"""
if sys.platform.startswith('linux') or sys.platform.startswith('darwin'):
os.system('clear')
elif sys.platform.startswith('win32'):
os.system('cls')
# [OK]
def format_time2str(seconds: float) -> str:
"""Format time to str"""
return time.strftime("%H:%M:%S", time.gmtime(seconds))
# [OK]
def format_progress_times(elapsed_time: float, remaining_time: float) -> Tuple[str, str]:
"""Format elapsed time& remaining time"""
return format_time2str(elapsed_time), format_time2str(remaining_time)
# [OK]
def print_progress_table(enforce: bool = False) -> None:
"""Print Progress table"""
now_time = time.time()
with config.print_lock:
if ((now_time - config.last_print) > config.BASIC_CONFIG["PRINT_INTERVAL_SECONDS"]) or enforce:
# Init Progress table
clear_screen()
tb = PrettyTable(["File", "Processed", "Total",
"Elapsed", "Remaining", "Data", "Status"])
with config.status_data_lock:
# Add info to table
for file_name, file_status_data in config.status_data_dict.items():
# Calculate elapsed time& remaining time
elapsed_time = now_time - file_status_data["start_time"]
this_elapsed_time = now_time - \
file_status_data["this_start_time"]
if (file_status_data["status"] != 3) and (file_status_data["status"] != -1):
# Calculate Processing ETH
processed_count = file_status_data["processed_count"]
all_count = file_status_data["all_count"]
if processed_count > 0:
remaining_time = (this_elapsed_time / processed_count) * \
(all_count - processed_count)
else:
remaining_time = 0
elapsed_str, remaining_str = format_progress_times(
elapsed_time, remaining_time)
else:
# Calculate Finished
elapsed_str = file_status_data["finished_elapsed_time_str"]
remaining_str = "00:00:00"
# Add line to table
tb.add_row([
file_name,
file_status_data["processed_count"],
file_status_data["all_count"],
elapsed_str,
remaining_str,
file_status_data["data"],
config.BASIC_CONFIG["STATUS_CODE"][file_status_data["status"]]
])
print(tb)
config.last_print = time.time()
# [OK]
def get_shard(begin: int, end: int, shard_count: int) -> list:
"""Shard "shard_count" shardes from "begin" to "end", num begin at 0"""
# Init
Sharding = []
count = end - begin + 1
# Calculate items count per chunk
if shard_count > 1:
if (count % shard_count) == 0:
per_chunk = count // shard_count
else:
per_chunk = count // shard_count + 1
else:
per_chunk = count
# Shard
for i in range(shard_count):
chunk_begin = begin + i * per_chunk
chunk_end = min(begin + (i + 1) * per_chunk - 1, end)
if chunk_begin > chunk_end:
# Wtf, Real Need This IF?
continue
Sharding.append((chunk_begin, chunk_end))
return Sharding
# [OK]
def process_csv_file(input_path, output_path):
"""Use Muilty Threades Process CSV File"""
# Read DataFrame
df = pd.read_csv(input_path)
file_name = os.path.basename(input_path)
# Echo
INFO_str = f"INFO: Begin Process {file_name}"
print(INFO_str)
if config.INFO_FILE != None:
with config.INFO_FILE_f_lock:
config.INFO_FILE_f.write(INFO_str + "\n")
# Get DF basic info
column_names = df.columns
row_count = len(df.index)
col_count = len(column_names)
act_row_count = (
row_count -
config.BASIC_CONFIG["FRAME_EDGE"]["ROW_START"] -
config.BASIC_CONFIG["FRAME_EDGE"]["ROW_END"]
)
act_col_count = (
col_count -
config.BASIC_CONFIG["FRAME_EDGE"]["COL_START"] -
config.BASIC_CONFIG["FRAME_EDGE"]["COL_END"]
)
all_count = act_row_count * act_col_count
# Init Processing tracker
if config.BASIC_CONFIG["TRACK_SWITCH"]:
with config.status_data_lock:
config.status_data_dict[file_name] = {
"start_time": time.time(),
"this_start_time": time.time(),
"finished_elapsed_time_str": None,
"processed_count": 0,
"all_count": all_count,
"data": "\\",
"status": 0
}
print_progress_table(True)
# Pre Processing
df = custom_func.pre_processing(
df, file_name, column_names, all_count, False)
# Shard DataFrame
row_chunk_list = get_shard(
config.BASIC_CONFIG["FRAME_EDGE"]["ROW_START"],
row_count - config.BASIC_CONFIG["FRAME_EDGE"]["ROW_END"] - 1,
config.BASIC_CONFIG["ROW_SHARD_COUNT"]
)
col_chunk_list = get_shard(
config.BASIC_CONFIG["FRAME_EDGE"]["COL_START"],
col_count - config.BASIC_CONFIG["FRAME_EDGE"]["COL_END"] - 1,
config.BASIC_CONFIG["COL_SHARD_COUNT"]
)
# Begin Processing
with ThreadPoolExecutor(max_workers=config.CSV_PROCESS_MAX_THREAD) as executor:
try:
# Init Thread Pool
thread_pool = []
# Init output dataframe
processed_df = df.copy()
# Update tracker info
if config.BASIC_CONFIG["TRACK_SWITCH"]:
config.update_progress_queue.put((
file_name,
{
"processed_count": 0,
"this_start_time": time.time(),
"status": 1
}
))
# Add Thread to pool
for row_begin, row_end in row_chunk_list:
for col_begin, col_end in col_chunk_list:
df_shard = df.iloc[row_begin:row_end +
1, col_begin:col_end+1]
future = executor.submit(
custom_func.processing,
df_shard,
file_name,
{
"row_begin": row_begin,
"row_end": row_end,
"col_begin": col_begin,
"col_end": col_end
},
column_names
)
thread_pool.append(future)
# Finished
for future in as_completed(thread_pool):
processed_shard = future.result()
row_idx = processed_shard.index.intersection(
processed_df.index)
col_idx = processed_shard.columns.intersection(
processed_df.columns)
if not row_idx.empty and not col_idx.empty:
processed_df.loc[row_idx,
col_idx] = processed_shard.loc[row_idx, col_idx]
# Update tracker info
if config.BASIC_CONFIG["TRACK_SWITCH"]:
config.update_progress_queue.put((
file_name,
{
"processed_count": 0,
"this_start_time": time.time(),
"status": 2
}
))
# Post Process
processed_df = custom_func.post_processing(
processed_df, None, False)
# Save DataFrame
processed_df.to_csv(output_path, index=False)
# Update tracker info
if config.BASIC_CONFIG["TRACK_SWITCH"]:
this_time = time.time()
config.update_progress_queue.put((
file_name,
{
"this_start_time": this_time,
"finished_elapsed_time_str": format_time2str(
this_time -
config.status_data_dict[file_name]["start_time"]
),
"processed_count": all_count,
"data": "\\",
"status": 3
},
True
))
except Exception as e:
ERROR_str = f"Error processing {file_name}: {e}"
print(ERROR_str)
if config.ERROR_FILE != None:
with config.ERROR_FILE_f_lock:
config.ERROR_FILE_f.write(ERROR_str + "\n")
return
# [OK]
def main():
"""Main Function"""
# Print Runinfo
print("INFO: Program Run Begin.")
print("*** *** *** *** ***")
# Set Stop Signal
signal.signal(signal.SIGINT, signal_handler)
# Init data progress thread
processing_thread = threading.Thread(target=update_progress_queue)
processing_thread.daemon = True
processing_thread.start()
# Ignore Unnecessary Warning
warnings.simplefilter(action='ignore', category=FutureWarning)
# Verify directory validity& Create output directory
if not os.path.exists(config.BASIC_CONFIG["DATA_SOURCE_DIR"]):
print(
"Error: Directory "
f"{config.BASIC_CONFIG["DATA_SOURCE_DIR"]}"
" does not exist"
)
return 1
os.makedirs(config.BASIC_CONFIG["DATA_OUTPUT_DIR"], exist_ok=True)
# Process All CSV File
csv_files = []
for f in os.listdir(config.BASIC_CONFIG["DATA_SOURCE_DIR"]):
if f.endswith('.csv'):
csv_files.append(f)
print(f"INFO: Found {f}")
if not csv_files:
print(
"ERROR: No CSV files found in "
f"{config.BASIC_CONFIG["DATA_SOURCE_DIR"]}"
)
return 0
# Init log files
if config.INFO_FILE != None:
try:
config.INFO_FILE_f = open(config.INFO_FILE, 'w+')
except Exception as e:
print(
"Error: Can't open/create info log file "
f"{config.INFO_FILE}"
f" with {e}"
)
return 0
if config.ERROR_FILE != None:
try:
config.ERROR_FILE_f = open(config.ERROR_FILE, 'w+')
except Exception as e:
print(
"Error: Can't open/create error log file "
f"{config.ERROR_FILE}"
f" with {e}"
)
return 0
# Init file rename files
file_rename = None
if config.BASIC_CONFIG["DATA_FILE_RENAME_FILE"] != None:
try:
config.DATA_FILE_RENAME_FILE_df = pd.read_csv(
config.BASIC_CONFIG["DATA_FILE_RENAME_FILE"]
)
file_rename = \
config.DATA_FILE_RENAME_FILE_df[["From", "To"]].to_dict(
orient='dict'
)
except Exception as e:
ERROR_str = \
"Error: Can't open file rename file " \
f"{config.BASIC_CONFIG["DATA_FILE_RENAME_FILE"]}" \
f" with {e}"
print(ERROR_str)
if config.ERROR_FILE != None:
with config.ERROR_FILE_f_lock:
config.ERROR_FILE_f.write(ERROR_str + "\n")
return 0
# Print Found Files
INFO_str = f"INFO: Found {len(csv_files)} CSV files to process"
print(INFO_str)
if config.INFO_FILE != None:
with config.INFO_FILE_f_lock:
config.INFO_FILE_f.write(INFO_str + "\n")
print("*** *** *** *** ***")
# Begin Processing
with ThreadPoolExecutor(max_workers=config.FILE_MAX_THREAD) as executor:
try:
# Init Thread Pool
thread_pool = []
for file_name in csv_files:
# Prepare parameters
input_path = os.path.join(
config.BASIC_CONFIG["DATA_SOURCE_DIR"],
file_name
)
if file_rename == None:
output_path = os.path.join(
config.BASIC_CONFIG["DATA_OUTPUT_DIR"],
file_name
)
else:
output_path = os.path.join(
config.BASIC_CONFIG["DATA_OUTPUT_DIR"],
file_rename[file_name]
)
# Add Thread to pool
thread_pool.append(executor.submit(
process_csv_file,
input_path,
output_path
))
# Finished
for future in as_completed(thread_pool):
if config.force_exit:
break
future.result()
# Summary
INFO_str = "INFO: All files processed successfully, Wating for summary."
print(INFO_str)
if config.BASIC_CONFIG["TRACK_SWITCH"]:
while not config.update_progress_queue.empty:
continue
with config.status_data_lock:
summary = {
"start_time": time.time(),
"this_start_time": 0,
"finished_elapsed_time_str": None,
"processed_count": 0,
"all_count": 0,
"data": "\\",
"status": -1
}
for file_name in config.status_data_dict.keys():
summary["start_time"] = min(
summary["start_time"],
config.status_data_dict[file_name]["start_time"]
)
summary["this_start_time"] = max(
summary["this_start_time"],
config.status_data_dict[file_name]["this_start_time"]
)
summary["all_count"] += config.status_data_dict[file_name]["all_count"]
summary["processed_count"] = summary["all_count"]
summary["finished_elapsed_time_str"] = format_time2str(
summary["this_start_time"] -
summary["start_time"]
)
config.status_data_dict["All"] = summary
print_progress_table(True)
INFO_str = "INFO: All files processed successfully."
print(INFO_str)
if config.INFO_FILE != None:
with config.INFO_FILE_f_lock:
config.INFO_FILE_f.write(INFO_str + "\n")
return 0
except Exception as e:
ERROR_str = f"ERROR: Error during processing with {e}"
print(ERROR_str)
if config.ERROR_FILE != None:
with config.ERROR_FILE_f_lock:
config.ERROR_FILE_f.write(ERROR_str + "\n")
return 1
finally:
executor.shutdown(wait=False, cancel_futures=True)
if __name__ == "__main__":
sys.exit(main())