Skip to content

Normalization

treat2p.normalisation.delta_over_F

Performs deltaF over F0 ( with eq : DF0 = F - F0 / F0 )

Source code in src/treat2p/normalisation.py
@filter_save_kwargs("array")
def delta_over_F(
    array: np.ndarray, *, F0_index: float = 0, F0_span: float = 1, fig: Optional[RoiFigure] = None
) -> np.ndarray:
    """Performs deltaF over F0 ( with eq : DF0 = F - F0 / F0 )"""

    if array.min() <= 0:
        array = non_zero_raw_fluorescence(array.copy())

    F0_frame = array[F0_index : F0_index + F0_span].mean(axis=0)

    F0_frames = np.repeat(F0_frame[np.newaxis], array.shape[0], axis=0)

    df_array = (array - F0_frames) / F0_frames

    if fig is not None:
        fig.df_ax.plot(df_array, label="DF/F0", lw=0.05)
    return df_array

treat2p.normalisation.center_normalize

Normalizes and centers an input array around its mean. The values are then scaled so that the mean will be at equidistance of outmin and outmax in the output array. The relationship between outmin and outmax and the original data values are : outmin, outmax values are snapped from - , + (respectively) of max( abolute (min) vs abolute(max)) from the values of the data after mean substraction (at 0)

It accepts an input array, subtracts its mean (thus centering it around zero), and then normalizes it to be within a specified range (outmin and outmax). By default, the range is between 0 and 1.

Parameters:

Name Type Description Default
array ndarray

Input array to be centered and normalized.

required
outmin float

Minimum value for the output normalized array. Defaults to 0.

0
outmax float

Maximum value for the output normalized array. Defaults to 1.

1
percentile float | None

The maximum percentile that corresponds to the un-normalized value that will snap to the output bounds (symetrically around the mean) If set to None (default) the current value snapped to the bounds is the maximum absolute value between array.min() and array.max() (e.g. normalization via scaling but not translation, to stay centered around the mean at 0, that occurs at the 2n line in the code)

None

Returns:

Type Description
ndarray

numpy.ndarray: Normalized and centered array with values ranging between specified minimum and maximum values.

Source code in src/treat2p/normalisation.py
def center_normalize(
    array: np.ndarray,
    *,
    outmin: float = 0,
    outmax: float = 1,
    percentile: Optional[float] = None,
    fig: Optional[RoiFigure] = None,
) -> np.ndarray:
    """Normalizes and centers an input array around its mean.
    The values are then scaled so that the mean will be at equidistance of outmin and outmax in the output array.
    The relationship between outmin and outmax and the original data values are :
    outmin, outmax values are snapped from - , + (respectively) of max( abolute (min) vs abolute(max))
    from the values of the data after mean substraction (at 0)

    It accepts an input array, subtracts its mean (thus centering it around zero),
    and then normalizes it to be within a specified range (outmin and outmax).
    By default, the range is between 0 and 1.

    Args:
        array: Input array to be centered and normalized.
        outmin: Minimum value for the output normalized array. Defaults to 0.
        outmax: Maximum value for the output normalized array. Defaults to 1.
        percentile: The maximum percentile that corresponds to the un-normalized value that
            will snap to the output bounds (symetrically around the mean)
            If set to None (default) the current value snapped to the bounds is the maximum absolute value
            between array.min() and array.max() (e.g. normalization via scaling but not translation,
            to stay centered around the mean at 0, that occurs at the 2n line in the code)

    Returns:
        numpy.ndarray: Normalized and centered array with values ranging between specified minimum and maximum values.
    """

    mean = array.mean()
    centered_array = array - mean
    if percentile:
        outermost_bound = max(
            abs(np.percentile(centered_array, 100 - percentile)), abs(np.percentile(centered_array, percentile))
        )
    else:
        outermost_bound = max(abs(centered_array.min()), abs(centered_array.max()))
    normalized_array = normalize(
        centered_array, min_val=-outermost_bound, max_val=outermost_bound, outmin=outmin, outmax=outmax
    )
    if fig is not None:
        fig.norm_ax.plot(normalized_array, label="normalized", lw=0.05)
        fig.norm_ax.axhline(0.5, label="center", ls="--", color="gray")
        fig.norm_ax.axhline(outmin, label="min", ls="--", color="gray")
        fig.norm_ax.axhline(outmax, label="max", ls="--", color="gray")
    return normalized_array