Class Compatibility Policy¶
This document defines the behavioral policy for class-average size compatibility in SIMPLE, implemented by the module ../../src/main/class/simple_class_compatibility.f90.
The policy governs how support-model fitting, inference, convergence, and telemetry behave. It is intended to make rejection decisions stable, explainable, and safe to extend.
1. Scope¶
The class compatibility workflow is responsible for:
- preprocessing class averages into binary support masks;
- extracting Feret min/max dimensions per class average;
- fitting latent support bounds
(c, b, a)from selected classes; - rejecting size-incompatible classes during inference;
- exposing fit metrics and convergence state for monitoring.
It is not responsible for quality-feature extraction, class-average quality modeling, or queue/chunk orchestration. Those remain owned by sieve and quality modules.
2. Public Contract¶
Public surface (type-bound methods on class_compatibility):
new,kill,kill_support_modeltrainvia generic (train_1on project,train_2on stack)inferconvergedget_support_model_metrics
Public metrics type:
support_model_metricswith fields:axis_c,axis_b,axis_adelta_c,delta_b,delta_adelta_valid,valid,converged
3. Core Semantics¶
3.1 Validity¶
valid means only one thing: the module currently has a fitted axis model.
valid=.false.when no successful fit has occurred, or afterkill_support_model.validmust not be used as a proxy for training batch presence.
3.2 Convergence¶
Convergence is axis-stability based and requires a previous fit.
Given previous axes (c_prev, b_prev, a_prev) and current axes (c, b, a),
axis deltas are:
dc = abs(c - c_prev)db = abs(b - b_prev)da = abs(a - a_prev)
An axis is stable when either absolute or relative tolerance passes:
d <= CONV_ABS_EPS- or
d <= CONV_REL_EPS * max(abs(prev), abs(curr), 1.0)
converged=.true. only when:
- a previous valid fit exists, and
- all three axes are stable.
3.3 Delta Validity¶
delta_valid indicates that delta values compare two real fits.
- First successful fit:
delta_valid=.false.. - Subsequent successful fits:
delta_valid=.true..
4. Training Policy¶
Training data is appended as batches (training_set(:)), each batch storing
vector pairs (min_dim(:), max_dim(:)) extracted from selected classes.
Policy requirements:
- each appended batch must satisfy
size(min_dim) == size(max_dim); - batches with fewer than 3 selected classes are ignored;
- update must preserve prior valid fit if no new successful fit is found;
- batch weighting must be balanced so each batch contributes equal total weight regardless of batch size.
Fitting strategy policy:
- grid search over
relax,qlo,qhicandidates; - stage 1: choose
bmaximizing weighted in-interval support; - stage 2: estimate
candafrom weighted quantiles on the support set; - enforce axis ordering invariant
c <= b <= avia clamped fallback; - score preference is: support first, compactness second, lower relaxation third.
5. Inference Policy¶
Inference is state-preserving unless a class is proven incompatible.
For each active class:
- preprocess image and compute
(min_dim, max_dim); - if model is invalid, skip compatibility rejection;
- test compatibility against relaxed
binterval and relaxed globalc/abounds; - apply edge-rescue slack when strict compatibility fails;
- incompatible class must be set to
state=0and receive rejection reasonclass_compatibility: size_incompatible_subset.
After per-class decisions, selection must be propagated through
map_cavgs_selection.
6. Preprocessing Policy¶
Preprocessing must remain deterministic and minimal:
- edge-average removal + low-pass bandpass
- resize to fixed working box (
PREPROCESS_BOXSIZE) - Otsu segmentation
- fixed morphological closing depth (
PREPROCESS_MORPH_SIZE) - largest connected component extraction
- Feret min/max measurement on final mask
Changes to preprocessing parameters are policy-significant and require re-baselining tests and downstream acceptance criteria.
7. Observability Requirements¶
Callers (notably sieve orchestration) should log:
- current axes
a/b/c - deltas
da/db/dc valid,delta_valid,converged
Convergence transitions should be logged explicitly when first observed.
8. Failure Handling¶
The module must fail fast on structural inconsistencies:
- image/state size mismatch
- malformed training batch allocation state
- min/max vector length mismatch
Non-fatal conditions (no fit update) must return cleanly without corrupting existing valid model state.
9. Test Policy¶
Policy-level tests must cover:
- default state and lifecycle reset behavior
- first-fit validity semantics (
valid=true,delta_valid=false) - repeat-fit convergence behavior (
delta_valid=true, stable deltas) - infer behavior with invalid model (no forced rejection)
- infer behavior with trained model (outlier rejection)
- small-batch no-fit path
- kill/reset semantics after valid fit
Reference tester module: ../../src/main/class/simple_class_compatibility_tester.f90.
10. Change Checklist¶
When modifying class compatibility behavior:
- preserve
validas fitted-axis validity only; - preserve axis ordering invariant
c <= b <= a; - preserve convergence gate requiring prior fit and 3-axis stability;
- keep rejection reason string stable unless migration is intentional;
- update tester coverage for any policy-level behavior change;
- update this policy document in the same change when contract changes.