Correct Measurement Offset from Replicate

Detect and correct constant offset between a measurement and a reference replicate. Uses brute-force search to find the offset that minimizes absolute difference between measurement and replicate.

Create synthetic replicate and measurement data

We create a reference signal (replicate) and a measurement with a known constant offset. This simulates a sensor that reads systematically high or low compared to a reference instrument.

import numpy as np
import pandas as pd

import diive as dv

dates = pd.date_range('2024-01-01', periods=500, freq='30min')

# Reference signal with diurnal variation
replicate = pd.Series(
    50 + 20 * np.sin(np.arange(500) * 2 * np.pi / 48),
    index=dates,
    name='reference'
)

# Measurement with constant +4.2 offset
measurement = replicate.copy() + 4.2
measurement.name = 'measurement'

print("Initial data:")
print(f"  Replicate range: {replicate.min():.2f} to {replicate.max():.2f}")
print(f"  Measurement range: {measurement.min():.2f} to {measurement.max():.2f}")
print(f"  Expected offset: 4.2")
Initial data:
  Replicate range: 30.00 to 70.00
  Measurement range: 34.20 to 74.20
  Expected offset: 4.2

Extract and evaluate correction

Get the detected offset and corrected measurement values.

measurement_corrected = offset_corrector.get_corrected_measurement()
detected_offset = offset_corrector.get_offset()

print(f"\nResults:")
print(f"  Detected offset: {detected_offset:.2f}")
print(f"  Corrected measurement range: {measurement_corrected.min():.2f} to {measurement_corrected.max():.2f}")
Results:
  Detected offset: -4.20
  Corrected measurement range: 30.00 to 70.00

Evaluate correction quality

Compare absolute differences before and after correction.

mae_before = (measurement - replicate).abs().mean()
mae_after = (measurement_corrected - replicate).abs().mean()

print(f"\nCorrection quality:")
print(f"  Mean absolute difference (before): {mae_before:.4f}")
print(f"  Mean absolute difference (after): {mae_after:.4f}")
print(f"  Improvement: {((mae_before - mae_after) / mae_before * 100):.1f}%")
Correction quality:
  Mean absolute difference (before): 4.2000
  Mean absolute difference (after): 0.0000
  Improvement: 100.0%

Apply known constant offset directly

When the offset is already known (from calibration data, documentation, or previous analysis), apply it directly without detection. This is simpler and faster than the brute-force search approach.

known_offset = 4.2

# Simple offset correction: subtract the offset from measurement
measurement_simple_corrected = measurement - known_offset

mae_simple = (measurement_simple_corrected - replicate).abs().mean()

print(f"\nSimple constant offset correction:")
print(f"  Known offset: {known_offset}")
print(f"  Corrected range: {measurement_simple_corrected.min():.2f} to {measurement_simple_corrected.max():.2f}")
print(f"  Mean absolute difference: {mae_simple:.4f}")
Simple constant offset correction:
  Known offset: 4.2
  Corrected range: 30.00 to 70.00
  Mean absolute difference: 0.0000

When to use each approach

Replicate-based detection (MeasurementOffsetFromReplicate): - Offset value is unknown - Reference/replicate data is available - Want to find optimal offset automatically

Direct offset application: - Offset is known (from calibration, documentation) - No reference data available - Need quick correction without computation

Example comparison:

print(f"\nComparison of methods:")
print(f"  Detected offset: {detected_offset:.2f}")
print(f"  Known offset: {known_offset:.2f}")
print(f"  Detection error: {abs(detected_offset - known_offset):.4f}")
Comparison of methods:
  Detected offset: -4.20
  Known offset: 4.20
  Detection error: 8.4000

Total running time of the script: (0 minutes 0.106 seconds)

Gallery generated by Sphinx-Gallery