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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
//! Divergence pins for `AgglomerativeClustering` dendrogram / label numbering
//! (`ferrolearn-cluster/src/agglomerative.rs`) vs scikit-learn 1.5.2 / scipy
//! `sklearn/cluster/_agglomerative.py`
//! (`class AgglomerativeClustering(ClusterMixin, BaseEstimator)`, `:781`).
//!
//! Pins the two NOT-STARTED REQs sharing root cause #938 (truncated reused-slot
//! tree + ascending-slot relabel vs sklearn's full scipy dendrogram + `_hc_cut`):
//!
//! * REQ-6 — `children_` FULL-DENDROGRAM format. sklearn delegates the default
//! unstructured ward case to scipy: `ward_tree` returns
//! `scipy.cluster.hierarchy.ward(X)[:, :2]` (`_agglomerative.py:314`); the
//! result is shape `(n_samples-1, 2)` with internal-node IDs `>= n_samples`
//! (leaves `0..n-1`; the i-th merge in scipy output-row order creates node
//! `n+i`). ferrolearn's `children_` is instead length `n_samples-n_clusters`
//! of reused merged-into-slot pairs (`fn agglomerate`: `children.push((ci,cj))`),
//! a TRUNCATED tree with neither the full length nor the `>= n_samples` IDs.
//!
//! * REQ-7 — `labels_` ABSOLUTE numbering via `_hc_cut`. sklearn numbers
//! `labels_` with `_hc_cut(n_clusters, children_, n_leaves)`
//! (`_agglomerative.py:731-778`), a negated-max-heap split of the top of the
//! dendrogram. ferrolearn relabels by ascending surviving-slot order (a
//! `HashMap` in `fn agglomerate`), giving the SAME partition (REQ-1, which
//! ships) but PERMUTED integer labels.
//!
//! Expected values come from the LIVE scipy/sklearn 1.5.2 oracle (R-CHAR-3,
//! R-DEV-3 exact-output) — NEVER literal-copied from the ferrolearn side. The
//! exact `python3 -c` command + its output are recorded in each doc comment.
//!
//! Both are marked `#[ignore]` (tracking #938) so the orchestrator suite stays
//! green; run with `--ignored` to confirm they FAIL against current code.
use ;
use Fit;
use Array2;
/// Small generic 6-point fixture, no degenerate ties. Three loose pairs/triples
/// arranged so the scipy merge order and the sklearn `_hc_cut` label numbering
/// are both well-defined and (for `labels_`) DIFFER from ferrolearn's
/// ascending-slot numbering.
/// REQ-6: `children_` must be the FULL scipy dendrogram — shape
/// `(n_samples-1, 2)` with internal-node IDs `>= n_samples` — equal to
/// `scipy.cluster.hierarchy.ward(X)[:, :2]`.
///
/// Live scipy 1.17.1 / sklearn 1.5.2 oracle (system python3):
/// ```text
/// python3 -c "import numpy as np; from scipy.cluster.hierarchy import ward; \
/// X=np.array([[0.,0.],[0.2,0.1],[0.9,1.1],[3.,3.2],[3.3,3.],[6.,0.2]]); \
/// print(ward(X)[:, :2].astype(int))"
/// # [[0 1]
/// # [3 4]
/// # [2 6]
/// # [5 7]
/// # [8 9]]
/// ```
/// n_samples = 6, so the full dendrogram has n_samples-1 = 5 rows, and IDs
/// 6,7,8,9 (>= n_samples) name the internal merge nodes.
///
/// ferrolearn-actual (current code, truncated reused-slot tree, n_clusters=2):
/// `children_` = `[(0,1),(3,4),(0,2),(3,5)]` — only 4 rows, IDs all < 6.
/// REQ-7: `labels_` must use sklearn's ABSOLUTE `_hc_cut` numbering, i.e. equal
/// `AgglomerativeClustering(n_clusters=2, linkage='ward').fit(X).labels_` as
/// INTEGERS (not merely up-to-permutation). The PARTITION already matches
/// (REQ-1, SHIPPED); this pins ONLY the integer label values.
///
/// Live sklearn 1.5.2 oracle (system python3):
/// ```text
/// python3 -c "import numpy as np; from sklearn.cluster import AgglomerativeClustering; \
/// X=np.array([[0.,0.],[0.2,0.1],[0.9,1.1],[3.,3.2],[3.3,3.],[6.,0.2]]); \
/// print(AgglomerativeClustering(n_clusters=2,linkage='ward').fit(X).labels_.tolist())"
/// # [1, 1, 1, 0, 0, 0]
/// ```
/// The `_hc_cut` heap split numbers the {0,1,2} group as label 1 and the
/// {3,4,5} group as label 0.
///
/// ferrolearn-actual (current code, ascending-slot relabel): `[0, 0, 0, 1, 1, 1]`
/// — SAME partition, but the two integer labels are SWAPPED.
// ─────────────────────────────────────────────────────────────────────────────
// REQ-6 / REQ-7 bit-exact parity across all four linkages, live scipy/sklearn.
//
// All expected values are the LIVE scipy 1.17.1 / sklearn 1.5.2 oracle, computed
// in-test from `python3 -c` (R-CHAR-3 — NEVER copied from ferrolearn). The exact
// command + its raw output is recorded in each helper's doc comment.
// ─────────────────────────────────────────────────────────────────────────────
/// Larger 10-point generic fixture (no degenerate ties).
/// REQ-6: `children_` EXACT-equals
/// `scipy.cluster.hierarchy.linkage(X6, method, 'euclidean')[:, :2]` for the
/// nn-chain linkages (ward/complete/average), which sklearn routes verbatim
/// through `hierarchy.linkage` (`_agglomerative.py:314`/`:586`).
///
/// Live scipy 1.17.1 oracle:
/// ```text
/// python3 -c "import numpy as np; from scipy.cluster.hierarchy import linkage; \
/// X=np.array([[0.,0.],[0.2,0.1],[0.9,1.1],[3.,3.2],[3.3,3.],[6.,0.2]]); \
/// [print(m, linkage(X,method=m,metric='euclidean')[:, :2].astype(int).tolist()) \
/// for m in ['ward','complete','average']]"
/// # ward [[0,1],[3,4],[2,6],[5,7],[8,9]]
/// # complete [[0,1],[3,4],[2,6],[5,7],[8,9]]
/// # average [[0,1],[3,4],[2,6],[7,8],[5,9]]
/// ```
/// REQ-6 (single linkage, R-DEV-7): for `single`, sklearn does NOT delegate to
/// `scipy.cluster.hierarchy.linkage(method='single')`; it uses its own
/// `mst_linkage_core` + `_single_linkage_label` path
/// (`_agglomerative.py:567-584`, `_hierarchical_fast.pyx`), whose
/// `_single_linkage_label` emits each merge as `(left_root, right_root)` in MST
/// order WITHOUT scipy's min/max column swap. The CONTRACT for the fitted
/// `children_` attribute is therefore sklearn's value (which ferrolearn
/// reproduces bit-exact), NOT `scipy.linkage`'s column ordering — the two differ
/// in pair order for single linkage.
///
/// Live sklearn 1.5.2 oracle (the actual `children_` attribute):
/// ```text
/// python3 -c "import numpy as np; from sklearn.cluster import AgglomerativeClustering as AC; \
/// X=np.array([[0.,0.],[0.2,0.1],[0.9,1.1],[3.,3.2],[3.3,3.],[6.,0.2]]); \
/// print(AC(n_clusters=2,linkage='single').fit(X).children_.tolist())"
/// # [[0,1],[3,4],[6,2],[8,7],[9,5]]
/// ```
/// REQ-6: `children_` EXACT-equals scipy linkage `[:, :2]` for the nn-chain
/// linkages on the 10-point fixture; single is checked against sklearn's own
/// `children_` (see [`children_exact_sklearn_single_10pt`], the R-DEV-7 path).
///
/// Live scipy 1.17.1 oracle:
/// ```text
/// python3 -c "import numpy as np; from scipy.cluster.hierarchy import linkage; \
/// X=np.array([[0.,0.],[1.,0.2],[0.3,1.],[5.,5.],[5.4,4.8],[4.7,5.5],[10.,0.],[9.6,0.5],[2.,8.],[2.5,7.6]]); \
/// [print(m, linkage(X,method=m,metric='euclidean')[:, :2].astype(int).tolist()) \
/// for m in ['ward','complete','average']]"
/// # ward [[3,4],[8,9],[6,7],[5,10],[0,1],[2,14],[11,13],[15,16],[12,17]]
/// # complete [[3,4],[8,9],[6,7],[5,10],[0,1],[2,14],[11,13],[15,16],[12,17]]
/// # average [[3,4],[8,9],[6,7],[5,10],[0,1],[2,14],[11,13],[15,16],[12,17]]
/// ```
/// REQ-6 (single linkage, R-DEV-7): `children_` EXACT-equals sklearn's own
/// `AgglomerativeClustering(linkage='single').children_` (the `mst_linkage_core`
/// + `_single_linkage_label` path) on the 10-point fixture.
///
/// Live sklearn 1.5.2 oracle:
/// ```text
/// python3 -c "import numpy as np; from sklearn.cluster import AgglomerativeClustering as AC; \
/// X=np.array([[0.,0.],[1.,0.2],[0.3,1.],[5.,5.],[5.4,4.8],[4.7,5.5],[10.,0.],[9.6,0.5],[2.,8.],[2.5,7.6]]); \
/// print(AC(n_clusters=2,linkage='single').fit(X).children_.tolist())"
/// # [[3,4],[10,5],[9,8],[7,6],[0,1],[14,2],[11,12],[16,13],[15,17]]
/// ```
/// REQ-7: `labels_` EXACT-equals
/// `sklearn.cluster.AgglomerativeClustering(n_clusters=k, linkage=…).fit(X6).labels_`
/// for k in {2,3} and all four linkages.
///
/// Live sklearn 1.5.2 oracle:
/// ```text
/// python3 -c "import numpy as np; from sklearn.cluster import AgglomerativeClustering as AC; \
/// X=np.array([[0.,0.],[0.2,0.1],[0.9,1.1],[3.,3.2],[3.3,3.],[6.,0.2]]); \
/// [print(m,k,AC(n_clusters=k,linkage=m).fit(X).labels_.tolist()) \
/// for m in ['ward','complete','average','single'] for k in (2,3)]"
/// # ward 2 [1,1,1,0,0,0] ward 3 [0,0,0,1,1,2]
/// # complete 2 [1,1,1,0,0,0] complete 3 [0,0,0,1,1,2]
/// # average 2 [0,0,0,0,0,1] average 3 [0,0,0,2,2,1]
/// # single 2 [0,0,0,0,0,1] single 3 [0,0,0,2,2,1]
/// ```
/// REQ-7: `labels_` EXACT-equals sklearn `.fit(X10).labels_` for k in {2,3} and
/// all four linkages.
///
/// Live sklearn 1.5.2 oracle:
/// ```text
/// python3 -c "import numpy as np; from sklearn.cluster import AgglomerativeClustering as AC; \
/// X=np.array([[0.,0.],[1.,0.2],[0.3,1.],[5.,5.],[5.4,4.8],[4.7,5.5],[10.,0.],[9.6,0.5],[2.,8.],[2.5,7.6]]); \
/// [print(m,k,AC(n_clusters=k,linkage=m).fit(X).labels_.tolist()) \
/// for m in ['ward','complete','average','single'] for k in (2,3)]"
/// # ward 2 [0,0,0,0,0,0,1,1,0,0] ward 3 [2,2,2,0,0,0,1,1,0,0]
/// # complete 2 [0,0,0,0,0,0,1,1,0,0] complete 3 [2,2,2,0,0,0,1,1,0,0]
/// # average 2 [0,0,0,0,0,0,1,1,0,0] average 3 [2,2,2,0,0,0,1,1,0,0]
/// # single 2 [1,1,1,0,0,0,0,0,0,0] single 3 [1,1,1,0,0,0,2,2,0,0]
/// ```