-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkprojection.py
More file actions
264 lines (207 loc) 路 7.96 KB
/
Copy pathkprojection.py
File metadata and controls
264 lines (207 loc) 路 7.96 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
from dataclasses import dataclass
from itertools import combinations, product
from typing import Any, Dict, Iterable, List, Optional, Tuple, TypeVar
from abc import ABC, abstractmethod
from typing import TypeVar
__all__ = ["KCoverageResult", "KProjectionCoverage"]
Self = TypeVar("Self")
class Metric(ABC):
def __init__(self, *args, **kwargs) -> None:
pass
@abstractmethod
def update(self: Self, *args, **kwargs) -> Self:
"""
Add values to the metric
"""
raise NotImplementedError
@abstractmethod
def compute(self, *args, **kwargs):
"""
Compute the metric
"""
raise NotImplementedError
class Projection:
"""
A single projection
"""
def __init__(self, n_values: List[int], names: Optional[List[str]] = None) -> None:
"""
Holds a subspace with the cartesian product of some dimension and
monitors coverage for this subspace.
:param n_values: list with number of values for each dimension in this projection
:param names: name of dimensions of this projection. just for debugging
"""
self.n_options = n_values
self.names = names or []
# counts hits for all points in subspace
self.counts: Dict[Tuple[int], int] = {}
for point in product(*[range(i) for i in n_values]):
self.counts[point] = 0
@property
def points(self) -> Iterable[Tuple[int]]:
"""
:returns: iterator over all points in this subspace
"""
return self.counts.keys()
def is_covered(self, point: Tuple[int]) -> bool:
"""
Checks of the particular point has been covered.
We consider it covered if it has been covered at least once.
"""
return self.counts[point] > 0
@property
def n_covered(self) -> int:
"""
number of covered points in this subspace
"""
return sum([int(count > 0) for count in self.counts.values()])
@property
def n_points(self) -> None:
"""
number of points in this subspace
"""
return len(self.counts)
def reset(self) -> None:
for key in self.counts:
self.counts[key] = 0
def cover(self, point: Tuple[int]) -> None:
"""
mark point as covered
"""
self.counts[point] += 1
def __repr__(self) -> str:
if self.names:
return f'Projection({",".join(self.names)})'
else:
return "Projection()"
@property
def k(self) -> int:
return len(self.n_options)
@dataclass
class KCoverageResult:
"""
Result of k-projection coverage calculation.
Args:
coverage: The proportion of covered points over the total points (a float between 0 and 1).
k: The number of dimensions used in the projection.
covered: The number of points that are covered by the added scenarios.
total: The total number of points in the k-dimensional space.
scenes: The total number of scenarios that have been added.
"""
coverage: float
k: int
covered: int
total: int
scenes: int
class KProjectionCoverage(Metric):
"""
Preliminary implementation of Quantitative Projection Coverage from the paper
[Quantitative Projection Coverage for Testing ML-enabled Autonomous Systems](https://arxiv.org/abs/1805.04333).
**Note: This implementation is still missing some features, such as weighting.**
Examples:
How to use this metric:
description = {
"weather": ["good", "bad", "ugly"],
"temperature": [1, 2, 3, 4],
"humidity": [0.1, 0.2, 0.3, 0.4, 0.5],
}
cov = KProjectionCoverage(k=2, desc=description)
cov.add_scenario({"weather": "bad", "temperature": 1, "humidity": 0.1})
cov.add_scenario({"weather": "ugly", "temperature": 2, "humidity": 0.1})
cov.add_scenario({"weather": "good", "temperature": 2, "humidity": 0.1})
cov.add_scenario({"weather": "good", "temperature": 3, "humidity": 0.1})
cov.add_scenario({"weather": "good", "temperature": 4, "humidity": 0.1})
print(cov.compute())
"""
def __init__(self, k: int, desc: Dict[str, List[Any]]) -> None:
"""
Initializes the KProjectionCoverage metric.
Args:
k: The number of dimensions in the projection space (k <= total dimensions).
desc: Domain description, a dictionary mapping each dimension to its list of possible values.
"""
assert k <= len(desc)
self.desc = desc
self.k = k
self.n_scenes = 0
self.dims = list(desc.keys())
self.n_dim_values = [len(desc[v]) for v in self.dims]
# Maps (dimension, value) pairs to an integer index for efficiency.
self.dim_value_to_index: Dict[Tuple[str, Any], int] = {}
for d in self.dims:
for n, o in enumerate(desc[d]):
self.dim_value_to_index[(d, o)] = n
# Projections identified by tuples of their corresponding dimensions.
self.projections: Dict[Tuple[int], Projection] = {}
# Create all k-dimensional projections
for c in combinations(range(self.n_dims), r=self.k):
self.projections[c] = Projection(
n_values=[self.n_dim_values[j] for j in c],
names=[self.dims[j] for j in c],
)
def reset(self) -> None:
"""
Resets all projections, clearing the coverage data.
"""
for p in self.projections.values():
p.reset()
@property
def n_dims(self) -> int:
"""
Returns the number of dimensions described by the domain.
Returns:
int: Number of dimensions.
"""
return len(self.dims)
def add_scenario(self, scenario: Dict[str, Any]) -> None:
"""
Adds a new scenario to the coverage calculation.
Args:
scenario: A dictionary mapping each dimension to a value representing a scenario.
"""
assert len(scenario) == self.n_dims
# Convert the scenario description to its integer representation.
scene = [self.dim_value_to_index[d, scenario[d]] for d in self.dims]
# Update each k-dimensional projection with the new scenario.
for c, projection in self.projections.items():
point = tuple(scene[i] for i in c)
projection.cover(point)
self.n_scenes += 1
def add_scenarios(self, scenarios: Iterable[Dict[str, Any]]) -> None:
"""
Adds multiple scenarios to the coverage calculation.
Args:
scenarios: An iterable of dictionaries where each dictionary represents a scenario.
"""
for scene in scenarios:
self.add_scenario(scene)
def update(self: Self, scenarios: Iterable[Dict[str, Any]]) -> Self:
"""
Updates the coverage calculation with multiple scenarios.
Args:
scenarios: An iterable of dictionaries where each dictionary represents a scenario.
Returns:
Self: Returns the current instance to allow method chaining.
"""
self.add_scenarios(scenarios=scenarios)
return self
def compute(self) -> KCoverageResult:
"""
Computes the current coverage metrics based on the added scenarios.
Returns:
KCoverageResult: A result object containing coverage, total points, covered points, and the number of scenes.
"""
covered = 0
total = 0
# Calculate total and covered points for each projection.
for projection in self.projections.values():
total += projection.n_points
covered += projection.n_covered
coverage = covered / total
return KCoverageResult(
coverage=coverage,
k=self.k,
covered=covered,
total=total,
scenes=self.n_scenes,
)