Source code for nav.nav_orchestrator.image_classifier

"""NavImageClassifier — quick-fail classifier for incoming images.

Operates on the entire sensor area and assigns an image to one of a small
set of classes.  Most "bad" classes never invoke an extractor —
corrupted images fail in milliseconds with a clear reason.

The classifier is global: no predicted feature positions are used.  Three
cheap statistics drive the decision:

    saturation_frac = fraction of pixels at saturation_threshold_dn or above
    missing_frac    = fraction of pixels equal to missing_data_marker_dn
    noise_sigma     = MAD-based image noise sigma

Per-instrument thresholds live in ``config_4N0_inst_*.yaml``; this module
takes them as constructor parameters so it stays pure-Python and unit-
testable without loading config.
"""

from dataclasses import dataclass, field

import numpy as np

from nav.nav_orchestrator.image_classifier_result import (
    ImageFlag,
    NavImageClassifierResult,
)
from nav.support.noise_estimate import estimate_image_noise_sigma
from nav.support.types import NDArrayBoolType, NDArrayFloatType

__all__ = [
    'ImageQualityThresholds',
    'NavImageClassifier',
]


[docs] @dataclass(frozen=True) class ImageQualityThresholds: """Per-instrument configuration for the image-quality classifier. Parameters: saturation_threshold_dn: Pixels at or above this DN are flagged saturated. missing_data_marker_dn: Pixels exactly equal to this value are treated as missing data (instrument-specific dropout marker). max_saturation_frac_clean: Above this fraction of saturated pixels the image is ``fully_overexposed``. max_missing_frac_clean: Above this fraction of missing pixels the image is ``mostly_missing_data``. partial_dropout_min_frac: Below this missing fraction, no ``partial_dropout`` flag is raised. At or above this fraction (and below ``max_missing_frac_clean``) the ``partial_dropout`` advisory flag is set on the result. blank_max_dn: If the image's max DN is below this, the image is ``blank``. noisy_threshold: Above this MAD-noise sigma, the ``noisy`` flag is raised (image stays ``clean``). """ saturation_threshold_dn: float = 4095.0 missing_data_marker_dn: float = 0.0 max_saturation_frac_clean: float = 0.80 max_missing_frac_clean: float = 0.30 partial_dropout_min_frac: float = 0.05 blank_max_dn: float = 5.0 noisy_threshold: float = 10.0