File size: 1,478 Bytes
37a9a36
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import pytest
import numpy as np
from openmusic_analysis.worker.preprocessing.audio import select_temporal_windows, aggregate_global_embedding

def test_select_temporal_windows():
    # Test short duration
    windows = select_temporal_windows(5.0)
    assert len(windows) == 1
    assert windows[0] == (0, 5.0)

    # Test exact 10s duration
    windows = select_temporal_windows(10.0)
    assert len(windows) == 1
    assert windows[0] == (0, 10.0)

    # Test longer duration
    windows = select_temporal_windows(15.0)
    assert len(windows) == 2
    assert windows[0] == (0, 10.0)
    assert windows[1] == (5.0, 15.0)

    # Test max windows constraint (e.g. 24 chunks limit by config)
    windows = select_temporal_windows(300.0)
    assert len(windows) <= 24

def test_aggregate_global_embedding():
    # Two identical embeddings should average out to same and remain normalized
    embs = [
        np.array([1.0, 0.0, 0.0], dtype=np.float32),
        np.array([1.0, 0.0, 0.0], dtype=np.float32),
    ]
    global_emb = aggregate_global_embedding(embs)
    np.testing.assert_allclose(global_emb, [1.0, 0.0, 0.0])

    # Orthogonal vectors should normalize to 1/sqrt(2)
    embs = [
        np.array([1.0, 0.0], dtype=np.float32),
        np.array([0.0, 1.0], dtype=np.float32),
    ]
    global_emb = aggregate_global_embedding(embs)
    np.testing.assert_allclose(global_emb, [0.70710677, 0.70710677], rtol=1e-5)
    assert np.isclose(np.linalg.norm(global_emb), 1.0)