Skip to content

patching

Patching functions.

PatchedOutput dataclass #

Dataclass to store patches and statistics.

Source code in src/careamics/dataset/patching/patching.py
@dataclass
class PatchedOutput:
    """Dataclass to store patches and statistics."""

    patches: Union[NDArray]
    """Image patches."""

    targets: Union[NDArray, None]
    """Target patches."""

    image_stats: Stats
    """Statistics of the image patches."""

    target_stats: Stats
    """Statistics of the target patches."""

image_stats: Stats instance-attribute #

Statistics of the image patches.

patches: Union[NDArray] instance-attribute #

Image patches.

target_stats: Stats instance-attribute #

Statistics of the target patches.

targets: Union[NDArray, None] instance-attribute #

Target patches.

Stats dataclass #

Dataclass to store statistics.

Source code in src/careamics/dataset/patching/patching.py
@dataclass
class Stats:
    """Dataclass to store statistics."""

    means: Union[NDArray, tuple, list, None]
    """Mean of the data across channels."""

    stds: Union[NDArray, tuple, list, None]
    """Standard deviation of the data across channels."""

    def get_statistics(self) -> tuple[list[float], list[float]]:
        """Return the means and standard deviations.

        Returns
        -------
        tuple of two lists of floats
            Means and standard deviations.
        """
        if self.means is None or self.stds is None:
            return [], []

        return list(self.means), list(self.stds)

means: Union[NDArray, tuple, list, None] instance-attribute #

Mean of the data across channels.

stds: Union[NDArray, tuple, list, None] instance-attribute #

Standard deviation of the data across channels.

get_statistics() #

Return the means and standard deviations.

Returns:

Type Description
tuple of two lists of floats

Means and standard deviations.

Source code in src/careamics/dataset/patching/patching.py
def get_statistics(self) -> tuple[list[float], list[float]]:
    """Return the means and standard deviations.

    Returns
    -------
    tuple of two lists of floats
        Means and standard deviations.
    """
    if self.means is None or self.stds is None:
        return [], []

    return list(self.means), list(self.stds)

prepare_patches_supervised(train_files, target_files, axes, patch_size, read_source_func) #

Iterate over data source and create an array of patches and corresponding targets.

The lists of Paths should be pre-sorted.

Parameters:

Name Type Description Default
train_files list of pathlib.Path

List of paths to training data.

required
target_files list of pathlib.Path

List of paths to target data.

required
axes str

Axes of the data.

required
patch_size list or tuple of int

Size of the patches.

required
read_source_func Callable

Function to read the data.

required

Returns:

Type Description
ndarray

Array of patches.

Source code in src/careamics/dataset/patching/patching.py
def prepare_patches_supervised(
    train_files: list[Path],
    target_files: list[Path],
    axes: str,
    patch_size: Union[list[int], tuple[int, ...]],
    read_source_func: Callable,
) -> PatchedOutput:
    """
    Iterate over data source and create an array of patches and corresponding targets.

    The lists of Paths should be pre-sorted.

    Parameters
    ----------
    train_files : list of pathlib.Path
        List of paths to training data.
    target_files : list of pathlib.Path
        List of paths to target data.
    axes : str
        Axes of the data.
    patch_size : list or tuple of int
        Size of the patches.
    read_source_func : Callable
        Function to read the data.

    Returns
    -------
    np.ndarray
        Array of patches.
    """
    means, stds, num_samples = 0, 0, 0
    all_patches, all_targets = [], []
    for train_filename, target_filename in zip(train_files, target_files):
        try:
            sample: np.ndarray = read_source_func(train_filename, axes)
            target: np.ndarray = read_source_func(target_filename, axes)
            means += sample.mean()
            stds += sample.std()
            num_samples += 1

            # reshape array
            sample = reshape_array(sample, axes)
            target = reshape_array(target, axes)

            # generate patches, return a generator
            patches, targets = extract_patches_sequential(
                sample, patch_size=patch_size, target=target
            )

            # convert generator to list and add to all_patches
            all_patches.append(patches)

            # ensure targets are not None (type checking)
            if targets is not None:
                all_targets.append(targets)
            else:
                raise ValueError(f"No target found for {target_filename}.")

        except Exception as e:
            # emit warning and continue
            logger.error(f"Failed to read {train_filename} or {target_filename}: {e}")

    # raise error if no valid samples found
    if num_samples == 0 or len(all_patches) == 0:
        raise ValueError(
            f"No valid samples found in the input data: {train_files} and "
            f"{target_files}."
        )

    image_means, image_stds = compute_normalization_stats(np.concatenate(all_patches))
    target_means, target_stds = compute_normalization_stats(np.concatenate(all_targets))

    patch_array: np.ndarray = np.concatenate(all_patches, axis=0)
    target_array: np.ndarray = np.concatenate(all_targets, axis=0)
    logger.info(f"Extracted {patch_array.shape[0]} patches from input array.")

    return PatchedOutput(
        patch_array,
        target_array,
        Stats(image_means, image_stds),
        Stats(target_means, target_stds),
    )

prepare_patches_supervised_array(data, axes, data_target, patch_size) #

Iterate over data source and create an array of patches.

This method expects an array of shape SC(Z)YX, where S and C can be singleton dimensions.

Patches returned are of shape SC(Z)YX, where S is now the patches dimension.

Parameters:

Name Type Description Default
data ndarray

Input data array.

required
axes str

Axes of the data.

required
data_target ndarray

Target data array.

