gluonts.dataset.schema.translate module#

gluonts.dataset.schema.translate#

This module provides a Translator class, which can be used to translate dictionaries. It is intended to be used with GluonTS datasets, to allow for more flexibility in the input data:

tl = Translator.parse(target="demand", feat_dynamic_real="[price]")
class gluonts.dataset.schema.translate.Get(name: str)[source]#

Bases: gluonts.dataset.schema.translate.Op

Extracts the field name from the input.

fields()[source]#
name: str#
class gluonts.dataset.schema.translate.GetAttr(obj: gluonts.dataset.schema.translate.Op, name: str)[source]#

Bases: gluonts.dataset.schema.translate.Op

Invokes obj.name.

fields()[source]#
name: str#
obj: gluonts.dataset.schema.translate.Op#
class gluonts.dataset.schema.translate.GetItem(obj: gluonts.dataset.schema.translate.Op, dims: List[int])[source]#

Bases: gluonts.dataset.schema.translate.Op

dims: List[int]#
fields()[source]#
obj: gluonts.dataset.schema.translate.Op#
class gluonts.dataset.schema.translate.Method(obj: gluonts.dataset.schema.translate.Op, args: list)[source]#

Bases: gluonts.dataset.schema.translate.Op

args: list#
fields()[source]#
obj: gluonts.dataset.schema.translate.Op#
class gluonts.dataset.schema.translate.Op[source]#

Bases: object

fields()[source]#
class gluonts.dataset.schema.translate.Parser(stream: gluonts.dataset.schema.translate.TokenStream)[source]#

Bases: object

parse_args()[source]#
parse_dot(obj)[source]#
parse_expr()[source]#
parse_getitem(obj)[source]#
parse_invoke(obj)[source]#
parse_number()[source]#
stream: gluonts.dataset.schema.translate.TokenStream#
class gluonts.dataset.schema.translate.Stack(objects: List[gluonts.dataset.schema.translate.Op])[source]#

Bases: gluonts.dataset.schema.translate.Op

fields()[source]#
objects: List[gluonts.dataset.schema.translate.Op]#
class gluonts.dataset.schema.translate.Token(name: str, value: str, match: Any)[source]#

Bases: object

match: Any#
name: str#
value: str#
class gluonts.dataset.schema.translate.TokenStream(tokens: List[gluonts.dataset.schema.translate.Token], idx: dataclasses.InitVar[int] = 0)[source]#

Bases: object

RX: ClassVar[str] = '(?P<DOT>\\.)|(?P<COMMA>,)|(?P<PAREN_OPEN>[\\[|\\(])|(?P<PARAN_CLOSE>[\\]|\\)])|(?P<NUMBER>\\-?\\d+)|(?P<NAME>\\w+)|(?P<WHITESPACE>\\s+)|(?P<INVALID>.+)'#
TOKENS: ClassVar[dict] = {'COMMA': ',', 'DOT': '\\.', 'INVALID': '.+', 'NAME': '\\w+', 'NUMBER': '\\-?\\d+', 'PARAN_CLOSE': '[\\]|\\)]', 'PAREN_OPEN': '[\\[|\\(]', 'WHITESPACE': '\\s+'}#
classmethod from_str(s)[source]#
idx: dataclasses.InitVar[int] = 0#
peek(ty=None, val=None)[source]#
pop(ty=None, val=None)[source]#
pop_if(ty=None, val=None)[source]#
tokens: List[gluonts.dataset.schema.translate.Token]#
class gluonts.dataset.schema.translate.Translator(fields: typing.Dict[str, gluonts.dataset.schema.translate.Op] = <factory>, drop: bool = False)[source]#

Bases: object

Simple translation for GluonTS Datasets.

A given translator transforms an input dictionary (data-entry) into an output dictionary.

Basic usage:

>>> tl = Translator.parse(x="a[0]")
>>> data = {"a": [1, 2, 3]}
>>> assert tl(data)["x"] == 1

A translator first copies all input fields into a new dictionary, before applying the translations. Thus, an empty Translator acts like the identity function for dictionaries:

>>> identity = Translator()
>>> data = {"a": 1, "b": 2, "c": 3}
>>> assert identity(data) == data

Using Translator.parse(...)`, one can define expressions to be applied to the input data. For example, Translator.parse(x="y") will write the the value of y to the x column in the output.

These right-hand expressions support indexing (e.g. y[1]), attribute access (e.g. x.T) and method invocation (e.g. y.transpose(1, 0)).

drop: bool = False#
fields: Dict[str, gluonts.dataset.schema.translate.Op]#
get_fields()[source]#
static parse(fields: Optional[dict] = None, drop: bool = False, **kwargs_fields) gluonts.dataset.schema.translate.Translator[source]#
gluonts.dataset.schema.translate.check_type(token, ty, val)[source]#
gluonts.dataset.schema.translate.one_of(s)[source]#
gluonts.dataset.schema.translate.parse(x: Union[str, list]) gluonts.dataset.schema.translate.Op[source]#