Prediction Regions
Overview
By default the classification pipeline predicts on all feature rows. Prediction regions allow restricting output to a user-selected frame range while preserving full temporal context for sequence models like the HMM.
Files:
src/MLCore/pipelines/BoundingSpan.hpp/.cpp— span computation and filtering utilitiessrc/MLCore/pipelines/ClassificationPipeline.hpp—PredictionRegionConfigstructtests/MLCore/BoundingSpan.test.cpp— 21 unit teststests/MLCore/ClassificationPipeline.test.cpp— 9 pipeline-level integration tests
Configuration
PredictionRegionConfig controls prediction scoping:
| Field | Type | Default | Description |
|---|---|---|---|
predict_all_rows |
bool |
true |
Predict on all rows of the training feature tensor |
prediction_tensor_key |
string |
"" |
Separate tensor for prediction (when predict_all_rows is false) |
prediction_interval_key |
string |
"" |
DigitalIntervalSeries key specifying output frames |
constrained_decoding |
bool |
true |
HMM initial-state warm-start from training labels |
When prediction_interval_key is set, the pipeline:
- Computes the bounding span — the temporal union of training label intervals and prediction intervals
- Filters features to the bounding span (so sequence models retain full context)
- Runs prediction on the filtered feature range
- Filters output to only frames within the prediction intervals
Bounding Span
Motivation
Consider training labels on frames 1–1000 and 2000–3000, with a prediction target of 1001–1999:
- Naively predicting only frames 1001–1999 loses temporal context at boundaries
- Bounding span predicts frames 1–3000 (the full union) and filters output to 1001–1999
- The HMM carries state through frame 1000→1001 seamlessly
Since Viterbi complexity is \(O(K^2 \times N)\) where \(K\) = states and \(N\) = frames, this is cheap. For \(K=2\) and \(N=100{,}000\): ~2.4M FLOPs (sub-millisecond).
API
namespace MLCore {
struct BoundingSpan {
TimeFrameIndex min_time;
TimeFrameIndex max_time;
};
/// Compute [min(start), max(end)] across all intervals
std::optional<BoundingSpan> computeIntervalBounds(
DigitalIntervalSeries const & series);
/// Compute [min, max] of a time vector
std::optional<BoundingSpan> computeRowTimeBounds(
std::vector<TimeFrameIndex> const & times);
/// Union of two spans
BoundingSpan mergeSpans(BoundingSpan a, BoundingSpan b);
/// Keep only feature columns whose time falls within [min, max]
FilteredRows filterRowsToSpan(
arma::mat const & features,
std::vector<TimeFrameIndex> const & times,
BoundingSpan span);
/// Keep only predictions at times inside any interval
FilteredPredictions filterPredictionsToIntervals(
arma::Row<std::size_t> const & predictions,
std::optional<arma::mat> const & probabilities,
std::vector<TimeFrameIndex> const & times,
DigitalIntervalSeries const & intervals);
} // namespace MLCorePipeline Integration
In ClassificationPipeline, bounding span logic executes when prediction_interval_key is non-empty:
Stage 6 (Predict) — input filtering:
- Load the prediction
DigitalIntervalSeriesfromDataManager - Compute its bounds; merge with training label interval bounds if available
filterRowsToSpan()restrictspredict_featuresandpredict_row_times- Prediction proceeds on the filtered (but temporally complete) range
Stage 8 (Output) — output filtering:
filterPredictionsToIntervals()restricts predictions, probabilities, and times to only frames within the prediction intervals- Filtered output is passed to
PredictionWriter
Constrained Viterbi Decoding
Motivation
When the prediction range is adjacent to labeled training regions, the HMM can leverage known ground truth at boundaries. If frame 1000 is labeled as state 1 with 100% certainty, the HMM should start from that state when predicting frame 1001 rather than guessing from the prior distribution.
Implementation
For each prediction segment adjacent to labeled data:
- Build a map from
TimeFrameIndex→ training label - For each segment’s first frame, check if there’s a known label at that frame or at the frame immediately before it
- If found, clamp
hmm.Initial()to a delta distribution on that state - Run Viterbi, then restore the original initial distribution
// Simplified pipeline logic:
std::unordered_map<int64_t, std::size_t> label_map;
for (std::size_t i = 0; i < train_times.size(); ++i) {
label_map[train_times[i].getValue()] = train_labels[i];
}
std::vector<std::optional<std::size_t>> constraints;
for (auto const & seg : pred_segments) {
auto first = seg.times.front().getValue();
if (auto it = label_map.find(first); it != label_map.end())
constraints.emplace_back(it->second);
else if (auto it2 = label_map.find(first - 1); it2 != label_map.end())
constraints.emplace_back(it2->second);
else
constraints.emplace_back(std::nullopt);
}
model->predictSequencesConstrained(feature_seqs, pred_seqs, constraints);Scope
- Applies only to sequence models (
isSequenceModel() == true) - Gated by
config.prediction_region.constrained_decoding - Frame-independent models (Random Forest, etc.) ignore it entirely
- The
MLModelOperationbase class default delegates topredictSequences()
Widget Integration
- PredictionPanel:
DigitalIntervalSeriescombo box setsprediction_interval_key. “Constrained decoding” checkbox (visible only for sequence models) setsconstrained_decoding. - State:
prediction_region_keyandconstrained_decodingfields inMLCoreWidgetStateDatawith bidirectional sync.
Test Coverage
Unit Tests (BoundingSpan.test.cpp)
| Category | Tests |
|---|---|
computeIntervalBounds |
Empty, single, multiple, overlapping intervals |
computeRowTimeBounds |
Empty, single, multiple elements |
mergeSpans |
Disjoint, overlapping, identical, commutativity |
filterRowsToSpan |
Full coverage, subset, empty result, inclusive endpoints, non-contiguous |
filterPredictionsToIntervals |
Single interval, multiple intervals, with probabilities, empty result, empty series, full coverage |
Pipeline Integration Tests (ClassificationPipeline.test.cpp)
| Test | Verifies |
|---|---|
| Prediction interval restricts output frames | Output is filtered to 50 frames in the prediction region |
| Prediction interval with Random Forest | Frame-independent models also respect output filtering |
| Empty prediction interval key means no filtering | All rows predicted when key is empty |
| Constrained decoding produces predictions | Pipeline completes successfully with constraints |
| Constrained vs unconstrained accuracy comparison | Both modes produce valid results on clean data |
| Constrained decoding ignored for non-sequence models | Random Forest works fine with constrained_decoding=true |
| HMM with diagonal covariance | Pipeline trains and predicts using diagonal variant |
| Diagonal covariance with event labels | Diagonal HMM + event label source combination |
| Prediction region + constrained + diagonal combined | All Phase 8 features used together |
| Missing prediction interval key fails gracefully | Non-existent key produces clear error |