Skip to content

Sample rate analysis

sample_rate(ds, unit='s')

Obtain an array of time difference between two consecutive samples

If the index it's a timestamp, the time difference will be converted to the provided unit

Parameters:

Name Type Description Default
ds AbstractPDMDataset

The dataset

required
unit str

Unit to convert the timestamps differences

's'

Returns:

Type Description
ndarray

Array of time differences

Source code in ceruleo/dataset/analysis/sample_rate.py
def sample_rate(ds: AbstractPDMDataset, unit: str = "s") -> np.ndarray:
    """Obtain an array of time difference between two consecutive samples

    If the index it's a timestamp, the time difference will be converted to the provided unit

    Parameters:
        ds: The dataset
        unit: Unit to convert the timestamps differences

    Returns:
        Array of time differences

    """
    time_diff : List[float ]= []
    for life in ds:
        diff = np.diff(life.index.values)
        diff = diff[diff <= np.median(diff)]
        if pd.api.types.is_timedelta64_ns_dtype(diff.dtype):
            diff = diff / np.timedelta64(1, unit)
        time_diff.extend(diff)
    return np.array(time_diff)

sample_rate_summary(ds, unit='s')

Obtain the mean, median and standard deviation of the sample rate of the dataset

Parameters:

Name Type Description Default
ds AbstractPDMDataset

The dataset

required
unit str

Unit to convert the time differences

's'

Returns:

Type Description
SampleRateAnalysis

A SampleRateAnalysis with the following information: Mean sample rate, Std sample rate, Mode sample rate

Source code in ceruleo/dataset/analysis/sample_rate.py
def sample_rate_summary(
    ds: AbstractPDMDataset, unit: str = "s"
) -> SampleRateAnalysis:
    """
    Obtain the mean, median and standard deviation of the sample rate of the dataset

    Parameters:
        ds: The dataset
        unit: Unit to convert the time differences

    Returns:
        A SampleRateAnalysis with the following information: Mean sample rate, Std sample rate, Mode sample rate
    """
    sr = sample_rate(ds, unit)
    return SampleRateAnalysis(
        mean=np.mean(sr),
        std=np.std(sr),
        median=np.median(sr),
        unit=unit
    )