flexmeasures.api.common.schemas.sensor_data

Functions

flexmeasures.api.common.schemas.sensor_data.select_schema_to_ensure_list_of_floats(values: list[float] | float, _) fields.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.

Classes

class flexmeasures.api.common.schemas.sensor_data.GetSensorDataSchema(*, only: types.StrSequenceOrSet | None = None, exclude: types.StrSequenceOrSet = (), many: bool = False, context: dict | None = None, load_only: types.StrSequenceOrSet = (), dump_only: types.StrSequenceOrSet = (), partial: bool | types.StrSequenceOrSet | None = None, unknown: str | 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.PostSensorDataSchema(*, only: types.StrSequenceOrSet | None = None, exclude: types.StrSequenceOrSet = (), many: bool = False, context: dict | None = None, load_only: types.StrSequenceOrSet = (), dump_only: types.StrSequenceOrSet = (), partial: bool | types.StrSequenceOrSet | None = None, unknown: str | None = None)

This schema includes data, so it can be used for POST requests or GET responses.

TODO: For the GET use case, look at api/common/validators.py::get_data_downsampling_allowed

(sets a resolution parameter which we can pass to the data collection function).

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. Currently, only upsampling is supported (e.g. converting hourly events to 15-minute events).

static load_bdf(sensor_data: dict) BeliefsDataFrame

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

static possibly_convert_units(data)

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

static possibly_upsample_values(data)

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

post_load_sequence(data: dict, **kwargs) BeliefsDataFrame

If needed, upsample and convert units, then deserialize to a BeliefsDataFrame.

class flexmeasures.api.common.schemas.sensor_data.SensorDataDescriptionSchema(*, only: types.StrSequenceOrSet | None = None, exclude: types.StrSequenceOrSet = (), many: bool = False, context: dict | None = None, load_only: types.StrSequenceOrSet = (), dump_only: types.StrSequenceOrSet = (), partial: bool | types.StrSequenceOrSet | None = None, unknown: str | None = None)

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

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.SingleValueField(*, allow_nan: bool = False, as_string: bool = False, **kwargs)

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

_deserialize(value, attr, obj, **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.

  • kwargs – Field-specific keyword arguments.

Raises:

ValidationError – In case of formatting or validation failure.

Returns:

The deserialized value.

Changed in version 2.0.0: Added attr and data parameters.

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

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

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