From 0b8492b1abe0fad0447076bcd591f80fe56e0f03 Mon Sep 17 00:00:00 2001 From: Julien Buteau Date: Tue, 6 May 2025 16:15:21 +0200 Subject: [PATCH 1/2] Improves multi dim examples --- .../measurements/dimensional/client/main.py | 89 +++++++++++-------- .../measurements/dimensional/openhtf/main.py | 9 +- 2 files changed, 57 insertions(+), 41 deletions(-) diff --git a/docs_tofupilot/measurements/dimensional/client/main.py b/docs_tofupilot/measurements/dimensional/client/main.py index 775582e..1a4e394 100644 --- a/docs_tofupilot/measurements/dimensional/client/main.py +++ b/docs_tofupilot/measurements/dimensional/client/main.py @@ -1,4 +1,5 @@ import random +import numpy as np from datetime import datetime from tofupilot import MeasurementOutcome, PhaseOutcome, TofuPilotClient @@ -6,49 +7,65 @@ client = TofuPilotClient() -def phase_voltage_measure(): - start_time_millis = datetime.now().timestamp() * 1000 - - structured_measurements = [] +def standard(): + """Python lists approach - one point at a time""" + start = datetime.now().timestamp() * 1000 + measurements = [] + for t in range(100): - timestamp = t / 100 - voltage = round(random.uniform(3.3, 3.5), 2) - current = round(random.uniform(0.3, 0.8), 3) - resistance = voltage / current - structured_measurements.append( - (timestamp, voltage, current, resistance)) - - phase = [ - { - "name": "power_phase", - "outcome": PhaseOutcome.PASS, - "start_time_millis": start_time_millis, - "end_time_millis": start_time_millis + 30 * 1000, - "measurements": [ - { - "name": "current_voltage_resistence_over_time", - "units": ["s", "V", "A", "Ohm"], - "measured_value": structured_measurements, - "outcome": MeasurementOutcome.PASS, - } - ], - } - ] - - return phase + timestamp = t / 100 # Time dimension + voltage = round(random.uniform(3.3, 3.5), 2) # Voltage dimension + current = round(random.uniform(0.3, 0.8), 3) # Current dimension + measurements.append((timestamp, voltage, current, voltage / current)) + return { + "name": "loop_approach", + "outcome": PhaseOutcome.PASS, + "start_time_millis": start, + "end_time_millis": start + 30000, + "measurements": [{ + "name": "current_voltage_resistance_over_time", + "units": ["s", "V", "A", "Ohm"], + "measured_value": measurements, + "outcome": MeasurementOutcome.PASS, + }], + } -def main(): - phases = phase_voltage_measure() +def numpy_way(): + """NumPy approach - all points at once""" + start = datetime.now().timestamp() * 1000 + + # Generate all dimensions simultaneously + timestamps = np.linspace(0, 0.99, 100) + voltages = np.round(np.random.uniform(3.3, 3.5, 100), 2) + currents = np.round(np.random.uniform(0.3, 0.8, 100), 3) + + measurements = [tuple(x) for x in np.column_stack( + (timestamps, voltages, currents, voltages / currents))] + + return { + "name": "vector_approach", + "outcome": PhaseOutcome.PASS, + "start_time_millis": start, + "end_time_millis": start + 30000, + "measurements": [{ + "name": "current_voltage_resistance_over_time", + "units": ["s", "V", "A", "Ohm"], + "measured_value": measurements, + "outcome": MeasurementOutcome.PASS, + }], + } + + +def main(): client.create_run( - procedure_id="FVT1", # Create the procedure first in the Application + procedure_id="FVT1", unit_under_test={"serial_number": "PCB1A001", "part_number": "PCB1"}, - phases=phases, - run_passed=all( - phase["outcome"] == PhaseOutcome.PASS for phase in phases), + phases=[standard(), numpy_way()], + run_passed=True, ) if __name__ == "__main__": - main() + main() \ No newline at end of file diff --git a/docs_tofupilot/measurements/dimensional/openhtf/main.py b/docs_tofupilot/measurements/dimensional/openhtf/main.py index 9aba21c..da04136 100644 --- a/docs_tofupilot/measurements/dimensional/openhtf/main.py +++ b/docs_tofupilot/measurements/dimensional/openhtf/main.py @@ -6,9 +6,9 @@ @htf.measures( - htf.Measurement("current_voltage_resistence_over_time") - .with_dimensions(units.SECOND, units.VOLT, units.AMPERE) - .with_units(units.OHM), + htf.Measurement("current_voltage_resistance_over_time") + .with_dimensions(units.SECOND, units.VOLT, units.AMPERE) # Input axes: time, voltage, current + .with_units(units.OHM) # Output unit: resistance in ohms ) def power_phase(test): for t in range(100): @@ -16,7 +16,7 @@ def power_phase(test): voltage = round(random.uniform(3.3, 3.5), 2) current = round(random.uniform(0.3, 0.8), 3) resistance = voltage / current - test.measurements.current_voltage_resistence_over_time[ + test.measurements.current_voltage_resistance_over_time[ timestamp, voltage, current ] = resistance @@ -28,7 +28,6 @@ def main(): part_number="PCB01", ) - # Execute the test with TofuPilot(test): test.execute(lambda: "PCB1A003") From 44add38dccc178ccbcfdacb915fdc8394b5e86db Mon Sep 17 00:00:00 2001 From: upview Date: Tue, 6 May 2025 14:15:54 +0000 Subject: [PATCH 2/2] Auto code format --- .../measurements/dimensional/client/main.py | 44 +++++++++++-------- .../measurements/dimensional/openhtf/main.py | 4 +- 2 files changed, 28 insertions(+), 20 deletions(-) diff --git a/docs_tofupilot/measurements/dimensional/client/main.py b/docs_tofupilot/measurements/dimensional/client/main.py index 1a4e394..2953753 100644 --- a/docs_tofupilot/measurements/dimensional/client/main.py +++ b/docs_tofupilot/measurements/dimensional/client/main.py @@ -1,7 +1,7 @@ import random -import numpy as np from datetime import datetime +import numpy as np from tofupilot import MeasurementOutcome, PhaseOutcome, TofuPilotClient client = TofuPilotClient() @@ -11,7 +11,7 @@ def standard(): """Python lists approach - one point at a time""" start = datetime.now().timestamp() * 1000 measurements = [] - + for t in range(100): timestamp = t / 100 # Time dimension voltage = round(random.uniform(3.3, 3.5), 2) # Voltage dimension @@ -23,38 +23,44 @@ def standard(): "outcome": PhaseOutcome.PASS, "start_time_millis": start, "end_time_millis": start + 30000, - "measurements": [{ - "name": "current_voltage_resistance_over_time", - "units": ["s", "V", "A", "Ohm"], - "measured_value": measurements, - "outcome": MeasurementOutcome.PASS, - }], + "measurements": [ + { + "name": "current_voltage_resistance_over_time", + "units": ["s", "V", "A", "Ohm"], + "measured_value": measurements, + "outcome": MeasurementOutcome.PASS, + } + ], } def numpy_way(): """NumPy approach - all points at once""" start = datetime.now().timestamp() * 1000 - + # Generate all dimensions simultaneously timestamps = np.linspace(0, 0.99, 100) voltages = np.round(np.random.uniform(3.3, 3.5, 100), 2) currents = np.round(np.random.uniform(0.3, 0.8, 100), 3) - - measurements = [tuple(x) for x in np.column_stack( - (timestamps, voltages, currents, voltages / currents))] + + measurements = [ + tuple(x) + for x in np.column_stack((timestamps, voltages, currents, voltages / currents)) + ] return { "name": "vector_approach", "outcome": PhaseOutcome.PASS, "start_time_millis": start, "end_time_millis": start + 30000, - "measurements": [{ - "name": "current_voltage_resistance_over_time", - "units": ["s", "V", "A", "Ohm"], - "measured_value": measurements, - "outcome": MeasurementOutcome.PASS, - }], + "measurements": [ + { + "name": "current_voltage_resistance_over_time", + "units": ["s", "V", "A", "Ohm"], + "measured_value": measurements, + "outcome": MeasurementOutcome.PASS, + } + ], } @@ -68,4 +74,4 @@ def main(): if __name__ == "__main__": - main() \ No newline at end of file + main() diff --git a/docs_tofupilot/measurements/dimensional/openhtf/main.py b/docs_tofupilot/measurements/dimensional/openhtf/main.py index da04136..9d52d7a 100644 --- a/docs_tofupilot/measurements/dimensional/openhtf/main.py +++ b/docs_tofupilot/measurements/dimensional/openhtf/main.py @@ -7,7 +7,9 @@ @htf.measures( htf.Measurement("current_voltage_resistance_over_time") - .with_dimensions(units.SECOND, units.VOLT, units.AMPERE) # Input axes: time, voltage, current + .with_dimensions( + units.SECOND, units.VOLT, units.AMPERE + ) # Input axes: time, voltage, current .with_units(units.OHM) # Output unit: resistance in ohms ) def power_phase(test):