88
99
1010def stable_keys (mapping : dict [Any , Any ]) -> list [Any ]:
11- """Return mapping keys in a deterministic order across Python processes."""
11+ """Return mapping keys in deterministic order.
12+
13+ Args:
14+ mapping: Mapping whose keys should be ordered independently of process hash
15+ randomisation.
16+
17+ Returns:
18+ A list of keys sorted by key type name and representation.
19+ """
1220 return sorted (mapping .keys (), key = lambda key : (type (key ).__name__ , repr (key )))
1321
1422
1523def merge_means (old_mean : Any , old_n : int , new_mean : Any , new_n : int ) -> Any :
16- """Merge two means with their sample counts."""
24+ """Merge two running means using their sample counts.
25+
26+ Args:
27+ old_mean: Existing mean value, or ``None`` if no samples have been seen.
28+ old_n: Number of samples represented by ``old_mean``.
29+ new_mean: New mean value to merge.
30+ new_n: Number of samples represented by ``new_mean``.
31+
32+ Returns:
33+ The merged mean. If ``new_n`` is zero or negative, ``old_mean`` is returned.
34+ """
1735 if new_n <= 0 :
1836 return old_mean
1937 if old_mean is None or old_n <= 0 :
@@ -23,7 +41,16 @@ def merge_means(old_mean: Any, old_n: int, new_mean: Any, new_n: int) -> Any:
2341
2442
2543def incremental_mean (old : Any , new : Any , n : int ) -> Any :
26- """Compute an incremental mean."""
44+ """Update a running mean with one new sample.
45+
46+ Args:
47+ old: Existing running mean, or ``None`` for the first sample.
48+ new: New sample to incorporate.
49+ n: One-based sample count after adding ``new``.
50+
51+ Returns:
52+ The updated running mean.
53+ """
2754 if old is None :
2855 return new .copy () if hasattr (new , "copy" ) else new
2956 return old + (new - old ) / float (n )
@@ -34,7 +61,12 @@ class NeighborReducer:
3461
3562 @staticmethod
3663 def initialise (shared_data : dict [str , Any ]) -> None :
37- """Initialise parent-side neighbour reduction state."""
64+ """Initialise parent-side neighbour accumulators.
65+
66+ Args:
67+ shared_data: Shared workflow data containing ``groups``. The method writes
68+ ``neighbor_totals`` and ``neighbor_samples``.
69+ """
3870 shared_data ["neighbor_totals" ] = {
3971 group_id : 0 for group_id in shared_data ["groups" ].keys ()
4072 }
@@ -47,7 +79,13 @@ def reduce_frame_output(
4779 shared_data : dict [str , Any ],
4880 frame_neighbors : dict [int , tuple [int , int ]] | None ,
4981 ) -> None :
50- """Reduce one frame's neighbour-count payload."""
82+ """Merge one frame's neighbour-count payload.
83+
84+ Args:
85+ shared_data: Shared workflow data containing neighbour total/sample
86+ accumulators.
87+ frame_neighbors: Optional mapping of group id to ``(count, sample_count)``.
88+ """
5189 if frame_neighbors is None :
5290 return
5391
@@ -64,7 +102,13 @@ def merge_chunk_partial(
64102 neighbor_totals : dict [int , int ],
65103 neighbor_samples : dict [int , int ],
66104 ) -> None :
67- """Merge chunk-level neighbour totals/samples into parent accumulators."""
105+ """Merge chunk-level neighbour totals and samples.
106+
107+ Args:
108+ shared_data: Shared workflow data containing neighbour accumulators.
109+ neighbor_totals: Mapping of group id to additive neighbour totals.
110+ neighbor_samples: Mapping of group id to additive sample counts.
111+ """
68112 totals = shared_data .get ("neighbor_totals" )
69113 samples = shared_data .get ("neighbor_samples" )
70114 if totals is None or samples is None :
@@ -79,7 +123,13 @@ def merge_chunk_partial(
79123
80124 @staticmethod
81125 def finalise (shared_data : dict [str , Any ]) -> None :
82- """Convert reduced neighbour totals into average neighbour counts."""
126+ """Compute average neighbour counts from reduced totals.
127+
128+ Args:
129+ shared_data: Shared workflow data containing ``groups``,
130+ ``neighbor_totals``, and ``neighbor_samples``. The method writes
131+ ``neighbors``.
132+ """
83133 neighbors = {}
84134 for group_id in stable_keys (shared_data ["groups" ]):
85135 sample_count = shared_data ["neighbor_samples" ].get (group_id , 0 )
@@ -100,7 +150,13 @@ def reduce_frame_output(
100150 shared_data : dict [str , Any ],
101151 frame_out : dict [str , Any ],
102152 ) -> None :
103- """Reduce one frame's covariance outputs into shared running means."""
153+ """Reduce one frame covariance payload into parent accumulators.
154+
155+ Args:
156+ shared_data: Shared workflow data containing covariance accumulators.
157+ frame_out: Frame covariance payload with force, torque, and optional
158+ force-torque sections.
159+ """
104160 self ._reduce_force_and_torque (shared_data , frame_out )
105161 self ._reduce_forcetorque (shared_data , frame_out )
106162
@@ -109,7 +165,12 @@ def merge_chunk_partial(
109165 shared_data : dict [str , Any ],
110166 partial : CovarianceChunkPartial ,
111167 ) -> None :
112- """Merge a compact worker covariance partial into parent accumulators."""
168+ """Merge a worker covariance partial into parent accumulators.
169+
170+ Args:
171+ shared_data: Shared workflow data containing covariance accumulators.
172+ partial: Compact covariance partial returned by a worker frame chunk.
173+ """
113174 self ._merge_force_and_torque_partial (shared_data , partial )
114175 self ._merge_forcetorque_partial (shared_data , partial )
115176
@@ -118,7 +179,14 @@ def reduce_frame_map_output(
118179 shared_data : dict [str , Any ],
119180 frame_out : dict [str , Any ],
120181 ) -> None :
121- """Reduce one serial frame's complete MAP output into parent shared_data."""
182+ """Reduce a complete serial MAP output.
183+
184+ Args:
185+ shared_data: Shared workflow data containing covariance and neighbour
186+ accumulators.
187+ frame_out: MAP output containing optional ``covariance`` and ``neighbors``
188+ entries.
189+ """
122190 covariance = frame_out .get ("covariance" )
123191 if covariance is not None :
124192 self .reduce_frame_output (shared_data , covariance )
@@ -132,6 +200,13 @@ def _merge_force_and_torque_partial(
132200 shared_data : dict [str , Any ],
133201 partial : CovarianceChunkPartial ,
134202 ) -> None :
203+ """Merge chunk force and torque means into parent accumulators.
204+
205+ Args:
206+ shared_data: Shared workflow data containing force/torque accumulators,
207+ frame counts, and ``group_id_to_index``.
208+ partial: Worker covariance partial with force, torque, and count mappings.
209+ """
135210 f_cov = shared_data ["force_covariances" ]
136211 t_cov = shared_data ["torque_covariances" ]
137212 counts = shared_data ["frame_counts" ]
@@ -183,6 +258,13 @@ def _merge_forcetorque_partial(
183258 shared_data : dict [str , Any ],
184259 partial : CovarianceChunkPartial ,
185260 ) -> None :
261+ """Merge chunk force-torque block means into parent accumulators.
262+
263+ Args:
264+ shared_data: Shared workflow data containing force-torque accumulators,
265+ force-torque counts, and ``group_id_to_index``.
266+ partial: Worker covariance partial with force-torque matrices and counts.
267+ """
186268 ft_cov = shared_data ["forcetorque_covariances" ]
187269 ft_counts = shared_data ["forcetorque_counts" ]
188270 gid2i = shared_data ["group_id_to_index" ]
@@ -210,6 +292,13 @@ def _reduce_force_and_torque(
210292 shared_data : dict [str , Any ],
211293 frame_out : dict [str , Any ],
212294 ) -> None :
295+ """Reduce frame force and torque matrices into running means.
296+
297+ Args:
298+ shared_data: Shared workflow data containing force/torque accumulators,
299+ frame counts, and ``group_id_to_index``.
300+ frame_out: Frame covariance payload with ``force`` and ``torque`` sections.
301+ """
213302 f_cov = shared_data ["force_covariances" ]
214303 t_cov = shared_data ["torque_covariances" ]
215304 counts = shared_data ["frame_counts" ]
@@ -266,6 +355,14 @@ def _reduce_forcetorque(
266355 shared_data : dict [str , Any ],
267356 frame_out : dict [str , Any ],
268357 ) -> None :
358+ """Reduce frame force-torque matrices into running means.
359+
360+ Args:
361+ shared_data: Shared workflow data containing force-torque accumulators,
362+ force-torque counts, and ``group_id_to_index``.
363+ frame_out: Frame covariance payload that may contain a ``forcetorque``
364+ section.
365+ """
269366 if "forcetorque" not in frame_out :
270367 return
271368
0 commit comments