Skip to content

Experiment directives

directives

{experiment} directives: results woven into a literate document.

A directive is a fence whose info string is {experiment} and the name of an experiment of the document's module::

```{experiment} fit_boolean
:hash: 3f2a...
:program: 9c41...
responses: mean 0.734 sd 0.051
```

Weaving (qvr weave) runs the experiment, or reads its stored record, and writes the body: the experiment's hash, the hash of the program it ran, and the result. The hash makes every woven number traceable to the exact program, data, and configuration that produced it, and a woven result whose recorded hash is no longer the experiment's hash is stale and refused.

DIRECTIVE module-attribute

DIRECTIVE = re.compile('^(?P<indent>[ \\t]*)```\\{experiment\\}[ \\t]+(?P<name>[A-Za-z_][A-Za-z0-9_.]*)[ \\t]*\\n(?P<body>.*?)^(?P=indent)```', re.M | re.S)

An {experiment} directive fence: its indentation, experiment, and body.

ExperimentDirective didactic-model

ExperimentDirective(*, experiment: str, line: int, column: int, indent: str, start: int, end: int, body: str, recorded_hash: str = '')

Bases: Model

One {experiment} directive of a document.

PARAMETER DESCRIPTION
experiment

The experiment it weaves.

TYPE: str

line

The 1-based line of the opening fence.

TYPE: int

column

The 0-based column of the experiment's name.

TYPE: int

indent

The fence's indentation.

TYPE: str

start

The character offset of the body in the document.

TYPE: int

end

The character offset one past the body.

TYPE: int

body

The body with the indentation removed.

TYPE: str

recorded_hash

The :hash: the body records, or empty for an unwoven directive.

TYPE: str DEFAULT: ''

experiment_directives

experiment_directives(text: str) -> tuple[ExperimentDirective, ...]

The {experiment} directives of a document.

PARAMETER DESCRIPTION
text

The document.

TYPE: str

RETURNS DESCRIPTION
tuple[ExperimentDirective, ...]

The directives in document order.

Source code in src/quivers/dsl/literate/directives.py
def experiment_directives(text: str) -> tuple[ExperimentDirective, ...]:
    """The ``{experiment}`` directives of a document.

    Parameters
    ----------
    text : str
        The document.

    Returns
    -------
    tuple[ExperimentDirective, ...]
        The directives in document order.
    """
    found: list[ExperimentDirective] = []
    for match in DIRECTIVE.finditer(text):
        body = dedent(match.group("indent"), match.group("body"))
        fields = {
            item.group("key"): item.group("value") for item in _FIELD.finditer(body)
        }
        name_start = match.start("name")
        found.append(
            ExperimentDirective(
                experiment=match.group("name"),
                line=text.count("\n", 0, match.start()) + 1,
                column=name_start - (text.rfind("\n", 0, name_start) + 1),
                indent=match.group("indent"),
                start=match.start("body"),
                end=match.end("body"),
                body=body,
                recorded_hash=fields.get("hash", ""),
            )
        )
    return tuple(found)

woven_body

woven_body(fields: tuple[tuple[str, str], ...], lines: tuple[str, ...]) -> str

The body a woven directive holds.

PARAMETER DESCRIPTION
fields

The :key: value fields, in order; hash first.

TYPE: tuple[tuple[str, str], ...]

lines

The rendered result.

TYPE: tuple[str, ...]

RETURNS DESCRIPTION
str

The fields, then the result, one per line.

Source code in src/quivers/dsl/literate/directives.py
def woven_body(fields: tuple[tuple[str, str], ...], lines: tuple[str, ...]) -> str:
    """The body a woven directive holds.

    Parameters
    ----------
    fields : tuple[tuple[str, str], ...]
        The ``:key: value`` fields, in order; ``hash`` first.
    lines : tuple[str, ...]
        The rendered result.

    Returns
    -------
    str
        The fields, then the result, one per line.
    """
    rendered = [f":{key}: {value}" for key, value in fields]
    rendered.extend(lines)
    return "".join(line + "\n" for line in rendered)

weave_directives

weave_directives(text: str, bodies: dict[int, str]) -> str

A document with some directives' bodies replaced.

PARAMETER DESCRIPTION
text

The document.

TYPE: str

bodies

The new body (dedented) of each directive, by its fence's line.

TYPE: dict[int, str]

RETURNS DESCRIPTION
str

The document with those bodies written in, indented like their fences; every other character is unchanged.

Source code in src/quivers/dsl/literate/directives.py
def weave_directives(text: str, bodies: dict[int, str]) -> str:
    """A document with some directives' bodies replaced.

    Parameters
    ----------
    text : str
        The document.
    bodies : dict[int, str]
        The new body (dedented) of each directive, by its fence's line.

    Returns
    -------
    str
        The document with those bodies written in, indented like their
        fences; every other character is unchanged.
    """
    pieces: list[str] = []
    cursor = 0
    for directive in experiment_directives(text):
        if directive.line not in bodies:
            continue
        pieces.append(text[cursor : directive.start])
        pieces.append(indent_body(directive.indent, bodies[directive.line]))
        cursor = directive.end
    pieces.append(text[cursor:])
    return "".join(pieces)