import numpy as np
from scipy.ndimage import gaussian_filter1d
import json
import os

def generate_data():
    np.random.seed(42)
    # Generate a signal with some structure and noise
    x = np.linspace(0, 20, 200)
    # Signal: Sine wave + Step function + Random Noise
    signal = np.sin(x) + (x > 10).astype(float) * 2.0
    noise = np.random.normal(0, 0.5, size=x.shape)
    data = signal + noise
    return data.tolist()

def get_scipy_kernel(sigma, truncate):
    # Radius calculation from SciPy source/docs: int(truncate * sigma + 0.5)
    radius = int(truncate * sigma + 0.5)
    size = 2 * radius + 1
    
    # Create a delta function (impulse)
    delta = np.zeros(size)
    delta[radius] = 1.0
    
    # Apply filter with constant mode and cval=0 to get the impulse response (kernel)
    # We use the same sigma and truncate
    kernel = gaussian_filter1d(delta, sigma=sigma, truncate=truncate, mode='constant', cval=0.0)
    return kernel.tolist()

def main():
    sigma = 2.5
    truncate = 3.5
    mode = 'reflect'
    
    print(f"Generating data with sigma={sigma}, truncate={truncate}, mode={mode}")
    
    input_data = generate_data()
    
    # Run SciPy filter
    output_data = gaussian_filter1d(input_data, sigma=sigma, truncate=truncate, mode=mode)
    
    # Extract effective kernel
    kernel = get_scipy_kernel(sigma, truncate)
    
    data_dump = {
        "input": input_data,
        "sigma": sigma,
        "truncate": truncate,
        "mode": mode,
        "expected_output": output_data.tolist(),
        "expected_kernel": kernel
    }
    
    file_path = "test_data.json"
    with open(file_path, "w") as f:
        json.dump(data_dump, f, indent=2)
    
    print(f"Saved test data to {os.path.abspath(file_path)}")
    print(f"Kernel size: {len(kernel)}")
    print(f"Input size: {len(input_data)}")

if __name__ == "__main__":
    main()
