Skip to content

tdfs4ds.agent.historian_plot

tdfs4ds.agent.historian_plot

PLOT skill, historian part (0.3.8.10): A versus B of two tags, charts per period, which historian.

Read by code, never guessed. Three kinds of request reach :func:handle:

Anything unclear (several candidate columns, tags or tables, a unit that is not documented, no link between the dataset and the historian, a tag not found, period mode or not) is answered with a short explanation and numbered rephrased questions built from the real names found; a numbered reply runs the chosen one (pending["choices"]). Nothing is run before that.

logger module-attribute

logger = logging.getLogger(__name__)

_VS_RE module-attribute

_VS_RE = re.compile("\\s+(?:versus|vs\\.?|against|as\\s+a\\s+function\\s+of|in\\s+function\\s+of|en\\s+fonction\\s+d[eu']\\s*|contre|par\\s+rapport\\s+[Ă a]|over(?!\\s+(?:time|the\\b|a\\b|an\\b|last\\b|past\\b|\\d|each\\b|every\\b|all\\b|several\\b)))\\s+", re.IGNORECASE)

_LEAD_RE module-attribute

_LEAD_RE = re.compile("^.*?\\b(?:plot|chart|draw|show|graph|scatter|trace[rz]?|dessine[rz]?|montre[rz]?|affiche[rz]?)\\b\\s*(?:(?:me|moi|a|an|the|le|la|les|un|une)\\s+)?(?:(?:scatter|graph|chart|plot|courbe|graphique|nuage)\\s+(?:of|de|du|des|d')\\s*)?", re.IGNORECASE)

_RHS_STOP_RE module-attribute

_RHS_STOP_RE = re.compile('\\s+(?:for|per|using|with|from|in|on|over|during|between|where|of\\s+the|over\\s+the|since|until|pour|par|avec|dans|depuis|sur|entre|o[uĂ¹]|pendant|du|de\\s+la|des)\\b|[,;?!]|\\.(?=\\s|$)|\\(', re.IGNORECASE)

_PERIOD_OF_RE module-attribute

_PERIOD_OF_RE = re.compile('\\b(?:for|per|over)\\s+(?:the\\s+|each\\s+|every\\s+|all\\s+(?:the\\s+)?)?([A-Za-zĂ€-Ă¿][\\wĂ€-Ă¿-]*)\\s+(?:of|in|from)\\s+(?:the\\s+)?(?:dataset\\s+|table\\s+|view\\s+)?([\\w$#\\".]+)|\\bpour\\s+(?:les\\s+|chaque\\s+|tou(?:s|tes)\\s+les\\s+)?([A-Za-zĂ€-Ă¿][\\wĂ€-Ă¿-]*)\\s+(?:de\\s+la\\s+|du\\s+|des\\s+|de\\s+|d\')(?:dataset\\s+|table\\s+|vue\\s+)?([\\w$#\\".]+)', re.IGNORECASE)

_CLOCK_RE module-attribute

_CLOCK_RE = re.compile('\\b(real\\s+time|clock\\s+time|actual\\s+time|wall[-\\s]clock|calendar\\s+time|temps\\s+r[ée]el|heure\\s+r[ée]elle|date\\s+r[ée]elle)\\b', re.IGNORECASE)

_COND_START_RE module-attribute

_COND_START_RE = re.compile('\\b(?:where|with|whose|having|which|that|o[uĂ¹]|avec|dont|ayant|qui)\\b', re.IGNORECASE)

_CATEGORY_RE module-attribute

_CATEGORY_RE = re.compile('\\b(?:of|de)\\s+(?:the\\s+)?(type|category|cat[ée]gorie|class|classe|grade|family|famille|kind|sorte|model|mod[eè]le|status|statut)\\s+([\\w.\\-/]+(?:\\s*(?:,|\\bor\\b|\\bou\\b)\\s*[\\w.\\-/]+)*)', re.IGNORECASE)

_BUCKET_RE module-attribute

_BUCKET_RE = re.compile('\\b(?:per|every|each|by|averaged\\s+(?:over|per)|par|chaque|toutes?\\s+les|moyenn[ée]e?s?\\s+(?:sur|par))\\s+(\\d+\\s*)?(seconds?|secs?|minutes?|mins?|hours?|heures?)\\b', re.IGNORECASE)

