flexmeasures.data.schemas.sensors

Classes

class flexmeasures.data.schemas.sensors.JSON(*, load_default: typing.Any = <marshmallow.missing>, missing: typing.Any = <marshmallow.missing>, dump_default: typing.Any = <marshmallow.missing>, default: typing.Any = <marshmallow.missing>, data_key: str | None = None, attribute: str | None = None, validate: types.Validator | typing.Iterable[types.Validator] | None = None, required: bool = False, allow_none: bool | None = None, load_only: bool = False, dump_only: bool = False, error_messages: dict[str, str] | None = None, metadata: typing.Mapping[str, typing.Any] | None = None, **additional_metadata)
_deserialize(value, attr, data, **kwargs) dict

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, data, **kwargs) str

Serializes value to a basic Python datatype. Noop by default. Concrete Field classes should implement this method.

Example:

class TitleCase(Field):
    def _serialize(self, value, attr, obj, **kwargs):
        if not value:
            return ""
        return str(value).title()
Parameters:
  • value – The value to be serialized.

  • attr – The attribute or key on the object to be serialized.

  • obj – The object the value was pulled from.

  • kwargs – Field-specific keyword arguments.

Returns:

The serialized value

class flexmeasures.data.schemas.sensors.QuantityOrSensor(*args, **kwargs)
__init__(*args, **kwargs)

Deprecated class. Use VariableQuantityField instead.

class flexmeasures.data.schemas.sensors.RepurposeValidatorToIgnoreSensorsAndLists(original_validator, *, error: str | None = None)

Validator that executes another validator (the one you initialize it with) only on non-Sensor and non-list values.

__init__(original_validator, *, error: str | None = None)
class flexmeasures.data.schemas.sensors.SensorDataFileSchema(*, only: types.StrSequenceOrSet | None = None, exclude: types.StrSequenceOrSet = (), many: bool | None = None, context: dict | None = None, load_only: types.StrSequenceOrSet = (), dump_only: types.StrSequenceOrSet = (), partial: bool | types.StrSequenceOrSet | None = None, unknown: str | None = None)
post_load(fields, **kwargs)

Process the deserialized and validated fields. Remove the ‘sensor’ and ‘files’ fields, and add the ‘data’ field containing a list of BeliefsDataFrames.

validate_uploaded_files(files: list[FileStorage])

Validate the deserialized fields.

class flexmeasures.data.schemas.sensors.SensorIdField(asset: GenericAsset | None = None, unit: str | ur.Quantity | None = None, *args, **kwargs)

Field that deserializes to a Sensor and serializes back to an integer.

__init__(asset: GenericAsset | None = None, unit: str | ur.Quantity | None = None, *args, **kwargs)
_deserialize(value: int, attr, obj, **kwargs) Sensor

Turn a sensor id into a Sensor.

_serialize(sensor: Sensor, attr, data, **kwargs) int

Turn a Sensor into a sensor id.

class flexmeasures.data.schemas.sensors.SensorSchema(*args, **kwargs)

Sensor schema, with validations.

class Meta
model

alias of Sensor

opts: LoadInstanceMixin.Opts = <flask_marshmallow.sqla.SQLAlchemySchemaOpts object>
class flexmeasures.data.schemas.sensors.SensorSchemaMixin(*, only: types.StrSequenceOrSet | None = None, exclude: types.StrSequenceOrSet = (), many: bool | None = None, context: dict | None = None, load_only: types.StrSequenceOrSet = (), dump_only: types.StrSequenceOrSet = (), partial: bool | types.StrSequenceOrSet | None = None, unknown: str | None = None)

Base sensor schema.

Here we include all fields which are implemented by timely_beliefs.SensorDBMixin All classes inheriting from timely beliefs sensor don’t need to repeat these. In a while, this schema can represent our unified Sensor class.

When subclassing, also subclass from ma.SQLAlchemySchema and add your own DB model class, e.g.:

class Meta:

model = Asset

class flexmeasures.data.schemas.sensors.TimeSeriesOrSensor(*args, **kwargs)
__init__(*args, **kwargs)

Deprecated class. Use VariableQuantityField instead.

