From c67b74ed2e8051ede93b1e30a7b9267756a7386b Mon Sep 17 00:00:00 2001 From: Mika Date: Wed, 17 Dec 2025 12:03:32 +0000 Subject: [PATCH] Add trace_analysis_script/tests/test_core.py --- trace_analysis_script/tests/test_core.py | 51 ++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 trace_analysis_script/tests/test_core.py diff --git a/trace_analysis_script/tests/test_core.py b/trace_analysis_script/tests/test_core.py new file mode 100644 index 0000000..914b537 --- /dev/null +++ b/trace_analysis_script/tests/test_core.py @@ -0,0 +1,51 @@ +import json +import math +import tempfile +from pathlib import Path + +import numpy as np +import pandas as pd +import pytest + +from trace_analysis_script import core + +def _create_sample_trace_file(tmp_path: Path) -> Path: + data = { + 'time': np.arange(0, 1, 0.01), + 'signal': np.sin(2 * np.pi * 5 * np.arange(0, 1, 0.01)) * 0.5 + 0.5 + } + df = pd.DataFrame(data) + file_path = tmp_path / 'sample_trace.csv' + df.to_csv(file_path, index=False) + return file_path + +def test_analyze_trace_returns_dict_with_expected_keys(tmp_path): + trace_file = _create_sample_trace_file(tmp_path) + result = core.analyze_trace(str(trace_file)) + + assert isinstance(result, dict) + expected_keys = {'peak_amplitude', 'median_bandpower', 'crosscorr_with_clockevents'} + assert set(result.keys()) == expected_keys + + for key in expected_keys: + assert isinstance(result[key], (float, int)) + + +def test_peak_amplitude_value_is_reasonable(tmp_path): + trace_file = _create_sample_trace_file(tmp_path) + result = core.analyze_trace(str(trace_file)) + assert 0.4 <= result['peak_amplitude'] <= 0.6 + + +def test_crosscorr_with_clockevents_is_numeric(tmp_path): + trace_file = _create_sample_trace_file(tmp_path) + result = core.analyze_trace(str(trace_file)) + val = result['crosscorr_with_clockevents'] + assert isinstance(val, (float, int)) + assert -1.0 <= val <= 1.0 + + +def test_handles_invalid_input_file_gracefully(tmp_path): + invalid_path = tmp_path / 'nonexistent.csv' + with pytest.raises((FileNotFoundError, ValueError)): + _ = core.analyze_trace(str(invalid_path)) \ No newline at end of file