_OPS module-attribute

_OPS = [('>=', '>=|\\bat\\s+least\\b|\\bgreater\\s+than\\s+or\\s+equal\\s+to\\b|\\bau\\s+moins\\b|\\bsup[ée]rieure?s?\\s+ou\\s+[ée]gale?s?\\s+[àa]\\b'), ('<=', '<=|\\bat\\s+most\\b|\\bless\\s+than\\s+or\\s+equal\\s+to\\b|\\bau\\s+plus\\b|\\binf[ée]rieure?s?\\s+ou\\s+[ée]gale?s?\\s+[àa]\\b'), ('<>', "<>|!=|\\bnot\\s+equal\\s+to\\b|\\bdifferent\\s+from\\b|\\bdiff[ée]rente?s?\\s+de\\b|\\bis\\s+not\\b|\\bn'est\\s+pas\\b"), ('>', '>|\\b(?:greater|more|higher|larger|longer|bigger|later)\\s+than\\b|\\babove\\b|\\bover\\b|\\bafter\\b|\\bapr[eè]s\\b|\\bpost[ée]rieure?s?\\s+[àa]\\b|\\bexceed(?:s|ed|ing)?\\b|\\bsup[ée]rieure?s?\\s+[àa]\\b|\\bplus\\s+grande?s?\\s+que\\b|\\bplus\\s+de\\b|\\bau[-\\s]dessus\\s+de\\b|\\b(?:a\\s+)?d[ée]pass[ée]e?s?\\b|\\bd[ée]passant\\b|\\bd[ée]passe\\b'), ('<', '<|\\b(?:less|lower|smaller|shorter|fewer|earlier)\\s+than\\b|\\bbelow\\b|\\bunder\\b|\\bbefore\\b|\\bavant\\b|\\bant[ée]rieure?s?\\s+[àa]\\b|\\binf[ée]rieure?s?\\s+[àa]\\b|\\bplus\\s+petite?s?\\s+que\\b|\\bmoins\\s+de\\b|\\ben[-\\s]dessous\\s+de\\b'), ('=', '==|=|\\b(?:is\\s+)?equal\\s+to\\b|\\bequals\\b|\\b(?:est\\s+)?[ée]gale?s?\\s+[àa]\\b'), ('=', '\\bis\\b|\\bwas\\b|\\best\\b|\\bvaut\\b')]

_BETWEEN_RE module-attribute

_BETWEEN_RE = re.compile('^(?P<col>.+?)\\s+(?:is\\s+|est\\s+|was\\s+)?(?:between|entre)\\s+(?P<a>\\S+(?:\\s*[A-Za-zµ°%]+)?)\\s+(?:and|et)\\s+(?P<b>\\S+(?:\\s*[A-Za-zµ°%]+)?)$', re.IGNORECASE)

_AGG_WORDS module-attribute

_AGG_WORDS = [('AVG', '\\b(?:average|mean|moyenne?)\\b'), ('MAX', '\\b(?:max|maximum|peak|pic)\\b'), ('MIN', '\\b(?:min|minimum)\\b')]

_THROUGHOUT_RE module-attribute

_THROUGHOUT_RE = re.compile('\\b(?:stay(?:s|ed)?|remain(?:s|ed)?|kept|always|throughout|(?:est\\s+|sont\\s+)?rest[ée]e?s?|reste(?:nt)?|toujours)\\b', re.IGNORECASE)

_ART_RE module-attribute

_ART_RE = re.compile("^(?:the|its|their|a|an|le|la|les|l'|son|sa|ses|leur|leurs|un|une)\\s+", re.IGNORECASE)

_START_WORDS module-attribute

_START_WORDS = re.compile('\\b(start\\w*|begin\\w*|d[ée]but\\w*|commenc\\w*|opened|from|since|d[ée]marr\\w*)\\b', re.IGNORECASE)

_END_WORDS module-attribute

_END_WORDS = re.compile('\\b(end|ends|ended|ending|stop\\w*|finish\\w*|fin|termin\\w*|closed|until|jusqu\\w*|arr[eĂª]t\\w*)\\b', re.IGNORECASE)

