Repository navigation
Expand file tree
/
Copy pathutils.py
More file actions
executable file
·119 lines (98 loc) · 4.57 KB
/
Copy pathutils.py
File metadata and controls
executable file
·119 lines (98 loc) · 4.57 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
import torch
import random
import numpy as np
import pandas as pd
import os
import sys
import logging
from sklearn.metrics import classification_report, cohen_kappa_score, confusion_matrix, accuracy_score
from shutil import copy
import matplotlib.pyplot as plt
import seaborn as sns
def set_requires_grad(model, dict_, requires_grad=True):
for param in model.named_parameters():
if param[0] in dict_:
param[1].requires_grad = requires_grad
def fix_randomness(SEED):
random.seed(SEED)
np.random.seed(SEED)
torch.manual_seed(SEED)
torch.cuda.manual_seed(SEED)
torch.backends.cudnn.deterministic = True
def epoch_time(start_time, end_time):
elapsed_time = end_time - start_time
elapsed_mins = int(elapsed_time / 60)
elapsed_secs = int(elapsed_time - (elapsed_mins * 60))
return elapsed_mins, elapsed_secs
def _calc_metrics(pred_labels, true_labels, log_dir, home_path):
pred_labels = np.array(pred_labels).astype(int)
true_labels = np.array(true_labels).astype(int)
# save targets
labels_save_path = os.path.join(log_dir, "labels")
os.makedirs(labels_save_path, exist_ok=True)
np.save(os.path.join(labels_save_path, "predicted_labels.npy"), pred_labels)
np.save(os.path.join(labels_save_path, "true_labels.npy"), true_labels)
r = classification_report(true_labels, pred_labels, digits=6, output_dict=True)
cm = confusion_matrix(true_labels, pred_labels)
df = pd.DataFrame(r)
df["cohen"] = cohen_kappa_score(true_labels, pred_labels)
df["accuracy"] = accuracy_score(true_labels, pred_labels)
df = df * 100
# save classification report
exp_name = os.path.split(os.path.dirname(log_dir))[-1]
training_mode = os.path.basename(log_dir)
file_name = f"{exp_name}_{training_mode}_classification_report.xlsx"
report_Save_path = os.path.join(home_path, log_dir, file_name)
df.to_excel(report_Save_path)
# save confusion matrix
cm_file_name = f"{exp_name}_{training_mode}_confusion_matrix.torch"
cm_Save_path = os.path.join(home_path, log_dir, cm_file_name)
torch.save(cm, cm_Save_path)
plt.figure(figsize=(10,7))
sns.heatmap(cm, annot=True, fmt='g', cmap="Blues")
plt.xlabel('Predicted labels')
plt.ylabel('True labels')
plt.savefig(os.path.join(home_path, log_dir, f"{exp_name}_{training_mode}_confusion_matrix.png"))
plt.close('all')
accuracy = accuracy_score(true_labels, pred_labels)
tn, fp, fn, tp = cm.ravel()
precision = tp / (tp + fp)
sensitivity = tp / (tp + fn)
specificity = tn / (tn + fp)
data = {
'Metric': ['Accuracy', 'Precision', 'Sensitivity', 'Specificity'],
'Value': [accuracy, precision, sensitivity, specificity]
}
df = pd.DataFrame(data)
df.to_excel(os.path.join(home_path, log_dir, f"{exp_name}_{training_mode}_metrics.xlsx"), index=False, engine='openpyxl')
return accuracy, precision, sensitivity, specificity
def _logger(logger_name, level=logging.DEBUG):
"""
Method to return a custom logger with the given name and level
"""
logger = logging.getLogger(logger_name)
logger.setLevel(level)
# format_string = ("%(asctime)s — %(name)s — %(levelname)s — %(funcName)s:"
# "%(lineno)d — %(message)s")
format_string = "%(message)s"
log_format = logging.Formatter(format_string)
# Creating and adding the console handler
console_handler = logging.StreamHandler(sys.stdout)
console_handler.setFormatter(log_format)
logger.addHandler(console_handler)
# Creating and adding the file handler
file_handler = logging.FileHandler(logger_name, mode='a')
file_handler.setFormatter(log_format)
logger.addHandler(file_handler)
return logger
def copy_Files(destination, data_type):
destination_dir = os.path.join(destination, "model_files")
os.makedirs(destination_dir, exist_ok=True)
copy("main.py", os.path.join(destination_dir, "main.py"))
copy("trainer/trainer.py", os.path.join(destination_dir, "trainer.py"))
copy(f"config_files/{data_type}_Configs.py", os.path.join(destination_dir, f"{data_type}_Configs.py"))
copy("dataloader/augmentations.py", os.path.join(destination_dir, "augmentations.py"))
copy("dataloader/dataloader.py", os.path.join(destination_dir, "dataloader.py"))
copy(f"models/model.py", os.path.join(destination_dir, f"model.py"))
copy("models/loss.py", os.path.join(destination_dir, "loss.py"))
copy("models/TC.py", os.path.join(destination_dir, "TC.py"))