A PySpark feature job adds a Deequ anomaly check on a feature's `Completeness`, yet a sharp completeness drop never stopped training; what is the likely cause?
answer
- history, not a fixed line
- the default check level
- a gate that fails only on Error
- PyDeequ cannot pass the config
- a repository that outlives the job
basics
~20 sDeequ's addAnomalyCheck defaults to CheckLevel.Warning, so a detected drop only makes the run's status Warning, which a gate failing on Error ignores. PyDeequ 1.7.0 cannot pass an AnomalyCheckConfig, so the gate must treat that check's Warning as blocking.
solid answer
~40 sAn anomaly check compares today's metric, here `Completeness("ad_slot")`, with its history in a metrics repository given through `useRepository` and `saveOrAppendResult(ResultKey(...))`; a strategy such as `RelativeRateOfChangeStrategy(maxRateDecrease=0.9)` flags a value below 0.9 times the previous one. When `addAnomalyCheck` gets no config, Deequ builds `AnomalyCheckConfig(CheckLevel.Warning, "Anomaly check for ...")`, so the flagged drop yields a `Warning` status and a gate written as `status == "Error"` lets training run. In Scala you pass `Some(AnomalyCheckConfig(CheckLevel.Error, ...))`; PyDeequ 1.7.0 raises `NotImplementedError` if you pass any config, so the Python gate must read the anomaly check's row in `checkResultsAsDataFrame`, or run anomaly checks in their own suite and fail on anything but `"Success"`. Also confirm the repository persists between runs: an in-memory one starts empty every day.
code
scala · 22 linesimport com.amazon.deequ.{AnomalyCheckConfig, VerificationSuite}
import com.amazon.deequ.analyzers.Completeness
import com.amazon.deequ.anomalydetection.RelativeRateOfChangeStrategy
import com.amazon.deequ.checks.{CheckLevel, CheckStatus}
import com.amazon.deequ.repository.ResultKey
import com.amazon.deequ.repository.fs.FileSystemMetricsRepository
val repository = FileSystemMetricsRepository(spark, metricsPath)
val result = VerificationSuite()
.onData(features)
.useRepository(repository)
.saveOrAppendResult(ResultKey(System.currentTimeMillis(), Map("table" -> "ctr_features")))
.addAnomalyCheck(
RelativeRateOfChangeStrategy(maxRateDecrease = Some(0.9)),
Completeness("ad_slot"),
Some(AnomalyCheckConfig(CheckLevel.Error, "ad_slot completeness drop")))
.run()
if (result.status == CheckStatus.Error) {
throw new IllegalStateException("ad_slot completeness fell sharply; training stops")
}go deeper
Recall that Deequ can compare today's metric with its stored history, and that every check, including an anomaly check, has a level that decides whether it blocks.
Explain the repository, analyzer and strategy that make up an anomaly check, and why its default Warning level slips past a gate that fails only on Error.
Diagnose the silent pass from the default level and PyDeequ's missing config support, then fix the gate, persist and seed the repository, and say which checks block.
Decide whether anomaly checks may stop retrains at all, who tunes their bounds per metric, and how a shared metrics repository is owned and kept durable.
## How a Deequ anomaly check works A fixed constraint such as `hasCompleteness("ad_slot", _ >= 0.7)` passes until the value crosses a line chosen in advance. An **anomaly check** asks a different question: is today's metric unusual **compared with that metric's own history**? Deequ answers it in four steps: 1. The `VerificationSuite` is given a **metrics repository** with `useRepository(...)` and a key for today's result with `saveOrAppendResult(ResultKey(...))`. 2. `addAnomalyCheck(strategy, analyzer)` names the metric, for example `Completeness("ad_slot")`, and an **anomaly detection strategy**. 3. At `run()`, Deequ loads the metric's earlier values from the repository, computes today's value and asks the strategy whether the newest point is anomalous. 4. After evaluating the checks, Deequ appends today's metrics to the repository under the key. With `RelativeRateOfChangeStrategy(maxRateDecrease = Some(0.9))`, a value below 0.9 times the previous one is anomalous, so a completeness drop from 0.99 to 0.80 is flagged although the fixed 0.7 line still passes. ## The default level is Warning `addAnomalyCheck` takes an optional third argument, `anomalyCheckConfig`. When it is left out, Deequ builds `AnomalyCheckConfig(CheckLevel.Warning, "Anomaly check for ...")`. A flagged anomaly therefore produces a `Warning` check status and, through the worst-status rule, at most a `Warning` overall status. A gate written as "fail on `Error`" lets it through, which is the silent failure in this question. | Surface | Can you set the anomaly check's level? | How | |---|---|---| | Scala Deequ (3.0.2-spark-3.5 tag) | yes | pass `Some(AnomalyCheckConfig(CheckLevel.Error, "..."))` as the third argument | | PyDeequ 1.7.0 | no | passing any `anomalyCheckConfig` raises `NotImplementedError`; the check always uses the Warning default | ## Making it block in PyDeequ Because the level cannot be raised from Python, the gate has to read the result more closely than the overall status: - read `VerificationResult.checkResultsAsDataFrame(spark, result)` and fail when the row for the anomaly check (its description begins "Anomaly check for") has a `check_status` other than `Success`; - or run the anomaly checks in a **separate** `VerificationSuite` and fail that run on any status other than `"Success"`, leaving truly advisory warnings in the other suite. Either way, the decision that this warning blocks training is written in your code, where a reviewer can see it. ## Keeping the history real An anomaly check is only as good as the series it reads. - `InMemoryMetricsRepository` lives inside one process: a daily job that creates it afresh has no history the next day. Use a durable store, `FileSystemMetricsRepository(spark, path)` on shared storage, or in Scala a `SparkTableMetricsRepository(session, tableName)`. - The check **requires previous results**. The source fails the constraint with "There have to be previous results in the MetricsRepository!" when none exist, so the first run, or a run against a wiped repository, reports the anomaly check as failed. Seed the repository from a few known-good runs before the gate goes live. - Several tables can share one repository: tag result keys, for example `ResultKey(timestamp, Map("table" -> "ctr_features"))`, and in Scala filter the history with the config's `withTagValues`, `afterDate` and `beforeDate`. ## Fixed constraint or anomaly check | | `hasCompleteness` floor in a `Check` | `addAnomalyCheck` on `Completeness` | |---|---|---| | Needs history | no | yes, in a metrics repository | | Level | the level the `Check` was built with | `Warning` unless a config says otherwise | | Catches | a value below an absolute line | a sudden change relative to earlier values | | First run | works | fails for lack of previous results | The two answer different questions, which is why one feature can carry both. ## Choosing a strategy | Strategy | Flags the newest value when | |---|---| | `RelativeRateOfChangeStrategy(maxRateDecrease, maxRateIncrease)` | new divided by previous falls outside the bounds | | `AbsoluteChangeStrategy(maxRateDecrease, maxRateIncrease)` | new minus previous falls outside the bounds | | `SimpleThresholdStrategy(lowerBound, upperBound)` | the value itself leaves fixed bounds | | `OnlineNormalStrategy`, `BatchNormalStrategy` | it lies more than a deviation factor (3.0 by default) of standard deviations from the mean | The change strategies require at least one of their two bounds. Which bound suits a metric is a monitoring-design judgement; the tool lesson is that the strategy, the analyzer and the level are three separate choices, and the level is the part a status-based gate reads.
- The gate now reads the anomaly check's row, but the very first run failed it. Why?The anomaly check needs history: Deequ requires previous results in the metrics repository and fails the constraint with "There have to be previous results in the MetricsRepository!" when there are none. Seed the repository with metrics from several known-good runs before turning the gate on, and keep it on durable storage so a restart cannot empty it.
- Why not simply raise the fixed completeness threshold instead of adding an anomaly check?A fixed `hasCompleteness` floor high enough to catch a 0.99 to 0.80 drop also fails on any feature whose normal completeness varies around that level. The anomaly check judges the change relative to the feature's own recent values, so one strategy can serve features with very different baselines. You can keep both: a hard floor at Error for the impossible values, a rate-of-change check for sudden moves.
- A Deequ anomaly check fails the run at Error level by default
- PyDeequ's addAnomalyCheck accepts an AnomalyCheckConfig to set the level
- An InMemoryMetricsRepository keeps history between daily job runs
- Anomaly checks work from the first run with an empty repository
- A fixed hasCompleteness threshold catches every sharp completeness drop
Assumes Scala Deequ at the 3.0.2-spark-3.5 tag and PyDeequ 1.7.0, whose addAnomalyCheck raises NotImplementedError when given an anomalyCheckConfig.