flexmeasures.api.common.schemas.sensor_data

Functions

flexmeasures.api.common.schemas.sensor_data.downsampling_method(sensor, posted_unit: str) → str

How values posted in this unit to this sensor are aggregated when downsampled: sum, mean, max, min or last.

Changes over the interval are summed (see sums_values); otherwise, the sensor’s cell-method decides (maximum and minimum bounds take the maximum and minimum, a point takes the last value), and values are averaged if it doesn’t say.

flexmeasures.api.common.schemas.sensor_data.select_schema_to_ensure_list_of_floats(values: list[float] | float, _) → List | SingleValueField

Allows both a single float and a list of floats. Always returns a list of floats.

Meant to improve user experience by not needing to make a list out of a single item, such that:

{
    "values": [3.7]
}

can be written as:

{
    "values": 3.7
}

Either will be de-serialized to [3.7].

Note that serialization always results in a list of floats. This ensures that we are not requiring the same flexibility from users who are retrieving data.

flexmeasures.api.common.schemas.sensor_data.sums_values(sensor, posted_unit: str) → bool

Whether values posted in this unit to this sensor are changes over the interval: split when upsampled, summed when downsampled.

That is so for a sensor saying so with the attribute cell-method: sum (in any unit: a meter’s kWh or m³ per quarter-hour, a ramp in kW, a price change in EUR/MWh, a temperature change in °C), and for a flow (such as kW) posted to a sensor in an energy unit (such as kWh), as only energy per interval can be converted from a flow. Values on a sensor without a cell-method are repeated and averaged, which is right for levels, flows and prices.

Raises:

ValidationError – if the sensor’s cell-method is unknown, or if a flow is posted to a sensor whose values are not changes over the interval (such as a state of charge in kWh, cell-method: mean)

Classes

class flexmeasures.api.common.schemas.sensor_data.GetSensorDataFilterSchemaMixin

Shared filters for GET sensor data request parsing and docs.

support_legacy_field_name(data, **kwargs)

Accept old snake_case input for backwards compatibility.

class flexmeasures.api.common.schemas.sensor_data.GetSensorDataQuerySchema(*, only: Sequence[str] | AbstractSet[str] | None = None, exclude: Sequence[str] | AbstractSet[str] = (), many: bool | None = None, load_only: Sequence[str] | AbstractSet[str] = (), dump_only: Sequence[str] | AbstractSet[str] = (), partial: bool | Sequence[str] | AbstractSet[str] | None = None, unknown: Literal['exclude', 'include', 'raise'] | None = None)

Document the actual query parameters for GET /sensors/<id>/data.

class flexmeasures.api.common.schemas.sensor_data.GetSensorDataSchema(*, only: Sequence[str] | AbstractSet[str] | None = None, exclude: Sequence[str] | AbstractSet[str] = (), many: bool | None = None, load_only: Sequence[str] | AbstractSet[str] = (), dump_only: Sequence[str] | AbstractSet[str] = (), partial: bool | Sequence[str] | AbstractSet[str] | None = None, unknown: Literal['exclude', 'include', 'raise'] | None = None)
static load_data_and_make_response(sensor_data_description: dict) → dict

Turn the de-serialized and validated data description into a response.

Specifically, this function:

  • queries data according to the given description

  • converts to a single deterministic belief per event

  • ensures the response respects the requested time frame

  • converts values to the requested unit

  • converts values to the requested resolution

class flexmeasures.api.common.schemas.sensor_data.GetSensorDataSchemaEntityAddress(*, only: Sequence[str] | AbstractSet[str] | None = None, exclude: Sequence[str] | AbstractSet[str] = (), many: bool | None = None, load_only: Sequence[str] | AbstractSet[str] = (), dump_only: Sequence[str] | AbstractSet[str] = (), partial: bool | Sequence[str] | AbstractSet[str] | None = None, unknown: Literal['exclude', 'include', 'raise'] | None = None)

DEPRECATED, only here to support deprecated endpoints

class flexmeasures.api.common.schemas.sensor_data.PostSensorDataRequestSchema(*args, source_user: User | None = None, **kwargs)

Validate posted sensor data without building a BeliefsDataFrame.

post_load_sequence(data: dict, **kwargs) → dict

If needed, convert units and upsample (in the order that keeps the amounts right, see possibly_convert_and_upsample_values), then deserialize to a BeliefsDataFrame, and downsample if needed (summing stock changes, see possibly_downsample_bdf). Returns a dict with the BDF in it, as that is expected by webargs when used with as_kwargs=True.

class flexmeasures.api.common.schemas.sensor_data.PostSensorDataSchema(*args, source_user: User | None = None, **kwargs)

This schema includes data (values) and still describes it.

__init__(*args, source_user: User | None = None, **kwargs)
check_cell_method_of_sensor(data, **kwargs)

Refuse data whose resampling or conversion the sensor’s cell-method doesn’t allow (see sums_values).

Checked when the request comes in, so the client hears about it (422) also when the data is ingested later by a worker.

check_multiple_instantaneous_values(data, **kwargs)

Ensure that we are not getting multiple instantaneous values that overlap. That is, two values spanning the same moment (a zero duration).

check_resolution_compatibility_of_sensor_data(data, **kwargs)

