Build a reproducible shipment-temperature report that preserves raw units, sensor corrections and uncertainty near the operating limit.
Project: audit cold-chain sensor measurements before classifying excursions
Create the raw fixture
Create 47 shipments and at least two sensor models. Mix Celsius and Fahrenheit payloads, include three duplicate event IDs, one unknown unit, two missing readings and a device whose reference-check error changes after maintenance. Include shipment IDs, sensor IDs, occurrence and known-at times, raw value, raw unit, schema version and calibration version. Preserve all raw rows. The unit contract governs canonical conversion.
Reconcile and correct
Deduplicate by stable event ID, quarantine unknown units and impossible timestamps, and link each sensor reading to the calibration record valid at measurement time. Keep uncorrected and corrected Celsius values separately. Do not apply a July correction to a March reading simply because it is the latest record. Reference checks define the valid range and correction review.
Classify with an uncertainty band
Use an 8.5-degree Celsius operating limit. If a corrected reading plus its bound is at or below the limit, mark it clear; if the reading minus its bound is above the limit, mark it confirmed high; otherwise mark it uncertain. Do not force the uncertain class into either pass or fail. Shared sensor error may affect several readings on one shipment.
Publish an audit table
For each shipment, show accepted reading count, quarantined count, maximum corrected temperature, its uncertainty bound and classification. Include the raw-unit distribution and missing-reading count. A shipment with no valid readings is unknown, not clear. Record whether the metric is any excursion, duration above limit or maximum temperature; those are different decision quantities.
Deliver the review packet
Save fixture, canonicalization rules, calibration history, corrected-readings table, quarantine ledger, code revision, analysis cutoff and assertions. A peer should reproduce 46.4 degrees Fahrenheit as 8 degrees Celsius, see one borderline reading remain uncertain, and trace any confirmed high classification back to a raw event without a private dashboard.
Implementation
def excursion_state(measured_celsius, bound_celsius, limit_celsius=8.5):
if bound_celsius < 0:
raise ValueError("uncertainty bound must be nonnegative")
if measured_celsius + bound_celsius <= limit_celsius:
return "clear"
if measured_celsius - bound_celsius > limit_celsius:
return "confirmed_high"
return "uncertain"
assert excursion_state(8.6, 0.3) == "uncertain"
assert excursion_state(7.9, 0.2) == "clear"
assert excursion_state(9.1, 0.3) == "confirmed_high"Performance and operating cost
Classifying N accepted readings costs O(N) time and O(1) state per reading. Shipment aggregation needs O(S) state for S shipments; event deduplication and historical calibration lookup add keyed storage and lookup cost. Manual investigation of uncertain or quarantined records is the dominant operating cost.
Common Mistakes
- Do not treat an absent sensor reading as evidence that temperature stayed below the limit.
- Do not apply a calibration correction outside its documented time or measurement range.
- Do not collapse a boundary-crossing uncertainty band into a certain pass or fail.