class flexmeasures.data.schemas.sensors.TimedEventSchema(timezone: str | None = None, value_validator: Validator | None = None, to_unit: str | None = None, default_src_unit: str | None = None, return_magnitude: bool = True, *args, **kwargs)
__init__(timezone: str | None = None, value_validator: Validator | None = None, to_unit: str | None = None, default_src_unit: str | None = None, return_magnitude: bool = True, *args, **kwargs)

A time period (or single point) with a value.

Parameters:

timezone – Optionally, set a timezone to be able to interpret nominal durations.

check_time_window(data: dict, **kwargs)

Checks whether a complete time interval can be derived from the timing fields.

The data is updated in-place, guaranteeing that the ‘start’ and ‘end’ fields are filled out.

class flexmeasures.data.schemas.sensors.VariableQuantityField(to_unit, *args, default_src_unit: str | None = None, return_magnitude: bool = False, timezone: str | None = None, value_validator: Validator | None = None, **kwargs)
__init__(to_unit, *args, default_src_unit: str | None = None, return_magnitude: bool = False, timezone: str | None = None, value_validator: Validator | None = None, **kwargs)

Field for validating, serializing and deserializing a variable quantity.

A variable quantity can be represented by a sensor, time series or fixed quantity.

# todo: Sensor should perhaps deserialize already to sensor data

NB any value validators passed are only applied to Quantities. For example, value_validator=validate.Range(min=0) will raise a ValidationError in case of negative quantities, but will let pass any sensor that has recorded negative values.

Parameters:
  • to_unit

    Unit to which the sensor, time series or quantity should be convertible. - Sensors are checked for convertibility, but the original sensor is returned,

    so its values are not yet converted.

    • Time series and quantities are already converted to the given unit.

    • Units starting with ‘/’ (e.g. ‘/MWh’) lead to accepting any value, which will be converted to the given unit. For example, a quantity of 1 EUR/kWh with to_unit=’/MWh’ is deserialized to 1000 EUR/MWh.

  • default_src_unit – What unit to use in case of getting a numeric value. Does not apply to time series or sensors. In case to_unit is dimensionless, default_src_unit defaults to dimensionless; as a result, numeric values are accepted.

  • return_magnitude – In case of getting a time series, whether the result should include the magnitude of each quantity, or each Quantity object itself.

  • timezone – Only used in case a time series is specified and one of the timed events in the time series uses a nominal duration, such as “P1D”.

_deserialize(value: dict[str, int] | list[dict] | str, attr, obj, **kwargs) Sensor | list[dict] | ur.Quantity

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.

_deserialize_dict(value: dict[str, int]) Sensor

Deserialize a sensor reference to a Sensor.

_deserialize_list(value: list[dict]) list[dict]

Deserialize a time series to a list of timed events.

_deserialize_numeric(value: Real, attr, obj, **kwargs) Quantity

Try to deserialize a numeric value to a Quantity, using the default_src_unit.

_deserialize_str(value: str) Quantity

Deserialize a string to a Quantity.

_get_unit(variable_quantity: ur.Quantity | list[dict | Sensor]) str

Obtain the unit from the variable quantity.

_serialize(value: Sensor | pd.Series | ur.Quantity, attr, data, **kwargs) str | dict[str, int]

Serializes value to a basic Python datatype. Noop by default. Concrete Field classes should implement this method.

Example:

class TitleCase(Field):
    def _serialize(self, value, attr, obj, **kwargs):
        if not value:
            return ""
        return str(value).title()
Parameters:
  • value – The value to be serialized.

  • attr – The attribute or key on the object to be serialized.

  • obj – The object the value was pulled from.

  • kwargs – Field-specific keyword arguments.

Returns:

The serialized value

convert(value, param, ctx, **kwargs)

Convert the value to the correct type. This is not called if the value is None (the missing value).

This must accept string values from the command line, as well as values that are already the correct type. It may also convert other compatible types.

The param and ctx arguments may be None in certain situations, such as when converting prompt input.

If the value cannot be converted, call fail() with a descriptive message.

Parameters:
  • value – The value to convert.

  • param – The parameter that is using this type to convert its value. May be None.

  • ctx – The current context that arrived at this value. May be None.