Skip to content

Reading and writing text

A model written in the notation is read with tq.Model.from_text (a string) or tq.load (a .tq file); any model is written back with m.to_text() and typeset with m.to_latex() (methods of tq.Model).

A single formula is read with tq.formula, with the same grammar, functions and messages as in a model, for programs that use TenQSP's formula language outside a model (for example a fitting part's covariate formulas). It lists the names the formula uses and gives a JAX function of values for them:

f = tq.formula("CL_pop * (WT/70)^0.75")
f.names                                 # ('CL_pop', 'WT')
g = f.jax(["CL_pop", "WT"])             # arguments in this order
g(0.04, jnp.array([60.0, 80.0]))        # works with jax.grad, jax.jit and jax.vmap

Outside a model every name is one value, so what works over a model's axes (shape functions, grad, k[s], a field at one end) is refused with a message.

tenqsp.load(path, *, cells=None, params=None)

Read a model file (.tq, plain text in the TenQSP notation). The model is named by its model line, or else by the file name. See from_text for cells and params.

tenqsp.table(data, *, axes, value)

A long table (one row per value: a column per axis and a value column) as the nested dict a parameter over several axes takes, e.g. tq.table(pd.read_csv("volumes.csv"), axes=["organ", "sub"], value="V").

data is anything indexable by column name (a pandas DataFrame, a dict of lists). A combination given twice is an error; a missing one is reported when the parameter is made.

tenqsp.formula

Formulas outside a model: tq.formula(text).

Another program that writes formulas in TenQSP's language (tenqsp-fit's fit part, for one) reads them here with the same grammar, functions and messages as a model, lists the names a formula uses, and evaluates it as a JAX function of values for those names. Inside a model, names mean parameters, compartments and fields; here they are plain values given by the caller. What only makes sense over a model's axes (shape functions, grad, a parameter over an axis k[s], a field at one end F(z=lower)) is refused with a message.

names_in(text)

The names a formula uses (not the functions it calls), sorted, after checking that the formula is one usable outside a model.

formula(text)

A formula in TenQSP's formula language, read and checked now (the same grammar, functions and messages as in a model, docs/language/formulas.md).

f.names lists the names it uses; f.jax(["a", "b"]) is a JAX function of values for those names, in that order::

f = tq.formula("CL_pop * (WT/70)^0.75")
f.names                           # ('CL_pop', 'WT')
g = f.jax(["CL_pop", "WT"])
g(0.04, jnp.array([60.0, 80.0]))  # broadcasts like any JAX function

It is an ordinary formula: tq.exp(f), 2*f and model statements accept it.

compile_free(text, names)

Formula.jax: a JAX function of values for names (positional, in that order).