Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 4 additions & 22 deletions src/frequenz/sdk/timeseries/_resampling/_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from __future__ import annotations

import logging
import statistics
from collections.abc import Sequence
from dataclasses import dataclass
from datetime import datetime, timedelta
Expand Down Expand Up @@ -79,27 +80,6 @@ def __call__(
... # pylint: disable=unnecessary-ellipsis


def average(
samples: Sequence[tuple[datetime, float]],
resampler_config: ResamplerConfig, # pylint: disable=unused-argument
source_properties: SourceProperties, # pylint: disable=unused-argument
) -> float:
"""Calculate average of all the provided values.

Args:
samples: The samples to apply the average to. It must be non-empty.
resampler_config: The configuration of the resampler calling this
function.
source_properties: The properties of the source being resampled.

Returns:
The average of all `samples` values.
"""
assert len(samples) > 0, "Average cannot be given an empty list of samples"
values = list(sample[1] for sample in samples)
return sum(values) / len(values)


@dataclass(frozen=True)
class ResamplerConfig:
"""Resampler configuration."""
Expand Down Expand Up @@ -134,7 +114,9 @@ class ResamplerConfig:
passed to the resampling function.
"""

resampling_function: ResamplingFunction = average
resampling_function: ResamplingFunction = lambda samples, _, __: statistics.fmean(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we sure there are no NaNs?

Copy link
Contributor Author

@llucax llucax Jun 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, no NaNs, no Nones, no infs. That's filtered out before they are fed to the resampling function.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, inf is still allowed:

async for sample in self._source:
if sample.value is not None and not sample.value.isnan():
self._helper.add_sample((sample.timestamp, sample.value.base_value))

s[1] for s in samples
)
"""The resampling function.

This function will be applied to the sequence of relevant samples at
Expand Down