Skip to content

Commit 909e69b

Browse files
author
F Saad
committed
Merge branch '20160624-riastradh-cgpm' of github.com:probcomp/bayeslite into 20160628-fsaad-cgpm
2 parents f0f1d77 + 31c2a8c commit 909e69b

File tree

19 files changed

+195
-497
lines changed

19 files changed

+195
-497
lines changed

MANIFEST.in

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,9 @@ include shell/scripts/bayeslite
110110
include shell/tests/test_pretty.py
111111
include shell/tests/test_shell.py
112112
include shell/tests/thooks.py
113-
include src/grammar.sha256
114113
include src/grammar.y
115114
include tests/dha.csv
116115
include tests/dha_codebook.csv
117-
include tests/dha_models.pkl.gz
118116
include tests/test_bql.py
119117
include tests/test_codebook.py
120118
include tests/test_column_dep.py
@@ -123,7 +121,6 @@ include tests/test_correlation.py
123121
include tests/test_csv.py
124122
include tests/test_geweke.py
125123
include tests/test_guess.py
126-
include tests/test_legacy.py
127124
include tests/test_math_util.py
128125
include tests/test_metamodels.py
129126
include tests/test_parse.py

shell/src/core.py

Lines changed: 50 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ def __init__(self, bdb, metamodel, stdin=None, stdout=None, stderr=None):
6969
self._installcmd('guess', self.dot_guess)
7070
self._installcmd('help', self.dot_help)
7171
self._installcmd('hook', self.dot_hook)
72-
self._installcmd('legacymodels', self.dot_legacymodels)
7372
self._installcmd('open', self.dot_open)
7473
self._installcmd('pythexec', self.dot_pythexec)
7574
self._installcmd('python', self.dot_python)
@@ -480,49 +479,22 @@ def dot_codebook(self, line):
480479
self.stdout.write(traceback.format_exc())
481480

