-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Copy pathsqlite.py
383 lines (327 loc) · 12.9 KB
/
sqlite.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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
# Copyright 2020 The PyMC Developers
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""SQLite trace backend
Store and retrieve sampling values in SQLite database file.
Database format
---------------
For each variable, a table is created with the following format:
recid (INT), draw (INT), chain (INT), v0 (FLOAT), v1 (FLOAT), v2 (FLOAT) ...
The variable column names are extended to reflect additional dimensions.
For example, a variable with the shape (2, 2) would be stored as
key (INT), draw (INT), chain (INT), v0_0 (FLOAT), v0_1 (FLOAT), v1_0 (FLOAT) ...
The key is autoincremented each time a new row is added to the table.
The chain column denotes the chain index and starts at 0.
"""
import numpy as np
import sqlite3
import warnings
from ..backends import base, ndarray
from . import tracetab as ttab
TEMPLATES = {
'table': ('CREATE TABLE IF NOT EXISTS [{table}] '
'(recid INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, '
'draw INTEGER, chain INT(5), '
'{value_cols})'),
'insert': ('INSERT INTO [{table}] '
'(recid, draw, chain, {value_cols}) '
'VALUES (NULL, ?, ?, {values})'),
'max_draw': ('SELECT MAX(draw) FROM [{table}] '
'WHERE chain = ?'),
'draw_count': ('SELECT COUNT(*) FROM [{table}] '
'WHERE chain = ?'),
# Named placeholders are used in the selection templates because
# some values occur more than once in the same template.
'select': ('SELECT * FROM [{table}] '
'WHERE (chain = :chain)'),
'select_burn': ('SELECT * FROM [{table}] '
'WHERE (chain = :chain) AND (draw > :burn)'),
'select_thin': ('SELECT * FROM [{table}] '
'WHERE (chain = :chain) AND '
'(draw - (SELECT draw FROM [{table}] '
'WHERE chain = :chain '
'ORDER BY draw LIMIT 1)) % :thin = 0'),
'select_burn_thin': ('SELECT * FROM [{table}] '
'WHERE (chain = :chain) AND (draw > :burn) '
'AND (draw - (SELECT draw FROM [{table}] '
'WHERE (chain = :chain) AND (draw > :burn) '
'ORDER BY draw LIMIT 1)) % :thin = 0'),
'select_point': ('SELECT * FROM [{table}] '
'WHERE (chain = :chain) AND (draw = :draw)'),
}
sqlite3.register_adapter(np.int32, int)
sqlite3.register_adapter(np.int64, int)
class SQLite(base.BaseTrace):
"""SQLite trace object
Parameters
----------
name: str
Name of database file
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.
test_point: dict
use different test point that might be with changed variables shapes
"""
def __init__(self, name, model=None, vars=None, test_point=None):
warnings.warn(
'The `SQLite` backend will soon be removed. '
'Please switch to a different backend. '
'If you have good reasons for using the SQLite backend, file an issue and tell us about them.',
DeprecationWarning,
)
super().__init__(name, model, vars, test_point)
self._var_cols = {}
self.var_inserts = {} # varname -> insert statement
self.draw_idx = 0
self._is_setup = False
self._len = None
self.db = _SQLiteDB(name)
# Inserting sampling information is queued to avoid locks
# caused by hitting the database with transactions each
# iteration.
self._queue = {varname: [] for varname in self.varnames}
self._queue_limit = 5000
# Sampling methods
def setup(self, draws, chain):
"""Perform chain-specific setup.
Parameters
----------
draws: int
Expected number of draws
chain: int
Chain number
"""
self.db.connect()
self.chain = chain
if self._is_setup:
self.draw_idx = self._get_max_draw(chain) + 1
self._len = None
else: # Table has not been created.
self._var_cols = {varname: ttab.create_flat_names('v', shape)
for varname, shape in self.var_shapes.items()}
self._create_table()
self._is_setup = True
self._create_insert_queries()
self._closed = False
def _create_table(self):
template = TEMPLATES['table']
with self.db.con:
for varname, var_cols in self._var_cols.items():
if np.issubdtype(self.var_dtypes[varname], np.integer):
dtype = 'INT'
else:
dtype = 'FLOAT'
colnames = ', '.join([v + ' ' + dtype for v in var_cols])
statement = template.format(table=varname,
value_cols=colnames)
self.db.cursor.execute(statement)
def _create_insert_queries(self):
template = TEMPLATES['insert']
for varname, var_cols in self._var_cols.items():
# Create insert statement for each variable.
var_str = ', '.join(var_cols)
placeholders = ', '.join(['?'] * len(var_cols))
statement = template.format(table=varname,
value_cols=var_str,
values=placeholders)
self.var_inserts[varname] = statement
def record(self, point):
"""Record results of a sampling iteration.
Parameters
----------
point: dict
Values mapped to variable names
"""
for varname, value in zip(self.varnames, self.fn(point)):
values = (self.draw_idx, self.chain) + tuple(np.ravel(value))
self._queue[varname].append(values)
if len(self._queue[self.varnames[0]]) > self._queue_limit:
self._execute_queue()
self.draw_idx += 1
def _execute_queue(self):
with self.db.con:
for varname in self.varnames:
if not self._queue[varname]:
continue
self.db.cursor.executemany(self.var_inserts[varname],
self._queue[varname])
self._queue[varname] = []
def close(self):
if self._closed:
return
self._execute_queue()
self.db.close()
self._closed = True
# Selection methods
def __len__(self):
if not self._is_setup:
return 0
if self._len is None:
self._len = self._get_number_draws()
return self._len
def _get_number_draws(self):
self.db.connect()
statement = TEMPLATES['draw_count'].format(table=self.varnames[0])
self.db.cursor.execute(statement, (self.chain,))
counts = self.db.cursor.fetchall()[0][0]
if counts is None:
return 0
else:
return counts
def _get_max_draw(self, chain):
self.db.connect()
statement = TEMPLATES['max_draw'].format(table=self.varnames[0])
self.db.cursor.execute(statement, (chain, ))
counts = self.db.cursor.fetchall()[0][0]
if counts is None:
return 0
else:
return counts
def get_values(self, varname, burn=0, thin=1):
"""Get values from trace.
Parameters
----------
varname: str
burn: int
thin: int
Returns
-------
A NumPy array
"""
if burn is None:
burn = 0
if thin is None:
thin = 1
if burn < 0:
burn = max(0, len(self) + burn)
if thin < 1:
raise ValueError('Only positive thin values are supported '
'in SQLite backend.')
varname = str(varname)
statement_args = {'chain': self.chain}
if burn == 0 and thin == 1:
action = 'select'
elif thin == 1:
action = 'select_burn'
statement_args['burn'] = burn - 1
elif burn == 0:
action = 'select_thin'
statement_args['thin'] = thin
else:
action = 'select_burn_thin'
statement_args['burn'] = burn - 1
statement_args['thin'] = thin
self.db.connect()
shape = (-1,) + self.var_shapes[varname]
statement = TEMPLATES[action].format(table=varname)
self.db.cursor.execute(statement, statement_args)
values = _rows_to_ndarray(self.db.cursor)
return values.reshape(shape)
def _slice(self, idx):
if idx.stop is not None:
raise ValueError('Stop value in slice not supported.')
return ndarray._slice_as_ndarray(self, idx)
def point(self, idx):
"""Return dictionary of point values at `idx` for current chain
with variables names as keys.
"""
idx = int(idx)
if idx < 0:
idx = self._get_max_draw(self.chain) + idx + 1
statement = TEMPLATES['select_point']
self.db.connect()
var_values = {}
statement_args = {'chain': self.chain, 'draw': idx}
for varname in self.varnames:
self.db.cursor.execute(statement.format(table=varname),
statement_args)
values = _rows_to_ndarray(self.db.cursor)
var_values[varname] = values.reshape(self.var_shapes[varname])
return var_values
class _SQLiteDB:
def __init__(self, name):
self.name = name
self.con = None
self.cursor = None
self.connected = False
def connect(self):
if self.connected:
return
self.con = sqlite3.connect(self.name)
self.connected = True
self.cursor = self.con.cursor()
def close(self):
if not self.connected:
return
self.con.commit()
self.cursor.close()
self.con.close()
self.connected = False
def load(name, model=None):
"""Load SQLite database.
Parameters
----------
name: str
Path to SQLite database file
model: Model
If None, the model is taken from the `with` context.
Returns
-------
A MultiTrace instance
"""
warnings.warn(
'The `sqlite.load` function will soon be removed. '
'Please use ArviZ to save traces. '
'If you have good reasons for using the `load` function, file an issue and tell us about them. ',
DeprecationWarning,
)
db = _SQLiteDB(name)
db.connect()
varnames = _get_table_list(db.cursor)
if len(varnames) == 0:
raise ValueError(('Can not get variable list for database'
'`{}`'.format(name)))
chains = _get_chain_list(db.cursor, varnames[0])
straces = []
for chain in chains:
strace = SQLite(name, model=model)
strace.chain = chain
strace._var_cols = {varname: ttab.create_flat_names('v', shape)
for varname, shape in strace.var_shapes.items()}
strace._is_setup = True
strace.db = db # Share the db with all traces.
straces.append(strace)
return base.MultiTrace(straces)
def _get_table_list(cursor):
"""Return a list of table names in the current database."""
# Modified from Django. Skips the sqlite_sequence system table used
# for autoincrement key generation.
cursor.execute("SELECT name FROM sqlite_master "
"WHERE type='table' AND NOT name='sqlite_sequence' "
"ORDER BY name")
return [row[0] for row in cursor.fetchall()]
def _get_var_strs(cursor, varname):
cursor.execute('SELECT * FROM [{}]'.format(varname))
col_names = (col_descr[0] for col_descr in cursor.description)
return [name for name in col_names if name.startswith('v')]
def _get_chain_list(cursor, varname):
"""Return a list of sorted chains for `varname`."""
cursor.execute('SELECT DISTINCT chain FROM [{}]'.format(varname))
chains = sorted([chain[0] for chain in cursor.fetchall()])
return chains
def _rows_to_ndarray(cursor):
"""Convert SQL row to NDArray."""
return np.squeeze(np.array([row[3:] for row in cursor.fetchall()]))