forked from HKU-MedAI/WSI-HGNN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
57 lines (45 loc) · 1.75 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
import yaml
try:
from yaml import CLoader as Loader, CDumper as Dumper
except ImportError:
from yaml import Loader, Dumper
import logging
from collections import OrderedDict
import numpy as np
from sklearn.metrics import precision_score, recall_score, f1_score, auc, roc_curve, roc_auc_score
import warnings
warnings.filterwarnings('ignore')
def ordered_yaml():
"""
yaml orderedDict support
"""
_mapping_tag = yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG
def dict_representer(dumper, data):
return dumper.represent_dict(data.items())
def dict_constructor(loader, node):
return OrderedDict(loader.construct_pairs(node))
Dumper.add_representer(OrderedDict, dict_representer)
Loader.add_constructor(_mapping_tag, dict_constructor)
return Loader, Dumper
def acc(outputs, targets):
return np.mean(outputs.detach().cpu().numpy().argmax(axis=1) == targets.detach().cpu().numpy())
def metrics(outputs, targets, average):
preds = outputs.argmax(1)
precision = precision_score(targets, preds, average=average)
recall = recall_score(targets, preds, average=average)
f1 = f1_score(targets, preds, average=average)
if average == 'binary':
fpr, tpr, thresholds = roc_curve(targets, preds)
aucroc = auc(fpr, tpr)
else:
aucroc = roc_auc_score(targets, outputs, multi_class='ovr')
return precision, recall, f1, aucroc
def get_logger():
logger_name = 'main-logger'
logger = logging.getLogger(logger_name)
logger.setLevel(logging.INFO)
handler = logging.StreamHandler()
fmt = "[%(asctime)s %(levelname)s %(filename)s line %(lineno)d %(process)d] %(message)s"
handler.setFormatter(logging.Formatter(fmt))
logger.addHandler(handler)
return logger