-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_global_settings.py
More file actions
126 lines (109 loc) · 4.57 KB
/
Copy pathtest_global_settings.py
File metadata and controls
126 lines (109 loc) · 4.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
"""
===============================================================================
NeuroEng Voltage Transient Processor - "TEST" Global Settings
===============================================================================
Author: Ifra Ilyas Ansari
Created: 2025-10-17
Modified: 2025-10-21
Code: test_global_settings.py
Version: v1.0.0
DESCRIPTION
---------------------------
Unit test script for the `VT_Module_Py_GlobalSettings` module.
This script uses `pytest` to verify the robustness and correctness of the
`standardize_dataframe_to_µs` function. It ensures this core function
behaves as expected across a variety of input data conditions.
Test coverage includes:
- **Time Standardization:** Correctly converts time columns from seconds (s),
milliseconds (ms), and microseconds (µs/us) into the canonical "Time (µs)".
- **Channel Mapping:** Correctly identifies voltage and current columns using
various aliases (e.Gas, "Voltage (V)", "Current (A)", "CH1", "CH2").
- **Type Coercion:** Successfully converts string-based numerical data
(e.g., "0.5") into floats.
- **Error Handling:** Properly raises a `KeyError` if essential columns
(time, voltage, or current) are missing.
Running these tests confirms that the data standardization logic is reliable
and helps prevent data corruption bugs in the processing pipeline.
"""
import numpy as np
import pandas as pd
import pytest
# Try preferred location first; fall back to Stage2 script if needed.
try:
from VT_Processor_Py_GlobalSettings import (
standardize_dataframe_to_µs,
CANON_TIME, CANON_VOLT, CANON_CURR,
)
except Exception:
# Fallback: import from your Stage2 module path
from VT_Processor_PyStage2_VoltageSelector import ( # type: ignore
standardize_dataframe_to_µs,
CANON_TIME, CANON_VOLT, CANON_CURR,
)
@pytest.fixture
def base_vectors():
# "True" underlying time in microseconds and simple signals
t_us = np.linspace(0, 9, 10) # 0..9 µs
v = np.sin(t_us * 0.1)
i = np.cos(t_us * 0.1)
return t_us, v, i
def df_with_headers(time_header, volt_header, curr_header, t_vals, v, i):
return pd.DataFrame({
time_header: t_vals,
volt_header: v,
curr_header: i,
})
@pytest.mark.parametrize(
"time_header, scale",
[
("Time (µs)", 1.0), # micro sign
("Time (us)", 1.0), # ascii 'us'
("Time (ms)", 1e3), # ms -> µs
("Time (s)", 1e6), # s -> µs
],
)
@pytest.mark.parametrize(
"volt_header, curr_header",
[
("Voltage (V)", "Current (A)"),
("CH1", "CH2"), # Tek aliases
("Voltage", "Current"), # generic
],
)
def test_time_unit_standardization(base_vectors, time_header, scale, volt_header, curr_header):
t_us, v, i = base_vectors
# create "raw" time in the chosen unit
t_in = t_us / scale
df_raw = df_with_headers(time_header, volt_header, curr_header, t_in, v, i)
df_std = standardize_dataframe_to_µs(df_raw)
assert list(df_std.columns) == ["Time (µs)", "Voltage (V)", "Current (A)"]
# numeric close due to float ops
np.testing.assert_allclose(df_std["Time (µs)"].to_numpy(), t_us, rtol=0, atol=1e-12)
np.testing.assert_allclose(df_std["Voltage (V)"].to_numpy(), v, rtol=0, atol=1e-12)
np.testing.assert_allclose(df_std["Current (A)"].to_numpy(), i, rtol=0, atol=1e-12)
def test_missing_time_raises(base_vectors):
t_us, v, i = base_vectors
df = pd.DataFrame({"CH1": v, "CH2": i})
with pytest.raises(KeyError):
standardize_dataframe_to_µs(df)
def test_missing_voltage_or_current_raises(base_vectors):
t_us, v, i = base_vectors
df = pd.DataFrame({"Time (µs)": t_us, "CH1": v})
with pytest.raises(KeyError):
standardize_dataframe_to_µs(df)
df = pd.DataFrame({"Time (µs)": t_us, "CH2": i})
with pytest.raises(KeyError):
standardize_dataframe_to_µs(df)
def test_non_numeric_coercion(base_vectors):
t_us, v, i = base_vectors
df = pd.DataFrame({
"Time (ms)": (t_us / 1e3).astype(str), # strings, should coerce
"CH1": pd.Series(v).astype(str),
"CH2": pd.Series(i).astype(str),
})
out = standardize_dataframe_to_µs(df)
# ensure they were coerced to numeric and scaled
assert out["Time (µs)"].dtype.kind in "fc"
assert out["Voltage (V)"].dtype.kind in "fc"
assert out["Current (A)"].dtype.kind in "fc"
np.testing.assert_allclose(out["Time (µs)"].to_numpy(), t_us, atol=1e-12)