482481
def dot_guess(self, line):
483-
'''guess data generator
484-
<generator> <table>
482+
'''guess population schema
483+
<population> <table>
485484
486-
Create a generator named <generator> for the table <table>,
487-
guessing the statistical types of the columns in <table>.
485+
Create a population named <population> with variables
486+
corresponding to columns in table <table>, heuristically
487+
guessing their statistical types.
488488
'''
489489
# XXX Lousy, lousy tokenizer.
490490
tokens = line.split()
491491
if len(tokens) != 2:
492-
self.stdout.write('Usage: .guess <generator> <table>\n')
492+
self.stdout.write('Usage: .guess <population> <table>\n')
493493
return
494-
generator = tokens[0]
494+
population = tokens[0]
495495
table = tokens[1]
496496
try:
497-
guess.bayesdb_guess_generator(self._bdb, generator, table,
498-
self._metamodel)
499-
except Exception:
500-
self.stdout.write(traceback.format_exc())
501-
502-
def dot_legacymodels(self, line):
503-
'''load legacy models
504-
<generator> <table> </path/to/models.pkl.gz>
505-
506-
Create a Crosscat generator named <generator> for the table
507-
<table> from the legacy models stored in
508-
</path/to/models.pkl.gz>.
509-
'''
510-
# XXX Lousy, lousy tokenizer.
511-
tokens = line.split()
512-
if len(tokens) != 3:
513-
self.stdout.write('Usage:'
514-
' .legacymodels <generator> <table>'
515-
' </path/to/models.pkl.gz>\n')
516-
return
517-
generator = tokens[0]
518-
table = tokens[1]
519-
pathname = tokens[2]
520-
try:
521-
bayeslite.bayesdb_load_legacy_models(self._bdb, generator, table,
522-
self._metamodel, pathname,
523-
create=True)
524-
except IOError as e:
525-
self.stdout.write('%s\n' % (e,))
497+
guess.bayesdb_guess_population(self._bdb, population, table)
526498
except Exception:
527499
self.stdout.write(traceback.format_exc())
528500

@@ -537,8 +509,9 @@ def dot_describe(self, line):
537509
tokens = line.split()
538510
if len(tokens) == 0:
539511
self.stdout.write('Usage: .describe table(s) [<table>...]\n')
512+
self.stdout.write(' .describe population(s) [<pop>...]\n')
513+
self.stdout.write(' .describe variables <pop>\n')
540514
self.stdout.write(' .describe generator(s) [<gen>...]\n')
541-
self.stdout.write(' .describe columns <gen>\n')
542515
self.stdout.write(' .describe model(s) <gen> [<model>...]\n')
543516
return
544517
if casefold(tokens[0]) == 'table' or \
@@ -570,6 +543,31 @@ def dot_describe(self, line):
570543
''' % (qualifier,)
571544
with self._bdb.savepoint():
572545
pretty.pp_cursor(self.stdout, self._bdb.execute(sql, params))
546+
elif casefold(tokens[0]) in ('population', 'populations'):
547+
params = None
548+
qualifier = None
549+
if len(tokens) == 1:
550+
params = ()
551+
qualifier = '1'
552+
else:
553+
params = tokens[1:]
554+
names = ','.join('?%d' % (i + 1,) for i in xrange(len(params)))
555+
qualifier = '(name IN (%s))' % (names,)
556+
ok = True
557+
for population in params:
558+
if not core.bayesdb_has_population(self._bdb, population):
559+
self.stdout.write('No such population: %s\n' %
560+
(repr(population),))
561+
ok = False
562+
if not ok:
563+
return
564+
with self._bdb.savepoint():
565+
cursor = self._bdb.sql_execute('''
566+
SELECT id, name, tabname
567+
FROM bayesdb_population
568+
WHERE %s
569+
''' % (qualifier,), params)
570+
pretty.pp_cursor(self.stdout, cursor)
573571
elif casefold(tokens[0]) == 'generator' or \
574572
casefold(tokens[0]) == 'generators':
575573
params = None
@@ -599,29 +597,30 @@ def dot_describe(self, line):
599597
with self._bdb.savepoint():
600598
pretty.pp_cursor(self.stdout,
601599
self._bdb.sql_execute(sql, params))
602-
elif casefold(tokens[0]) == 'columns':
600+
elif casefold(tokens[0]) == 'variables':
603601
if len(tokens) != 2:
604-
self.stdout.write('Describe columns of what generator?\n')
602+
self.stdout.write('Usage: .describe variables <population>\n')
605603
return
606-
generator = tokens[1]
604+
population = tokens[1]
607605
with self._bdb.savepoint():
608-
if not core.bayesdb_has_generator(self._bdb, generator):
609-
self.stdout.write('No such generator: %s\n' %
610-
(repr(generator),))
606+
if not core.bayesdb_has_population(self._bdb, population):
607+
self.stdout.write('No such population: %r\n' %
608+
(population,))
611609
return
612-
generator_id = core.bayesdb_get_generator(self._bdb, generator)
610+
population_id = core.bayesdb_get_population(
611+
self._bdb, population)
613612
sql = '''
614613
SELECT c.colno AS colno, c.name AS name,
615-
gc.stattype AS stattype, c.shortname AS shortname
616-
FROM bayesdb_generator AS g,
614+
v.stattype AS stattype, c.shortname AS shortname
615+
FROM bayesdb_population AS p,
617616
(bayesdb_column AS c LEFT OUTER JOIN
618-
bayesdb_generator_column AS gc
617+
bayesdb_variable AS v
619618
USING (colno))
620-
WHERE g.id = ? AND g.id = gc.generator_id
621-
AND g.tabname = c.tabname
619+
WHERE p.id = ? AND p.id = v.population_id
620+
AND p.tabname = c.tabname
622621
ORDER BY colno ASC;
623622
'''
624-
cursor = self._bdb.sql_execute(sql, (generator_id,))
623+
cursor = self._bdb.sql_execute(sql, (population_id,))
625624
pretty.pp_cursor(self.stdout, cursor)
626625
elif casefold(tokens[0]) == 'model' or \
627626
casefold(tokens[0]) == 'models':
@@ -665,5 +664,5 @@ def dot_describe(self, line):
665664
else:
666665
self.stdout.write('Usage: .describe table(s) [<table>...]\n')
667666
self.stdout.write(' .describe generator(s) [<gen>...]\n')
668-
self.stdout.write(' .describe columns <gen>\n')
667+
self.stdout.write(' .describe variables <pop>\n')
669668
self.stdout.write(' .describe model(s) <gen> [<model>...]\n')

shell/tests/test_shell.py

Lines changed: 65 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -104,17 +104,26 @@ def spawntable():
104104

105105