_KEY_WORDS module-attribute

_KEY_WORDS = re.compile('\\b(id|ids|identifier|identifiant|key|cl[ée]|code|number|num[ée]ro|nr|no|name|nom)\\b', re.IGNORECASE)

_GENERIC module-attribute

_GENERIC = {'id', 'ids', 'identifier', 'identifiant', 'number', 'numero', 'numéro', 'nr', 'no', 'code', 'key', 'the', 'of', 'a', 'an', 'le', 'la', 'les', 'de', 'du', 'des', 'which', 'where', 'on', 'in', 'is', 'was', 'this', 'that', 'each', 'row', 'value', 'name', 'column', 'en', 'est', 'par', 'for', 'per', 'one', 'un', 'une', 'at', 'to', 'by', 'with', 'and', 'et', 'or', 'ou', 'time', 'date'}

_EACH_ITEM_RE module-attribute

_EACH_ITEM_RE = re.compile('\\b(?:for\\s+(?:each|every|all\\s+(?:the\\s+)?)|per|pour\\s+(?:chaque|tou(?:s|tes)\\s+les))\\s+([A-Za-zĂ€-Ă¿][\\w-]*)', re.IGNORECASE)

_ID_PHRASE_RE module-attribute

_ID_PHRASE_RE = re.compile("\\b(?:for|on|of|in|pour|sur|de|dans)\\s+(?:the\\s+|la\\s+|le\\s+|les\\s+|l')?([A-Za-zĂ€-Ă¿]+)\\s+(\\d+(?:\\s*(?:,|and|et)\\s*\\d+)*)\\b(?!\\s*(?:minutes?|hours?|days?|weeks?|months?|years?|heures?|jours?|semaines?|mois|ans?|kg|t|bar|°C|%)(?![\\w°]))", re.IGNORECASE)

_HOW_EN module-attribute

_HOW_EN = {'hint': 'as written in the question', 'name': 'by name', 'catalog': "the dataset's entity (catalog)", 'description': 'by its documentation'}

_HOW_FR module-attribute

_HOW_FR = {'hint': 'comme écrit dans la question', 'name': 'par le nom', 'catalog': "l'entité du dataset (catalogue)", 'description': 'par sa documentation'}

_SENSOR_SCAN module-attribute

_SENSOR_SCAN = 100

_layout

cm instance-attribute

cm = plotting.using_layout(fq, o['layout']) if o.get('inferred') else None

__init__

__init__(plotting, o, fq)

__enter__

__enter__()

__exit__

__exit__(*a)

_ps

_ps()

parse_vs

parse_vs(question: str)

(y phrase, x phrase) of "plot Y vs X" / "trace Y en fonction de X", else None. Pure.

parse_bucket

parse_bucket(question: str) -> Optional[str]

The time bucket an A-vs-B chart is asked for ("per minute", "every 30 seconds", "par heure"), as " " for :func:tdfs4ds.plotting.plot_historian_xy, else None (the automatic one). A fast cycle averaged over the automatic bucket can hide the relation (seen live: a 30-minute ramp averaged per 5 minutes drew 6 clusters), so the bucket can be asked. Pure.

parse_period_request

parse_period_request(question: str, names) -> Optional[dict]

{"noun", "name", "end"}: "for the of " / "pour les de " where NAME is one of names (a dataset or a documented table/view), else None. Pure.

singular

singular(word: str) -> str

The user's noun in the singular, for "each " ("batches" -> "batch", "runs" -> "run"). Pure.

parse_hints

parse_hints(text: str) -> dict

The roles a rephrased question states in brackets after the dataset: "(key RUN_ID, LINE_NO = LINE_ID, from T_START to T_END)" / "(clé ..., de T_START à T_END)". Pure.

_value

_value(text: str)

(value, unit) of "1 km", "30kW", "2024-01-31", "A" (text). Pure.

parse_clause

parse_clause(clause: str) -> Optional[dict]

One condition: {"phrase", "op", "value", "unit", "agg"} (value a list for BETWEEN / IN), else None. Pure.

parse_conditions

parse_conditions(text: str) -> list

The conditions of a question's tail: category phrases ("of type A or B", "de catĂ©gorie A ou B") and clauses after where / with / avec / oĂ¹, joined by and / et. Pure.

