from __future__ import annotations import numpy as np import pytest import torch from gmnet.spectral.metrics import measure_spectrum from gmnet.spectral.streaming import RadialPSDAccumulator, torch_fft_lowpass from gmnet.spectral.transforms import ( dct_coefficients, dct_reconstruct, fft_coefficients, fft_reconstruct, hard_band_reconstruction, haar_decompose2d, haar_reconstruct2d, relative_l2_error, ) @pytest.mark.parametrize("shape", [(2, 3, 32, 32), (1, 1, 16, 24)]) def test_transforms_are_numerically_invertible(shape: tuple[int, ...]) -> None: x = np.random.default_rng(7).standard_normal(shape) fft_result = fft_reconstruct(fft_coefficients(x)) dct_result = dct_reconstruct(dct_coefficients(x)) haar_result = haar_reconstruct2d(haar_decompose2d(x, levels=3)) assert relative_l2_error(x, fft_result) < 1e-12 assert relative_l2_error(x, dct_result) < 1e-12 assert relative_l2_error(x, haar_result) < 1e-12 assert relative_l2_error( x, hard_band_reconstruction(x, "fft", (0.0, 0.25, 0.5, 1.0)) ) < 1e-12 assert relative_l2_error( x, hard_band_reconstruction(x, "dct", (0.0, 0.25, 0.5, 1.0)) ) < 1e-12 def test_parseval_energy_is_preserved() -> None: x = np.random.default_rng(11).standard_normal((2, 2, 32, 32)) reference = float(np.square(x).sum()) assert np.isclose(np.square(np.abs(fft_coefficients(x))).sum(), reference) assert np.isclose(np.square(dct_coefficients(x)).sum(), reference) wavelet = haar_decompose2d(x, levels=3) wavelet_energy = np.square(wavelet.approximation).sum() + sum( np.square(detail).sum() for level in wavelet.details for detail in level ) assert np.isclose(wavelet_energy, reference) def test_high_frequency_tone_scores_above_low_frequency_tone() -> None: grid = np.arange(64) low = np.sin(2 * np.pi * 2 * grid / 64)[None, None, None, :] high = np.sin(2 * np.pi * 24 * grid / 64)[None, None, None, :] low = np.broadcast_to(low, (1, 1, 64, 64)) high = np.broadcast_to(high, (1, 1, 64, 64)) for method in ("fft", "dct", "haar"): assert measure_spectrum(high, method).frequency_auc > measure_spectrum( low, method ).frequency_auc def test_invalid_haar_shape_is_rejected() -> None: with pytest.raises(ValueError, match="divisible"): haar_decompose2d(np.ones((10, 10)), levels=2) def test_streaming_psd_orders_low_and_high_tones() -> None: grid = torch.arange(32, dtype=torch.float32) low = torch.sin(2 * torch.pi * 2 * grid / 32)[None, None, None, :] high = torch.sin(2 * torch.pi * 12 * grid / 32)[None, None, None, :] low = low.expand(2, 3, 32, 32) high = high.expand(2, 3, 32, 32) low_accumulator = RadialPSDAccumulator() high_accumulator = RadialPSDAccumulator() low_accumulator.update(low[:1]) low_accumulator.update(low[1:]) high_accumulator.update(high) low_result = low_accumulator.compute() high_result = high_accumulator.compute() assert low_result.valid and high_result.valid assert high_result.spectral_centroid > low_result.spectral_centroid assert high_result.high_low_ratio > low_result.high_low_ratio def test_streaming_psd_marks_one_by_one_features_invalid() -> None: accumulator = RadialPSDAccumulator() accumulator.update(torch.randn(2, 4, 1, 1)) result = accumulator.compute() assert not result.valid assert result.spectral_centroid is None def test_streaming_psd_rejects_constant_roundoff_and_degenerate_split() -> None: constant = RadialPSDAccumulator() constant.update(torch.ones(2, 3, 32, 32) * 0.3) assert not constant.compute().valid tiny_grid = RadialPSDAccumulator() tiny_grid.update(torch.randn(2, 3, 2, 2)) result = tiny_grid.compute() assert result.valid assert not result.high_low_valid assert result.high_low_ratio is None def test_torch_lowpass_endpoints_and_attenuation() -> None: images = torch.randn(2, 3, 32, 32) dc = torch_fft_lowpass(images, 0.0) identity = torch_fft_lowpass(images, 1.0) filtered = torch_fft_lowpass(images, 0.25) assert torch.allclose(dc, images.mean(dim=(-2, -1), keepdim=True).expand_as(images)) assert identity.data_ptr() == images.data_ptr() assert filtered.square().mean() < images.square().mean()