Commit a2cee07
[Stack 4/25] Two-level hierarchical clustering matching Clojure architecture (#2431)
* Implement two-level hierarchical clustering matching Clojure architecture
Level 1: Participants → ~100 base clusters
Level 2: Base clusters → 2-5 group clusters (silhouette-based k selection)
Key changes:
- Add sklearn-based kmeans with first-k initialization (matching Clojure)
- Add silhouette coefficient calculation for optimal k selection
- Implement participant filtering (in-conv threshold logic)
- Add fold/unfold utilities for hierarchical cluster storage
- Update tests to unfold and compare hierarchical structures
- Document Clojure two-level architecture and non-determinism issue
This implements the core two-level clustering but does not yet include
incremental clustering (warm-start from previous state).
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
* Fix two-level clustering edge cases and update tests
Bug fixes from review:
- clusters.py: adjust k down when fewer distinct points than requested
- conversation.py: group_clusters members are base-cluster IDs (not participants)
when only 1 base cluster; convert pandas Index to list for JSON serialization
- pca.py: fallback comps shape uses min(n_comps, n_cols) not min(n_comps, 2)
Test fixes:
- test_clusters: init_clusters returns empty members with two-level clustering
- test_conversation: use 10 comments to meet vote threshold
- test_edge_cases: group_repness is {} (not {0: []}) when no clusters
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
* Mark known Clojure discrepancies as xfail and re-record golden snapshots
- test_basic_outputs: xfail for D9/D5/D7 (wrong z-score thresholds and
repness formulas produce empty comment_repness)
- test_group_clustering: xfail for D2/D3 (wrong participant threshold
and missing k-smoother produce different cluster counts)
- test_comment_priorities: update xfail reason to reference D12
- Re-record golden snapshots after two-level clustering changes
Test baseline: 11 passed, 6 xfailed, 0 failures.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
* Unfold group-cluster base-cluster IDs to participant IDs for downstream consumers
Group clusters store base-cluster IDs in 'members' (matching Clojure's
two-level clustering architecture), but downstream functions (conv_repness,
participant_stats, group_votes) need participant IDs to join against the
vote matrix.
Add _unfolded_group_clusters() helper (equivalent to Clojure's
clusters/group-members) and use it in all 5 call sites:
- _compute_repness
- _compute_participant_info
- _compute_group_votes
- to_dict group-votes
- to_dynamo_dict group_votes
Also re-record golden snapshots and remove xfail from test_repness_structure
(now passes with correct unfolding).
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
* Tighten Clojure comparison tests and fix outdated docstrings
- test_group_clustering: unfold Python clusters via _unfolded_group_clusters()
(both sides now use two-level clustering), tighten thresholds to 0.99
Jaccard / 0.01 distribution tolerance, require overall_match
- test_comment_priorities: require exact match (1e-6 tolerance) for all
comment IDs instead of 70% at 20% tolerance
- clojure_comparer: fix docstring to reflect Python also uses two-level
clustering
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
* Fix serialization outputting base-cluster IDs instead of participant IDs
to_dict(), get_full_data(), and to_dynamo_dict() were writing
self.group_clusters directly, whose 'members' contain base-cluster IDs
(integers 0..N). Downstream consumers (group_data.py, Clojure compat,
client apps) expect participant IDs.
The internal helper _unfolded_group_clusters() already existed and was
used for repness/participant_info/group_votes computation, but the five
serialization sites were missed.
Fix all five sites to unfold before serializing:
- to_dict(): group-clusters, group_clusters, base-clusters
- get_full_data(): group_clusters
- to_dynamo_dict(): base_clusters / group_clusters
Add test_serialization_unfolding.py (TDD: 6 tests fail on the bug,
8/8 pass after fix) using real recompute() pipeline output.
Re-record golden snapshots to reflect the corrected output.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
* Fix missing Set import in conversation.py
Added Set to the typing imports — it was used in the
_get_in_conv_participants return type annotation but never imported,
causing NameError on CI.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
---------
Co-authored-by: Claude Sonnet 4.5 <noreply@anthropic.com>1 parent 1f3113f commit a2cee07
16 files changed
Lines changed: 45338 additions & 29052 deletions
File tree
- delphi
- docs
- polismath
- conversation
- pca_kmeans_rep
- regression
- real_data
- r4tykwac8thvzv35jrn53-biodiversity
- r6vbnhffkxbd7ifmfbdrd-vw
- tests
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 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 | + | |
0 commit comments