Browse documentation
Guides

Coordinate aware station anomalies

A complete scientific station example with explicit full set coordinate alignment, temperature difference units and an asserted mismatch diagnostic.

Two station files contain the same number of values but list stations in different orders. A positional subtraction would pair current station A with baseline station B and produce −2 and 11. Equal shapes cannot establish that pairing is scientifically valid.

A native SemanticTensor carries a declared axis, unique ordered coordinates and units. Arithmetic requires exact semantic compatibility. Explicit reindexing reconciles the complete coordinate set; it does not intersect files, drop a station or interpolate a missing measurement.

Dataset and complete runnable program

The entire deterministic dataset is in the program. Current A/B values are 10/20 degC. Baseline B/A values are 12/9 degC. No data download is required.

sh
aner check examples/contextual_coordinates.aner
aner run examples/contextual_coordinates.aner
aner
// Two station files have equal shapes but different coordinate order.
import aner.tensor

let current = tensor.semantic([[10.0], [20.0]], "station", List("A", "B"), "degC")
let baseline = tensor.semantic([[12.0], [9.0]], "station", List("B", "A"), "degC")
// Reindexing explicitly reconciles the declared coordinate identities.
let aligned = baseline.reindex_like(current)
let anomaly = current.subtract(aligned)

print("Baseline aligned to A, B")
print(aligned.value(0, 0))
print(aligned.value(1, 0))
print("Station anomalies")
print(anomaly.value(0, 0))
print(anomaly.value(1, 0))
print(anomaly.unit())
print("Coordinate order is preserved")
print(anomaly.coordinates())
print("First current temperature in Kelvin")
print(current.convert("K").value(0, 0))

Expected output

text
Baseline aligned to A, B
9.0
12.0
Station anomalies
1.0
8.0
delta_degC
Coordinate order is preserved
List("A", "B")
First current temperature in Kelvin
283.15

The aligned baseline is 9/12 in A/B order. The correct anomalies are 1/8 in temperature difference units. An absolute value 10 degC converts to 283.15 K; the conversion offset does not apply to a temperature difference.

Intentional failure: positional arithmetic

Download the negative program. It checks successfully and must fail when the subtraction is executed.

aner
// Intentional failure: equal shape does not establish coordinate identity/order.
import aner.tensor
let current = tensor.semantic([[10.0], [20.0]], "station", List("A", "B"), "degC")
let baseline = tensor.semantic([[12.0], [9.0]], "station", List("B", "A"), "degC")
let forbidden = current.subtract(baseline)

Expected diagnostic: a located error[R3502] containing E_AXIS_ALIGNMENT. The correction is explicit baseline.reindex_like(current) after verifying that the declared labels represent the same stations. Display names alone cannot establish common scientific identity automatically.

Intentional failure: adding absolute temperatures

Download the negative temperature program. This source uses the same axis on both operands, so the failure isolates the affine quantity rule.

aner
// Intentional failure: ordinary addition is undefined for two absolute temperatures.
import aner.tensor
let observed = tensor.semantic([[10.0]], "station", List("A"), "degC")
let forbidden = observed.add(observed)

Expected diagnostic: a located error[R3502] containing E_AFFINE_QUANTITY. Absolute temperatures are points. Use an explicitly defined mean when that operation is intended; temperature differences use different arithmetic.

Why the structure helps

The sample domain remains attached to the numeric values rather than reconstructed beside an array. Coordinate mismatches fail at the operation that would combine them. Unit rules also distinguish absolute temperatures from differences and refuse incompatible quantities. Unsupported transformations cannot retain labels whose meaning has changed.

Dependencies, backends and limitations

Use the compatible native development engine with aner.tensor and VS Code helper 0.1.38 or newer. This exact example executes on CPU Float64 without files, networking or GPU libraries. The SemanticTensor reference lists supported conversion/placement contracts and time/space costs; backend support is operation specific.

General interpolation, scientific reshape, named axis broadcasting, regridding and arbitrary unit aware autodifferentiation are future functionality. The complete source and expected output above are compared with the automated runnable fixture. Its intentional failure asserts a located diagnostic.

Aner documentation · Reference and guides