File size: 9,032 Bytes
8648e23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Unsupervised embedding-space analysis for the trained BaroqueHarmonyLSTM.

Functions
---------
extract_chord_embeddings          Pull chord embedding vectors from a checkpoint.
reduce_dimensions                 PCA / UMAP 2-D projection.
cluster_embeddings                K-Means for multiple k values.
compute_cluster_purity            Purity + ARI against ground-truth function labels.
compute_tonal_distance_correlation  Circle-of-fifths vs. cosine distance Mantel test.
"""

from __future__ import annotations

import warnings
from typing import Dict, List, Tuple

import numpy as np
import torch
from scipy.spatial.distance import pdist
from scipy.stats import pearsonr
from sklearn.cluster import KMeans
from sklearn.decomposition import PCA
from sklearn.metrics import adjusted_rand_score
from sklearn.preprocessing import StandardScaler

from src.data.encoder import CHORD_OFFSET, CHORD_VOCAB_SIZE, Encoder
from src.models.baseline_lstm import BaroqueHarmonyLSTM

# Chromatic root index β†’ note name (enharmonic spellings chosen to match encoder labels)
_ROOT_NAMES: List[str] = [
    'C', 'C#', 'D', 'Eb', 'E', 'F', 'F#', 'G', 'Ab', 'A', 'Bb', 'B'
]


# ---------------------------------------------------------------------------
# 1. Extract embeddings
# ---------------------------------------------------------------------------

def extract_chord_embeddings(
    checkpoint_path: str,
    encoder: Encoder,
) -> Tuple[np.ndarray, List[str], List[int], List[str]]:
    """
    Load a checkpoint and return the chord embedding vectors with metadata.

    Parameters
    ----------
    checkpoint_path : str
        Path to a saved ``model.state_dict()`` file.
    encoder : Encoder
        An Encoder whose ``build_chord_function_map`` has already been called
        so that ``token_function`` returns majority-vote labels.

    Returns
    -------
    embeddings : np.ndarray, shape (60, 64)
        One row per chord token (tokens CHORD_OFFSET … CHORD_OFFSET+59).
    token_labels : list of str
        Human-readable chord names, e.g. ``"C:major"``, ``"D:minor"``.
    function_ids : list of int
        Majority-vote harmonic function (0 = T, 1 = PD, 2 = D) for each token.
    quality_labels : list of str
        Canonical quality string for each token.
    """
    model = BaroqueHarmonyLSTM()
    state = torch.load(checkpoint_path, map_location='cpu', weights_only=True)
    model.load_state_dict(state)
    model.eval()

    # Full embedding matrix: (vocab_size, embed_dim)
    weight: np.ndarray = model.embedding.weight.detach().cpu().numpy()

    # Chord tokens occupy CHORD_OFFSET … CHORD_OFFSET + CHORD_VOCAB_SIZE βˆ’ 1
    chord_rows = weight[CHORD_OFFSET: CHORD_OFFSET + CHORD_VOCAB_SIZE]  # (60, 64)

    token_labels: List[str] = []
    function_ids: List[int] = []
    quality_labels: List[str] = []

    for offset in range(CHORD_VOCAB_SIZE):
        tok = CHORD_OFFSET + offset
        root, quality = encoder.token_to_chord(tok)
        token_labels.append(f"{_ROOT_NAMES[root]}:{quality}")
        function_ids.append(encoder.token_function(tok))
        quality_labels.append(quality)

    return chord_rows, token_labels, function_ids, quality_labels


# ---------------------------------------------------------------------------
# 2. Dimensionality reduction
# ---------------------------------------------------------------------------

def reduce_dimensions(
    embeddings: np.ndarray,
    method: str = 'pca',
    n_components: int = 2,
) -> Tuple[np.ndarray, object]:
    """
    Project embeddings to *n_components* dimensions.

    Always standardises with ``StandardScaler`` before reducing.

    Parameters
    ----------
    embeddings : np.ndarray, shape (n, d)
    method : {'pca', 'umap'}
        ``'umap'`` falls back to PCA with a warning if *umap-learn* is not installed.
    n_components : int
        Target dimensionality (default 2).

    Returns
    -------
    reduced : np.ndarray, shape (n, n_components)
    reducer : fitted reducer object (PCA or UMAP instance)
    """
    scaler = StandardScaler()
    scaled = scaler.fit_transform(embeddings)

    if method == 'umap':
        try:
            from umap import UMAP  # type: ignore
            reducer: object = UMAP(n_components=n_components, random_state=42)
        except ImportError:
            warnings.warn(
                "umap-learn is not installed; falling back to PCA.",
                ImportWarning,
                stacklevel=2,
            )
            method = 'pca'

    if method == 'pca':
        reducer = PCA(n_components=n_components, random_state=42)

    reduced: np.ndarray = reducer.fit_transform(scaled)  # type: ignore[union-attr]
    return reduced, reducer


# ---------------------------------------------------------------------------
# 3. Clustering
# ---------------------------------------------------------------------------

def cluster_embeddings(
    embeddings: np.ndarray,
    k_values: List[int] = None,
) -> Dict[int, np.ndarray]:
    """
    Run K-Means for each k in *k_values* and return label arrays.

    Parameters
    ----------
    embeddings : np.ndarray, shape (n, d)
    k_values : list of int
        Number-of-clusters values to try. Defaults to ``[3, 5, 7]``.

    Returns
    -------
    dict mapping k β†’ cluster label array of shape (n,)
    """
    if k_values is None:
        k_values = [3, 5, 7]

    results: Dict[int, np.ndarray] = {}
    for k in k_values:
        km = KMeans(n_clusters=k, random_state=42, n_init=10)
        results[k] = km.fit_predict(embeddings)
    return results


# ---------------------------------------------------------------------------
# 4. Cluster purity
# ---------------------------------------------------------------------------

def compute_cluster_purity(
    cluster_labels: np.ndarray,
    true_labels: np.ndarray,
) -> Tuple[float, dict]:
    """
    Compute cluster purity and adjusted Rand index.

    Purity = (1/N) Γ— Ξ£_k  max_c |C_k ∩ T_c|

    Parameters
    ----------
    cluster_labels : np.ndarray, shape (n,)
        K-Means (or any) cluster assignments.
    true_labels : np.ndarray, shape (n,)
        Ground-truth class labels (ints).

    Returns
    -------
    overall_purity : float in [0, 1]
    per_cluster_breakdown : dict
        ``{cluster_id: {'dominant_label': int, 'purity': float, 'size': int}}``
        plus ``'_ari'`` key containing the adjusted Rand index.
    """
    n = len(cluster_labels)
    total_correct = 0
    breakdown: dict = {}

    for cid in np.unique(cluster_labels):
        mask = cluster_labels == cid
        members = true_labels[mask]
        n_classes = int(true_labels.max()) + 1
        counts = np.bincount(members, minlength=n_classes)
        dominant = int(np.argmax(counts))
        correct = int(counts[dominant])
        total_correct += correct
        breakdown[int(cid)] = {
            'dominant_label': dominant,
            'purity': correct / len(members),
            'size': int(len(members)),
        }

    overall_purity = total_correct / n
    ari = float(adjusted_rand_score(true_labels, cluster_labels))
    breakdown['_ari'] = ari
    return overall_purity, breakdown


# ---------------------------------------------------------------------------
# 5. Tonal distance correlation (Mantel test approximation)
# ---------------------------------------------------------------------------

def compute_tonal_distance_correlation(
    embeddings: np.ndarray,
    token_labels: List[str],
) -> Tuple[float, float]:
    """
    Correlate pairwise cosine distances in embedding space with pairwise
    circle-of-fifths distances between chord roots (Mantel test approximation).

    If the model independently recovered tonal structure the correlation
    should be positive and significant.

    Parameters
    ----------
    embeddings : np.ndarray, shape (n, d)
    token_labels : list of str
        Labels in ``"RootName:quality"`` format, as returned by
        :func:`extract_chord_embeddings`.

    Returns
    -------
    r : float  β€” Pearson correlation coefficient
    p : float  β€” two-tailed p-value
    """
    # Parse chromatic root index from label (e.g. "C#:major" β†’ 1, "Eb:minor" β†’ 3)
    roots = np.array(
        [_ROOT_NAMES.index(lbl.split(':')[0]) for lbl in token_labels],
        dtype=int,
    )

    # Map chromatic root β†’ circle-of-fifths position (0–11)
    # C=0, G=1, D=2, A=3, E=4, B=5, F#=6, Db=7, Ab=8, Eb=9, Bb=10, F=11
    fifths_pos = (roots * 7) % 12

    # Pairwise circle-of-fifths distance: circular distance on a 12-element ring
    n = len(token_labels)
    cof_dists: List[float] = []
    for i in range(n):
        for j in range(i + 1, n):
            d = abs(int(fifths_pos[i]) - int(fifths_pos[j]))
            cof_dists.append(float(min(d, 12 - d)))

    cof_arr = np.array(cof_dists)

    # Pairwise cosine distance in embedding space
    emb_dists = pdist(embeddings, metric='cosine')

    r, p = pearsonr(emb_dists, cof_arr)
    return float(r), float(p)