_words

_words(s) -> set

resolve_tag_phrase

resolve_tag_phrase(phrase: str, tags) -> list

The tags a phrase means: the exact name (case-insensitive); else the tags all of whose words are in the phrase (match_tags); else the tags holding every word of the phrase ("oven temp" -> L1_Oven_Temp.PV). Several = ambiguous (the caller asks). Pure.

_historians

_historians(ca) -> list

_fq

_fq(o) -> str

_named_historian

_named_historian(cands, text)

_holds

_holds(o, phrases) -> dict

{phrase: [tags of o]} for the phrases o's documented tags resolve.

historian_choice

historian_choice(ca, text: str, phrases, fr: bool, question: str) -> dict

Which historian serves phrases: {"use": object, "tags": {phrase: [tags]}, "note"} or {"ask": skill result} or {"none": True} when there is no historian.

_with_historian

_with_historian(q: str, fq: str, fr: bool) -> str

q naming the historian fq, inserted after what to draw and before the items, conditions and window ("... for each run of X where grade is premium" + "using H" used to end as the value "premium using H", seen live). Pure.

ask_historian

ask_historian(cands, holders, phrases, fr: bool, question: str) -> dict

The numbered choice of a historian: name, documented description, tags it holds.

reformulate

reformulate(fr: bool, why: list, questions: list, question: str) -> dict

A short explanation and numbered rephrased questions; a number runs that question.

_text_of

_text_of(col, descs) -> str

_meaning

_meaning(col, descs) -> set

_norm

_norm(name) -> str

resolve_roles

resolve_roles(columns: dict, descriptions: dict, *, entity=(), hist_ids=(), hist_descriptions=None, noun: Optional[str] = None, hints: Optional[dict] = None)

(roles, problems): the dataset columns playing each role, from documentation and types.

roles = {"key", "start", "end", "id_map": {dataset column: historian id column}, "how": {...}}; how says how each role was found (hint = written in the question, name, catalog, description; how["id"] per historian key). problems [{"role", "candidates", "text"}] (empty when every role has exactly one column; id_missing when a historian key has no column in the dataset: the join is not possible).

  • start / end: date/time columns - a start/end pair by name, else the one documented as a start and the one documented as an end;
  • every historian key (id column): the dataset column with the same name (case and _ ignored), else the one whose documentation shares the most meaning words with the historian column's name and documentation (one best only);
  • key of each item: the dataset entity (catalog) minus the keys, else the column whose name, then documentation, says it identifies an item. Pure.

resolve_column

resolve_column(phrase: str, columns: dict, descriptions: dict, *, kinds=('number', 'time', 'text')) -> list

Dataset columns a condition's phrase means: the exact name, else the columns of the right type whose name or documentation holds every word of the phrase. Pure.

_period_question

_period_question(fr: bool, *, tags, xy, noun, ds, roles, conds, clock=False) -> str

_plain_question

_plain_question(fr: bool, *, tags, xy, historian) -> str

window_note

window_note(window, default_used: bool, fr: bool) -> Optional[str]

_tag_phrases

_tag_phrases(o, text) -> list

Documented tags of o named in text (exact names, then match_tags).

handle

handle(ca, plotting, question: str, window, fr: bool, datasets: dict)

The historian requests of the module docstring, else None (the PLOT skill goes on).

Returns a skill result (plot_needs_detail), or {"result", "understood", "window"} for :func:tdfs4ds.agent.plot_skill._finish, or {"hit", "note"}: the historian to draw the usual way (one holder of the tags named).

_name_historian_in_choices

_name_historian_in_choices(out, o, fq, fr)

Every rephrased question offered names the historian it was built for, so choosing it never asks "which historian?" again (seen live: a confirmation "1" re-opened the historian choice, a loop).

_without_conditions

_without_conditions(question: str, per: Optional[dict]) -> str

question without its conditions (the clause after where / with / that following the period dataset, or after the first "where" when no dataset is named) and without its category phrases. Pure.

_documented_period_tables

_documented_period_tables(objs, period_names) -> list

The names among period_names whose column documentation describes a start and an end (a table of periods), so a suggestion never points at a feature dataset with no period (seen live). Pure.

