Normalize#

class torch_ecg.preprocessors.Normalize(method: Literal['naive', 'min-max', 'z-score'] = 'z-score', mean: Real | Iterable[Real] = 0.0, std: Real | Iterable[Real] = 1.0, per_channel: bool = False, inplace: bool = True, **kwargs: Any)[source]#

Bases: Module

Normalization preprocessor.

This preprocessor performs z-score normalization on sig, to make it has fixed mean and standard deviation, or performs min-max normalization on sig, or normalizes sig using mean and std \(via (sig - mean) / std\). More precisely,

\[\begin{split}\begin{align*} \text{Min-Max normalization:}\quad & \frac{sig - \min(sig)}{\max(sig) - \min(sig)} \\ \text{Naive normalization:}\quad & \frac{sig - m}{s} \\ \text{Z-score normalization:}\quad & \left(\frac{sig - \operatorname{mean}(sig)}{\operatorname{std}(sig)}\right) \cdot s + m \end{align*}\end{split}\]
Parameters:
  • method ({"naive", "min-max", "z-score"}, default "z-score",) – Normalization method, by default “z-score”, case-insensitive.

  • mean (numbers.Real or array_like, default 0.0) – If method is “z-score”, then mean is the mean value of the normalized signal, or mean values for each lead of the normalized signal. If `method is “naive”, then mean is the mean value to be subtracted from the original signal. Useless if method is "min-max".

  • std (numbers.Real or array_like, default 1.0) – If method is “z-score”, then std is the standard deviation of the normalized signal, or standard deviations for each lead of the normalized signal. If method is “naive”, then std is the standard deviation to be divided from the original signal. Useless if method is "min-max".

  • per_channel (bool, default False) – Whether to perform the normalization per channel.

  • inplace (bool, default True) – Whether to perform the normalization in-place.

forward(sig: ndarray | Tensor) ndarray | Tensor[source]#

Apply the preprocessor to the signal.

Parameters:

sig (numpy.ndarray or torch.Tensor) – The input signal, of shape (batch_size, num_leads, num_samples) or (num_leads, num_samples).

Returns:

The normalized signal, of same shape and type as sig.

Return type:

numpy.ndarray or torch.Tensor