forked from NetManAIOps/LogClass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
96 lines (79 loc) · 3.05 KB
/
utils.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
import os
import json
import shutil
import pandas as pd
# trim is only used when showing the top keywords for each class
def trim(s):
"""Trim string to fit on terminal (assuming 80-column display)"""
return s if len(s) <= 80 else s[:77] + "..."
class TestingParameters():
def __init__(self, params):
self.params = params
self.original_state = params['train']
def __enter__(self):
self.params['train'] = False
def __exit__(self, exc_type, exc_value, traceback):
self.params['train'] = self.original_state
def load_params(params):
params_file = os.path.join(
params['id_dir'], f"best_params.json")
with open(params_file, "r") as fp:
best_params = json.load(fp)
params.update(best_params)
def save_params(params):
params_file = os.path.join(
params['id_dir'], f"best_params.json")
with open(params_file, "w") as fp:
json.dump(params, fp)
def file_handling(params):
if "raw_logs" in params:
if not os.path.exists(params['raw_logs']):
raise FileNotFoundError(
f"File {params['raw_logs']} doesn't exist. "
+ "Please provide the raw logs path."
)
logs_directory = os.path.dirname(params['logs'])
if not os.path.exists(logs_directory):
os.makedirs(logs_directory)
else:
# Checks if preprocessed logs exist as input
if not os.path.exists(params['logs']):
raise FileNotFoundError(
f"File {params['base_dir']} doesn't exist. "
+ "Preprocess target logs first and provide their path."
)
if params['train']:
# Checks if the experiment id already exists
if os.path.exists(params["id_dir"]) and not params["force"]:
raise FileExistsError(
f"directory '{params['id_dir']} already exists. "
+ "Run with --force to overwrite."
+ f"If --force is used, you could lose your training results."
)
if os.path.exists(params["id_dir"]):
shutil.rmtree(params["id_dir"])
for target_dir in ['id_dir', 'models_dir', 'features_dir']:
os.makedirs(params[target_dir])
else:
# Checks if input models and features are provided
for concern in ['models_dir', 'features_dir']:
target_path = params[concern]
if not os.path.exists(target_path):
raise FileNotFoundError(
"directory '{} doesn't exist. ".format(target_path)
+ "Run train first before running inference."
)
def print_params(params):
print("{:-^80}".format("params"))
print("Beginning experiment using the following configuration:\n")
for param, value in params.items():
print("\t{:>13}: {}".format(param, value))
print()
print("-" * 80)
def save_results(results, params):
df = pd.DataFrame(results)
file_name = os.path.join(
params['id_dir'],
"results.csv",
)
df.to_csv(file_name, index=False)