-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaggregate.py
337 lines (263 loc) · 10.8 KB
/
aggregate.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
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
# -*- coding: utf-8 -*-
from abc import ABC, abstractmethod
from collections.abc import Callable, Iterator
from enum import Enum
from numpy import zeros, ndarray
from src.data_structures.field import FieldLike, FieldType, FieldNullError, FieldTypeError
from src.utilities.geometry import orthogonalise_matrix
from src.utilities.utils import format_sig_figs
class AggregateType(Enum):
COUNT = "count"
CHECK = "check"
AVERAGE = "average"
class AggregateLike[VALUE_TYPE](ABC):
def __init__(self, aggregate_type: AggregateType, field_type: FieldType, nullable: bool = False):
self.aggregate_type = aggregate_type
self.field_type = field_type
self.nullable = nullable
@property
@abstractmethod
def group_count(self) -> int:
...
@property
@abstractmethod
def group_ids(self) -> Iterator[int]:
...
@abstractmethod
def get_value_for(self, id: int) -> VALUE_TYPE:
...
def serialize_value_for(self, id: int, null_serialization: str = "", sig_figs: int = None) -> list[str]:
def format(value: VALUE_TYPE) -> str:
if sig_figs is not None and self.field_type.roundable:
return format_sig_figs(value, sig_figs)
else:
return str(value)
if not self.field_type.serializable:
raise FieldTypeError.lacks_property(self.field_type, "serializable")
else:
if self.field_type.size == 1:
try:
return [format(self.get_value_for(id))]
except AggregateNullError:
return [null_serialization]
else:
try:
return [format(element) for element in self.get_value_for(id)]
except AggregateNullError:
return [null_serialization for _ in range(self.field_type.size)]
class Aggregate[VALUE_TYPE](AggregateLike, ABC):
def __init__(
self,
aggregate_type: AggregateType,
field_type: FieldType,
group_id_field: FieldLike[int],
nullable: bool = False
):
super().__init__(aggregate_type, field_type, nullable)
self._group_ids = group_id_field
self._aggregates: dict[int, VALUE_TYPE] = None
@property
def group_count(self) -> int:
if self._aggregates is None:
self._init_aggregates()
return len(self._aggregates)
@property
def group_ids(self) -> Iterator[int]:
if self._aggregates is None:
self._init_aggregates()
for id in sorted(self._aggregates):
yield id
def get_value_for(self, id: int) -> VALUE_TYPE:
if self._aggregates is None:
self._init_aggregates()
if id not in self._aggregates:
raise AggregateLookupError(id)
else:
value = self._aggregates[id]
if value is None:
raise AggregateNullError(id)
else:
return value
@abstractmethod
def _init_aggregates(self) -> None:
...
class CountAggregate(Aggregate):
def __init__(self, group_id_field: FieldLike[int]):
super().__init__(AggregateType.COUNT, FieldType.DISCRETE, group_id_field)
def _init_aggregates(self):
self._aggregates: dict[int, int] = dict()
counts: dict[int, int] = dict()
for y in range(self._group_ids.height):
for x in range(self._group_ids.width):
try:
group_id = self._group_ids.get_value_at(x, y)
except FieldNullError:
continue
if group_id not in counts:
counts[group_id] = 0
counts[group_id] += 1
for group_id in counts:
aggregate = counts[group_id]
self._aggregates[group_id] = aggregate
class CheckAggregate[VALUE_TYPE](Aggregate):
def __init__(self, value_field: FieldLike[VALUE_TYPE], group_id_field: FieldLike[int]):
super().__init__(AggregateType.AVERAGE, value_field.field_type, group_id_field, value_field.nullable)
self._values = value_field
def _init_aggregates(self) -> None:
self._aggregates: dict[int, VALUE_TYPE] = dict()
values: dict[int, VALUE_TYPE | None] = dict()
for y in range(self._values.height):
for x in range(self._values.width):
try:
group_id = self._group_ids.get_value_at(x, y)
except FieldNullError:
continue
if group_id not in values:
values[group_id] = None
try:
value = self._values.get_value_at(x, y)
except FieldNullError:
continue
if values[group_id] is None:
values[group_id] = value
if values[group_id] != value:
raise CheckAggregationError(group_id, (values[group_id], value))
else:
continue
for group_id in values:
aggregate = values[group_id]
self._aggregates[group_id] = aggregate
class AverageAggregate[VALUE_TYPE](Aggregate):
def __init__(self, value_field: FieldLike[VALUE_TYPE], group_id_field: FieldLike[int]):
super().__init__(AggregateType.AVERAGE, value_field.field_type, group_id_field, value_field.nullable)
if not value_field.field_type.averageable:
raise FieldTypeError.lacks_property(value_field.field_type, "averageable")
else:
self._values = value_field
def _init_aggregates(self) -> None:
self._aggregates: dict[int, VALUE_TYPE] = dict()
match self._values.field_type:
case FieldType.SCALAR: self._init_scalar_aggregates()
case FieldType.MATRIX: self._init_matrix_aggregates()
def _init_scalar_aggregates(self) -> None:
totals: dict[int, float] = dict()
counts: dict[int, int] = dict()
for y in range(self._values.height):
for x in range(self._values.width):
try:
group_id = self._group_ids.get_value_at(x, y)
except FieldNullError:
continue
if group_id not in totals:
totals[group_id] = 0.0
counts[group_id] = 0
try:
value = self._values.get_value_at(x, y)
except FieldNullError:
continue
totals[group_id] += value
counts[group_id] += 1
for group_id in totals:
if counts[group_id] == 0:
aggregate = None
else:
aggregate = totals[group_id] / counts[group_id]
self._aggregates[group_id] = aggregate
def _init_matrix_aggregates(self) -> None:
totals: dict[int, ndarray] = dict()
counts: dict[int, int] = dict()
for y in range(self._values.height):
for x in range(self._values.width):
try:
group_id = self._group_ids.get_value_at(x, y)
except FieldNullError:
continue
if group_id not in totals:
totals[group_id] = zeros((3, 3))
counts[group_id] = 0
try:
value = self._values.get_value_at(x, y)
except FieldNullError:
continue
totals[group_id] += value
counts[group_id] += 1
for group_id in totals:
if counts[group_id] == 0:
aggregate = None
else:
aggregate = orthogonalise_matrix(totals[group_id] / counts[group_id])
self._aggregates[group_id] = aggregate
class CustomAggregate[VALUE_TYPE](Aggregate):
def __init__(
self,
aggregate_type: AggregateType,
values: dict[int, VALUE_TYPE],
field_type: FieldType,
group_id_field: FieldLike[int],
nullable: bool,
):
super().__init__(aggregate_type, field_type, group_id_field, nullable)
self._values = values
def _init_aggregates(self) -> None:
self._aggregates = self._values
class DiscreteAggregateMapper[VALUE_TYPE](AggregateLike):
def __init__(self, field_type: FieldType, discrete_aggregate: AggregateLike[int], mapping: dict[int, VALUE_TYPE]):
super().__init__(discrete_aggregate.aggregate_type, field_type, discrete_aggregate.nullable)
self._mapping = mapping
self._aggregate = discrete_aggregate
@property
def group_count(self) -> int:
return self._aggregate.group_count
@property
def group_ids(self) -> Iterator[int]:
return self._aggregate.group_ids
def get_value_for(self, id: int) -> VALUE_TYPE:
key = self._aggregate.get_value_for(id)
return self._mapping[key]
class FunctionalAggregateMapper[INPUT_TYPE, OUTPUT_TYPE](AggregateLike):
def __init__(
self,
field_type: FieldType,
aggregate: AggregateLike[INPUT_TYPE],
forward_mapping: Callable[[INPUT_TYPE], OUTPUT_TYPE],
):
super().__init__(aggregate.aggregate_type, field_type, aggregate.nullable)
self._forward_mapping = forward_mapping
self._aggregate = aggregate
@property
def group_count(self) -> int:
return self._aggregate.group_count
@property
def group_ids(self) -> Iterator[int]:
return self._aggregate.group_ids
def get_value_for(self, id: int) -> OUTPUT_TYPE:
return self._forward_mapping(self._aggregate.get_value_for(id))
class AggregateLookupError(KeyError):
def __init__(self, id: int):
"""
Exception raised when attempting to look up an invalid group ID in an aggregate.
:param id: The group ID.
"""
self.id = id
self.message = f"Aggregate does not contain a group with ID: {self.id}"
super().__init__(self.message)
class AggregateNullError(ValueError):
def __init__(self, id: int = None):
"""
Exception raised when a null value in a nullable aggregate is accessed.
:param id: ID of the group containing the null value.
"""
self.id = id
self.message = f"Aggregate has a null value for group {self.id}."
super().__init__(self.message)
class CheckAggregationError(ValueError):
def __init__(self, id: int, values: tuple[int, int]):
"""
Exception raised when a group of points in a check-aggregate have inconsistent values.
:param id: ID of the group.
:param values: Inconsistent values.
"""
self.id = id
self.values = values
self.message = f"Value field for check aggregate has inconsistent values {self.values[0]} and {self.values[1]} for group {self.id}."
super().__init__(self.message)