io
I/O helpers for brmspy.
This module contains helpers for:
- loading example datasets from R packages
- saving/loading R objects via saveRDS / readRDS
Notes
These functions are executed inside the worker process that hosts the embedded R session.
Attributes¶
FitResult = IDResult[IDBrm]
module-attribute
¶
ProxyListSexpVector = Union[SexpWrapper, ListSexpVector, None]
module-attribute
¶
Classes¶
RListVectorExtension
dataclass
¶
Generic result container with R objects.
Attributes:
| Name | Type | Description |
|---|---|---|
r |
ListVector
|
R object from brms |
Source code in brmspy/types/brms_results.py
Functions¶
brmsfit_to_idata(brmsfit_obj, model_data=None)
¶
Source code in brmspy/helpers/_rpy2/_conversion.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
r_to_py(obj, shm=None)
¶
Convert R objects to Python objects via rpy2.
Comprehensive converter that handles R lists (named/unnamed), vectors, formulas, and language objects. Provides sensible Python equivalents for all R types with special handling for edge cases.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
rpy2 R object
|
R object to convert to Python |
required |
Returns:
| Type | Description |
|---|---|
any
|
Python representation of the R object: - R NULL → None - Named list → dict (recursively) - Unnamed list → list (recursively) - Length-1 vector → scalar (int, float, str, bool) - Length-N vector → list of scalars - Formula/Language object → str (descriptive representation) - Other objects → default rpy2 conversion or str fallback |
Notes
Conversion Rules:
- R NULL: → Python None
- Atomic vectors (numeric, character, logical):
- Length 1: → Python scalar (int, float, str, bool)
- Length >1: → Python list of scalars
- Named lists (ListVector with names): → Python dict, recursively
- Unnamed lists: → Python list, recursively
- Formulas (e.g.,
y ~ x): → String representation - Language objects (calls, expressions): → String representation
- Functions: → String representation
- Everything else: Try default rpy2 conversion, fallback to string
Recursive Conversion:
List elements and dictionary values are recursively converted:
Safe Fallback:
R language objects, formulas, and functions are converted to descriptive strings rather than attempting complex conversions that might fail.
Examples:
from brmspy.helpers.conversion import r_to_py
import rpy2.robjects as ro
# R NULL
r_to_py(ro.NULL) # None
# Scalars
r_to_py(ro.IntVector([5])) # 5
r_to_py(ro.FloatVector([3.14])) # 3.14
r_to_py(ro.StrVector(["hello"])) # "hello"
# Vectors
r_to_py(ro.IntVector([1, 2, 3])) # [1, 2, 3]
See Also
py_to_r : Convert Python objects to R brmspy.brms.summary : Returns Python-friendly summary dict
Source code in brmspy/helpers/_rpy2/_converters/_dispatch.py
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 | |
get_data(dataset_name, **kwargs)
¶
Load an R dataset and return it as a pandas DataFrame.
This is a thin wrapper around R's data() that loads the object
into the R global environment and converts it to a
:class:pandas.DataFrame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dataset_name
|
str
|
Name of the dataset as used in R (e.g. |
required |
**kwargs
|
Additional keyword arguments forwarded to R's |
{}
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
Dataset converted to a pandas DataFrame. |
Raises:
| Type | Description |
|---|---|
KeyError
|
If the dataset is not found in the R global environment after
calling |
RuntimeError
|
If conversion from the R object to a pandas DataFrame fails. |
See Also
get_brms_data
Convenience wrapper for datasets from the brms package.
Source code in brmspy/_brms_functions/io.py
get_brms_data(dataset_name, **kwargs)
¶
Load an example dataset from the R brms package.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dataset_name
|
str
|
Dataset name (for example |
required |
**kwargs
|
Forwarded to R |
{}
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
Dataset converted to a DataFrame. |
Examples:
Source code in brmspy/_brms_functions/io.py
save_rds(object, file, **kwargs)
¶
Save an R object to an .rds file via R saveRDS().
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
object
|
RListVectorExtension or ProxyListSexpVector
|
Object to save. If you pass a |
required |
file
|
str
|
Output path. |
required |
**kwargs
|
Forwarded to R |
{}
|
Returns:
| Type | Description |
|---|---|
None
|
|
Examples:
from brmspy import brms
model = brms.brm("y ~ x", data=df, chains=4)
brms.save_rds(model, "model.rds")
Source code in brmspy/_brms_functions/io.py
read_rds_raw(file, **kwargs)
¶
Load an R object from an .rds file via R readRDS().
This returns the raw R object handle.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file
|
str
|
Input path. |
required |
**kwargs
|
Forwarded to R |
{}
|
Returns:
| Type | Description |
|---|---|
ListSexpVector
|
Raw R object. |
Examples:
Source code in brmspy/_brms_functions/io.py
read_rds_fit(file, **kwargs)
¶
Load a saved brms model from an .rds file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file
|
str
|
Input path containing a saved brmsfit. |
required |
**kwargs
|
Forwarded to R |
{}
|
Returns:
| Type | Description |
|---|---|
FitResult
|
|
Examples: