Skip to content

prediction_outputs

Module containing functions to convert prediction outputs to desired form.

combine_batches(predictions, tiled) #

combine_batches(predictions: List[Any], tiled: Literal[True]) -> Tuple[List[NDArray], List[TileInformation]]
combine_batches(predictions: List[Any], tiled: Literal[False]) -> List[NDArray]
combine_batches(predictions: List[Any], tiled: Union[bool, Literal[True], Literal[False]]) -> Union[List[NDArray], Tuple[List[NDArray], List[TileInformation]]]

If predictions are in batches, they will be combined.

Parameters:

Name Type Description Default
predictions list

Predictions that are output from Trainer.predict.

required
tiled bool

Whether the predictions are tiled.

required

Returns:

Type Description
(list of numpy.ndarray) or tuple of (list of numpy.ndarray, list of TileInformation)

Combined batches.

Source code in src/careamics/prediction_utils/prediction_outputs.py
def combine_batches(
    predictions: List[Any], tiled: bool
) -> Union[List[NDArray], Tuple[List[NDArray], List[TileInformation]]]:
    """
    If predictions are in batches, they will be combined.

    Parameters
    ----------
    predictions : list
        Predictions that are output from `Trainer.predict`.
    tiled : bool
        Whether the predictions are tiled.

    Returns
    -------
    (list of numpy.ndarray) or tuple of (list of numpy.ndarray, list of TileInformation)
        Combined batches.
    """
    if tiled:
        return _combine_tiled_batches(predictions)
    else:
        return _combine_array_batches(predictions)

convert_outputs(predictions, tiled) #

Convert the Lightning trainer outputs to the desired form.

This method allows stitching back together tiled predictions.

Parameters:

Name Type Description Default
predictions list

Predictions that are output from Trainer.predict.

required
tiled bool

Whether the predictions are tiled.

required

Returns:

Type Description
list of numpy.ndarray or numpy.ndarray

List of arrays with the axes SC(Z)YX. If there is only 1 output it will not be in a list.

Source code in src/careamics/prediction_utils/prediction_outputs.py
def convert_outputs(predictions: List[Any], tiled: bool) -> list[NDArray]:
    """
    Convert the Lightning trainer outputs to the desired form.

    This method allows stitching back together tiled predictions.

    Parameters
    ----------
    predictions : list
        Predictions that are output from `Trainer.predict`.
    tiled : bool
        Whether the predictions are tiled.

    Returns
    -------
    list of numpy.ndarray or numpy.ndarray
        List of arrays with the axes SC(Z)YX. If there is only 1 output it will not
        be in a list.
    """
    if len(predictions) == 0:
        return predictions

    # this layout is to stop mypy complaining
    if tiled:
        predictions_comb = combine_batches(predictions, tiled)
        predictions_output = stitch_prediction(*predictions_comb)
    else:
        predictions_output = combine_batches(predictions, tiled)

    return predictions_output