Ensure event frequency is compatible with the sensor’s event resolution.

For a sensor recording instantaneous values, any event frequency is compatible. For a sensor recording non-instantaneous values, the event frequency must fit the sensor’s event resolution. Upsampling and downsampling are supported when the inferred resolution and the sensor resolution are multiples of each other.

check_single_value_zero_duration_for_non_instantaneous_sensor(data, **kwargs)

Reject inputs where a non-instantaneous sensor cannot infer any resolution.

load_bdf(sensor_data: dict) → BeliefsDataFrame

Turn the de-serialized and validated data into a BeliefsDataFrame.

static possibly_convert_and_upsample_values(data)

Convert to the sensor’s unit and upsample to the sensor’s resolution, in the order that keeps the amounts right.

A flow (say kW posted to a kWh sensor) is repeated first, and each value is then converted over the sensor’s resolution. Anything else (say kWh posted to a kW sensor) is converted over the posted resolution first, and then upsampled (see sums_values for when values are repeated and when they are split).

static possibly_convert_units(data)

Convert values if needed, to fit the sensor’s unit. Marshmallow runs this after validation.

static possibly_downsample_bdf(bdf: BeliefsDataFrame, required_resolution: timedelta, method: str = 'mean') → BeliefsDataFrame

Downsample the data if needed, to fit the sensor’s resolution. Marshmallow runs this after validation.

Parameters:

method – how to aggregate the values (see downsampling_method): sum for changes over the interval (1 kWh in each quarter-hour is 4 kWh in the hour), mean for levels, flows and prices, max or min for bounds, or last for instants

static possibly_upsample_values(data, split: bool = False)

Upsample the data if needed, to fit to the sensor’s resolution. Marshmallow runs this after validation.

Parameters:

split – whether to split each value over the new events, for changes over the interval (4 kWh in one hour is 1 kWh in each quarter-hour), rather than repeat it, for levels, flows and prices (see sums_values)

post_load_sequence(data: dict, **kwargs) → dict[str, BeliefsDataFrame]

If needed, convert units and upsample (in the order that keeps the amounts right, see possibly_convert_and_upsample_values), then deserialize to a BeliefsDataFrame, and downsample if needed (summing stock changes, see possibly_downsample_bdf). Returns a dict with the BDF in it, as that is expected by webargs when used with as_kwargs=True.

validate_prior_is_storable(value, **kwargs)

Refuse a prior that cannot be stored as a belief time, which a GET may still filter on.

class flexmeasures.api.common.schemas.sensor_data.PostSensorDataSchemaEntityAddress(*args, source_user: User | None = None, **kwargs)

DEPRECATED, only here to support deprecated endpoints

class flexmeasures.api.common.schemas.sensor_data.SensorDataDescriptionSchema(*, only: Sequence[str] | AbstractSet[str] | None = None, exclude: Sequence[str] | AbstractSet[str] = (), many: bool | None = None, load_only: Sequence[str] | AbstractSet[str] = (), dump_only: Sequence[str] | AbstractSet[str] = (), partial: bool | Sequence[str] | AbstractSet[str] | None = None, unknown: Literal['exclude', 'include', 'raise'] | None = None)

Schema describing sensor data (specifically, adding the sensor to timing of the data and adding validation).

check_schema_unit_against_sensor_unit(data, **kwargs)

Allows units compatible with that of the sensor. For example, a sensor with W units allows data to be posted with units:

  • W, kW, MW, etc. (i.e. units with different prefixes)

  • J/s, Nm/s, etc. (i.e. units that can be converted using some multiplier)

  • Wh, kWh, etc. (i.e. units that represent a stock delta, which knowing the duration can be converted to a flow)

For compatible units, the SensorDataSchema converts values to the sensor’s unit.

class flexmeasures.api.common.schemas.sensor_data.SensorDataTimingDescriptionSchema(*, only: Sequence[str] | AbstractSet[str] | None = None, exclude: Sequence[str] | AbstractSet[str] = (), many: bool | None = None, load_only: Sequence[str] | AbstractSet[str] = (), dump_only: Sequence[str] | AbstractSet[str] = (), partial: bool | Sequence[str] | AbstractSet[str] | None = None, unknown: Literal['exclude', 'include', 'raise'] | None = None)

Schema describing sensor data (specifically, the timing of the data).

class flexmeasures.api.common.schemas.sensor_data.SingleValueField(*, allow_nan: bool = False, as_string: bool = False, **kwargs: Unpack[_BaseFieldKwargs])

Field that both de-serializes and serializes a single value to a list of floats (length 1).

_deserialize(value, attr, data, **kwargs) → list[float]

Deserialize value. Concrete Field classes should implement this method.

Parameters:
  • value – The value to be deserialized.

  • attr – The attribute/key in data to be deserialized.

  • data – The raw input data passed to the Schema.load <marshmallow.Schema.load>.

  • kwargs – Field-specific keyword arguments.

Raises:

ValidationError – In case of formatting or validation failure.

Returns:

The deserialized value.

Changed in version 3.0.0: Added **kwargs to signature.

_serialize(value, attr, obj, **kwargs) → list[float]

Return a string if self.as_string=True, otherwise return this field’s num_type.