106106
@pytest.fixture
107-
def spawntablegen(spawntable):
107+
def spawntablepop(spawntable):
108108
table, c = spawntable
109-
c.sendexpectcmd('.guess dha_cc %s' % (table,))
109+
c.sendexpectcmd('.guess hospitals %s' % (table,))
110110
c.expect_prompt()
111-
return table, 'dha_cc', c
111+
return table, 'hospitals', c
112112

113113

114114
@pytest.fixture
115-
def spawngen(spawntablegen):
116-
table, gen, c = spawntablegen
117-
return gen, c
115+
def spawntablepopgen(spawntablepop):
116+
table, pop, c = spawntablepop
117+
c.sendexpectcmd(
118+
'create generator hospitals_cc for %s using crosscat;' % (pop,))
119+
c.expect_prompt()
120+
return table, pop, 'hospitals_cc', c
121+
122+
123+
@pytest.fixture
124+
def spawnpop(spawntablepop):
125+
table, pop, c = spawntablepop
126+
return pop, c
118127

119128

120129
# Tests begin
@@ -153,20 +162,19 @@ def test_help_returns_list_of_commands(spawnbdb):
153162
c = spawnbdb
154163
c.sendexpectcmd('.help')
155164
c.expect_lines([
156-
' .codebook load codebook for table',
157-
' .csv create table from CSV file',
158-
' .describe describe BayesDB entities',
159-
' .guess guess data generator',
160-
' .help show help for commands',
161-
' .hook add custom commands from a python source file',
162-
' .legacymodels load legacy models',
163-
' .open close existing database and open new one',
164-
' .pythexec execute a Python statement',
165-
' .python evaluate a Python expression',
166-
' .read read a file of shell commands',
167-
' .sql execute a SQL query',
168-
' .trace trace queries',
169-
' .untrace untrace queries',
165+
' .codebook load codebook for table',
166+
' .csv create table from CSV file',
167+
' .describe describe BayesDB entities',
168+
' .guess guess population schema',
169+
' .help show help for commands',
170+
' .hook add custom commands from a python source file',
171+
' .open close existing database and open new one',
172+
' .pythexec execute a Python statement',
173+
' .python evaluate a Python expression',
174+
' .read read a file of shell commands',
175+
' .sql execute a SQL query',
176+
' .trace trace queries',
177+
' .untrace untrace queries',
170178
"Type `.help <cmd>' for help on the command <cmd>.",
171179
])
172180
c.expect_prompt()
@@ -204,10 +212,10 @@ def test_dot_csv_dup(spawnbdb):
204212
c.expect_prompt()
205213

206214

207-
def test_describe_columns_without_generator(spawntable):
215+
def test_describe_columns_without_population(spawntable):
208216
table, c = spawntable
209-
c.sendexpectcmd('.describe columns %s' % (table,))
210-
c.expect_lines(['No such generator: %s' % (repr(table),)])
217+
c.sendexpectcmd('.describe variables %s' % (table,))
218+
c.expect_lines(['No such population: %r' % (table,)])
211219
c.expect_prompt()
212220

213221

@@ -229,7 +237,7 @@ def test_bql_select(spawntable):
229237

230238
def test_guess(spawntable):
231239
table, c = spawntable
232-
c.sendexpectcmd('.guess dha_cc %s' % (table,))
240+
c.sendexpectcmd('.guess hospitals %s' % (table,))
233241
c.expect_prompt()
234242

235243

@@ -253,23 +261,23 @@ def test_sql(spawntable):
253261
c.expect_prompt()
254262

255263

