Commit e1949e5
fix(kuramoto): canonicalise signed-community labels for recalibration stability (#226)
* fix(kuramoto): canonicalise signed-community labels for recalibration stability
The inverse-problem stack feeds ``signed_communities`` a coupling matrix
``K`` that carries estimator noise. Two sources of label ambiguity
previously caused partition-preserving perturbations to *swap* cluster
ids between calls:
* ``np.linalg.eigh`` returns principal eigenvectors with arbitrary
sign; the ``v >= 0`` split then sends different halves to
``new_id = max + 1`` across runs.
* The raw recursive output has no documented ordering, so even with
a stable split the ids depend on split-interleaving order.
Because ``EmergentMetrics.R_cluster`` keys and the
``NetworkKuramotoFeature`` feature vocabulary ``kuramoto_R_cluster_{c}``
are bound to these ids across batch recalibrations, the ambiguity
corrupted downstream per-cluster features: ``R_cluster[0]`` could mean
"largest cluster" after one recalibration and "smallest cluster" after
the next.
Fix:
* ``_canonical_eigvec_sign`` pins the eigenvector sign by requiring
the max-magnitude coordinate to be non-negative (deterministic for
every non-zero ``v``).
* ``_canonicalize_labels`` relabels the partition by (descending
size, ascending smallest-member-index) so the biggest community
always has id 0 and labels are a dense 0..C-1 range.
Tests:
* 5 new unit tests in ``TestSignedCommunities`` for dense range,
biggest-first ordering, perturbation stability, sign-ambiguity
stability, and node-permutation invariance.
* 2 new witness tests in T24: a Hypothesis property over random
signed matrices (40 examples) witnessing that partition-preserving
perturbations yield bit-identical labels, plus a biggest-first
ordering regression witness.
228 + 7 = 235 passed, 1 skipped (unchanged). mypy --strict clean on
``core/kuramoto/metrics.py``.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
* style: black-format the 2 new test files
CI's python-quality gate runs both ruff and black; ruff format
produced clean output but black's line-length defaults differ.
Pure formatting, no logic change.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>1 parent 84d728d commit e1949e5
3 files changed
Lines changed: 375 additions & 12 deletions
File tree
- core/kuramoto
- tests/unit
- core
- physics
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
176 | 176 | | |
177 | 177 | | |
178 | 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 | + | |
179 | 237 | | |
180 | 238 | | |
181 | 239 | | |
| |||
234 | 292 | | |
235 | 293 | | |
236 | 294 | | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
237 | 308 | | |
238 | 309 | | |
239 | 310 | | |
| |||
259 | 330 | | |
260 | 331 | | |
261 | 332 | | |
262 | | - | |
263 | | - | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
264 | 339 | | |
265 | 340 | | |
266 | 341 | | |
| |||
280 | 355 | | |
281 | 356 | | |
282 | 357 | | |
283 | | - | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
284 | 364 | | |
285 | 365 | | |
286 | 366 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
158 | 158 | | |
159 | 159 | | |
160 | 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 | + | |
161 | 330 | | |
162 | 331 | | |
163 | 332 | | |
| |||
374 | 543 | | |
375 | 544 | | |
376 | 545 | | |
377 | | - | |
378 | | - | |
379 | | - | |
| 546 | + | |
380 | 547 | | |
381 | 548 | | |
382 | 549 | | |
| |||
467 | 634 | | |
468 | 635 | | |
469 | 636 | | |
470 | | - | |
471 | | - | |
472 | | - | |
| 637 | + | |
473 | 638 | | |
474 | 639 | | |
475 | 640 | | |
| |||
511 | 676 | | |
512 | 677 | | |
513 | 678 | | |
514 | | - | |
515 | | - | |
516 | | - | |
| 679 | + | |
517 | 680 | | |
518 | 681 | | |
519 | 682 | | |
| |||
0 commit comments