<commit_before># -*- 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_metamodel
from bayeslite.exception import BQLError
from bayeslite.math_util import abserr
from bayeslite.metamodels.cgpm_metamodel import CGPM_Metamodel

from test_cgpm import cgpm_dummy_satellites_bdb


def test_cgpm_alter_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
            )
        ''')

        metamodel = CGPM_Metamodel(dict(), multiprocess=0)
        bayesdb_register_metamodel(bdb, metamodel)

        bdb.execute('''
            create analysis schema g0 for satellites using cgpm(
                subsample 10
            );
        ''')
        bdb.execute('initialize 4 analyses 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 analysis schema g0 analyses (0,1)
                ensure variables * independent;
        ''')
        dependencies = bdb.execute('''
            estimate dependence probability from pairwise variables of
            satellites modeled by g0 using models 0-1;
        ''').fetchall()
        for _, var0, var1, value in dependencies:
            assert value == int(var0 == var1)

        # Make all variables dependent in all models.
        bdb.execute('''
            alter analysis schema g0
                ensure variables * dependent;
        ''')
        dependencies = bdb.execute('''
            estimate dependence probability from pairwise variables of
            satellites
        ''').fetchall()
        for _, _, _, value in dependencies:
            assert value == 1

        # Make all variables independent again.
        bdb.execute('''
            alter analysis schema g0
                ensure variables * independent;
        ''')

        # Move apogee, perigee->view(period), launch_mass->view(class_of_orbit)
        # All other variables should remain independent.
        bdb.execute('''
            alter analysis schema g0
                ensure variables (apogee, period, perigee) in context of period,
                ensure variable launch_mass in context of class_of_orbit;
        ''')
        dependencies = bdb.execute('''
            estimate dependence probability from pairwise variables of
            satellites
        ''').fetchall()
        blocks = [
            ['apogee','perigee','period'],
            ['launch_mass', 'class_of_orbit']
        ]
        for _, var0, var1, value in dependencies:
            if var0 in blocks[0] and var1 in blocks[0]:
                assert value == 1.
            elif var0 in blocks[1] and var1 in blocks[1]:
                assert value == 1.
            elif var0 == var1:
                assert value == 1.
            else:
                assert value == 0.

        # Move period to a singleton in model 3.
        bdb.execute('''
            alter analysis schema g0 analysis (3)
                ensure variable period in singleton view
        ''')
        dependencies = bdb.execute('''
            estimate dependence probability with period from variables of
            satellites modeled by g0 using models 0,1,2
        ''')
        for _, var0, value in dependencies:
            if var0 in blocks[0]:
                assert value == 1
            else:
                assert value == 0
        dependencies = bdb.execute('''
            estimate dependence probability with period from variables of
            satellites modeled by g0 using models 3
        ''')
        for _, var0, value in dependencies:
            assert value == 0

        # Change the column crp concentration.
        bdb.execute('''
            alter analysis schema g0
                set view concentration parameter to 1000
        ''')
        engine = metamodel._engine(bdb, generator_id)
        for state in engine.states:
            assert abserr(1./1000, state.alpha()) < 1e-5

        # Change row crp concentration in view of period.
        engine = metamodel._engine(bdb, generator_id)
        varno = bayeslite.core.bayesdb_variable_number(
            bdb, population_id, generator_id, 'period')
        initial_alphas = [s.view_for(varno).alpha() for s in engine.states]
        bdb.execute('''
            alter analysis schema g0 analysis (1,3)
                set row cluster concentration parameter
                    within view of period to 12;
        ''')
        for i, state in enumerate(engine.states):
            view_alpha = state.view_for(varno).alpha()
            if i in [1,3]:
                assert abserr(1./12, view_alpha) < 1e-5
            else:<commit_msg>Reduce boilerplate by defining population in satellites bdb.<commit_after># -*- 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_metamodel