256-
def test_describe_generator(spawntablegen):
257-
table, gen, c = spawntablegen
258-
generator_output = [
259-
'id | name | tabname | metamodel',
260-
'---+--------+---------+----------',
261-
' 1 | dha_cc | dha | crosscat',
264+
def test_describe_population(spawntablepop):
265+
table, pop, c = spawntablepop
266+
population_output = [
267+
'id | name | tabname',
268+
'---+-----------+--------',
269+
' 1 | hospitals | dha',
262270
]
263-
c.sendexpectcmd('.describe generator %s' % (table,))
264-
c.expect_lines(['No such generator: %s' % (repr(table),),])
271+
c.sendexpectcmd('.describe population %s' % (table,))
272+
c.expect_lines(['No such population: %r' % (table,),])
265273
c.expect_prompt()
266-
c.sendexpectcmd('.describe generator %s' % (gen,))
267-
c.expect_lines(generator_output)
274+
c.sendexpectcmd('.describe population %s' % (pop,))
275+
c.expect_lines(population_output)
268276
c.expect_prompt()
269277

270278

271-
def test_describe_models(spawntablegen):
272-
table, gen, c = spawntablegen
279+
def test_describe_models(spawntablepopgen):
280+
table, pop, gen, c = spawntablepopgen
273281
# XXX apsw bug: There is no way to discover the description of a
274282
# cursor that yields no rows. The best we can do without failing
275283
# is to assume that such a cursor has no columns either. This is
@@ -291,9 +299,9 @@ def test_describe_models(spawntablegen):
291299
c.expect_prompt()
292300

293301

294-
def test_describe_column_with_generator(spawntablegen):
295-
table, gen, c = spawntablegen
296-
columns_output = [
302+
def test_describe_column_with_generator(spawntablepopgen):
303+
table, pop, gen, c = spawntablepopgen
304+
variables_output = [
297305
'colno | name | stattype | shortname',
298306
'------+---------------------+-----------+----------',
299307
' 1 | N_DEATH_ILL | numerical | None',
@@ -360,11 +368,11 @@ def test_describe_column_with_generator(spawntablegen):
360368
' 62 | CHF_SCORE | numerical | None',
361369
' 63 | PNEUM_SCORE | numerical | None',
362370
]
363-
c.sendexpectcmd('.describe columns %s' % (table,))
364-
c.expect_lines(['No such generator: %s' % (repr(table),),])
371+
c.sendexpectcmd('.describe variables %s' % (table,))
372+
c.expect_lines(['No such population: %s' % (repr(table),),])
365373
c.expect_prompt()
366-
c.sendexpectcmd('.describe columns %s' % (gen,))
367-
c.expect_lines(columns_output)
374+
c.sendexpectcmd('.describe variables %s' % (pop,))
375+
c.expect_lines(variables_output)
368376
c.expect_prompt()
369377

370378

@@ -378,21 +386,20 @@ def test_hook(spawnbdb):
378386
c.expect_prompt()
379387
c.sendexpectcmd('.help')
380388
c.expect_lines([
381-
' .codebook load codebook for table',
382-
' .csv create table from CSV file',
383-
' .describe describe BayesDB entities',
384-
' .guess guess data generator',
385-
' .help show help for commands',
386-
' .hook add custom commands from a python source file',
387-
' .legacymodels load legacy models',
388-
' .myhook myhook help string',
389-
' .open close existing database and open new one',
390-
' .pythexec execute a Python statement',
391-
' .python evaluate a Python expression',
392-
' .read read a file of shell commands',
393-
' .sql execute a SQL query',
394-
' .trace trace queries',
395-
' .untrace untrace queries',
389+
' .codebook load codebook for table',
390+
' .csv create table from CSV file',
391+
' .describe describe BayesDB entities',
392+
' .guess guess population schema',
393+
' .help show help for commands',
394+
' .hook add custom commands from a python source file',
395+
' .myhook myhook help string',
396+
' .open close existing database and open new one',
397+
' .pythexec execute a Python statement',
398+
' .python evaluate a Python expression',
399+
' .read read a file of shell commands',
400+
' .sql execute a SQL query',
401+
' .trace trace queries',
402+
' .untrace untrace queries',
396403
"Type `.help <cmd>' for help on the command <cmd>."
397404
])
398405
c.expect_prompt()

src/__init__.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@
7272
from bayeslite.codebook import bayesdb_load_codebook_csv_file
7373
from bayeslite.exception import BayesDBException
7474
from bayeslite.exception import BQLError
75-
from bayeslite.legacy_models import bayesdb_load_legacy_models
7675
from bayeslite.metamodel import IBayesDBMetamodel
7776
from bayeslite.metamodel import bayesdb_builtin_metamodel
7877
from bayeslite.metamodel import bayesdb_deregister_metamodel
@@ -102,7 +101,6 @@ def bql_quote_name(name):
102101
'BayesDBTxnError',
103102
'bayesdb_deregister_metamodel',
104103
'bayesdb_load_codebook_csv_file',
105-
'bayesdb_load_legacy_models',
106104
'bayesdb_open',
107105
'bayesdb_read_csv',
108106
'bayesdb_read_csv_file',

src/ast.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@
117117
'ckpt_iterations',
118118
'ckpt_seconds',
119119
'wait',
120+
'program',
120121
])
121122
DropModels = namedtuple('DropModels', [
122123
'generator',

0 commit comments

Comments
 (0)