|
| 1 | +import numpy as np |
| 2 | +import pytest |
| 3 | +from pyannote.core import SlidingWindow, SlidingWindowFeature |
| 4 | + |
| 5 | +from diart.blocks.aggregation import ( |
| 6 | + AggregationStrategy, |
| 7 | + HammingWeightedAverageStrategy, |
| 8 | + FirstOnlyStrategy, |
| 9 | + AverageStrategy, |
| 10 | + DelayedAggregation, |
| 11 | +) |
| 12 | + |
| 13 | + |
| 14 | +def test_strategy_build(): |
| 15 | + strategy = AggregationStrategy.build("mean") |
| 16 | + assert isinstance(strategy, AverageStrategy) |
| 17 | + |
| 18 | + strategy = AggregationStrategy.build("hamming") |
| 19 | + assert isinstance(strategy, HammingWeightedAverageStrategy) |
| 20 | + |
| 21 | + strategy = AggregationStrategy.build("first") |
| 22 | + assert isinstance(strategy, FirstOnlyStrategy) |
| 23 | + |
| 24 | + with pytest.raises(Exception): |
| 25 | + AggregationStrategy.build("invalid") |
| 26 | + |
| 27 | + |
| 28 | +def test_aggregation(): |
| 29 | + duration = 5 |
| 30 | + frames = 500 |
| 31 | + step = 0.5 |
| 32 | + speakers = 2 |
| 33 | + start_time = 10 |
| 34 | + resolution = duration / frames |
| 35 | + |
| 36 | + dagg1 = DelayedAggregation(step=step, latency=2, strategy="mean") |
| 37 | + dagg2 = DelayedAggregation(step=step, latency=2, strategy="hamming") |
| 38 | + dagg3 = DelayedAggregation(step=step, latency=2, strategy="first") |
| 39 | + |
| 40 | + for dagg in [dagg1, dagg2, dagg3]: |
| 41 | + assert dagg.num_overlapping_windows == 4 |
| 42 | + |
| 43 | + buffers = [ |
| 44 | + SlidingWindowFeature( |
| 45 | + np.random.rand(frames, speakers), |
| 46 | + SlidingWindow( |
| 47 | + start=(i + start_time) * step, duration=resolution, step=resolution |
| 48 | + ), |
| 49 | + ) |
| 50 | + for i in range(dagg1.num_overlapping_windows) |
| 51 | + ] |
| 52 | + |
| 53 | + for dagg in [dagg1, dagg2, dagg3]: |
| 54 | + assert dagg(buffers).data.shape == (51, 2) |
0 commit comments