flexmeasures.api.common.schemas.sensor_data
Functions
- 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.
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.
- 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 possibly_upsample_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, unit: str | None = None) BeliefsDataFrame
Downsample the data if needed, to fit the sensor’s resolution. Marshmallow runs this after validation.
Values in an energy unit (such as kWh) are taken to be stock changes, as only a sensor with a non-zero resolution is downsampled, so they are summed: 1 kWh in each quarter-hour is 4 kWh in the hour. Anything else, such as a flow (kW) or a price, is averaged. This mirrors how possibly_upsample_values splits stock changes and repeats anything else.
- Parameters:
unit – the unit the values are in at this point (they were converted to the sensor’s unit before); by default, they are averaged.
- static possibly_upsample_values(data, unit: str | None = None)
Upsample the data if needed, to fit to the sensor’s resolution. Marshmallow runs this after validation.
Values in an energy unit (such as kWh) at this point are taken to be stock changes, as only a sensor with a non-zero resolution is upsampled (an instantaneous sensor, whose energy values would be stocks, is not), so they are split over the new events: 4 kWh in one hour becomes 1 kWh in each quarter-hour. Anything else, such as a flow (kW) or a price, is repeated.
- Parameters:
unit – the unit the values are in at this point; by default the posted unit.
- 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
Fieldclasses 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
**kwargsto signature.