from bayeslite.exception import BQLError
from bayeslite.math_util import abserr
from bayeslite.metamodels.cgpm_metamodel import CGPM_Metamodel

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
            )
        ''')
        metamodel = CGPM_Metamodel(dict(), multiprocess=0)
        bayesdb_register_metamodel(bdb, metamodel)
        yield bdb


def test_cgpm_alter_basic():
    with cgpm_dummy_satellites_pop_bdb() as bdb:
        bdb.execute('''
            create analysis schema g0 for satellites using cgpm(
                subsample 10
            );
        ''')
        bdb.execute('initialize 4 analyses 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 analysis schema g0 analyses (0,1)
                ensure variables * independent;
        ''')
        dependencies = bdb.execute('''
            estimate dependence probability from pairwise variables of
            satellites modeled by g0 using models 0-1;
        ''').fetchall()
        for _, var0, var1, value in dependencies:
            assert value == int(var0 == var1)

        # Make all variables dependent in all models.
        bdb.execute('''
            alter analysis schema g0
                ensure variables * dependent;
        ''')
        dependencies = bdb.execute('''
            estimate dependence probability from pairwise variables of
            satellites
        ''').fetchall()
        for _, _, _, value in dependencies:
            assert value == 1

        # Make all variables independent again.
        bdb.execute('''
            alter analysis schema g0
                ensure variables * independent;
        ''')

        # Move apogee, perigee->view(period), launch_mass->view(class_of_orbit)
        # All other variables should remain independent.
        bdb.execute('''
            alter analysis schema g0
                ensure variables (apogee, period, perigee) in context of period,
                ensure variable launch_mass in context of class_of_orbit;
        ''')
        dependencies = bdb.execute('''
            estimate dependence probability from pairwise variables of
            satellites
        ''').fetchall()
        blocks = [
            ['apogee','perigee','period'],
            ['launch_mass', 'class_of_orbit']
        ]
        for _, var0, var1, value in dependencies:
            if var0 in blocks[0] and var1 in blocks[0]:
                assert value == 1.
            elif var0 in blocks[1] and var1 in blocks[1]:
                assert value == 1.
            elif var0 == var1:
                assert value == 1.
            else:
                assert value == 0.

        # Move period to a singleton in model 3.
        bdb.execute('''
            alter analysis schema g0 analysis (3)
                ensure variable period in singleton view
        ''')
        dependencies = bdb.execute('''
            estimate dependence probability with period from variables of
            satellites modeled by g0 using models 0,1,2
        ''')
        for _, var0, value in dependencies:
            if var0 in blocks[0]:
                assert value == 1
            else:
                assert value == 0
        dependencies = bdb.execute('''
            estimate dependence probability with period from variables of
            satellites modeled by g0 using models 3
        ''')
        for _, var0, value in dependencies:
            assert value == 0

        # Change the column crp concentration.
        bdb.execute('''
            alter analysis schema g0
                set view concentration parameter to 1000
        ''')
        engine = bdb.metamodels['cgpm']._engine(bdb, generator_id)
        for state in engine.states:
            assert abserr(1./1000, state.alpha()) < 1e-5

        # Change row crp concentration in view of period.
        engine = bdb.metamodels['cgpm']._engine(bdb, generator_id)
        varno = bayeslite.core.bayesdb_variable_number(
            bdb, population_id, generator_id, 'period')
        initial_alphas = [s.view_for(varno).alpha() for s in engine.states]
        bdb.execute('''
            alter analysis schema g0 analysis (1,3)
                set row cluster concentration parameter
                    within view of period to 12;
        ''')
        for i, state in enumerate(engine.states):
            view_alpha = state.view_for(varno).alpha()
            if i in [1,3]:
                assert abserr(1./12, view_alpha) < 1e-5
            else:
{'fraction_non_alphanumeric': 0.07677397606607113, 'fraction_numerical': 0.015506489128602731, 'mean_word_length': 1.9615672572997256, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1747257', 'n_tokens_mistral': 3249, 'n_tokens_neox': 3032, 'n_words': 1051}