Skip to content

Running Stats

Source

Computing data statistics.

WelfordStatistics

Compute Welford statistics iteratively.

The Welford algorithm is used to compute the mean and variance of an array iteratively. Based on the implementation from: https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance#Welford's_online_algorithm

finalize()

Finalize the Welford statistics.

Returns:

Type Description
tuple or numpy arrays

Final mean and standard deviation.

update(array, sample_idx)

Update the Welford statistics.

Parameters:

Name Type Description Default
array NDArray

Input array.

required
sample_idx int

Current sample number.

required

compute_normalization_stats(image)

Compute mean and standard deviation of an array.

Expected input shape is (S, C, (Z), Y, X). The mean and standard deviation are computed per channel.

Parameters:

Name Type Description Default
image NDArray

Input array.

required

Returns:

Type Description
tuple of (list of floats, list of floats)

Lists of mean and standard deviation values per channel.

finalize_iterative_stats(count, mean, m2)

Finalize the mean and variance computation.

Parameters:

Name Type Description Default
count NDArray

Number of elements in the array. Shape: (C,).

required
mean NDArray

Mean of the array. Shape: (C,).

required
m2 NDArray

Variance of the array. Shape: (C,).

required

Returns:

Type Description
tuple[NDArray, NDArray]

Final channel-wise mean and standard deviation.

update_iterative_stats(count, mean, m2, new_values)

Update the mean and variance of an array iteratively.

Parameters:

Name Type Description Default
count NDArray

Number of elements in the array. Shape: (C,).

required
mean NDArray

Mean of the array. Shape: (C,).

required
m2 NDArray

Variance of the array. Shape: (C,).

required
new_values NDArray

New values to add to the mean and variance. Shape: (C, 1, 1, Z, Y, X).

required

Returns:

Type Description
tuple[NDArray, NDArray, NDArray]

Updated count, mean, and variance.