_plain_conditions

_plain_conditions(q, cands, phrases, period_names, fr)

A plain chart (no table of periods) with a "where" condition: a condition applies to rows of a table of periods, so it is never applied to a time window nor dropped in silence (seen live: "heater power for line 3 over the last 2 days where the oven temperature is above 200" drew the oven temperature too and ignored the condition). The chart without it and the per-period form are offered.

_per_items_without_dataset

_per_items_without_dataset(q, objs, datasets, period_names, fr)

"for each run where grade is premium plot the pressure": items of a table of periods asked for, but no table named. Answered with the tables whose name or documentation names the item (rephrased questions naming each), never by drawing the plain historian (seen live: the condition and the items were silently dropped). None when the question does not ask for items, or names an id of the historian ("for each line").

_close_tags

_close_tags(cands, q, fr)

No tag has all its words in the question, but some share one ("plot the temperature" -> OVEN_TEMP.PV of two historians): numbered rephrased questions, one per (historian, tag), so a number runs one (seen live: "I could not tell what to plot", then "2" drew nothing). None when no tag shares a word.

_historians_of

_historians_of(objs) -> list

_ask_tag

_ask_tag(cands, ca, q, per, fr) -> dict

Period or A-vs-B mode with no tag named (seen live: an empty chart request was sent, "Plot over time"): the documented tags, as rephrased questions.

_conditions_tail

_conditions_tail(q, per) -> str

The conditions of q as written (" where weight > 900 kg"), to keep them in a rephrased question.

id_filters

id_filters(text: str, layout: dict, descriptions: dict) -> list

[(historian id column, [values], span)] for "on line 2", "for lines 1 and 3", "pour la ligne 2": the word names an id column (by its name or its documentation), the numbers are its values. Pure.

_fr_text

_fr_text(text: str) -> bool

_window_in_conditions

_window_in_conditions(question: str, per: dict) -> bool

The conditions of a per-period question hold something read as a window. Pure.

_window_outside_conditions

_window_outside_conditions(question: str, per: dict)

The time window of a per-period question, read OUTSIDE its conditions: "where weight between 700 and 1000 kg" or "where PRODUCTION_DAY is between and " is a condition on the dataset, not a window on the runs' starts (seen live: both were read as a window). None when only the conditions hold one.

_alternatives

_alternatives(resolved, o, n=3) -> list

Up to n complete tag tuples from per-phrase candidates (a None takes the documented tags).

_ask_mode

_ask_mode(cands, q, phrases, ds_name, datasets, fr) -> dict

A tag and a dataset named, but no "for each of ": period mode is not guessed.

_xy_window

_xy_window(plotting, window)

_xy_plain

_xy_plain(ca, plotting, o, fq, tags, q, window, fr)

_dataset_docs

_dataset_docs(ca, fq: str) -> dict

understanding

understanding(fr: bool, *, historian, tags, xy, dataset=None, noun=None, roles=None, ds_conds=(), tag_conds=(), window_text=None, n_periods=None, n_left=0, clock=False) -> list

What the agent understood, as bullets written by code (EN / FR). Pure.

_needs_confirmation

_needs_confirmation(roles, ds_conds) -> bool

_confirm

_confirm(fr, bullets, run_question, alternatives, question) -> dict

_missing_keys

_missing_keys(fr, problems, dsq, fq, tags, xy, noun, question) -> dict

_per_period

_per_period(ca, plotting, o, fq, tags, xy, per, q, window, fr, datasets)

_hinted

_hinted(roles) -> dict

The roles, every one written out in the rephrased question (it then runs as given).

_num

_num(v)

_is_date

_is_date(v) -> bool

_fmt

_fmt(v)

_role_en

_role_en(pb)

_role_fr

_role_fr(pb)

_cond_text

_cond_text(c) -> str

_period_alternatives

_period_alternatives(fr, tags, xy, noun, dsq, roles, problems, ds_conds, tag_conds, columns) -> list

Up to three rephrased questions: each ambiguous role / condition filled with one candidate.

_condition_why

_condition_why(exc, fr, dsq) -> str

_condition_alternatives

_condition_alternatives(fr, exc, tags, xy, noun, dsq, roles, ds_conds, tag_conds) -> list