Repository navigation
Expand file tree
/
Copy pathaccumulators.py
More file actions
194 lines (148 loc) · 6.66 KB
/
Copy pathaccumulators.py
File metadata and controls
194 lines (148 loc) · 6.66 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
"""Initialize covariance accumulators.
This module defines a LevelDAG static node that allocates all per-frame reduction
accumulators (means) and counters used by downstream frame processing.
The node owns only initialization concerns (single responsibility):
- create group-id <-> index mappings
- allocate force/torque covariance mean containers
- allocate optional combined force-torque (FT) mean containers
- allocate per-level frame counters
The structure created here is treated as the canonical storage layout for the
rest of the pipeline.
"""
from __future__ import annotations
import logging
from collections.abc import MutableMapping
from dataclasses import dataclass
from typing import Any
import numpy as np
logger = logging.getLogger(__name__)
SharedData = MutableMapping[str, Any]
@dataclass(frozen=True)
class GroupIndex:
"""Bidirectional mapping between group ids and contiguous indices."""
group_id_to_index: dict[int, int]
index_to_group_id: list[int]
@dataclass(frozen=True)
class CovarianceAccumulators:
"""Container for covariance mean accumulators and frame counters."""
force_covariances: dict[str, Any]
torque_covariances: dict[str, Any]
frame_counts: dict[str, Any]
forcetorque_covariances: dict[str, Any]
forcetorque_counts: dict[str, Any]
class InitCovarianceAccumulatorsNode:
"""Allocate accumulators and counters for per-frame reductions.
Produces the following keys in `shared_data`:
Canonical mean accumulators:
- force_covariances: {"ua": dict, "res": list, "poly": list}
- torque_covariances: {"ua": dict, "res": list, "poly": list}
- forcetorque_covariances: {"res": list, "poly": list} (6N x 6N means)
Counters:
- frame_counts: {"ua": dict, "res": np.ndarray[int], "poly": np.ndarray[int]}
- forcetorque_counts: {"res": np.ndarray[int], "poly": np.ndarray[int]}
Group index mapping:
- group_id_to_index: {group_id: index}
- index_to_group_id: [group_id_by_index]
Backwards-compatible aliases (kept for older consumers):
- force_torque_stats -> forcetorque_covariances
- force_torque_counts -> forcetorque_counts
"""
def run(self, shared_data: dict[str, Any]) -> dict[str, Any]:
"""Initialize and attach all accumulator structures into shared_data.
Args:
shared_data: Shared pipeline dictionary. Must contain "groups".
Returns:
A dict of keys written into shared_data.
Raises:
KeyError: If "groups" is missing from shared_data.
"""
groups = shared_data["groups"]
group_index = self._build_group_index(groups)
accumulators = self._build_accumulators(
n_groups=len(group_index.index_to_group_id)
)
self._attach_to_shared_data(shared_data, group_index, accumulators)
self._attach_backwards_compatible_aliases(shared_data)
return self._build_return_payload(shared_data)
@staticmethod
def _build_group_index(groups: dict[int, Any]) -> GroupIndex:
"""Build group id <-> index mappings.
Args:
groups: Mapping of group id to group members.
Returns:
GroupIndex mapping object.
"""
group_ids = sorted(groups.keys())
gid2i = {gid: i for i, gid in enumerate(group_ids)}
return GroupIndex(group_id_to_index=gid2i, index_to_group_id=list(group_ids))
@staticmethod
def _build_accumulators(n_groups: int) -> CovarianceAccumulators:
"""Allocate empty covariance means and counters.
Args:
n_groups: Number of molecule groups.
Returns:
CovarianceAccumulators containing allocated containers.
"""
force_cov = {"ua": {}, "res": [None] * n_groups, "poly": [None] * n_groups}
torque_cov = {"ua": {}, "res": [None] * n_groups, "poly": [None] * n_groups}
frame_counts = {
"ua": {},
"res": np.zeros(n_groups, dtype=int),
"poly": np.zeros(n_groups, dtype=int),
}
forcetorque_cov = {"res": [None] * n_groups, "poly": [None] * n_groups}
forcetorque_counts = {
"res": np.zeros(n_groups, dtype=int),
"poly": np.zeros(n_groups, dtype=int),
}
return CovarianceAccumulators(
force_covariances=force_cov,
torque_covariances=torque_cov,
frame_counts=frame_counts,
forcetorque_covariances=forcetorque_cov,
forcetorque_counts=forcetorque_counts,
)
@staticmethod
def _attach_to_shared_data(
shared_data: SharedData, group_index: GroupIndex, acc: CovarianceAccumulators
) -> None:
"""Attach canonical structures to shared_data.
Args:
shared_data: Shared pipeline dictionary.
group_index: GroupIndex object.
acc: CovarianceAccumulators object.
"""
shared_data["group_id_to_index"] = group_index.group_id_to_index
shared_data["index_to_group_id"] = group_index.index_to_group_id
shared_data["force_covariances"] = acc.force_covariances
shared_data["torque_covariances"] = acc.torque_covariances
shared_data["frame_counts"] = acc.frame_counts
shared_data["forcetorque_covariances"] = acc.forcetorque_covariances
shared_data["forcetorque_counts"] = acc.forcetorque_counts
@staticmethod
def _attach_backwards_compatible_aliases(shared_data: SharedData) -> None:
"""Attach backwards-compatible aliases.
Args:
shared_data: Shared pipeline dictionary.
"""
shared_data["force_torque_stats"] = shared_data["forcetorque_covariances"]
shared_data["force_torque_counts"] = shared_data["forcetorque_counts"]
@staticmethod
def _build_return_payload(shared_data: SharedData) -> dict[str, Any]:
"""Build the return payload containing initialized keys.
Args:
shared_data: Shared pipeline dictionary.
Returns:
Dict of keys to values that were set in shared_data.
"""
return {
"group_id_to_index": shared_data["group_id_to_index"],
"index_to_group_id": shared_data["index_to_group_id"],
"force_covariances": shared_data["force_covariances"],
"torque_covariances": shared_data["torque_covariances"],
"frame_counts": shared_data["frame_counts"],
"forcetorque_covariances": shared_data["forcetorque_covariances"],
"forcetorque_counts": shared_data["forcetorque_counts"],
"force_torque_stats": shared_data["force_torque_stats"],
"force_torque_counts": shared_data["force_torque_counts"],
}