required
patch_size list or tuple of int

Size of the patches.

required

Returns:

Type Description
PatchedOutput

Dataclass holding the source and target patches, with their statistics.

Source code in src/careamics/dataset/patching/patching.py
def prepare_patches_supervised_array(
    data: NDArray,
    axes: str,
    data_target: NDArray,
    patch_size: Union[list[int], tuple[int]],
) -> PatchedOutput:
    """Iterate over data source and create an array of patches.

    This method expects an array of shape SC(Z)YX, where S and C can be singleton
    dimensions.

    Patches returned are of shape SC(Z)YX, where S is now the patches dimension.

    Parameters
    ----------
    data : numpy.ndarray
        Input data array.
    axes : str
        Axes of the data.
    data_target : numpy.ndarray
        Target data array.
    patch_size : list or tuple of int
        Size of the patches.

    Returns
    -------
    PatchedOutput
        Dataclass holding the source and target patches, with their statistics.
    """
    # reshape array
    reshaped_sample = reshape_array(data, axes)
    reshaped_target = reshape_array(data_target, axes)

    # compute statistics
    image_means, image_stds = compute_normalization_stats(reshaped_sample)
    target_means, target_stds = compute_normalization_stats(reshaped_target)

    # generate patches, return a generator
    patches, patch_targets = extract_patches_sequential(
        reshaped_sample, patch_size=patch_size, target=reshaped_target
    )

    if patch_targets is None:
        raise ValueError("No target extracted.")

    logger.info(f"Extracted {patches.shape[0]} patches from input array.")

    return PatchedOutput(
        patches,
        patch_targets,
        Stats(image_means, image_stds),
        Stats(target_means, target_stds),
    )

prepare_patches_unsupervised(train_files, axes, patch_size, read_source_func) #

Iterate over data source and create an array of patches.

This method returns the mean and standard deviation of the image.

Parameters:

Name Type Description Default
train_files list of pathlib.Path

List of paths to training data.

required
axes str

Axes of the data.

required
patch_size list or tuple of int

Size of the patches.

required
read_source_func Callable

Function to read the data.

required

Returns:

Type Description
PatchedOutput

Dataclass holding patches and their statistics.

Source code in src/careamics/dataset/patching/patching.py
def prepare_patches_unsupervised(
    train_files: list[Path],
    axes: str,
    patch_size: Union[list[int], tuple[int]],
    read_source_func: Callable,
) -> PatchedOutput:
    """Iterate over data source and create an array of patches.

    This method returns the mean and standard deviation of the image.

    Parameters
    ----------
    train_files : list of pathlib.Path
        List of paths to training data.
    axes : str
        Axes of the data.
    patch_size : list or tuple of int
        Size of the patches.
    read_source_func : Callable
        Function to read the data.

    Returns
    -------
    PatchedOutput
        Dataclass holding patches and their statistics.
    """
    means, stds, num_samples = 0, 0, 0
    all_patches = []
    for filename in train_files:
        try:
            sample: np.ndarray = read_source_func(filename, axes)
            means += sample.mean()
            stds += sample.std()
            num_samples += 1

            # reshape array
            sample = reshape_array(sample, axes)

            # generate patches, return a generator
            patches, _ = extract_patches_sequential(sample, patch_size=patch_size)

            # convert generator to list and add to all_patches
            all_patches.append(patches)
        except Exception as e:
            # emit warning and continue
            logger.error(f"Failed to read {filename}: {e}")

    # raise error if no valid samples found
    if num_samples == 0:
        raise ValueError(f"No valid samples found in the input data: {train_files}.")

    image_means, image_stds = compute_normalization_stats(np.concatenate(all_patches))

    patch_array: np.ndarray = np.concatenate(all_patches)
    logger.info(f"Extracted {patch_array.shape[0]} patches from input array.")

    return PatchedOutput(
        patch_array, None, Stats(image_means, image_stds), Stats((), ())
    )

prepare_patches_unsupervised_array(data, axes, patch_size) #

Iterate over data source and create an array of patches.

This method expects an array of shape SC(Z)YX, where S and C can be singleton dimensions.

Patches returned are of shape SC(Z)YX, where S is now the patches dimension.

Parameters:

Name Type Description Default
data ndarray

Input data array.

required
axes str

Axes of the data.

required
patch_size list or tuple of int

Size of the patches.

required

Returns:

Type Description
PatchedOutput

Dataclass holding the patches and their statistics.

Source code in src/careamics/dataset/patching/patching.py
def prepare_patches_unsupervised_array(
    data: NDArray,
    axes: str,
    patch_size: Union[list[int], tuple[int]],
) -> PatchedOutput:
    """
    Iterate over data source and create an array of patches.

    This method expects an array of shape SC(Z)YX, where S and C can be singleton
    dimensions.

    Patches returned are of shape SC(Z)YX, where S is now the patches dimension.

    Parameters
    ----------
    data : numpy.ndarray
        Input data array.
    axes : str
        Axes of the data.
    patch_size : list or tuple of int
        Size of the patches.

    Returns
    -------
    PatchedOutput
        Dataclass holding the patches and their statistics.
    """
    # reshape array
    reshaped_sample = reshape_array(data, axes)

    # calculate mean and std
    means, stds = compute_normalization_stats(reshaped_sample)

    # generate patches, return a generator
    patches, _ = extract_patches_sequential(reshaped_sample, patch_size=patch_size)

    return PatchedOutput(patches, None, Stats(means, stds), Stats((), ()))