|
12 | 12 | # See the License for the specific language governing permissions and
|
13 | 13 | # limitations under the License.
|
14 | 14 |
|
15 |
| -"""Backends for traces |
| 15 | +"""Storage backends for traces |
16 | 16 |
|
17 |
| -Available backends |
18 |
| ------------------- |
19 |
| -
|
20 |
| -1. NumPy array (pymc3.backends.NDArray) |
21 |
| -2. Text files (pymc3.backends.Text) |
22 |
| -3. SQLite (pymc3.backends.SQLite) |
23 |
| -
|
24 |
| -The NDArray backend holds the entire trace in memory, whereas the Text |
25 |
| -and SQLite backends store the values while sampling. |
26 |
| -
|
27 |
| -Selecting a backend |
28 |
| -------------------- |
29 |
| -
|
30 |
| -By default, a NumPy array is used as the backend. To specify a different |
31 |
| -backend, pass a backend instance to `sample`. |
32 |
| -
|
33 |
| -For example, the following would save the sampling values to CSV files |
34 |
| -in the directory 'test'. |
35 |
| -
|
36 |
| - >>> import pymc3 as pm |
37 |
| - >>> with pm.Model(): |
38 |
| - >>> db = pm.backends.Text('test') |
39 |
| - >>> trace = pm.sample(..., trace=db) |
40 |
| -
|
41 |
| -Note that as in the example above, one must have an active model context, |
42 |
| -or pass a `model` parameter in order to create a backend. |
| 17 | +The NDArray (pymc3.backends.NDArray) backend holds the entire trace in memory. |
43 | 18 |
|
44 | 19 | Selecting values from a backend
|
45 | 20 | -------------------------------
|
|
77 | 52 | >>> sliced_trace = trace[1000:]
|
78 | 53 |
|
79 | 54 | The backend for the new trace is always NDArray, regardless of the
|
80 |
| -type of original trace. Only the NDArray backend supports a stop |
81 |
| -value in the slice. |
| 55 | +type of original trace. |
82 | 56 |
|
83 | 57 | Loading a saved backend
|
84 | 58 | -----------------------
|
85 | 59 |
|
86 |
| -Saved backends can be loaded using `load` function in the module for the |
87 |
| -specific backend. |
88 |
| -
|
89 |
| - >>> trace = pm.backends.text.load('test') |
90 |
| -
|
91 |
| -Writing custom backends |
92 |
| ------------------------ |
93 |
| -
|
94 |
| -Backends consist of a class that handles sampling storage and value |
95 |
| -selection. Three sampling methods of backend will be called: |
96 |
| -
|
97 |
| -- setup: Before sampling is started, the `setup` method will be called |
98 |
| - with two arguments: the number of draws and the chain number. This is |
99 |
| - useful setting up any structure for storing the sampling values that |
100 |
| - require the above information. |
101 |
| -
|
102 |
| -- record: Record the sampling results for the current draw. This method |
103 |
| - will be called with a dictionary of values mapped to the variable |
104 |
| - names. This is the only sampling function that *must* do something to |
105 |
| - have a meaningful backend. |
106 |
| -
|
107 |
| -- close: This method is called following sampling and should perform any |
108 |
| - actions necessary for finalizing and cleaning up the backend. |
109 |
| -
|
110 |
| -The base storage class `backends.base.BaseTrace` provides common model |
111 |
| -setup that is used by all the PyMC backends. |
112 |
| -
|
113 |
| -Several selection methods must also be defined: |
114 |
| -
|
115 |
| -- get_values: This is the core method for selecting values from the |
116 |
| - backend. It can be called directly and is used by __getitem__ when the |
117 |
| - backend is indexed with a variable name or object. |
118 |
| -
|
119 |
| -- _slice: Defines how the backend returns a slice of itself. This |
120 |
| - is called if the backend is indexed with a slice range. |
121 |
| -
|
122 |
| -- point: Returns values for each variable at a single iteration. This is |
123 |
| - called if the backend is indexed with a single integer. |
124 |
| -
|
125 |
| -- __len__: This should return the number of draws. |
126 |
| -
|
127 |
| -When `pymc3.sample` finishes, it wraps all trace objects in a MultiTrace |
128 |
| -object that provides a consistent selection interface for all backends. |
129 |
| -If the traces are stored on disk, then a `load` function should also be |
130 |
| -defined that returns a MultiTrace object. |
| 60 | +Saved backends can be loaded using `arviz.from_netcdf` |
131 | 61 |
|
132 |
| -For specific examples, see pymc3.backends.{ndarray,text,sqlite}.py. |
133 | 62 | """
|
134 | 63 | from ..backends.ndarray import NDArray, save_trace, load_trace, point_list_to_multitrace
|
135 |
| -from ..backends.text import Text |
136 |
| -from ..backends.sqlite import SQLite |
137 |
| -from ..backends.hdf5 import HDF5 |
138 |
| - |
139 |
| -_shortcuts = { |
140 |
| - "text": {"backend": Text, "name": "mcmc"}, |
141 |
| - "sqlite": {"backend": SQLite, "name": "mcmc.sqlite"}, |
142 |
| - "hdf5": {"backend": HDF5, "name": "mcmc.hdf5"}, |
143 |
| -} |
0 commit comments