Skip to content

Commit 0e37605

Browse files
test updated
1 parent f4c3978 commit 0e37605

3 files changed

Lines changed: 64 additions & 4 deletions

File tree

pica/keithley/delta_mode/Instrument_Control/Delta_K6221_K2182_Simple.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import pyvisa
77
import time
8+
from pica.utils.parser import parse_keithley_output
89

910
# --- Configuration ---
1011
# Use constants for easier changes later.
@@ -64,11 +65,11 @@ def run_delta_measurement():
6465
# The 6221 in delta mode returns a string like "Voltage,Resistance"
6566
raw_data = keithley_6221.query('SENSe:DATA:FRESh?')
6667

67-
# Split the string at the comma to get a list of values
68-
data_points = raw_data.strip().split(',')
68+
# Use the new parsing function
69+
data_points = parse_keithley_output(raw_data)
6970

70-
# The first item is Voltage, the second is Resistance
71-
voltage = float(data_points[0])
71+
# The first item is Voltage
72+
voltage = data_points[0]
7273
# Use the resistance calculated by the keithley_6221
7374
resistance = float(voltage / DELTA_CURRENT)
7475

pica/utils/parser.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
"""
2+
This module provides utility functions for parsing data from instruments.
3+
"""
4+
5+
def parse_keithley_output(data_string: str) -> list[float]:
6+
"""
7+
Parses a comma-separated string of scientific notation numbers from a Keithley instrument
8+
and returns a list of floats.
9+
10+
Args:
11+
data_string: A string containing comma-separated numbers,
12+
e.g., "+1.234E-06,+4.567E+00".
13+
14+
Returns:
15+
A list of floats parsed from the string.
16+
"""
17+
try:
18+
parts = data_string.strip().split(',')
19+
return [float(part) for part in parts]
20+
except (ValueError, IndexError) as e:
21+
raise ValueError(f"Failed to parse data string: '{data_string}'. Reason: {e}") from e
22+

tests/test_backends_logic.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,43 @@ def test_delta_backend_structure():
8585
else:
8686
print("\n[Delta] Flat script verified (ran on import).")
8787

88+
# ---------------------------------------------------------------------------
89+
# 4. Data Parsing Test
90+
# ---------------------------------------------------------------------------
91+
def test_keithley_data_parser():
92+
"""
93+
Tests the utility function that parses comma-separated scientific notation strings.
94+
This is a functional test, not just a structural one.
95+
"""
96+
try:
97+
from pica.utils.parser import parse_keithley_output
98+
except ImportError:
99+
pytest.skip("Could not import the parser module.")
100+
101+
# Test case 1: Standard scientific notation
102+
raw_string_1 = "+1.2345E-06,+2.5000E+00"
103+
expected_1 = [1.2345e-6, 2.5]
104+
assert parse_keithley_output(raw_string_1) == expected_1, "Failed on standard scientific notation."
105+
print(f"\n[Parser] Verified: '{raw_string_1}' -> {expected_1}")
106+
107+
# Test case 2: Negative values and different spacing
108+
raw_string_2 = "-5.0E-03, -1.0E+01"
109+
expected_2 = [-0.005, -10.0]
110+
assert parse_keithley_output(raw_string_2) == expected_2, "Failed on negative values."
111+
print(f"[Parser] Verified: '{raw_string_2}' -> {expected_2}")
112+
113+
# Test case 3: Single value
114+
raw_string_3 = "+3.14159E+00"
115+
expected_3 = [3.14159]
116+
assert parse_keithley_output(raw_string_3) == expected_3, "Failed on single value."
117+
print(f"[Parser] Verified: '{raw_string_3}' -> {expected_3}")
118+
119+
# Test case 4: Malformed string (should raise ValueError)
120+
with pytest.raises(ValueError):
121+
parse_keithley_output("1.23, NOT_A_NUMBER")
122+
print("[Parser] Verified: Correctly raises ValueError on malformed string.")
123+
124+
88125
# ---------------------------------------------------------------------------
89126
# 3. Keithley 2400 Test
90127
# ---------------------------------------------------------------------------

0 commit comments

Comments
 (0)