files: # -*- coding: utf-8 -*-
  
  #   Copyright (c) 2010-2016, MIT Probabilistic Computing Project
  #
  #   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.
  
  """Backend interface.
  
  To be used as a modeling and inference backend in a :class:`bayeslite.BayesDB`
  handle, a backends must first be registered with
  :func:`bayesdb_register_backend`.
  
  The CGPM backend is registered by default, but we can suppress
  that for illustration::
  
     import bayeslite
     from bayeslite.backends.cgpm_backend import CGPM_Backend
  
     bdb = bayeslite.bayesdb_open(pathname='foo.bdb', builtin_backends=False)
     backend = CGPM_Backend(cgpm_registry=dict(), multiprocess=False)
     bayeslite.bayesdb_register_backend(bdb, backend)
  
  Then you can model a table and query the probable implications of the data in
  the table::
  
     bdb.execute('create population p for t with schema(guess stattypes of (*))')
     bdb.execute('create generator p_cc for t using cgpm;')
     bdb.execute('initialize 10 models for t_cc')
     bdb.execute('analyze t_cc for 10 iterations')
     for x in bdb.execute('estimate pairwise dependence probability from t_cc'):
         print x
  """
  
  from bayeslite.util import cursor_value
  
  builtin_backends = []
  builtin_backend_names = set()
  
  def bayesdb_builtin_backend(backend):
      name = backend.name()
      assert name not in builtin_backend_names
      builtin_backends.append(backend)
      builtin_backend_names.add(name)
  
  def bayesdb_register_builtin_backends(bdb):
      """Register all builtin backends in `bdb`."""
      for backend in builtin_backends:
          bayes
  
  [... the rest is omitted ...]
files: src/backend.py
files: # -*- coding: utf-8 -*-
  
  #   Copyright (c) 2010-2016, MIT Probabilistic Computing Project
  #
  #   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.
  
  """Backend interface.
  
  To be used as a modeling and inference backend in a :class:`bayeslite.BayesDB`
  handle, a backends must first be registered with
  :func:`bayesdb_register_backend`.
  
  The CGPM backend is registered by default, but we can suppress
  that for illustration::
  
     import bayeslite
     from bayeslite.backends.cgpm_backend import CGPM_Backend
  
     bdb = bayeslite.bayesdb_open(pathname='foo.bdb', builtin_backends=False)
     backend = CGPM_Backend(cgpm_registry=dict(), multiprocess=False)
     bayeslite.bayesdb_register_backend(bdb, backend)
  
  Then you can model a table and query the probable implications of the data in
  the table::
  
     bdb.execute('create population p for t with schema(guess stattypes for (*))')
     bdb.execute('create generator p_cc for t using cgpm;')
     bdb.execute('initialize 10 models for t_cc')
     bdb.execute('analyze t_cc for 10 iterations')
     for x in bdb.execute('estimate pairwise dependence probability from t_cc'):
         print x
  """
  
  from bayeslite.util import cursor_value
  
  builtin_backends = []
  builtin_backend_names = set()
  
  def bayesdb_builtin_backend(backend):
      name = backend.name()
      assert name not in builtin_backend_names
      builtin_backends.append(backend)
      builtin_backend_names.add(name)
  
  def bayesdb_register_builtin_backends(bdb):
      """Register all builtin backends in `bdb`."""
      for backend in builtin_backends:
          bayes
  
  [... the rest is omitted ...]
files: src/backend.py
files: /*
   *  Copyright (c) 2010-2016, MIT Probabilistic Computing Project
   *
   *  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.
   */
  
  bql(start)              ::= phrases(phrases).
  
  phrases(none)           ::= .
  phrases(some)           ::= phrases(phrases) phrase_opt(phrase) T_SEMI.
  phrase_opt(none)        ::= .
  phrase_opt(some)        ::= phrase(phrase).
  phrase(command)         ::= command(c).
  phrase(query)           ::= query(q).
  
  /*
   * Transactions
   */
  command(begin)          ::= K_BEGIN.
  command(rollback)       ::= K_ROLLBACK.
  command(commit)         ::= K_COMMIT.
  
  /* XXX Need database names.      */
  
  /*
   * SQL Data Definition Language subset
   */
  command(createtab_as)   ::= K_CREATE temp_opt(temp) K_TABLE
                                  ifnotexists(ifnotexists)
                                  table_name(name) K_AS query(query).
  command(createtab_csv)   ::= K_CREATE temp_opt(temp) K_TABLE
                                  ifnotexists(ifnotexists)
                                  table_name(name) K_FROM pathname(csv).
  command(droptab)        ::= K_DROP K_TABLE ifexists(ifexists) table_name(name).
  command(altertab)       ::= K_ALTER K_TABLE table_name(table)
                                  altertab_cmds(cmds).
  
  altertab_cmds(one)      ::= altertab_cmd(cmd).
  altertab_cmds(many)     ::= altertab_cmds(cmds) T_COMMA altertab_cmd(cmd).
  
  altertab_cmd(renametab) ::= K_RENAME K_TO table_name(name).
  altertab_cmd(renamecol) ::= K_RENAME k_column_opt column_name(old)
                                  K_TO column_name(new).
  
  k
  
  [... the rest is omitted ...]
files: src/grammar.y
files: /*
   *  Copyright (c) 2010-2016, MIT Probabilistic Computing Project
   *
   *  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.
   */
  
  bql(start)              ::= phrases(phrases).
  
  phrases(none)           ::= .
  phrases(some)           ::= phrases(phrases) phrase_opt(phrase) T_SEMI.
  phrase_opt(none)        ::= .
  phrase_opt(some)        ::= phrase(phrase).
  phrase(command)         ::= command(c).
  phrase(query)           ::= query(q).
  
  /*
   * Transactions
   */
  command(begin)          ::= K_BEGIN.
  command(rollback)       ::= K_ROLLBACK.
  command(commit)         ::= K_COMMIT.
  
  /* XXX Need database names.      */
  
  /*
   * SQL Data Definition Language subset
   */
  command(createtab_as)   ::= K_CREATE temp_opt(temp) K_TABLE
                                  ifnotexists(ifnotexists)
                                  table_name(name) K_AS query(query).
  command(createtab_csv)   ::= K_CREATE temp_opt(temp) K_TABLE
                                  ifnotexists(ifnotexists)
                                  table_name(name) K_FROM pathname(csv).
  command(droptab)        ::= K_DROP K_TABLE ifexists(ifexists) table_name(name).
  command(altertab)       ::= K_ALTER K_TABLE table_name(table)
                                  altertab_cmds(cmds).
  
  altertab_cmds(one)      ::= altertab_cmd(cmd).
  altertab_cmds(many)     ::= altertab_cmds(cmds) T_COMMA altertab_cmd(cmd).
  
  altertab_cmd(renametab) ::= K_RENAME K_TO table_name(name).
  altertab_cmd(renamecol) ::= K_RENAME k_column_opt column_name(old)
                                  K_TO column_name(new).
  
  k
  
  [... the rest is omitted ...]
