formula
Formula helpers and DSL.
This module provides a small Pythonic DSL for composing brms formulas. The public
functions (bf, lf, nlf, acformula, set_rescor, set_mecor, set_nl)
build a FormulaConstruct that can be passed to brmspy.brms.brm() or combined
using the + operator.
Notes
- The returned objects are lightweight formula specifications; the actual R brms formula object is built in the worker when fitting / generating Stan code.
- This module is part of the public API documented under docs/api/brms_functions/formula.md.
Attributes¶
_FORMULA_FUNCTION_WHITELIST = Literal['bf', 'lf', 'nlf', 'acformula', 'set_rescor', 'set_mecor', 'set_nl']
module-attribute
¶
ProxyListSexpVector = Union[SexpWrapper, ListSexpVector, None]
module-attribute
¶
Classes¶
SexpWrapper
dataclass
¶
Lightweight handle for an R object stored in the worker.
The worker keeps the real rpy2 Sexp in an internal cache and replaces it in
results with this wrapper. When passed back to the worker, the wrapper is
resolved to the original Sexp again.
Notes
SexpWrapperinstances are only meaningful within the lifetime of the worker process that produced them. After a worker restart, previously returned wrappers can no longer be reattached.- This type exists to keep the main process free of rpy2 / embedded-R state.
Source code in brmspy/types/session.py
FormulaConstruct
dataclass
¶
A composite formula expression built from parts.
FormulaConstruct stores a tree of nodes (FormulaPart and/or R objects)
representing expressions combined with +. It is primarily created by
calling the public formula helpers exposed by brmspy.brms.
Notes
The + operator supports grouping:
a + b + cbecomes a single summand (one “group”)(a + b) + (a + b)becomes two summands (two “groups”)
Use iter_summands()
to iterate over these groups in a deterministic way.
Source code in brmspy/types/formula_dsl.py
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 | |
Attributes¶
_parts
instance-attribute
¶
Functions¶
_formula_parse(obj)
classmethod
¶
Convert a supported value into a FormulaConstruct.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
Other
|
One of: |
required |
Returns:
| Type | Description |
|---|---|
FormulaConstruct
|
|
Source code in brmspy/types/formula_dsl.py
__add__(other)
¶
Combine two formula expressions with +.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Other
|
Value to add. Strings are treated as |
required |
Returns:
| Type | Description |
|---|---|
FormulaConstruct
|
New combined expression. |
Source code in brmspy/types/formula_dsl.py
__radd__(other)
¶
iter_summands()
¶
Iterate over arithmetic groups (summands).
Returns:
| Type | Description |
|---|---|
Iterator[tuple[FormulaPart | ProxyListSexpVector, ...]]
|
Each yielded tuple represents one summand/group. |
Examples:
from brmspy.brms import bf, gaussian, set_rescor
f = bf("y ~ x") + gaussian() + set_rescor(True)
for summand in f.iter_summands():
print(summand)
Source code in brmspy/types/formula_dsl.py
__iter__()
¶
Alias for iter_summands().
iterate()
¶
Iterate over all leaf nodes in left-to-right order.
This flattens the expression tree, unlike
iter_summands(), which
respects grouping.
Returns:
| Type | Description |
|---|---|
Iterator[FormulaPart | ProxyListSexpVector]
|
|
Source code in brmspy/types/formula_dsl.py
__str__()
¶
_pretty(node, _outer=True)
¶
Source code in brmspy/types/formula_dsl.py
__repr__()
¶
__init__(_parts)
¶
FormulaPart
dataclass
¶
A single formula helper invocation.
Instances of this type represent a call like bf("y ~ x") or set_rescor(True)
without executing anything. They are primarily used as nodes inside a
FormulaConstruct.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
_fun
|
Literal[...]
|
Whitelisted formula helper name. |
required |
_args
|
Sequence[Primitive]
|
Positional arguments for the helper. |
required |
_kwargs
|
Mapping[str, Primitive]
|
Keyword arguments for the helper. |
required |
Notes
This is a low-level type. Most users should construct these via the public
helper functions in brmspy.brms.
Source code in brmspy/types/formula_dsl.py
Attributes¶
_fun
instance-attribute
¶
_args
instance-attribute
¶
_kwargs
instance-attribute
¶
Functions¶
__post_init__()
¶
Validate _fun, _args, and _kwargs types after construction.
Source code in brmspy/types/formula_dsl.py
__str__()
¶
Render a readable fun(arg1, ..., kw=...) representation.
Source code in brmspy/types/formula_dsl.py
__repr__()
¶
__init__(_fun, _args, _kwargs)
¶
Functions¶
log(*msg, method_name=None, level=logging.INFO)
¶
Log a message with automatic method name detection.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
msg
|
str
|
The message to log |
()
|
method_name
|
str
|
The name of the method/function. If None, will auto-detect from call stack. |
None
|
level
|
int
|
Logging level (default: logging.INFO) |
INFO
|
Source code in brmspy/helpers/log.py
kwargs_r(kwargs)
¶
Convert Python keyword arguments to R-compatible format.
Convenience function that applies py_to_r() to all values in a keyword arguments dictionary, preparing them for R function calls.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
kwargs
|
dict or None
|
Dictionary of keyword arguments where values may be Python objects (dicts, lists, DataFrames, arrays, etc.) |
required |
Returns:
| Type | Description |
|---|---|
dict
|
Dictionary with same keys but R-compatible values, or empty dict if None |
Notes
This is a thin wrapper around py_to_r() that operates on dictionaries.
It's commonly used to prepare keyword arguments for R function calls via rpy2.
Examples:
from brmspy.helpers.conversion import kwargs_r
import pandas as pd
import numpy as np
# Prepare kwargs for R function
py_kwargs = {
'data': pd.DataFrame({'y': [1, 2], 'x': [1, 2]}),
'prior': {'b': [0, 1]},
'chains': 4,
'iter': 2000
}
r_kwargs = kwargs_r(py_kwargs)
# All values converted to R objects
# Can now call: r_function(**r_kwargs)
See Also
py_to_r : Underlying conversion function for individual values brmspy.brms.fit : Uses this to prepare user kwargs for R
Source code in brmspy/helpers/_rpy2/_conversion.py
py_to_r(obj)
¶
Convert arbitrary Python objects to R objects via rpy2.
Comprehensive converter that handles nested structures (dicts, lists), DataFrames, arrays, and scalars. Uses rpy2's converters with special handling for dictionaries (→ R named lists) and lists of dicts.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
any
|
Python object to convert. Supported types: - None → R NULL - dict → R named list (ListVector), recursively - list/tuple of dicts → R list of named lists - list/tuple (other) → R vector or list - pd.DataFrame → R data.frame - np.ndarray → R vector/matrix - scalars (int, float, str, bool) → R atomic types |
required |
Returns:
| Type | Description |
|---|---|
rpy2 R object
|
R representation of the Python object |
Notes
Conversion Rules:
- None: → R NULL
- DataFrames: → R data.frame (via pandas2ri)
- Dictionaries: → R named list (ListVector), recursively converting values
- Lists of dicts: → R list with 1-based indexed names containing named lists
- Other lists/tuples: → R vectors or lists (via rpy2 default)
- NumPy arrays: → R vectors/matrices (via numpy2ri)
- Scalars: → R atomic values
Recursive Conversion:
Dictionary values are recursively converted, allowing nested structures:
List of Dicts:
Lists containing only dicts are converted to R lists with 1-based indexing:
Examples:
from brmspy.helpers.conversion import py_to_r
import numpy as np
import pandas as pd
# Scalars
py_to_r(5) # R: 5
py_to_r("hello") # R: "hello"
py_to_r(None) # R: NULL
# Arrays
py_to_r(np.array([1, 2, 3])) # R: c(1, 2, 3)
# DataFrames
df = pd.DataFrame({'x': [1, 2], 'y': [3, 4]})
py_to_r(df) # R: data.frame(x = c(1, 2), y = c(3, 4))
See Also
r_to_py : Convert R objects back to Python kwargs_r : Convert keyword arguments dict for R function calls brmspy.brms.fit : Uses this for converting data to R
Source code in brmspy/helpers/_rpy2/_converters/_dispatch.py
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 | |
bf(*formulas, **formula_args)
¶
Build a brms model formula.
This is the primary entrypoint for specifying the mean model and can be
combined with other formula parts (e.g. lf, nlf, acformula) using +.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*formulas
|
str
|
One or more brms formula strings (e.g. |
()
|
**formula_args
|
Keyword arguments forwarded to R |
{}
|
Returns:
| Type | Description |
|---|---|
FormulaConstruct
|
A composable formula specification. |
See Also
brms::brmsformula : R documentation
Examples:
Basic formula:
QR decomposition (often helps with collinearity):
Multivariate formula + residual correlation:
Source code in brmspy/_brms_functions/formula.py
lf(*formulas, flist=None, dpar=None, resp=None, center=None, cmc=None, sparse=None, decomp=None)
¶
Add linear formulas for distributional / non-linear parameters.
This wraps R brms::lf() and is typically used to model distributional
parameters such as sigma (heteroskedasticity) or to specify predictors
for non-linear parameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*formulas
|
str | FormulaConstruct | FormulaPart | ProxyListSexpVector
|
One or more formulas such as |
()
|
flist
|
Optional list of formulas (advanced; mirrors brms). |
None
|
|
dpar
|
str or None
|
Distributional parameter name (e.g. |
None
|
resp
|
str or None
|
Response name for multivariate models. |
None
|
center
|
bool | None
|
Forwarded to R |
None
|
cmc
|
bool | None
|
Forwarded to R |
None
|
sparse
|
bool | None
|
Forwarded to R |
None
|
decomp
|
bool | None
|
Forwarded to R |
None
|
Returns:
| Type | Description |
|---|---|
FormulaConstruct
|
A composable formula specification that can be combined using |
See Also
brms::lf : R documentation
Examples:
Model mean + sigma:
Source code in brmspy/_brms_functions/formula.py
nlf(*formulas, flist=None, dpar=None, resp=None, loop=None)
¶
Add non-linear formulas.
Wraps R brms::nlf(). This is used together with set_nl() and parameter
definitions in lf() to specify non-linear models.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*formulas
|
str | FormulaConstruct | FormulaPart | ProxyListSexpVector
|
One or more non-linear formulas (e.g. |
()
|
flist
|
Optional list of formulas (advanced; mirrors brms). |
None
|
|
dpar
|
str or None
|
Distributional parameter name (optional). |
None
|
resp
|
str or None
|
Response name for multivariate models. |
None
|
loop
|
bool or None
|
Forwarded to R |
None
|
Returns:
| Type | Description |
|---|---|
FormulaConstruct
|
A composable formula specification. |
See Also
brms::nlf : R documentation
Examples:
Source code in brmspy/_brms_functions/formula.py
acformula(autocor, resp=None)
¶
Add an autocorrelation structure.
Wraps R brms::acformula().
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
autocor
|
str
|
One-sided autocorrelation formula (e.g. |
required |
resp
|
str or None
|
Response name for multivariate models. |
None
|
Returns:
| Type | Description |
|---|---|
FormulaConstruct
|
A composable formula specification. |
See Also
brms::acformula : R documentation
Examples:
Source code in brmspy/_brms_functions/formula.py
set_rescor(rescor=True)
¶
Control residual correlations in multivariate models.
Wraps R brms::set_rescor().
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rescor
|
bool
|
Whether to model residual correlations. |
True
|
Returns:
| Type | Description |
|---|---|
FormulaConstruct
|
A composable formula specification. |
See Also
brms::set_rescor : R documentation
Examples:
Source code in brmspy/_brms_functions/formula.py
set_mecor(mecor=True)
¶
Control correlations between latent me() terms.
Wraps R brms::set_mecor().
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mecor
|
bool
|
Whether to model correlations between latent variables introduced by |
True
|
Returns:
| Type | Description |
|---|---|
FormulaConstruct
|
A composable formula specification. |
See Also
brms::set_mecor : R documentation
Examples:
Source code in brmspy/_brms_functions/formula.py
set_nl(dpar=None, resp=None)
¶
Mark a model (or part of it) as non-linear.
Wraps R brms::set_nl().
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dpar
|
str or None
|
Distributional parameter name (if only part of the model is non-linear). |
None
|
resp
|
str or None
|
Response name for multivariate models. |
None
|
Returns:
| Type | Description |
|---|---|
FormulaConstruct
|
A composable formula specification. |
See Also
brms::set_nl : R documentation
Examples:
from brmspy.brms import bf, lf, set_nl
f = bf("y ~ a * inv_logit(x * b)") + lf("a + b ~ z") + set_nl()