Add laser_echo_analysis/tests/test_core.py
This commit is contained in:
parent
512b9c5517
commit
f2d27e158b
1 changed files with 61 additions and 0 deletions
61
laser_echo_analysis/tests/test_core.py
Normal file
61
laser_echo_analysis/tests/test_core.py
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
import pytest
|
||||
import math
|
||||
from laser_echo_analysis import core
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def sample_data():
|
||||
# Simulated sample data based on LaserMeasurement model
|
||||
# Mix of signal and background noise values
|
||||
return [
|
||||
{'timestamp': '2024-01-01T00:00:00Z', 'pixel_value': 5, 'delta_t': 0.1},
|
||||
{'timestamp': '2024-01-01T00:00:01Z', 'pixel_value': 8, 'delta_t': 0.1},
|
||||
{'timestamp': '2024-01-01T00:00:02Z', 'pixel_value': 12, 'delta_t': 0.1},
|
||||
{'timestamp': '2024-01-01T00:00:03Z', 'pixel_value': 6, 'delta_t': 0.1},
|
||||
{'timestamp': '2024-01-01T00:00:04Z', 'pixel_value': 10, 'delta_t': 0.1},
|
||||
]
|
||||
|
||||
|
||||
def test_analyze_data_basic(sample_data):
|
||||
results = core.analyze_data(sample_data)
|
||||
|
||||
assert isinstance(results, dict), "Result should be a dictionary"
|
||||
assert set(results.keys()) == {'peak', 'average_noise', 'signal_to_noise_ratio'}
|
||||
|
||||
# Validate expected numeric ranges
|
||||
assert results['peak'] == 12
|
||||
assert results['average_noise'] > 0
|
||||
assert results['signal_to_noise_ratio'] > 1
|
||||
assert math.isfinite(results['signal_to_noise_ratio'])
|
||||
|
||||
|
||||
def test_analyze_data_with_constant_signal():
|
||||
constant_data = [{'timestamp': '2024-01-01T00:00:00Z', 'pixel_value': 10, 'delta_t': 0.1} for _ in range(10)]
|
||||
results = core.analyze_data(constant_data)
|
||||
|
||||
assert results['peak'] == 10
|
||||
# With constant signal, noise should be zero or near-zero
|
||||
assert abs(results['average_noise']) < 1e-9
|
||||
# Avoid division by zero, ensure SNR behaves correctly
|
||||
assert results['signal_to_noise_ratio'] == pytest.approx(float('inf')) or results['signal_to_noise_ratio'] > 1e9
|
||||
|
||||
|
||||
def test_analyze_data_empty():
|
||||
with pytest.raises((ValueError, AssertionError)):
|
||||
core.analyze_data([])
|
||||
|
||||
|
||||
def test_analyze_data_invalid_input():
|
||||
# Missing required field
|
||||
invalid_data = [{'timestamp': '2024-01-01T00:00:00Z', 'delta_t': 0.2}]
|
||||
with pytest.raises((KeyError, ValueError, AssertionError)):
|
||||
core.analyze_data(invalid_data)
|
||||
|
||||
|
||||
def test_signal_to_noise_ratio_behavior(sample_data):
|
||||
noisy_data = [dict(d, pixel_value=d['pixel_value'] + i % 3 - 1) for i, d in enumerate(sample_data)]
|
||||
results_noisy = core.analyze_data(noisy_data)
|
||||
results_clean = core.analyze_data(sample_data)
|
||||
|
||||
# More noise -> lower SNR expected
|
||||
assert results_noisy['signal_to_noise_ratio'] < results_clean['signal_to_noise_ratio']
|
||||
Loading…
Reference in a new issue