-
Notifications
You must be signed in to change notification settings - Fork 5
/
data.py
369 lines (293 loc) · 11.8 KB
/
data.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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
# cspell:ignore csqrt ndmin ufunc vstack
# pylint: disable=too-many-ancestors
"""Data containers for working with four-momenta.
.. seealso:: :doc:`numpy:user/basics.dispatch`
"""
from collections import abc
from typing import (
Dict,
ItemsView,
Iterable,
Iterator,
KeysView,
Mapping,
Optional,
Sequence,
Tuple,
Union,
ValuesView,
)
import numpy as np
from numpy.lib.mixins import NDArrayOperatorsMixin
from numpy.lib.scimath import sqrt as csqrt
try:
# pyright: reportMissingImports=false
from numpy.typing import ArrayLike, DTypeLike
except ImportError:
ArrayLike = Union[Sequence, np.ndarray] # type: ignore
DTypeLike = object # type: ignore
class ScalarSequence(NDArrayOperatorsMixin, abc.Sequence):
"""`numpy.array` data container of rank 1."""
def __init__(
self, data: ArrayLike, dtype: Optional[DTypeLike] = None
) -> None:
self.__data = np.array(data, dtype)
if len(self.__data.shape) != 1:
raise ValueError(
f"{self.__class__.__name__} has to be of rank 1,"
f" but input data is of rank {len(self.__data.shape)}"
)
def __array__(self, _: Optional[DTypeLike] = None) -> np.ndarray:
return self.__data
def __getitem__(self, i: Union[int, slice]) -> np.ndarray: # type: ignore
return self.__data[i]
def __len__(self) -> int:
return len(self.__data)
def __repr__(self) -> str:
return f"{self.__class__.__name__}({np.array(self)}"
class ThreeMomentum(NDArrayOperatorsMixin, abc.Sequence):
def __init__(
self, data: ArrayLike, dtype: Optional[DTypeLike] = None
) -> None:
self.__data = np.array(data, dtype=dtype, ndmin=2)
if len(self.__data.shape) != 2:
raise ValueError(
f"{self.__class__.__name__} has to be of rank 2,"
f" but this data is of rank {len(self.__data.shape)}"
)
if self.__data.shape[1] != 3:
raise ValueError(
f"{self.__class__.__name__} has to be of shape (N, 3),"
f" but this data sample is of shape {self.__data.shape}"
)
def __array__(self, _: Optional[DTypeLike] = None) -> np.ndarray:
return self.__data
def __getitem__( # type: ignore
self,
i: Union[Tuple[Union[int, slice], Union[int, slice]], int, slice],
) -> np.ndarray:
return self.__data[i]
def __len__(self) -> int:
return len(self.__data)
def __repr__(self) -> str:
return f"{self.__class__.__name__}({np.array(self)})"
class FourMomentumSequence(NDArrayOperatorsMixin, abc.Sequence):
"""Container for a `numpy.array` of four-momentum tuples.
The input data has to be of shape (N, 4) and the order of the items has to
be :math:`(E, p)` (energy first).
"""
def __init__(self, data: ArrayLike) -> None:
self.__data = np.array(data)
if len(self.__data.shape) != 2:
raise ValueError(
f"{self.__class__.__name__} has to be of rank 2,"
f" but this data is of rank {len(self.__data.shape)}"
)
if self.__data.shape[1] != 4:
raise ValueError(
f"{self.__class__.__name__} has to be of shape (N, 4),"
f" but this data sample is of shape {self.__data.shape}"
)
if np.min(self.energy) < 0:
raise ValueError(
"Energy column contains entries that are less than 0."
" Did you order the four-momentum tuples as (E, p)?"
)
def __array__(self, _: Optional[DTypeLike] = None) -> np.ndarray:
return self.__data
def __getitem__( # type: ignore
self,
i: Union[Tuple[Union[int, slice], Union[int, slice]], int, slice],
) -> np.ndarray:
return self.__data[i]
def __len__(self) -> int:
return len(self.__data)
def __repr__(self) -> str:
return f"{self.__class__.__name__}({np.array(self)})"
@property
def energy(self) -> ScalarSequence:
return ScalarSequence(self[:, 0])
@property
def three_momentum(self) -> ThreeMomentum:
return ThreeMomentum(self[:, 1:])
@property
def p_x(self) -> ScalarSequence:
return ScalarSequence(self[:, 1])
@property
def p_y(self) -> ScalarSequence:
return ScalarSequence(self[:, 2])
@property
def p_z(self) -> ScalarSequence:
return ScalarSequence(self[:, 3])
def p_norm(self) -> ScalarSequence:
"""Norm of `.three_momentum`."""
return ScalarSequence(np.sqrt(self.p_squared()))
def p_squared(self) -> ScalarSequence:
"""Squared norm of `.three_momentum`."""
return ScalarSequence(np.sum(self.three_momentum ** 2, axis=1))
def phi(self) -> ScalarSequence:
return ScalarSequence(np.arctan2(self.p_y, self.p_x))
def theta(self) -> ScalarSequence:
return ScalarSequence(np.arccos(self.p_z / self.p_norm()))
def mass(self) -> ScalarSequence:
mass_squared = self.mass_squared(dtype=np.float64)
return ScalarSequence(csqrt(mass_squared))
def mass_squared(
self, dtype: Optional[DTypeLike] = None
) -> ScalarSequence:
return ScalarSequence(
self.energy ** 2 - self.p_norm() ** 2, dtype=dtype
)
class MatrixSequence(NDArrayOperatorsMixin, abc.Sequence):
"""Safe data container for a sequence of 4x4-matrices."""
def __init__(self, data: ArrayLike) -> None:
self.__data = np.array(data)
if len(self.__data.shape) != 3:
raise ValueError(
f"{self.__class__.__name__} has to be of rank 3,"
f" but this data is of rank {len(self.__data.shape)}"
)
if self.__data.shape[1:] != (4, 4):
raise ValueError(
f"{self.__class__.__name__} has to be of shape (N, 4, 4),"
f" but this data sample is of shape {self.__data.shape}"
)
def __array__(self, _: Optional[DTypeLike] = None) -> np.ndarray:
return self.__data
def __getitem__(self, i: Union[int, slice]) -> np.ndarray: # type: ignore
return self.__data[i]
def __len__(self) -> int:
return len(self.__data)
def __repr__(self) -> str:
return f"{self.__class__.__name__}({np.array(self)})"
def dot(self, vector: FourMomentumSequence) -> FourMomentumSequence:
return FourMomentumSequence(
np.einsum(
"ij...,j...",
np.transpose(self, axes=(1, 2, 0)),
np.transpose(vector),
)
)
class EventCollection(abc.Mapping):
"""A mapping of state IDs to their `FourMomentumSequence` data samples.
An `EventCollection` has to be converted to `DataSet` so that it can be
used to evaluate a `.HelicityModel`.
"""
def __init__(self, data: Mapping[int, ArrayLike]) -> None:
self.__data = {i: FourMomentumSequence(v) for i, v in data.items()}
n_events = self.n_events
if any(map(lambda v: len(v) != n_events, self.values())):
raise ValueError(
f"Not all {FourMomentumSequence.__name__} items"
f" are of length {n_events}"
)
def __getitem__(self, i: int) -> FourMomentumSequence:
return self.__data[i]
def __iter__(self) -> Iterator[int]:
return iter(self.__data)
def __len__(self) -> int:
return len(self.__data)
def __repr__(self) -> str:
return f"{self.__class__.__name__}({self.__data})"
@property
def n_events(self) -> int:
if len(self) == 0:
return 0
return len(next(iter(self.values())))
def sum( # noqa: A003
self, indices: Iterable[int]
) -> FourMomentumSequence:
return FourMomentumSequence(sum(self.__data[i] for i in indices)) # type: ignore
def keys(self) -> KeysView[int]:
return self.__data.keys()
def items(self) -> ItemsView[int, FourMomentumSequence]:
return self.__data.items()
def values(self) -> ValuesView[FourMomentumSequence]:
return self.__data.values()
def append(self, other: Mapping[int, ArrayLike]) -> None:
if not isinstance(other, EventCollection):
other = EventCollection(other)
if self.n_events != 0 and set(self) != set(other):
raise ValueError(
f"Trying to append a momentum pool with state IDs {set(other)}"
f" to a momentum pool with state IDs {set(self)}"
)
self.__data = {
i: FourMomentumSequence(np.vstack((values, other[i])))
for i, values in self.items()
}
def select_events(self, selection: Union[int, slice]) -> "EventCollection":
return EventCollection(
{i: values[selection] for i, values in self.items()}
)
def to_pandas(
self, _: Optional[DTypeLike] = None
) -> Dict[Tuple[int, str], np.ndarray]:
"""Converter for the :code:`data` argument of `pandas.DataFrame`.
The resulting `~pandas.DataFrame` has multi-columns (see
:doc:`pandas:user_guide/advanced`) where the first column layer
represents the state IDs and the second column layer represents
each of the four-momentum entries (:math:`E, p_x, p_y, p_z`).
"""
return {
(k, label): np.transpose(v)[i]
for k, v in self.items()
for i, label in enumerate(["E", "px", "py", "pz"])
}
class DataSet(abc.Mapping):
"""A mapping of variable names to their `ScalarSequence`.
The `~.DataSet.keys` of `DataSet` represent variable names in a
`.HelicityModel`, while its `~.DataSet.values` are inserted in their place.
"""
def __init__(
self, data: Mapping[str, ArrayLike], dtype: Optional[DTypeLike] = None
) -> None:
self.__data = {
name: ScalarSequence(v, dtype=dtype) for name, v in data.items()
}
if not all(map(lambda k: isinstance(k, str), self.__data)):
raise TypeError(f"Not all keys {set(data)} are strings")
n_events = self.n_events
if any(map(lambda v: len(v) != n_events, self.values())):
raise ValueError(
f"Not all {FourMomentumSequence.__name__} items"
f" are of length {n_events}"
)
def __repr__(self) -> str:
return f"{self.__class__.__name__}({self.__data})"
def __getitem__(self, i: str) -> ScalarSequence:
return self.__data[i]
def __iter__(self) -> Iterator[str]:
return iter(self.__data)
def __len__(self) -> int:
return len(self.__data)
@property
def n_events(self) -> int:
if len(self) == 0:
return 0
return len(next(iter(self.values())))
def keys(self) -> KeysView[str]:
return self.__data.keys()
def items(self) -> ItemsView[str, ScalarSequence]:
return self.__data.items()
def values(self) -> ValuesView[ScalarSequence]:
return self.__data.values()
def append(self, other: Mapping[str, ArrayLike]) -> None:
if not isinstance(other, DataSet):
other = DataSet(other)
if self.n_events != 0 and set(self) != set(other):
raise ValueError(
f"Trying to append a data set with state IDs {set(other)}"
f" to a data set with state IDs {set(self)}"
)
self.__data = {
i: ScalarSequence(np.vstack((values, other[i])))
for i, values in self.items()
}
def select_events(self, selection: Union[int, slice]) -> "DataSet":
return DataSet({i: values[selection] for i, values in self.items()})
def to_pandas(
self, _: Optional[DTypeLike] = None
) -> Dict[str, np.ndarray]:
"""Converter for the :code:`data` argument of `pandas.DataFrame`."""
return {k: np.array(v) for k, v in self.items()}