/
ndarray.py
342 lines (281 loc) · 11.5 KB
/
ndarray.py
1
2
3
4
5
6
7
8
9
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
113
114
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
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
"""NumPy array trace backend
Store sampling values in memory as a NumPy array.
"""
import glob
import json
import os
import shutil
from typing import Optional, Dict, Any
import numpy as np
from pymc3.backends import base
from pymc3.backends.base import MultiTrace
from pymc3.model import Model
from pymc3.exceptions import TraceDirectoryError
def save_trace(trace: MultiTrace, directory: Optional[str]=None, overwrite=False) -> str:
"""Save multitrace to file.
TODO: Also save warnings.
This is a custom data format for PyMC3 traces. Each chain goes inside
a directory, and each directory contains a metadata json file, and a
numpy compressed file. See https://docs.scipy.org/doc/numpy/neps/npy-format.html
for more information about this format.
Parameters
----------
trace : pm.MultiTrace
trace to save to disk
directory : str (optional)
path to a directory to save the trace
overwrite : bool (default False)
whether to overwrite an existing directory.
Returns
-------
str, path to the directory where the trace was saved
"""
if directory is None:
directory = '.pymc_{}.trace'
idx = 1
while os.path.exists(directory.format(idx)):
idx += 1
directory = directory.format(idx)
if os.path.isdir(directory):
if overwrite:
shutil.rmtree(directory)
else:
raise OSError('Cautiously refusing to overwrite the already existing {}! Please supply '
'a different directory, or set `overwrite=True`'.format(directory))
os.makedirs(directory)
for chain, ndarray in trace._straces.items():
SerializeNDArray(os.path.join(directory, str(chain))).save(ndarray)
return directory
def load_trace(directory: str, model=None) -> MultiTrace:
"""Loads a multitrace that has been written to file.
A the model used for the trace must be passed in, or the command
must be run in a model context.
Parameters
----------
directory : str
Path to a pymc3 serialized trace
model : pm.Model (optional)
Model used to create the trace. Can also be inferred from context
Returns
-------
pm.Multitrace that was saved in the directory
"""
straces = []
for subdir in glob.glob(os.path.join(directory, '*')):
if os.path.isdir(subdir):
straces.append(SerializeNDArray(subdir).load(model))
if not straces:
raise TraceDirectoryError("%s is not a PyMC3 saved chain directory." % directory)
return base.MultiTrace(straces)
class SerializeNDArray:
metadata_file = 'metadata.json'
samples_file = 'samples.npz'
metadata_path = None # type: str
samples_path = None # type: str
def __init__(self, directory: str):
"""Helper to save and load NDArray objects"""
self.directory = directory
self.metadata_path = os.path.join(self.directory, self.metadata_file)
self.samples_path = os.path.join(self.directory, self.samples_file)
@staticmethod
def to_metadata(ndarray):
"""Extract ndarray metadata into json-serializable content"""
if ndarray._stats is None:
stats = ndarray._stats
else:
stats = []
for stat in ndarray._stats:
stats.append({key: value.tolist() for key, value in stat.items()})
metadata = {
'draw_idx': ndarray.draw_idx,
'draws': ndarray.draws,
'_stats': stats,
'chain': ndarray.chain,
}
return metadata
def save(self, ndarray):
"""Serialize a ndarray to file
The goal here is to be modestly safer and more portable than a
pickle file. The expense is that the model code must be available
to reload the multitrace.
"""
if not isinstance(ndarray, NDArray):
raise TypeError('Can only save NDArray')
if os.path.isdir(self.directory):
shutil.rmtree(self.directory)
os.mkdir(self.directory)
with open(self.metadata_path, 'w') as buff:
json.dump(SerializeNDArray.to_metadata(ndarray), buff)
np.savez_compressed(self.samples_path, **ndarray.samples)
def load(self, model: Model) -> 'NDArray':
"""Load the saved ndarray from file"""
if not os.path.exists(self.samples_path) or not os.path.exists(self.metadata_path):
raise TraceDirectoryError("%s is not a trace directory" % self.directory)
new_trace = NDArray(model=model)
with open(self.metadata_path, 'r') as buff:
metadata = json.load(buff)
metadata['_stats'] = [{k: np.array(v) for k, v in stat.items()} for stat in metadata['_stats']]
for key, value in metadata.items():
setattr(new_trace, key, value)
new_trace.samples = dict(np.load(self.samples_path))
return new_trace
class NDArray(base.BaseTrace):
"""NDArray trace object
Parameters
----------
name : str
Name of backend. This has no meaning for the NDArray backend.
model : Model
If None, the model is taken from the `with` context.
vars : list of variables
Sampling values will be stored for these variables. If None,
`model.unobserved_RVs` is used.
"""
supports_sampler_stats = True
def __init__(self, name=None, model=None, vars=None, test_point=None):
super().__init__(name, model, vars, test_point)
self.draw_idx = 0
self.draws = None
self.samples = {}
self._stats = None
# Sampling methods
def setup(self, draws, chain, sampler_vars=None) -> None:
"""Perform chain-specific setup.
Parameters
----------
draws : int
Expected number of draws
chain : int
Chain number
sampler_vars : list of dicts
Names and dtypes of the variables that are
exported by the samplers.
"""
super().setup(draws, chain, sampler_vars)
self.chain = chain
if self.samples: # Concatenate new array if chain is already present.
old_draws = len(self)
self.draws = old_draws + draws
self.draw_idx = old_draws
for varname, shape in self.var_shapes.items():
old_var_samples = self.samples[varname]
new_var_samples = np.zeros((draws, ) + shape,
self.var_dtypes[varname])
self.samples[varname] = np.concatenate((old_var_samples,
new_var_samples),
axis=0)
else: # Otherwise, make array of zeros for each variable.
self.draws = draws
for varname, shape in self.var_shapes.items():
self.samples[varname] = np.zeros((draws, ) + shape,
dtype=self.var_dtypes[varname])
if sampler_vars is None:
return
if self._stats is None:
self._stats = []
for sampler in sampler_vars:
data = dict() # type: Dict[str, np.ndarray]
self._stats.append(data)
for varname, dtype in sampler.items():
data[varname] = np.zeros(draws, dtype=dtype)
else:
for data, vars in zip(self._stats, sampler_vars):
if vars.keys() != data.keys():
raise ValueError("Sampler vars can't change")
old_draws = len(self)
for varname, dtype in vars.items():
old = data[varname]
new = np.zeros(draws, dtype=dtype)
data[varname] = np.concatenate([old, new])
def record(self, point, sampler_stats=None) -> None:
"""Record results of a sampling iteration.
Parameters
----------
point : dict
Values mapped to variable names
"""
for varname, value in zip(self.varnames, self.fn(point)):
self.samples[varname][self.draw_idx] = value
if self._stats is not None and sampler_stats is None:
raise ValueError("Expected sampler_stats")
if self._stats is None and sampler_stats is not None:
raise ValueError("Unknown sampler_stats")
if sampler_stats is not None:
for data, vars in zip(self._stats, sampler_stats):
for key, val in vars.items():
data[key][self.draw_idx] = val
self.draw_idx += 1
def _get_sampler_stats(self, varname, sampler_idx, burn, thin):
return self._stats[sampler_idx][varname][burn::thin]
def close(self):
if self.draw_idx == self.draws:
return
# Remove trailing zeros if interrupted before completed all
# draws.
self.samples = {var: vtrace[:self.draw_idx]
for var, vtrace in self.samples.items()}
if self._stats is not None:
self._stats = [
{var: trace[:self.draw_idx] for var, trace in stats.items()}
for stats in self._stats]
# Selection methods
def __len__(self):
if not self.samples: # `setup` has not been called.
return 0
return self.draw_idx
def get_values(self, varname: str, burn=0, thin=1) -> np.ndarray:
"""Get values from trace.
Parameters
----------
varname : str
burn : int
thin : int
Returns
-------
A NumPy array
"""
return self.samples[varname][burn::thin]
def _slice(self, idx):
# Slicing directly instead of using _slice_as_ndarray to
# support stop value in slice (which is needed by
# iter_sample).
# Only the first `draw_idx` value are valid because of preallocation
idx = slice(*idx.indices(len(self)))
sliced = NDArray(model=self.model, vars=self.vars)
sliced.chain = self.chain
sliced.samples = {varname: values[idx]
for varname, values in self.samples.items()}
sliced.sampler_vars = self.sampler_vars
sliced.draw_idx = (idx.stop - idx.start) // idx.step
if self._stats is None:
return sliced
sliced._stats = []
for vars in self._stats:
var_sliced = {}
sliced._stats.append(var_sliced)
for key, vals in vars.items():
var_sliced[key] = vals[idx]
return sliced
def point(self, idx) -> Dict[str, Any]:
"""Return dictionary of point values at `idx` for current chain
with variable names as keys.
"""
idx = int(idx)
return {varname: values[idx]
for varname, values in self.samples.items()}
def _slice_as_ndarray(strace, idx):
sliced = NDArray(model=strace.model, vars=strace.vars)
sliced.chain = strace.chain
# Happy path where we do not need to load everything from the trace
if ((idx.step is None or idx.step >= 1) and
(idx.stop is None or idx.stop == len(strace))):
start, stop, step = idx.indices(len(strace))
sliced.samples = {v: strace.get_values(v, burn=idx.start, thin=idx.step)
for v in strace.varnames}
sliced.draw_idx = (stop - start) // step
else:
start, stop, step = idx.indices(len(strace))
sliced.samples = {v: strace.get_values(v)[start:stop:step]
for v in strace.varnames}
sliced.draw_idx = (stop - start) // step
return sliced