Skip to content

API Reference

This page provides detailed documentation for all public functions in the physics_informed_ml_odw package.

Module: physics_informed_ml_odw

predict_2d(image, model='ML')

Predict ODW values for a 2D image using a pre-trained model.

Parameters:

Name Type Description Default
image Image

The input image from sensingpy.

required
model Literal['ML', 'CS_ML']

The model type to use for prediction: - "ML": Machine Learning model - "CS_ML": Caballero&Stumpf Machine Learning model

"ML"

Returns:

Type Description
ndarray

2D array with predicted ODW values.

Source code in src\physics_informed_ml_odw\predict.py
def predict_2d(image: Image, model: Literal["ML", "CS_ML"] = "ML") -> np.ndarray:
    """Predict ODW values for a 2D image using a pre-trained model.

    Parameters
    ----------
    image : Image
        The input image from sensingpy.
    model : Literal["ML", "CS_ML"], default="ML"
        The model type to use for prediction:
        - "ML": Machine Learning model
        - "CS_ML": Caballero&Stumpf Machine Learning model

    Returns
    -------
    np.ndarray
        2D array with predicted ODW values.
    """
    pipeline = load_model(model)

    X = pd.DataFrame({band: image.select(band).ravel() for band in pipeline.feature_names_in_}).dropna()

    ODW = np.full(shape=(image.height, image.width), fill_value=np.nan, dtype=X.dtypes.iloc[0]).ravel()
    ODW[X.index] = pipeline.predict(X)
    ODW = ODW.reshape((image.height, image.width))

    return ODW

load_model(model)

Load a pre-trained model from the package resources.

Parameters:

Name Type Description Default
model Literal['ML', 'CS_ML']

The model type to load.

required

Returns:

Type Description
Pipeline

The loaded scikit-learn pipeline.

Raises:

Type Description
ValueError

If the model type is not recognized.

Source code in src\physics_informed_ml_odw\predict.py
def load_model(model: Literal["ML", "CS_ML"]) -> Pipeline:
    """Load a pre-trained model from the package resources.

    Parameters
    ----------
    model : Literal["ML", "CS_ML"]
        The model type to load.

    Returns
    -------
    Pipeline
        The loaded scikit-learn pipeline.

    Raises
    ------
    ValueError
        If the model type is not recognized.
    """
    if model not in MODEL_FILES:
        raise ValueError(f"Model must be one of {list(MODEL_FILES.keys())}, got '{model}'")

    model_filename = MODEL_FILES[model]
    with resources.files("physics_informed_ml_odw.models").joinpath(model_filename).open("rb") as f:
        return joblib.load(f)

Functions

predict_2d

predict_2d(image: Image, model: Literal["ML", "CS_ML"] = "ML") -> np.ndarray

Predict ODW values for a 2D image using a pre-trained model.

Parameters

Name Type Default Description
image Image required The input image from sensingpy
model Literal["ML", "CS_ML"] "ML" The model type to use for prediction

Returns

Type Description
np.ndarray 2D array with predicted ODW values

Model Options

  • "ML": Standard Machine Learning model
  • "CS_ML": Caballero & Stumpf Machine Learning model

Example

from sensingpy import reader
from physics_informed_ml_odw import predict_2d

image = reader.open('satellite_image.tif')
odw_prediction = predict_2d(image, model='ML')

load_model

load_model(model: Literal["ML", "CS_ML"]) -> Pipeline

Load a pre-trained model from the package resources.

Parameters

Name Type Default Description
model Literal["ML", "CS_ML"] required The model type to load

Returns

Type Description
Pipeline The loaded scikit-learn pipeline

Raises

Exception Description
ValueError If the model type is not recognized

Example

from physics_informed_ml_odw import load_model

# Load the ML model pipeline
pipeline = load_model('ML')

# Access model components
print(pipeline.feature_names_in_)

Constants

MODEL_FILES

Dictionary mapping model names to their corresponding pickle files.

MODEL_FILES = {
    "ML": "ML__MLP.pkl",
    "CS_ML": "CS_ML__MLP.pkl",
}