-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhigh_level_functions.py
More file actions
142 lines (119 loc) · 4.14 KB
/
high_level_functions.py
File metadata and controls
142 lines (119 loc) · 4.14 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
"""Module containing high level routines"""
import numpy as np
from .metrics import metric_f, single_metrics
from .heatmap import Heatmap, heatmap_list
def compute_all_metrics_globally(X: np.ndarray, T: np.ndarray) -> dict:
"""
High-level function to compute all metrics between X and T
Note that X and T must be 2D arrays
"""
assert (
np.ndim(X) == 2
), f"Input must be 2-dimensional; got {np.ndim(X)} dimensions for X"
if T is not None:
assert (
np.ndim(T) == 2
), f"Input must be 2-dimensional; got {np.ndim(T)} dimensions for T"
# Compute all evaluation metrics.
results = dict()
metrics_to_compute = single_metrics if T is None else metric_f.keys()
for metric in metrics_to_compute:
f = metric_f.get(metric)
if f is not None:
try:
if T is not None:
if metric in single_metrics:
results[metric] = f(X), f(T)
else:
results[metric] = f(X, T)
else:
results[metric] = f(X)
except Exception as e:
print(f"Failed to compute {metric}: {e}")
else:
print(f"Unknown metric name: {metric}")
return results
def compute_metric_globally(X, T, metric: str):
"""
High-level function to compute a single metric between X and T
Note that X and T must be 2D arrays
"""
assert (
np.ndim(X) == 2
), f"Input must be 2-dimensional; got {np.ndim(X)} dimensions for X"
if T is not None:
assert (
np.ndim(T) == 2
), f"Input must be 2-dimensional; got {np.ndim(T)} dimensions for T"
# Compute specified evaluation metric
f = metric_f.get(metric)
if f is None:
raise ValueError(f"Unknown metric name: {metric}")
if T is not None:
if metric in single_metrics:
return f(X), f(T)
else:
return f(X, T)
else:
return f(X)
def compute_all_metrics_locally(
X, T, block_size=None, pad_len=None, verbose=True
) -> dict:
"""
High-level function to compute all metric heatmaps between X and T
Note that X and T must be 2D arrays
"""
assert (
np.ndim(X) == 2
), f"Input must be 2-dimensional; got {np.ndim(X)} dimensions for X"
if T is not None:
assert (
np.ndim(T) == 2
), f"Input must be 2-dimensional; got {np.ndim(T)} dimensions for T"
# Compute all evaluation metrics."""
metrics_to_compute = single_metrics if T is None else metric_f.keys()
if block_size is None:
block_size = X.shape[0] // 8
if pad_len is None:
pad_len = X.shape[0] // 16
if verbose:
print(
f"Heatmap will be computed with blocks of size {block_size}, and has image "
f"padding of length {pad_len}"
)
if T is not None:
results = heatmap_list(X, T, metrics_to_compute, block_size, pad_len)
else:
results = dict()
for metric in metrics_to_compute:
f = metric_f.get(metric)
results[metric] = Heatmap(X, T, f, X.shape[0] // 8, X.shape[0] // 16)
return results
def compute_metric_locally(
X, T, metric: str, block_size=None, pad_len=None, verbose=True
):
"""
High-level function to compute a single metric heatmap between X and T
Note that X and T must be 2D arrays
"""
assert (
np.ndim(X) == 2
), f"Input must be 2-dimensional; got {np.ndim(X)} dimensions for X"
if T is not None:
assert (
np.ndim(T) == 2
), f"Input must be 2-dimensional; got {np.ndim(T)} dimensions for T"
# Compute specified evaluation metric"""
f = metric_f.get(metric)
if f is None:
raise ValueError(f"Unknown metric name: {metric}")
if block_size is None:
block_size = X.shape[0] // 8
if pad_len is None:
pad_len = X.shape[0] // 16
if verbose:
print(
f"Heatmap will be computed with blocks of size {block_size}, and has image "
f"padding of length {pad_len}"
)
return Heatmap(X, T, f, block_size, pad_len)