files: src/grammar.y
files: # -*- coding: utf-8 -*-
  
  #   Copyright (c) 2010-2016, MIT Probabilistic Computing Project
  #
  #   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.
  
  """BQL parser front end."""
  
  import StringIO
  
  import bayeslite.ast as ast
  import bayeslite.grammar as grammar
  import bayeslite.scan as scan
  from bayeslite.exception import BQLParseError
  
  def parse_bql_phrases(scanner):
      semantics = BQLSemantics()
      parser = grammar.Parser(semantics)
      while not semantics.failed:
          token = scanner.read()
          semantics.context.append(token)
          if token[0] == -1:      # error
              semantics.syntax_error(token)
          else:
              if token[0] == 0:   # EOF
                  # Implicit ; at EOF.
                  parser.feed((grammar.T_SEMI, ''))
              parser.feed(token)
          if semantics.phrase is not None:
              phrase = semantics.phrase
              semantics.phrase = None
              if 0 < len(semantics.errors):
                  # Keep parsing in order to detect more errors, but
                  # don't yield any broken phrases in case the caller
                  # will try to process them before we finish parsing
                  # the whole thing.
                  continue
              if 0 < scanner.n_numpar:
                  n_numpar = scanner.n_numpar
                  nampar_map = scanner.nampar_map
                  yield ast.Parametrized(phrase, n_numpar, nampar_map)
              else:
                  yield phrase
          if token[0] == 0:       # EOF
              break
      if 0 < len(semantics.errors):
          raise BQLParseError(semantics.errors)
      if semantics.failed:
          raise BQLParseError(['parse failed mysteriously!'])
  
  def parse_bql_string_pos(string):
      """Yield ``(phrase
  
  [... the rest is omitted ...]
files: src/parse.py
files: # -*- coding: utf-8 -*-
  
  #   Copyright (c) 2010-2016, MIT Probabilistic Computing Project
  #
  #   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.
  
  """BQL parser front end."""
  
  import StringIO
  
  import bayeslite.ast as ast
  import bayeslite.grammar as grammar
  import bayeslite.scan as scan
  from bayeslite.exception import BQLParseError
  
  def parse_bql_phrases(scanner):
      semantics = BQLSemantics()
      parser = grammar.Parser(semantics)
      while not semantics.failed:
          token = scanner.read()
          semantics.context.append(token)
          if token[0] == -1:      # error
              semantics.syntax_error(token)
          else:
              if token[0] == 0:   # EOF
                  # Implicit ; at EOF.
                  parser.feed((grammar.T_SEMI, ''))
              parser.feed(token)
          if semantics.phrase is not None:
              phrase = semantics.phrase
              semantics.phrase = None
              if 0 < len(semantics.errors):
                  # Keep parsing in order to detect more errors, but
                  # don't yield any broken phrases in case the caller
                  # will try to process them before we finish parsing
                  # the whole thing.
                  continue
              if 0 < scanner.n_numpar:
                  n_numpar = scanner.n_numpar
                  nampar_map = scanner.nampar_map
                  yield ast.Parametrized(phrase, n_numpar, nampar_map)
              else:
                  yield phrase
          if token[0] == 0:       # EOF
              break
      if 0 < len(semantics.errors):
          raise BQLParseError(semantics.errors)
      if semantics.failed:
          raise BQLParseError(['parse failed mysteriously!'])
  
  def parse_bql_string_pos(string):
      """Yield ``(phrase
  
  [... the rest is omitted ...]
files: src/parse.py
files: # -*- coding: utf-8 -*-
  
  #   Copyright (c) 2010-2016, MIT Probabilistic Computing Project
  #
  #   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.
  
  import StringIO
  import apsw
  import pytest
  import struct
  
  import bayeslite
  import bayeslite.ast as ast
  import bayeslite.compiler as compiler
  import bayeslite.core as core
  import bayeslite.guess as guess
  import bayeslite.backends.troll_rng as troll
  import bayeslite.parse as parse
  
  from bayeslite.exception import BQLError
  from bayeslite.math_util import relerr
  from bayeslite.backends.cgpm_backend import CGPM_Backend
  from bayeslite.util import cursor_value
  
  import test_core
  import test_csv
  
  from stochastic import stochastic
  
  def bql2sql(string, setup=None):
      with bayeslite.bayesdb_open(':memory:') as bdb:
          test_core.t1_schema(bdb)
          test_core.t1_data(bdb)
          bdb.execute('''
              create population p1 for t1 (
                  id ignore;
                  label categorical;
                  age numerical;
                  weight numerical
              )
          ''')
          if setup is not None:
              setup(bdb)
          phrases = parse.parse_bql_string(string)
          out = compiler.Output(0, {}, ())
          for phrase in phrases:
              assert ast.is_query(phrase)
              compiler.compile_query(bdb, phrase, out)
              out.write(';')
          return out.getvalue()
  
  # XXX Kludgey mess.  Please reorganize.
  def bql2sqlparam(string):
      with bayeslite.bayesdb_open(':memory:') as bdb:
          test_core.t1_schema(bdb)
          test_core.t1_data(bdb)
          bdb.execute('''
              create population p1 for t1 (
                  id ignore
  
  [... the rest is omitted ...]
files: tests/test_bql.py
files: # -*- coding: utf-8 -*-
  
  #   Copyright (c) 2010-2016, MIT Probabilistic Computing Project
  #
  #   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.
  
  import StringIO
  import apsw
  import pytest
  import struct
  
  import bayeslite
  import bayeslite.ast as ast
  import bayeslite.compiler as compiler
  import bayeslite.core as core
  import bayeslite.guess as guess
  import bayeslite.backends.troll_rng as troll
  import bayeslite.parse as parse
  
  from bayeslite.exception import BQLError
  from bayeslite.math_util import relerr
  from bayeslite.backends.cgpm_backend import CGPM_Backend
  from bayeslite.util import cursor_value
  
  import test_core
  import test_csv
  
  from stochastic import stochastic
  
  def bql2sql(string, setup=None):
      with bayeslite.bayesdb_open(':memory:') as bdb:
          test_core.t1_schema(bdb)
          test_core.t1_data(bdb)
          bdb.execute('''
              create population p1 for t1 (
                  id ignore;
                  label categorical;
                  age numerical;
                  weight numerical
              )
          ''')
          if setup is not None:
              setup(bdb)
          phrases = parse.parse_bql_string(string)
          out = compiler.Output(0, {}, ())
          for phrase in phrases:
              assert ast.is_query(phrase)
              compiler.compile_query(bdb, phrase, out)
              out.write(';')
          return out.getvalue()
  
  # XXX Kludgey mess.  Please reorganize.
  def bql2sqlparam(string):
      with bayeslite.bayesdb_open(':memory:') as bdb:
          test_core.t1_schema(bdb)
          test_core.t1_data(bdb)
          bdb.execute('''
              create population p1 for t1 (
                  id ignore
  
  [... the rest is omitted ...]
files: tests/test_bql.py
files: # -*- coding: utf-8 -*-
  
  #   Copyright (c) 2010-2016, MIT Probabilistic Computing Project
  #
  #   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.
  
  from __future__ import division
  
  import StringIO
  import contextlib
  import math
  import time
  
  import numpy as np
  import pytest
  
  from cgpm.cgpm import CGpm
  from cgpm.dummy.barebones import BareBonesCGpm
  from cgpm.dummy.piecewise import PieceWise
  from cgpm.dummy.trollnormal import TrollNormal
  from cgpm.factor.factor import FactorAnalysis
  from cgpm.regressions.linreg import LinearRegression
  from cgpm.utils import general as gu
  
  from bayeslite import bayesdb_nullify
  from bayeslite import bayesdb_open
  from bayeslite import bayesdb_read_csv
  from bayeslite import bayesdb_register_backend
  from bayeslite.core import bayesdb_get_generator
  from bayeslite.core import bayesdb_get_population
  from bayeslite.exception import BQLError
  from bayeslite.backends.cgpm_backend import CGPM_Backend
  from bayeslite.util import cursor_value
  
  import test_csv
  
  @contextlib.contextmanager
  def cgpm_smoke_bdb():
      with bayesdb_open(':memory:', builtin_backends=False) as bdb:
          registry = {
              'piecewise': PieceWise,
          }
          bayesdb_register_backend(
              bdb, CGPM_Backend(registry, multiprocess=0))
  
          bdb.sql_execute('CREATE TABLE t (Output, cat, Input)')
          for i in xrange(3):
              for j in xrange(3):
                  for k in xrange(3):
                      output = i + j/(k + 1)
                      cat = -1 if (i +
  
  [... the rest is omitted ...]
files: tests/test_cgpm.py
files: # -*- coding: utf-8 -*-
  
  #   Copyright (c) 2010-2016, MIT Probabilistic Computing Project
  #
  #   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.
  
  from __future__ import division
  
  import StringIO
  import contextlib
  import math
  import time
  
  import numpy as np
  import pytest
  
  from cgpm.cgpm import CGpm
  from cgpm.dummy.barebones import BareBonesCGpm
  from cgpm.dummy.piecewise import PieceWise
  from cgpm.dummy.trollnormal import TrollNormal
  from cgpm.factor.factor import FactorAnalysis
  from cgpm.regressions.linreg import LinearRegression
  from cgpm.utils import general as gu
  
  from bayeslite import bayesdb_nullify
  from bayeslite import bayesdb_open
  from bayeslite import bayesdb_read_csv
  from bayeslite import bayesdb_register_backend
  from bayeslite.core import bayesdb_get_generator
  from bayeslite.core import bayesdb_get_population
  from bayeslite.exception import BQLError
  from bayeslite.backends.cgpm_backend import CGPM_Backend
  from bayeslite.util import cursor_value
  
  import test_csv
  
  @contextlib.contextmanager
  def cgpm_smoke_bdb():
      with bayesdb_open(':memory:', builtin_backends=False) as bdb:
          registry = {
              'piecewise': PieceWise,
          }
          bayesdb_register_backend(
              bdb, CGPM_Backend(registry, multiprocess=0))
  
          bdb.sql_execute('CREATE TABLE t (Output, cat, Input)')
          for i in xrange(3):
              for j in xrange(3):
                  for k in xrange(3):
                      output = i + j/(k + 1)
                      cat = -1 if (i +
  
  [... the rest is omitted ...]
files: tests/test_cgpm.py
files: # -*- coding: utf-8 -*-
  
  #   Copyright (c) 2010-2016, MIT Probabilistic Computing Project
  #
  #   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.
  
  import contextlib
  import pytest
  
  import bayeslite.core
  
  from bayeslite import bayesdb_register_backend
  from bayeslite.exception import BQLError
  from bayeslite.math_util import abserr
  from bayeslite.backends.cgpm_backend import CGPM_Backend
  
  from test_cgpm import cgpm_dummy_satellites_bdb
  
  
  @contextlib.contextmanager
  def cgpm_dummy_satellites_pop_bdb():
      with cgpm_dummy_satellites_bdb() as bdb:
          bdb.execute('''
              create population satellites for satellites_ucs with schema(
                  apogee numerical;
                  class_of_orbit categorical;
                  country_of_operator categorical;
                  launch_mass numerical;
                  perigee numerical;
                  period numerical
              )
          ''')
          backend = CGPM_Backend(dict(), multiprocess=0)
          bayesdb_register_backend(bdb, backend)
          yield bdb
  
  
  def test_cgpm_alter_basic():
      with cgpm_dummy_satellites_pop_bdb() as bdb:
          bdb.execute('''
              create generator g0 for satellites using cgpm(
                  subsample 10
              );
          ''')
          bdb.execute('initialize 4 models for g0')
  
          population_id = bayeslite.core.bayesdb_get_population(
              bdb, 'satellites')
          generator_id = bayeslite.core.bayesdb_get_generator(
              bdb, population_id, 'g0')
  
          # Make all variables independent in models 0-1.
          bdb.execute('''
              alter generator g0 models (0,1)
                  ensure
  
  [... the rest is omitted ...]
files: tests/test_cgpm_alter.py
files: # -*- coding: utf-8 -*-
  
  #   Copyright (c) 2010-2016, MIT Probabilistic Computing Project
  #
  #   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.
  
  import contextlib
  import pytest
  
  import bayeslite.core
  
  from bayeslite import bayesdb_register_backend
  from bayeslite.exception import BQLError
  from bayeslite.math_util import abserr
  from bayeslite.backends.cgpm_backend import CGPM_Backend
  
  from test_cgpm import cgpm_dummy_satellites_bdb
  
  
  @contextlib.contextmanager
  def cgpm_dummy_satellites_pop_bdb():
      with cgpm_dummy_satellites_bdb() as bdb:
          bdb.execute('''
              create population satellites for satellites_ucs with schema(
                  model apogee as numerical;
                  model class_of_orbit as categorical;
                  model country_of_operator as categorical;
                  model launch_mass as numerical;
                  model perigee as numerical;
                  model period as numerical
              )
          ''')
          backend = CGPM_Backend(dict(), multiprocess=0)
          bayesdb_register_backend(bdb, backend)
          yield bdb
  
  
  def test_cgpm_alter_basic():
      with cgpm_dummy_satellites_pop_bdb() as bdb:
          bdb.execute('''
              create generator g0 for satellites using cgpm(
                  subsample 10
              );
          ''')
          bdb.execute('initialize 4 models for g0')
  
          population_id = bayeslite.core.bayesdb_get_population(
              bdb, 'satellites')
          generator_id = bayeslite.core.bayesdb_get_generator(
              bdb, population_id, 'g0')
  
          # Make all variables independent in models 0-1.
          bdb.execute('''
             
  
  [... the rest is omitted ...]
files: tests/test_cgpm_alter.py
files: # -*- coding: utf-8 -*-
  
  #   Copyright (c) 2010-2016, MIT Probabilistic Computing Project
  #
  #   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.
  
  import pytest
  
  import bayeslite.core
  
  from bayeslite import bayesdb_register_backend
  from bayeslite.exception import BQLError
  from bayeslite.backends.cgpm_backend import CGPM_Backend
  
  from test_cgpm import cgpm_dummy_satellites_bdb
  
  # This test suite needs expanding to confirm the subproblems are compiled to the
  # right inference kernels in cgpm_backend, but the information is quite nested
  # so it is not trivial to retrieve this information. Considering refactoring the
  # interpreter to make unit testing easier.
  
  
  def test_analysis_subproblems_basic():
      with cgpm_dummy_satellites_bdb() as bdb:
          bdb.execute('''
              CREATE POPULATION satellites FOR satellites_ucs WITH SCHEMA(
                  SET STATTYPE OF class_of_orbit TO CATEGORICAL;
                  SET STATTYPE OF country_of_operator TO CATEGORICAL;
                  SET STATTYPE OF launch_mass TO NUMERICAL;
                  SET STATTYPE OF perigee TO NUMERICAL;
                  SET STATTYPE OF period TO NUMERICAL
              )
          ''')
          bayesdb_register_backend(bdb, CGPM_Backend(dict(), multiprocess=0))
          bdb.execute('''
              CREATE GENERATOR g0 FOR satellites USING cgpm(
                  SUBSAMPLE 10
              );
          ''')
          bdb.execute('INITIALIZE 4 MODELS FOR g0')
  
          # Test each subproblem individually except for variable hyperparameters.
          for optimized in ['', 'OPTIMIZED;',]:
              for subproblem in [
                  'variable clustering',
                  'variable clustering concentration',
                  'row clustering',
                  'row clustering
  
  [... the rest is omitted ...]
files: tests/test_cgpm_analysis.py
files: # -*- coding: utf-8 -*-
  
  #   Copyright (c) 2010-2016, MIT Probabilistic Computing Project
  #
  #   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.
  
  import pytest
  
  import bayeslite.core
  
  from bayeslite import bayesdb_register_backend
  from bayeslite.exception import BQLError
  from bayeslite.backends.cgpm_backend import CGPM_Backend
  
  from test_cgpm import cgpm_dummy_satellites_bdb
  
  # This test suite needs expanding to confirm the subproblems are compiled to the
  # right inference kernels in cgpm_backend, but the information is quite nested
  # so it is not trivial to retrieve this information. Considering refactoring the
  # interpreter to make unit testing easier.
  
  
  def test_analysis_subproblems_basic():
      with cgpm_dummy_satellites_bdb() as bdb:
          bdb.execute('''
              CREATE POPULATION satellites FOR satellites_ucs WITH SCHEMA(
                  MODEL apogee AS NUMERICAL;
                  MODEL class_of_orbit AS CATEGORICAL;
                  MODEL country_of_operator AS CATEGORICAL;
                  MODEL launch_mass AS NUMERICAL;
                  MODEL perigee AS NUMERICAL;
                  MODEL period AS NUMERICAL
              )
          ''')
          bayesdb_register_backend(bdb, CGPM_Backend(dict(), multiprocess=0))
          bdb.execute('''
              CREATE GENERATOR g0 FOR satellites USING cgpm(
                  SUBSAMPLE 10
              );
          ''')
          bdb.execute('INITIALIZE 4 MODELS FOR g0')
  
          # Test each subproblem individually except for variable hyperparameters.
          for optimized in ['', 'OPTIMIZED;',]:
              for subproblem in [
                  'variable clustering',
                  'variable clustering concentration',
                  'row clustering',
                  'row clustering concentration',
              ]:
  
  
  [... the rest is omitted ...]
files: tests/test_cgpm_analysis.py
files: # -*- coding: utf-8 -*-
  
  #   Copyright (c) 2010-2016, MIT Probabilistic Computing Project
  #
  #   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.
  
  import contextlib
  import itertools
  import pytest
  
  from bayeslite import bayesdb_open
  from bayeslite.exception import BQLError
  from bayeslite.exception import BQLParseError
  
  
  '''This test provides coverage for ESTIMATE and SIMULATE of conditional mutual
  information queries for univariate and multivariate targets and conditioning
  statements.'''
  
  
  @contextlib.contextmanager
  def smoke_bdb():
      with bayesdb_open(':memory:') as bdb:
          bdb.sql_execute('CREATE TABLE t (a, b, c, d, e)')
  
          for a, b, c, d, e in itertools.product(*([range(2)]*4+[['x','y']])):
              # XXX Insert synthetic data generator here.
              bdb.sql_execute('''
                  INSERT INTO t (a, b, c, d, e) VALUES (?, ?, ?, ?, ?)
              ''', (a, b, c, d, e))
  
          bdb.execute('''
              CREATE POPULATION p FOR t WITH SCHEMA (
                  SET STATTYPES OF a, b, c, d TO NUMERICAL;
                  SET STATTYPES OF e TO NOMINAL
              )
          ''')
  
          bdb.execute('CREATE GENERATOR m1 FOR p;')
          bdb.execute('INITIALIZE 10 MODELS FOR m1;')
  
          bdb.execute('CREATE GENERATOR m2 FOR p;')
          bdb.execute('INITIALIZE 10 MODELS FOR m2;')
          yield bdb
  
  
  def test_estimate_cmi_basic__ci_slow():
      with smoke_bdb() as bdb:
          bql = '''
              ESTIMATE MUTUAL INFORMATION OF a WITH b
              BY p
  
  [... the rest is omitted ...]
files: tests/test_cmi.py
files: # -*- coding: utf-8 -*-
  
  #   Copyright (c) 2010-2016, MIT Probabilistic Computing Project
  #
  #   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.
  
  import contextlib
  import itertools
  import pytest
  
  from bayeslite import bayesdb_open
  from bayeslite.exception import BQLError
  from bayeslite.exception import BQLParseError
  
  
  '''This test provides coverage for ESTIMATE and SIMULATE of conditional mutual
  information queries for univariate and multivariate targets and conditioning
  statements.'''
  
  
  @contextlib.contextmanager
  def smoke_bdb():
      with bayesdb_open(':memory:') as bdb:
          bdb.sql_execute('CREATE TABLE t (a, b, c, d, e)')
  
          for a, b, c, d, e in itertools.product(*([range(2)]*4+[['x','y']])):
              # XXX Insert synthetic data generator here.
              bdb.sql_execute('''
                  INSERT INTO t (a, b, c, d, e) VALUES (?, ?, ?, ?, ?)
              ''', (a, b, c, d, e))
  
          bdb.execute('''
              CREATE POPULATION p FOR t WITH SCHEMA (
                  MODEL a, b, c, d AS NUMERICAL;
                  MODEL e AS NOMINAL
              )
          ''')
  
          bdb.execute('CREATE GENERATOR m1 FOR p;')
          bdb.execute('INITIALIZE 10 MODELS FOR m1;')
  
          bdb.execute('CREATE GENERATOR m2 FOR p;')
          bdb.execute('INITIALIZE 10 MODELS FOR m2;')
          yield bdb
  
  
  def test_estimate_cmi_basic__ci_slow():
      with smoke_bdb() as bdb:
          bql = '''
              ESTIMATE MUTUAL INFORMATION OF a WITH b
              BY p
              MODELED BY m1
  
  
  [... the rest is omitted ...]
files: tests/test_cmi.py
files: # -*- coding: utf-8 -*-
  
  #   Copyright (c) 2010-2016, MIT Probabilistic Computing Project
  #
  #   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.
  
  import bayeslite
  
  def test_conditional_probability_simple_inferences():
      data = [
          ['x', 'a'], ['x', 'a'], ['x', 'a'],
          ['y', 'b'], ['y', 'b'], ['y', 'b'],
      ]
      with bayeslite.bayesdb_open() as bdb:
          bdb.sql_execute('create table t(foo, bar)')
          for row in data:
              bdb.sql_execute('insert into t values (?, ?)', row)
          bdb.execute('''
              create population p for t (
                  foo categorical;
                  bar categorical;
              )
          ''')
          bdb.execute('create generator p_cc for p using cgpm;')
          bdb.execute('initialize 10 models for p_cc')
          bdb.execute('analyze p_cc for 100 iterations')
          cursor = bdb.execute('''
              estimate
                  probability density of foo = 'x',
                  probability density of foo = 'x' given (bar = 'a'),
                  probability density of foo = 'x' given (bar = 'b'),
                  probability density of foo = 'y',
                  probability density of foo = 'y' given (bar = 'a'),
                  probability density of foo = 'y' given (bar = 'b')
  
              by p
          ''').fetchall()
          px, pxa, pxb, py, pya, pyb = cursor[0]
          # Inferences on x.
          assert px < pxa
          assert pxb < px
          # Inferences on y.
          assert py < pyb
          assert pya < py
  
  
  [... the rest is omitted ...]
files: tests/test_condprob.py
files: # -*- coding: utf-8 -*-
  
  #   Copyright (c) 2010-2016, MIT Probabilistic Computing Project
  #
  #   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.
  
  import bayeslite
  
  def test_conditional_probability_simple_inferences():
      data = [
          ['x', 'a'], ['x', 'a'], ['x', 'a'],
          ['y', 'b'], ['y', 'b'], ['y', 'b'],
      ]
      with bayeslite.bayesdb_open() as bdb:
          bdb.sql_execute('create table t(foo, bar)')
          for row in data:
              bdb.sql_execute('insert into t values (?, ?)', row)
          bdb.execute('''
              create population p for t (
                  model foo, bar as categorical
              )
          ''')
          bdb.execute('create generator p_cc for p using cgpm;')
          bdb.execute('initialize 10 models for p_cc')
          bdb.execute('analyze p_cc for 100 iterations')
          cursor = bdb.execute('''
              estimate
                  probability density of foo = 'x',
                  probability density of foo = 'x' given (bar = 'a'),
                  probability density of foo = 'x' given (bar = 'b'),
                  probability density of foo = 'y',
                  probability density of foo = 'y' given (bar = 'a'),
                  probability density of foo = 'y' given (bar = 'b')
  
              by p
          ''').fetchall()
          px, pxa, pxb, py, pya, pyb = cursor[0]
          # Inferences on x.
          assert px < pxa
          assert pxb < px
          # Inferences on y.
          assert py < pyb
          assert pya < py
  
  
  [... the rest is omitted ...]
files: tests/test_condprob.py
files: # -*- coding: utf-8 -*-
  
  #   Copyright (c) 2010-2016, MIT Probabilistic Computing Project
  #
  #   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.
  
  import apsw
  import contextlib
  import itertools
  import json
  import pytest
  import tempfile
  
  import bayeslite
  import bayeslite.backend
  import bayeslite.bqlfn as bqlfn
  import bayeslite.core as core
  
  from bayeslite.backends.cgpm_backend import CGPM_Backend
  
  from bayeslite import bql_quote_name
  from bayeslite.sqlite3_util import sqlite3_connection
  from bayeslite.util import cursor_value
  
  def powerset(s):
      s = list(s)
      combinations = (itertools.combinations(s, r) for r in range(len(s) + 1))
      return itertools.chain.from_iterable(combinations)
  
  @contextlib.contextmanager
  def bayesdb(backend=None, **kwargs):
      if backend is None:
          backend = CGPM_Backend(cgpm_registry={}, multiprocess=False)
      bdb = bayeslite.bayesdb_open(builtin_backends=False, **kwargs)
      bayeslite.bayesdb_register_backend(bdb, backend)
      try:
          yield bdb
      finally:
          bdb.close()
  
  def test_bayesdb_instantiation():
      # Use bayesdb_open -- don't instantiate directly.
      with pytest.raises(TypeError):
          bayeslite.BayesDB()  # pylint: disable=no-value-for-parameter
      with pytest.raises(ValueError):
          bayeslite.BayesDB(':memory:', 0xdeadbeef)
  
  def test_prng_determinism():
      bdb = bayeslite.bayesdb_open(builtin_backends=False)
      assert bdb._prng.weakrandom_un
  
  [... the rest is omitted ...]
files: tests/test_core.py
files: # -*- coding: utf-8 -*-
  
  #   Copyright (c) 2010-2016, MIT Probabilistic Computing Project
  #
  #   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.
  
  import apsw
  import contextlib
  import itertools
  import json
  import pytest
  import tempfile
  
  import bayeslite
  import bayeslite.backend
  import bayeslite.bqlfn as bqlfn
  import bayeslite.core as core
  
  from bayeslite.backends.cgpm_backend import CGPM_Backend
  
  from bayeslite import bql_quote_name
  from bayeslite.sqlite3_util import sqlite3_connection
  from bayeslite.util import cursor_value
  
  def powerset(s):
      s = list(s)
      combinations = (itertools.combinations(s, r) for r in range(len(s) + 1))
      return itertools.chain.from_iterable(combinations)
  
  @contextlib.contextmanager
  def bayesdb(backend=None, **kwargs):
      if backend is None:
          backend = CGPM_Backend(cgpm_registry={}, multiprocess=False)
      bdb = bayeslite.bayesdb_open(builtin_backends=False, **kwargs)
      bayeslite.bayesdb_register_backend(bdb, backend)
      try:
          yield bdb
      finally:
          bdb.close()
  
  def test_bayesdb_instantiation():
      # Use bayesdb_open -- don't instantiate directly.
      with pytest.raises(TypeError):
          bayeslite.BayesDB()  # pylint: disable=no-value-for-parameter
      with pytest.raises(ValueError):
          bayeslite.BayesDB(':memory:', 0xdeadbeef)
  
  def test_prng_determinism():
      bdb = bayeslite.bayesdb_open(builtin_backends=False)
      assert bdb._prng.weakrandom_un
  
  [... the rest is omitted ...]
files: tests/test_core.py
files: # -*- coding: utf-8 -*-
  
  #   Copyright (c) 2010-2016, MIT Probabilistic Computing Project
  #
  #   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.
  
  '''Ensures that INFER EXPLICIT PREDICT col for a _rowid_ which exists in the
  base table, but not incorporated into the generator (i.e. did not exist in the
  table during INITIALIZE) correctly retrieves all other values in the row. The
  test cases creates a population of (0,1) and (1,0) -- the complement function --
  and ensures its recovery on a held-out test set.'''
  
  import pytest
  
  from bayeslite import bayesdb_open
  
  from bayeslite.core import bayesdb_get_generator
  from bayeslite.core import bayesdb_get_population
  from bayeslite.core import bayesdb_population_fresh_row_id
  from bayeslite.util import cursor_value
  
  
  @pytest.fixture(scope='module')
  def bdb():
      bdb = bayesdb_open(':memory:')
  
      # Create the population of complements.
      bdb.sql_execute('CREATE TABLE t (a TEXT, b TEXT)')
      for _ in xrange(20):
          bdb.sql_execute('INSERT INTO t (a, b) VALUES (0,1)')
      for _ in xrange(20):
          bdb.sql_execute('INSERT INTO t (a, b) VALUES (1,0)')
  
      # Create the population and generator on the existing rows.
      bdb.execute('CREATE POPULATION p FOR t (SET STATTYPES OF a, b TO NOMINAL)')
      bdb.execute('CREATE GENERATOR m FOR p;')
      bdb.execute('INITIALIZE 1 MODELS FOR m;')
      bdb.execute('ANALYZE m FOR 1000 ITERATION (OPTIMIZED);')
  
      # Add new 'hypothetical' rows into the
  
  [... the rest is omitted ...]
files: tests/test_infer_hypothetical.py
files: # -*- coding: utf-8 -*-
  
  #   Copyright (c) 2010-2016, MIT Probabilistic Computing Project
  #
  #   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.
  
  '''Ensures that INFER EXPLICIT PREDICT col for a _rowid_ which exists in the
  base table, but not incorporated into the generator (i.e. did not exist in the
  table during INITIALIZE) correctly retrieves all other values in the row. The
  test cases creates a population of (0,1) and (1,0) -- the complement function --
  and ensures its recovery on a held-out test set.'''
  
  import pytest
  
  from bayeslite import bayesdb_open
  
  from bayeslite.core import bayesdb_get_generator
  from bayeslite.core import bayesdb_get_population
  from bayeslite.core import bayesdb_population_fresh_row_id
  from bayeslite.util import cursor_value
  
  
  @pytest.fixture(scope='module')
  def bdb():
      bdb = bayesdb_open(':memory:')
  
      # Create the population of complements.
      bdb.sql_execute('CREATE TABLE t (a TEXT, b TEXT)')
      for _ in xrange(20):
          bdb.sql_execute('INSERT INTO t (a, b) VALUES (0,1)')
      for _ in xrange(20):
          bdb.sql_execute('INSERT INTO t (a, b) VALUES (1,0)')
  
      # Create the population and generator on the existing rows.
      bdb.execute('CREATE POPULATION p FOR t (MODEL a, b AS NOMINAL)')
      bdb.execute('CREATE GENERATOR m FOR p;')
      bdb.execute('INITIALIZE 1 MODELS FOR m;')
      bdb.execute('ANALYZE m FOR 1000 ITERATION (OPTIMIZED);')
  
      # Add new 'hypothetical' rows into the base table to
  
  [... the rest is omitted ...]
files: tests/test_infer_hypothetical.py
files: # -*- coding: utf-8 -*-
  
  #   Copyright (c) 2010-2016, MIT Probabilistic Computing Project
  #
  #   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.
  
  import pytest
  
  import bayeslite.core as core
  
  from bayeslite import BQLError
  from bayeslite import bayesdb_open
  from bayeslite import bayesdb_register_backend
  from bayeslite.backends.nig_normal import NIGNormalBackend
  
  def test_nig_normal_smoke():
      with bayesdb_open(':memory:') as bdb:
          bayesdb_register_backend(bdb, NIGNormalBackend())
          bdb.sql_execute('create table t(x)')
          for x in xrange(100):
              bdb.sql_execute('insert into t(x) values(?)', (x,))
          bdb.execute('create population p for t(x numerical)')
          bdb.execute('create generator g for p using nig_normal')
          bdb.execute('initialize 1 model for g')
          bdb.execute('analyze g for 1 iteration')
          bdb.execute('estimate probability density of x = 50 from p').fetchall()
          bdb.execute('simulate x from p limit 1').fetchall()
          bdb.execute('drop models from g')
          bdb.execute('drop generator g')
          bdb.execute('drop population p')
          bdb.execute('drop table t')
  
  def test_nig_normal_latent_numbering():
      with bayesdb_open(':memory:') as bdb:
          bayesdb_register_backend(bdb, NIGNormalBackend())
          bdb.sql_execute('create table t(id integer primary key, x, y)')
          for x in xrange(100):
              bdb.sql_execute('insert into t(x, y) values(?, ?)', (x, x*x - 100))
          bdb.execute('''
             
  
  [... the rest is omitted ...]
files: tests/test_nig_normal.py
files: # -*- coding: utf-8 -*-
  
  #   Copyright (c) 2010-2016, MIT Probabilistic Computing Project
  #
  #   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.
  
  import pytest
  
  import bayeslite.core as core
  
  from bayeslite import BQLError
  from bayeslite import bayesdb_open
  from bayeslite import bayesdb_register_backend
  from bayeslite.backends.nig_normal import NIGNormalBackend
  
  def test_nig_normal_smoke():
      with bayesdb_open(':memory:') as bdb:
          bayesdb_register_backend(bdb, NIGNormalBackend())
          bdb.sql_execute('create table t(x)')
          for x in xrange(100):
              bdb.sql_execute('insert into t(x) values(?)', (x,))
          bdb.execute('create population p for t(x numerical)')
          bdb.execute('create generator g for p using nig_normal')
          bdb.execute('initialize 1 model for g')
          bdb.execute('analyze g for 1 iteration')
          bdb.execute('estimate probability density of x = 50 from p').fetchall()
          bdb.execute('simulate x from p limit 1').fetchall()
          bdb.execute('drop models from g')
          bdb.execute('drop generator g')
          bdb.execute('drop population p')
          bdb.execute('drop table t')
  
  def test_nig_normal_latent_numbering():
      with bayesdb_open(':memory:') as bdb:
          bayesdb_register_backend(bdb, NIGNormalBackend())
          bdb.sql_execute('create table t(id integer primary key, x, y)')
          for x in xrange(100):
              bdb.sql_execute('insert into t(x, y) values(?, ?)', (x, x*x - 100))
          bdb.execute('''
             
  
  [... the rest is omitted ...]
files: tests/test_nig_normal.py
files: # -*- coding: utf-8 -*-
  
  #   Copyright (c) 2010-2016, MIT Probabilistic Computing Project
  #
  #   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.
  
  import contextlib
  import itertools
  import pytest
  
  import bayeslite
  import bayeslite.ast as ast
  import bayeslite.parse as parse
  
  def parse_bql_string(string):
      phrases = list(parse.parse_bql_string(string))
      phrase_pos = list(parse.parse_bql_string_pos(string))
      assert len(phrases) == len(phrase_pos)
      start = 0
      for i in range(len(phrase_pos)):
          phrase, pos = phrase_pos[i]
          assert phrases[i] == phrase
          substring = buffer(string, start, len(string) - start)
          phrase0, pos0 = parse.parse_bql_string_pos_1(substring)
          assert phrase0 == phrase
          assert pos0 == pos - start
          start = pos
      return phrases
  
  def test_empty():
      assert [] == parse_bql_string('')
      assert [] == parse_bql_string(';')
      assert [] == parse_bql_string(';;')
      assert [] == parse_bql_string(' ;')
      assert [] == parse_bql_string('; ')
      assert [] == parse_bql_string(' ; ')
      assert [] == parse_bql_string(' ; ; ')
  
  def test_multiquery():
      assert parse_bql_string('select 0; select 1;') == [
          ast.Select(ast.SELQUANT_ALL,
              [ast.SelColExp(ast.ExpLit(ast.LitInt(0)), None)],
              None, None, None, None, None),
          ast.Select(ast.SELQUANT_ALL,
              [ast.SelColExp(ast.ExpLit(ast.LitInt(1)), None)],
              None, None, None, None, None),
      ]
      assert parse_bql_string('select 
  
  [... the rest is omitted ...]
files: tests/test_parse.py
files: # -*- coding: utf-8 -*-
  
  #   Copyright (c) 2010-2016, MIT Probabilistic Computing Project
  #
  #   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.
  
  import contextlib
  import itertools
  import pytest
  
  import bayeslite
  import bayeslite.ast as ast
  import bayeslite.parse as parse
  
  def parse_bql_string(string):
      phrases = list(parse.parse_bql_string(string))
      phrase_pos = list(parse.parse_bql_string_pos(string))
      assert len(phrases) == len(phrase_pos)
      start = 0
      for i in range(len(phrase_pos)):
          phrase, pos = phrase_pos[i]
          assert phrases[i] == phrase
          substring = buffer(string, start, len(string) - start)
          phrase0, pos0 = parse.parse_bql_string_pos_1(substring)
          assert phrase0 == phrase
          assert pos0 == pos - start
          start = pos
      return phrases
  
  def test_empty():
      assert [] == parse_bql_string('')
      assert [] == parse_bql_string(';')
      assert [] == parse_bql_string(';;')
      assert [] == parse_bql_string(' ;')
      assert [] == parse_bql_string('; ')
      assert [] == parse_bql_string(' ; ')
      assert [] == parse_bql_string(' ; ; ')
  
  def test_multiquery():
      assert parse_bql_string('select 0; select 1;') == [
          ast.Select(ast.SELQUANT_ALL,
              [ast.SelColExp(ast.ExpLit(ast.LitInt(0)), None)],
              None, None, None, None, None),
          ast.Select(ast.SELQUANT_ALL,
              [ast.SelColExp(ast.ExpLit(ast.LitInt(1)), None)],
              None, None, None, None, None),
      ]
      assert parse_bql_string('select 
  
  [... the rest is omitted ...]
files: tests/test_parse.py
files: # -*- coding: utf-8 -*-
  
  #   Copyright (c) 2010-2016, MIT Probabilistic Computing Project
  #
  #   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.
  
  import pytest
  
  from bayeslite import BQLError
  from bayeslite import bayesdb_register_backend
  from bayeslite.backends.cgpm_backend import CGPM_Backend
  
  from test_cgpm import cgpm_dummy_satellites_bdb
  
  
  def test_regress_bonanza__ci_integration():
      with cgpm_dummy_satellites_bdb() as bdb:
          bayesdb_register_backend(
              bdb, CGPM_Backend(dict(), multiprocess=0))
          bdb.execute('''
              CREATE POPULATION satellites FOR satellites_ucs WITH SCHEMA(
                  apogee                  NUMERICAL;
                  class_of_orbit          NOMINAL;
                  country_of_operator     NOMINAL;
                  launch_mass             NUMERICAL;
                  perigee                 NUMERICAL;
                  period                  NUMERICAL;
              )
          ''')
          bdb.execute('''
              CREATE GENERATOR m FOR satellites;
          ''')
          bdb.execute('INITIALIZE 2 MODELS FOR m;')
  
          def check_regression_variables(results, numericals, nominals):
              seen = set()
              for r in results:
                  assert len(r) == 2
                  variable = r[0]
                  assert variable not in seen
                  assert variable in numericals or \
                      any(variable.startswith('%s_dum_' % (nominal,))
                          for nominal in nominals)
                  seen.add(variable)
  
          # Regression on 1 numerical variable.
          results = bdb.execute('''
              REGRESS apogee GIVEN (perigee) USING 12 SAMPLES BY satellites;
          ''').fetchall()
          assert len(results)
  
  [... the rest is omitted ...]
files: tests/test_regress.py
files: # -*- coding: utf-8 -*-
  
  #   Copyright (c) 2010-2016, MIT Probabilistic Computing Project
  #
  #   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.
  
  import pytest
  
  from bayeslite import BQLError
  from bayeslite import bayesdb_register_backend
  from bayeslite.backends.cgpm_backend import CGPM_Backend
  
  from test_cgpm import cgpm_dummy_satellites_bdb
  
  
  def test_regress_bonanza__ci_integration():
      with cgpm_dummy_satellites_bdb() as bdb:
          bayesdb_register_backend(
              bdb, CGPM_Backend(dict(), multiprocess=0))
          bdb.execute('''
              CREATE POPULATION satellites FOR satellites_ucs WITH SCHEMA(
                  MODEL apogee AS NUMERICAL;
                  MODEL class_of_orbit AS NOMINAL;
                  MODEL country_of_operator AS NOMINAL;
                  MODEL launch_mass AS NUMERICAL;
                  MODEL perigee AS NUMERICAL;
                  MODEL period AS NUMERICAL
              )
          ''')
          bdb.execute('''
              CREATE GENERATOR m FOR satellites;
          ''')
          bdb.execute('INITIALIZE 2 MODELS FOR m;')
  
          def check_regression_variables(results, numericals, nominals):
              seen = set()
              for r in results:
                  assert len(r) == 2
                  variable = r[0]
                  assert variable not in seen
                  assert variable in numericals or \
                      any(variable.startswith('%s_dum_' % (nominal,))
                          for nominal in nominals)
                  seen.add(variable)
  
          # Regression on 1 numerical variable.
          results = bdb.execute('''
              REGRESS apogee GIVEN (perigee) USING 12 SAMPLES BY satellites;
          ''').fetchall
  
  [... the rest is omitted ...]
files: tests/test_regress.py
files: # -*- coding: utf-8 -*-
  
  #   Copyright (c) 2010-2016, MIT Probabilistic Computing Project
  #
  #   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.
  
  import os
  
  import pytest
  
  import bayeslite
  import bayeslite.read_csv as read_csv
  
  from bayeslite.guess import bayesdb_guess_population
  
  
  root = os.path.dirname(os.path.abspath(__file__))
  dha_csv = os.path.join(root, 'dha.csv')
  
  
  # Test that simulating a column constrained to have a specific value
  # returns an error.
  #
  # XXX This should be a backend-independent test.
  def test_simulate_drawconstraint_error__ci_slow():
      with bayeslite.bayesdb_open() as bdb:
          with open(dha_csv, 'rU') as f:
              read_csv.bayesdb_read_csv(bdb, 'dha', f, header=True, create=True)
          bdb.backends['cgpm'].set_multiprocess(False)
          bayesdb_guess_population(
              bdb, 'hospital', 'dha', overrides=[('name', 'key')])
          bdb.execute(
              'CREATE GENERATOR hospital_cc FOR hospital USING cgpm;')
          bdb.execute('INITIALIZE 1 MODEL FOR hospital_cc')
          bdb.execute('ANALYZE hospital_cc FOR 1 ITERATION (OPTIMIZED);')
          with pytest.raises(ValueError):
              bdb.execute('''
                  SIMULATE ttl_mdcr_spnd, n_death_ill FROM hospital
                      GIVEN TTL_MDCR_SPND = 40000
                      LIMIT 100
              ''').fetchall()
          samples = bdb.execute('''
              SIMULATE n_death_ill FROM hospital
                  GIVEN TTL_MDCR_SPND = 40000
                  LIM
  
  [... the rest is omitted ...]
files: tests/test_simulate.py
files: # -*- coding: utf-8 -*-
  
  #   Copyright (c) 2010-2016, MIT Probabilistic Computing Project
  #
  #   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.
  
  import os
  
  import pytest
  
  import bayeslite
  import bayeslite.read_csv as read_csv
  
  from bayeslite.guess import bayesdb_guess_population
  
  
  root = os.path.dirname(os.path.abspath(__file__))
  dha_csv = os.path.join(root, 'dha.csv')
  
  
  # Test that simulating a column constrained to have a specific value
  # returns an error.
  #
  # XXX This should be a backend-independent test.
  def test_simulate_drawconstraint_error__ci_slow():
      with bayeslite.bayesdb_open() as bdb:
          with open(dha_csv, 'rU') as f:
              read_csv.bayesdb_read_csv(bdb, 'dha', f, header=True, create=True)
          bdb.backends['cgpm'].set_multiprocess(False)
          bayesdb_guess_population(
              bdb, 'hospital', 'dha', overrides=[('name', 'key')])
          bdb.execute(
              'CREATE GENERATOR hospital_cc FOR hospital USING cgpm;')
          bdb.execute('INITIALIZE 1 MODEL FOR hospital_cc')
          bdb.execute('ANALYZE hospital_cc FOR 1 ITERATION (OPTIMIZED);')
          with pytest.raises(ValueError):
              bdb.execute('''
                  SIMULATE ttl_mdcr_spnd, n_death_ill FROM hospital
                      GIVEN TTL_MDCR_SPND = 40000
                      LIMIT 100
              ''').fetchall()
          samples = bdb.execute('''
              SIMULATE n_death_ill FROM hospital
                  GIVEN TTL_MDCR_SPND = 40000
                  LIM
  
  [... the rest is omitted ...]
files: tests/test_simulate.py