<s> import os <EOL> import kabuki <EOL> import numpy as np <EOL> import unittest <EOL> from nose . tools import raises <EOL> import pymc as pm <EOL> from . utils import HNodeSimple , HNodeSimpleVar , sample_from_models , create_test_models <EOL> import pandas as pd <EOL> from . utils import gen_func_df <EOL> class TestHierarchicalBreakDown ( unittest . TestCase ) : <EOL> """<STR_LIT>""" <EOL> @ classmethod <EOL> def setUpClass ( self ) : <EOL> self . models , self . params = create_test_models ( ) <EOL> sample_from_models ( self . models , n_iter = <NUM_LIT> ) <EOL> def runTest ( self ) : <EOL> pass <EOL> def test_logp ( self ) : <EOL> for model in self . models : <EOL> model . logp <EOL> def test_map ( self ) : <EOL> np . random . seed ( <NUM_LIT> ) <EOL> for model in self . models : <EOL> model . find_starting_values ( ) <EOL> def test_emcee ( self ) : <EOL> self . models , self . params = create_test_models ( ) <EOL> np . random . seed ( <NUM_LIT> ) <EOL> for model in self . models : <EOL> model . find_starting_values ( ) <EOL> model . sample_emcee ( ) <EOL> def test_dic_info ( self ) : <EOL> for model in self . models : <EOL> model . dic_info <EOL> def test_print_stats ( self ) : <EOL> for model in self . models : <EOL> model . print_stats ( ) <EOL> model . print_stats ( subj = False ) <EOL> if model . is_group_model : <EOL> model . print_stats ( subj_idx = <NUM_LIT:1> ) <EOL> def test_load_db ( self ) : <EOL> new_models , params = create_test_models ( ) <EOL> for i , model in enumerate ( new_models ) : <EOL> print ( "<STR_LIT>" , i ) <EOL> model . sample ( <NUM_LIT:100> , dbname = '<STR_LIT>' , db = '<STR_LIT>' ) <EOL> model . load_db ( dbname = '<STR_LIT>' , db = '<STR_LIT>' ) <EOL> model . gen_stats ( ) <EOL> os . remove ( '<STR_LIT>' ) <EOL> @ unittest . skip ( "<STR_LIT>" ) <EOL> def test_init_from_existing_model ( self ) : <EOL> new_models = create_test_models ( ) <EOL> for ( i_m , pre_model ) in enumerate ( self . models ) : <EOL> new_models [ i_m ] . init_from_existing_model ( pre_model ) <EOL> def test_plot_posteriors ( self ) : <EOL> pass <EOL> def test_map ( self ) : <EOL> for model in self . models : <EOL> model . draw_from_prior ( ) <EOL> model . approximate_map ( minimizer = '<STR_LIT>' , use_basin = False , fall_to_simplex = False , individual_subjs = True , minimizer_kwargs = { '<STR_LIT>' : <NUM_LIT:100> } ) <EOL> model . draw_from_prior ( ) <EOL> model . approximate_map ( minimizer = '<STR_LIT>' , use_basin = True , fall_to_simplex = False , individual_subjs = False , minimizer_kwargs = { '<STR_LIT>' : <NUM_LIT:100> } , basin_kwargs = { '<STR_LIT:T>' : <NUM_LIT:2> } ) <EOL> model . draw_from_prior ( ) <EOL> model . approximate_map ( minimizer = '<STR_LIT>' , use_basin = False , fall_to_simplex = True , cycles = <NUM_LIT:2> , minimizer_kwargs = { '<STR_LIT>' : <NUM_LIT:100> } ) <EOL> model . draw_from_prior ( ) <EOL> model . approximate_map ( minimizer = '<STR_LIT>' , use_basin = True , fall_to_simplex = True , minimizer_kwargs = { '<STR_LIT>' : <NUM_LIT:100> } ) <EOL> model . draw_from_prior ( ) <EOL> model . approximate_map ( minimizer = '<STR_LIT>' , use_basin = False , fall_to_simplex = False , minimizer_kwargs = { '<STR_LIT>' : <NUM_LIT:100> } ) <EOL> model . draw_from_prior ( ) <EOL> model . approximate_map ( minimizer = '<STR_LIT>' , use_basin = True , fall_to_simplex = False , minimizer_kwargs = { '<STR_LIT>' : <NUM_LIT:100> } ) <EOL> return <EOL> class TestModelCreation ( unittest . TestCase ) : <EOL> @ classmethod <EOL> def setUpClass ( self ) : <EOL> self . n_subj = <NUM_LIT:3> <EOL> data , _ = kabuki . generate . gen_rand_data ( gen_func_df , { '<STR_LIT:A>' : { '<STR_LIT>' : <NUM_LIT:0> , '<STR_LIT>' : <NUM_LIT:1> } , '<STR_LIT:B>' : { '<STR_LIT>' : <NUM_LIT:0> , '<STR_LIT>' : <NUM_LIT:1> } } , <EOL> subjs = self . n_subj ) <EOL> data = pd . DataFrame ( data ) <EOL> data [ '<STR_LIT>' ] = np . random . randint ( <NUM_LIT:2> , size = len ( data ) ) <EOL> self . data = data <EOL> def runTest ( self ) : <EOL> pass <EOL> def test_save_and_load_breakdown ( self ) : <EOL> m = HNodeSimple ( self . data ) <EOL> m . sample ( <NUM_LIT> , dbname = '<STR_LIT>' , db = '<STR_LIT>' ) <EOL> m . save ( '<STR_LIT>' ) <EOL> m_load = kabuki . utils . load ( '<STR_LIT>' ) <EOL> os . remove ( '<STR_LIT>' ) <EOL> os . remove ( '<STR_LIT>' ) <EOL> def test_simple_no_deps ( self ) : <EOL> m = HNodeSimple ( self . data ) <EOL> n_nodes = <NUM_LIT:1> + self . n_subj * <NUM_LIT:2> <EOL> self . assertEqual ( len ( m . nodes_db ) , n_nodes ) <EOL> def test_simple_deps ( self ) : <EOL> m = HNodeSimple ( self . data , depends_on = { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> n_nodes = <NUM_LIT:2> * ( <NUM_LIT:1> + self . n_subj * <NUM_LIT:2> ) <EOL> self . assertEqual ( len ( m . nodes_db ) , n_nodes ) <EOL> @ raises ( AssertionError ) <EOL> def test_assertion_on_wrong_param_name ( self ) : <EOL> HNodeSimple ( self . data , depends_on = { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> def test_assertion_on_wrong_param_name_tofix ( self ) : <EOL> HNodeSimple ( self . data , depends_on = { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } ) <EOL> def test_simplevar_partly_deps ( self ) : <EOL> m = HNodeSimpleVar ( self . data , depends_on = { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> n_nodes = <NUM_LIT:1> + <NUM_LIT:1> + <NUM_LIT:2> * ( <NUM_LIT:1> + self . n_subj * <NUM_LIT:2> ) <EOL> self . assertEqual ( len ( m . nodes_db ) , n_nodes ) <EOL> def test_simplevar_deps ( self ) : <EOL> m = HNodeSimpleVar ( self . data , depends_on = { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } ) <EOL> n_nodes = <NUM_LIT:2> * ( <NUM_LIT:1> + <NUM_LIT:1> + <NUM_LIT:1> + self . n_subj * <NUM_LIT:2> ) <EOL> self . assertEqual ( len ( m . nodes_db ) , n_nodes ) <EOL> def test_simplevar_double_deps_A ( self ) : <EOL> m = HNodeSimpleVar ( self . data , depends_on = { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } ) <EOL> n_nodes = <NUM_LIT:2> + <NUM_LIT:2> + <NUM_LIT:2> + <NUM_LIT:4> * self . n_subj * <NUM_LIT:2> <EOL> self . assertEqual ( len ( m . nodes_db ) , n_nodes ) <EOL> def test_simplevar_double_deps_B ( self ) : <EOL> m = HNodeSimpleVar ( self . data , depends_on = { '<STR_LIT>' : [ '<STR_LIT>' , '<STR_LIT>' ] , '<STR_LIT>' : '<STR_LIT>' } ) <EOL> n_nodes = <NUM_LIT:2> + <NUM_LIT:2> + <NUM_LIT:4> + <NUM_LIT:4> * self . n_subj * <NUM_LIT:2> <EOL> self . assertEqual ( len ( m . nodes_db ) , n_nodes ) <EOL> class TestEstimation ( unittest . TestCase ) : <EOL> """<STR_LIT>""" <EOL> def test_map_approx ( self ) : <EOL> subjs = <NUM_LIT> <EOL> data , params_true = kabuki . generate . gen_rand_data ( gen_func_df , <EOL> { '<STR_LIT:A>' : { '<STR_LIT>' : <NUM_LIT:0> , '<STR_LIT>' : <NUM_LIT:1> } , '<STR_LIT:B>' : { '<STR_LIT>' : <NUM_LIT:2> , '<STR_LIT>' : <NUM_LIT:1> } } , <EOL> subj_noise = { '<STR_LIT>' : <NUM_LIT> } , size = <NUM_LIT:1000> , subjs = subjs ) <EOL> model = HNodeSimple ( data , depends_on = { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> model . approximate_map ( ) <EOL> model . approximate_map ( individual_subjs = False ) <EOL> counter = <NUM_LIT:0> <EOL> for condition , subj_params in params_true . items ( ) : <EOL> nodes = model . nodes_db [ model . nodes_db [ '<STR_LIT>' ] == condition ] <EOL> for idx , params in enumerate ( subj_params ) : <EOL> nodes_subj = nodes [ nodes [ '<STR_LIT>' ] == idx ] <EOL> for param_name , value in params . items ( ) : <EOL> if param_name != '<STR_LIT>' : <EOL> continue <EOL> node = nodes_subj [ nodes_subj . knode_name == '<STR_LIT>' ] <EOL> assert len ( node ) == <NUM_LIT:1> , "<STR_LIT>" <EOL> abs_error = np . abs ( node . map [ <NUM_LIT:0> ] - value ) <EOL> self . assertTrue ( abs_error < <NUM_LIT> ) <EOL> counter += <NUM_LIT:1> <EOL> self . assertEqual ( counter , subjs * <NUM_LIT:2> ) <EOL> class TestConcatenate ( unittest . TestCase ) : <EOL> def test_concat ( self ) : <EOL> n_subj = <NUM_LIT:5> <EOL> data , params = kabuki . generate . gen_rand_data ( gen_func_df , { '<STR_LIT:A>' : { '<STR_LIT>' : <NUM_LIT:0> , '<STR_LIT>' : <NUM_LIT:1> } , '<STR_LIT:B>' : { '<STR_LIT>' : <NUM_LIT:0> , '<STR_LIT>' : <NUM_LIT:1> } } , subjs = n_subj ) <EOL> data = pd . DataFrame ( data ) <EOL> models = [ ] <EOL> for i in range ( <NUM_LIT:4> ) : <EOL> m = HNodeSimple ( data ) <EOL> m . sample ( <NUM_LIT:100> , burn = <NUM_LIT:0> , db = '<STR_LIT>' , dbname = '<STR_LIT>' % i ) <EOL> models . append ( m ) <EOL> super_model = kabuki . utils . concat_models ( models ) <EOL> stochs = super_model . get_stochastics ( ) <EOL> for stoch in stochs . node : <EOL> self . assertEqual ( len ( stoch . trace [ : ] ) , <NUM_LIT:100> * <NUM_LIT:4> ) <EOL> for i in range ( <NUM_LIT:4> ) : <EOL> os . remove ( '<STR_LIT>' % i ) </s>
