_dispatch
Attributes¶
PyObject = Union[dict, list, str, float, int, np.dtype, None, Any, pd.DataFrame, pd.Series, np.ndarray, az.InferenceData, xr.DataArray, xr.Dataset]
module-attribute
¶
Union of common Python-side objects produced by R→Python conversion.
This is intentionally broad: brmspy frequently returns standard scientific Python types (NumPy/pandas/xarray/ArviZ), plus plain dict/list primitives.
Note
Avoid adding Any here unless absolutely necessary; it defeats the purpose of
having this alias.
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
ShmPool
¶
Minimal interface for allocating and attaching shared-memory blocks.
The concrete implementation lives in
brmspy._session.transport.ShmPool and tracks
blocks so they can be closed on teardown.
Source code in brmspy/types/shm.py
Functions¶
__init__(manager)
¶
Create a pool bound to an existing SharedMemoryManager.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
manager
|
SharedMemoryManager
|
Manager used to allocate blocks. |
required |
Source code in brmspy/types/shm.py
alloc(size, temporary=False)
¶
Allocate a new shared-memory block.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
size
|
int
|
Size in bytes. |
required |
Returns:
| Type | Description |
|---|---|
ShmBlock
|
Newly allocated block. |
Source code in brmspy/types/shm.py
attach(ref)
¶
Attach to an existing shared-memory block by name.
Returns:
| Type | Description |
|---|---|
ShmBlock
|
Attached block. |
close_all()
¶
Functions¶
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 | |
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 | |