<s> import unittest <EOL> import sys <EOL> import numpy as np <EOL> from sklearn . mixture import DPGMM , VBGMM <EOL> from sklearn . mixture . dpgmm import log_normalize <EOL> from sklearn . datasets import make_blobs <EOL> from sklearn . utils . testing import assert_array_less , assert_equal <EOL> from sklearn . mixture . tests . test_gmm import GMMTester <EOL> from sklearn . externals . six . moves import cStringIO as StringIO <EOL> np . seterr ( all = '<STR_LIT>' ) <EOL> def test_class_weights ( ) : <EOL> X , y = make_blobs ( random_state = <NUM_LIT:1> ) <EOL> for Model in [ DPGMM , VBGMM ] : <EOL> dpgmm = Model ( n_components = <NUM_LIT:10> , random_state = <NUM_LIT:1> , alpha = <NUM_LIT:20> , n_iter = <NUM_LIT:50> ) <EOL> dpgmm . fit ( X ) <EOL> indices = np . unique ( dpgmm . predict ( X ) ) <EOL> active = np . zeros ( <NUM_LIT:10> , dtype = np . bool ) <EOL> active [ indices ] = True <EOL> assert_array_less ( <NUM_LIT> , dpgmm . weights_ [ active ] ) <EOL> assert_array_less ( dpgmm . weights_ [ ~ active ] , <NUM_LIT> ) <EOL> def test_verbose_boolean ( ) : <EOL> X , y = make_blobs ( random_state = <NUM_LIT:1> ) <EOL> for Model in [ DPGMM , VBGMM ] : <EOL> dpgmm_bool = Model ( n_components = <NUM_LIT:10> , random_state = <NUM_LIT:1> , alpha = <NUM_LIT:20> , <EOL> n_iter = <NUM_LIT:50> , verbose = True ) <EOL> dpgmm_int = Model ( n_components = <NUM_LIT:10> , random_state = <NUM_LIT:1> , alpha = <NUM_LIT:20> , <EOL> n_iter = <NUM_LIT:50> , verbose = <NUM_LIT:1> ) <EOL> old_stdout = sys . stdout <EOL> sys . stdout = StringIO ( ) <EOL> try : <EOL> dpgmm_bool . fit ( X ) <EOL> verbose_output = sys . stdout <EOL> verbose_output . seek ( <NUM_LIT:0> ) <EOL> bool_output = verbose_output . readline ( ) <EOL> dpgmm_int . fit ( X ) <EOL> verbose_output = sys . stdout <EOL> verbose_output . seek ( <NUM_LIT:0> ) <EOL> int_output = verbose_output . readline ( ) <EOL> assert_equal ( bool_output , int_output ) <EOL> finally : <EOL> sys . stdout = old_stdout <EOL> def test_verbose_first_level ( ) : <EOL> X , y = make_blobs ( random_state = <NUM_LIT:1> ) <EOL> for Model in [ DPGMM , VBGMM ] : <EOL> dpgmm = Model ( n_components = <NUM_LIT:10> , random_state = <NUM_LIT:1> , alpha = <NUM_LIT:20> , n_iter = <NUM_LIT:50> , <EOL> verbose = <NUM_LIT:1> ) <EOL> old_stdout = sys . stdout <EOL> sys . stdout = StringIO ( ) <EOL> try : <EOL> dpgmm . fit ( X ) <EOL> finally : <EOL> sys . stdout = old_stdout <EOL> def test_verbose_second_level ( ) : <EOL> X , y = make_blobs ( random_state = <NUM_LIT:1> ) <EOL> for Model in [ DPGMM , VBGMM ] : <EOL> dpgmm = Model ( n_components = <NUM_LIT:10> , random_state = <NUM_LIT:1> , alpha = <NUM_LIT:20> , n_iter = <NUM_LIT:50> , <EOL> verbose = <NUM_LIT:2> ) <EOL> old_stdout = sys . stdout <EOL> sys . stdout = StringIO ( ) <EOL> try : <EOL> dpgmm . fit ( X ) <EOL> finally : <EOL> sys . stdout = old_stdout <EOL> def test_log_normalize ( ) : <EOL> v = np . array ( [ <NUM_LIT:0.1> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] ) <EOL> a = np . log ( <NUM_LIT:2> * v ) <EOL> assert np . allclose ( v , log_normalize ( a ) , rtol = <NUM_LIT> ) <EOL> def do_model ( self , ** kwds ) : <EOL> return VBGMM ( verbose = False , ** kwds ) <EOL> class DPGMMTester ( GMMTester ) : <EOL> model = DPGMM <EOL> do_test_eval = False <EOL> def score ( self , g , train_obs ) : <EOL> _ , z = g . score_samples ( train_obs ) <EOL> return g . lower_bound ( train_obs , z ) <EOL> class TestDPGMMWithSphericalCovars ( unittest . TestCase , DPGMMTester ) : <EOL> covariance_type = '<STR_LIT>' <EOL> setUp = GMMTester . _setUp <EOL> class TestDPGMMWithDiagCovars ( unittest . TestCase , DPGMMTester ) : <EOL> covariance_type = '<STR_LIT>' <EOL> setUp = GMMTester . _setUp <EOL> class TestDPGMMWithTiedCovars ( unittest . TestCase , DPGMMTester ) : <EOL> covariance_type = '<STR_LIT>' <EOL> setUp = GMMTester . _setUp <EOL> class TestDPGMMWithFullCovars ( unittest . TestCase , DPGMMTester ) : <EOL> covariance_type = '<STR_LIT>' <EOL> setUp = GMMTester . _setUp <EOL> class VBGMMTester ( GMMTester ) : <EOL> model = do_model <EOL> do_test_eval = False <EOL> def score ( self , g , train_obs ) : <EOL> _ , z = g . score_samples ( train_obs ) <EOL> return g . lower_bound ( train_obs , z ) <EOL> class TestVBGMMWithSphericalCovars ( unittest . TestCase , VBGMMTester ) : <EOL> covariance_type = '<STR_LIT>' <EOL> setUp = GMMTester . _setUp <EOL> class TestVBGMMWithDiagCovars ( unittest . TestCase , VBGMMTester ) : <EOL> covariance_type = '<STR_LIT>' <EOL> setUp = GMMTester . _setUp <EOL> class TestVBGMMWithTiedCovars ( unittest . TestCase , VBGMMTester ) : <EOL> covariance_type = '<STR_LIT>' <EOL> setUp = GMMTester . _setUp <EOL> class TestVBGMMWithFullCovars ( unittest . TestCase , VBGMMTester ) : <EOL> covariance_type = '<STR_LIT>' <EOL> setUp = GMMTester . _setUp </s>
