TofuPilotTofuPilot
FrameworkMeasurements

Examples

Explore real-world measurement examples for hardware testing, including voltage checks, temperature validation, and signal analysis in TofuPilot.

Complete Real Examples

Complex examples showing all features working together.

String Measurement with Aggregation

Real-world example: Run the device built-in self-test (BIST) and validate it returns PASS. Track the pass rate across runs.

def self_test(measurements, log):
    result = run_device_bist()
    measurements.bist_result = result

    # Count passed devices for yield tracking
    measurements.bist_result.aggregations.pass_count = 1 if result == "PASS" else 0

    log.info(f"BIST result: {result}")

def run_device_bist():
    """Trigger device built-in self-test and read result register"""
    return "PASS"
main:
  - name: Built-In Self Test
    python: self_test
    measurements:
      - name: BIST Result
        validators:
          - operator: "=="
            expected_value: "PASS"
        aggregations:
          - type: pass_count
            validators:
              - operator: ">="
                expected_value: 1

Multi-dimensional Measurement with Aggregations

Real-world example: Temperature profile over time with statistical analysis.

import time
import numpy as np

def thermal_test(measurements, log):
    """Measure device temperature every 30 seconds for 10 minutes"""

    test_duration_minutes = 10
    sample_interval_seconds = 30
    total_samples = int((test_duration_minutes * 60) / sample_interval_seconds)

    time_points = []
    temperatures = []

    log.info(f"Starting {test_duration_minutes}-minute temperature profile")

    for i in range(total_samples + 1):
        elapsed_minutes = i * (sample_interval_seconds / 60.0)
        time_points.append(elapsed_minutes)

        temp = read_case_temperature()
        temperatures.append(temp)

        log.info(f"Sample {i+1}/{total_samples+1}: {elapsed_minutes:.1f}min, {temp:.1f}°C")

        if i < total_samples:
            time.sleep(sample_interval_seconds)

    # Set multi-dimensional data using builder pattern
    measurements.temperature_vs_time.x_axis = time_points
    measurements.temperature_vs_time.y_axis.case_temperature = temperatures

    # Set aggregation values (computed in Python, validated by TofuPilot)
    y = measurements.temperature_vs_time.y_axis.case_temperature
    y.aggregations.mean = float(np.mean(temperatures))
    y.aggregations.max = float(np.max(temperatures))
    y.aggregations.std_dev = float(np.std(temperatures))

    log.info(f"Test completed: avg={np.mean(temperatures):.1f}°C, max={np.max(temperatures):.1f}°C")

def read_case_temperature():
    """Simulate reading from temperature sensor"""
    base_temp = 25 + (time.time() % 100) * 0.5
    noise = np.random.normal(0, 2)
    return base_temp + noise
main:
  - name: Characterize Thermal
    python: thermal_test
    measurements:
      - name: temperature_vs_time
        title: Temperature Profile
        x_axis:
          unit: min
          legend: Time
        y_axis:
          - unit: °C
            legend: Case Temperature
            key: case_temperature
            aggregations:
              - type: mean
                unit: °C
                validators:
                  - operator: "<="
                    expected_value: 85.0
              - type: max
                unit: °C
                validators:
                  - operator: "<="
                    expected_value: 95.0
              - type: std_dev
                unit: °C
                validators:
                  - operator: "<="
                    expected_value: 5.0

How is this guide?