Skip to content

modeler

py3dinterpolations.modelling.modeler

High-level modelling orchestrator.

Modeler(griddata, grid, model)

Orchestrates fitting a model and predicting on a 3D grid.

Handles normalization-aware grid selection and standardization reversal.

Parameters:

Name Type Description Default
griddata GridData

Training data.

required
grid Grid3D

3D grid for predictions.

required
model BaseModel

Fitted or unfitted BaseModel instance. Will be fit on construction.

required
Source code in py3dinterpolations/modelling/modeler.py
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
def __init__(
    self,
    griddata: GridData,
    grid: Grid3D,
    model: BaseModel,
):
    self._griddata = griddata
    self._grid = grid
    self._model = model
    self._result: InterpolationResult | None = None

    # Fit the model on training data
    data = griddata.numpy_data
    self._model.fit(data[:, 0], data[:, 1], data[:, 2], data[:, 3])
    logger.info("Model %s fitted on %d points", model.name, len(data))

predict(**kwargs)

Make predictions, handling normalization and standardization reversal.

Returns:

Type Description
ndarray

Interpolated numpy array.

Source code in py3dinterpolations/modelling/modeler.py
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
def predict(self, **kwargs: object) -> np.ndarray:
    """Make predictions, handling normalization and standardization reversal.

    Returns:
        Interpolated numpy array.
    """
    logger.info("Starting prediction on grid %s", self._grid)

    # Use normalized grid if normalization was applied
    params = self._griddata.preprocessing_params
    if params is not None and params.normalization is not None:
        grid_arrays = self._grid.normalized_grid
    else:
        grid_arrays = self._grid.grid

    # Predict
    result = self._model.predict(
        grid_arrays["X"],
        grid_arrays["Y"],
        grid_arrays["Z"],
        **kwargs,
    )

    interpolated = result.interpolated
    variance = result.variance

    # Reverse standardization if it was applied
    if params is not None and params.standardization is not None:
        std_params = params.standardization
        interpolated = interpolated * std_params.std + std_params.mean
        if variance is not None:
            # Variance scales with the square of the standard deviation
            # and is not shifted by the mean
            variance = variance * (std_params.std**2)

    self._result = InterpolationResult(
        interpolated=interpolated,
        variance=variance,
        probability=result.probability,
    )

    # Also attach to grid
    self._grid.result = self._result

    logger.info("Prediction complete")
    return interpolated