<s> '''<STR_LIT>''' <EOL> from bnpy . suffstats . SuffStatBag import SuffStatBag <EOL> import numpy as np <EOL> import unittest <EOL> class TestSuffStatBag ( unittest . TestCase ) : <EOL> def shortDescription ( self ) : <EOL> return None <EOL> def test_copy ( self , K = <NUM_LIT:2> , D = <NUM_LIT:2> ) : <EOL> SS = self . makeSuffStatBagAndFillWithOnes ( K , D ) <EOL> self . addELBOtoSuffStatBag ( SS , K ) <EOL> SS2 = SS . copy ( ) <EOL> assert SS2 . s == SS . s <EOL> assert np . all ( SS2 . N == SS . N ) <EOL> assert np . all ( SS2 . getELBOTerm ( '<STR_LIT>' ) == SS . getELBOTerm ( '<STR_LIT>' ) ) <EOL> assert id ( SS2 . s ) != id ( SS . s ) <EOL> assert id ( SS2 . N ) != id ( SS . N ) <EOL> def test_ampFactor ( self , K = <NUM_LIT:2> , D = <NUM_LIT:2> ) : <EOL> SS = self . makeSuffStatBagAndFillWithOnes ( K , D ) <EOL> SS . applyAmpFactor ( <NUM_LIT> ) <EOL> assert np . allclose ( SS . s , <NUM_LIT> ) <EOL> assert np . allclose ( SS . N , <NUM_LIT> * np . ones ( K ) ) <EOL> assert np . allclose ( SS . x , <NUM_LIT> * np . ones ( ( K , D ) ) ) <EOL> assert np . allclose ( SS . xxT , <NUM_LIT> * np . ones ( ( K , D , D ) ) ) <EOL> assert SS . hasAmpFactor ( ) <EOL> assert SS . ampF == <NUM_LIT> <EOL> def makeSuffStatBagAndFillWithOnes ( self , K , D ) : <EOL> SS = SuffStatBag ( K = K , D = D ) <EOL> s = <NUM_LIT:1.0> <EOL> N = np . ones ( K ) <EOL> x = np . ones ( ( K , D ) ) <EOL> xxT = np . ones ( ( K , D , D ) ) <EOL> SS . setField ( '<STR_LIT:s>' , s ) <EOL> SS . setField ( '<STR_LIT:N>' , N , dims = '<STR_LIT>' ) <EOL> SS . setField ( '<STR_LIT:x>' , x , dims = ( '<STR_LIT>' , '<STR_LIT:D>' ) ) <EOL> SS . setField ( '<STR_LIT>' , xxT , dims = ( '<STR_LIT>' , '<STR_LIT:D>' , '<STR_LIT:D>' ) ) <EOL> return SS <EOL> def addELBOtoSuffStatBag ( self , SS , K ) : <EOL> SS . setELBOTerm ( '<STR_LIT>' , np . ones ( K ) , dims = '<STR_LIT>' ) <EOL> SS . setELBOTerm ( '<STR_LIT>' , <NUM_LIT:1.0> , dims = None ) <EOL> SS . setMergeTerm ( '<STR_LIT>' , <NUM_LIT:2> * np . ones ( ( K , K ) ) , dims = ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> return SS <EOL> def getExpectedMergedFields ( self , K , D , kA = <NUM_LIT:0> ) : <EOL> s = <NUM_LIT:1.0> <EOL> N = np . ones ( K - <NUM_LIT:1> ) <EOL> N [ kA ] = <NUM_LIT:2> <EOL> x = np . ones ( ( K - <NUM_LIT:1> , D ) ) <EOL> x [ kA ] = <NUM_LIT:2> <EOL> xxT = np . ones ( ( K - <NUM_LIT:1> , D , D ) ) <EOL> xxT [ kA ] = <NUM_LIT:2> <EOL> return s , N , x , xxT <EOL> def test_mergeComps_K2_D3_noELBO ( self , K = <NUM_LIT:2> , D = <NUM_LIT:3> ) : <EOL> SS = self . makeSuffStatBagAndFillWithOnes ( K = K , D = D ) <EOL> SS . mergeComps ( <NUM_LIT:0> , <NUM_LIT:1> ) <EOL> assert SS . K == K - <NUM_LIT:1> <EOL> assert np . allclose ( SS . s , <NUM_LIT:1> ) <EOL> assert np . allclose ( SS . N , <NUM_LIT:2> ) <EOL> assert np . allclose ( SS . x , <NUM_LIT:2> * np . ones ( D ) ) <EOL> assert np . allclose ( SS . xxT , <NUM_LIT:2> * np . ones ( ( D , D ) ) ) <EOL> def test_mergeComps_K2_D3_withELBO ( self , K = <NUM_LIT:2> , D = <NUM_LIT:3> ) : <EOL> SS = self . makeSuffStatBagAndFillWithOnes ( K , D ) <EOL> self . addELBOtoSuffStatBag ( SS , K ) <EOL> SS . mergeComps ( <NUM_LIT:0> , <NUM_LIT:1> ) <EOL> assert SS . K == K - <NUM_LIT:1> <EOL> assert np . allclose ( SS . s , <NUM_LIT:1> ) <EOL> assert np . allclose ( SS . N , <NUM_LIT:2> ) <EOL> assert np . allclose ( SS . x , <NUM_LIT:2> * np . ones ( D ) ) <EOL> assert np . allclose ( SS . xxT , <NUM_LIT:2> * np . ones ( ( D , D ) ) ) <EOL> assert np . allclose ( SS . getELBOTerm ( '<STR_LIT>' ) , <NUM_LIT> ) <EOL> assert np . allclose ( SS . getELBOTerm ( '<STR_LIT>' ) , <NUM_LIT:1.0> ) <EOL> assert SS . _ELBOTerms . K == K - <NUM_LIT:1> <EOL> assert SS . _MergeTerms . K == K - <NUM_LIT:1> <EOL> def test_mergeComps_K5_D3_withELBO_kA0 ( self , K = <NUM_LIT:5> , D = <NUM_LIT:3> ) : <EOL> SS = self . makeSuffStatBagAndFillWithOnes ( K , D ) <EOL> self . addELBOtoSuffStatBag ( SS , K ) <EOL> SS . mergeComps ( <NUM_LIT:0> , <NUM_LIT:1> ) <EOL> s , N , x , xxT = self . getExpectedMergedFields ( K , D ) <EOL> assert SS . K == K - <NUM_LIT:1> <EOL> assert SS . _ELBOTerms . K == K - <NUM_LIT:1> <EOL> assert SS . _MergeTerms . K == K - <NUM_LIT:1> <EOL> assert np . allclose ( SS . s , s ) <EOL> assert np . allclose ( SS . N , N ) <EOL> assert np . allclose ( SS . x , x ) <EOL> assert np . allclose ( SS . xxT , xxT ) <EOL> assert np . allclose ( SS . getELBOTerm ( '<STR_LIT>' ) , [ <NUM_LIT> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> ] ) <EOL> assert np . allclose ( SS . getELBOTerm ( '<STR_LIT>' ) , <NUM_LIT:1.0> ) <EOL> assert np . all ( np . isnan ( SS . _MergeTerms . Elogz [ <NUM_LIT:0> , <NUM_LIT:1> : ] ) ) <EOL> assert np . all ( np . isnan ( SS . _MergeTerms . Elogz [ : <NUM_LIT:0> , <NUM_LIT:0> ] ) ) <EOL> def test_mergeComps_K5_D3_withELBO_kA3 ( self , K = <NUM_LIT:5> , D = <NUM_LIT:3> ) : <EOL> SS = self . makeSuffStatBagAndFillWithOnes ( K , D ) <EOL> self . addELBOtoSuffStatBag ( SS , K ) <EOL> SS . mergeComps ( <NUM_LIT:3> , <NUM_LIT:4> ) <EOL> s , N , x , xxT = self . getExpectedMergedFields ( K , D , kA = <NUM_LIT:3> ) <EOL> assert SS . K == K - <NUM_LIT:1> <EOL> assert SS . _ELBOTerms . K == K - <NUM_LIT:1> <EOL> assert SS . _MergeTerms . K == K - <NUM_LIT:1> <EOL> assert np . allclose ( SS . s , s ) <EOL> assert np . allclose ( SS . N , N ) <EOL> assert np . allclose ( SS . x , x ) <EOL> assert np . allclose ( SS . xxT , xxT ) <EOL> assert np . allclose ( SS . getELBOTerm ( '<STR_LIT>' ) , [ <NUM_LIT:1.> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT> ] ) <EOL> assert np . allclose ( SS . getELBOTerm ( '<STR_LIT>' ) , <NUM_LIT:1.0> ) <EOL> assert np . all ( np . isnan ( SS . _MergeTerms . Elogz [ <NUM_LIT:3> , <NUM_LIT:4> : ] ) ) <EOL> assert np . all ( np . isnan ( SS . _MergeTerms . Elogz [ : <NUM_LIT:3> , <NUM_LIT:3> ] ) ) <EOL> def test_mergeComps_K5_D3_withELBO_back2back ( self , K = <NUM_LIT:5> , D = <NUM_LIT:3> ) : <EOL> SS = self . makeSuffStatBagAndFillWithOnes ( K , D ) <EOL> self . addELBOtoSuffStatBag ( SS , K ) <EOL> SS . mergeComps ( <NUM_LIT:3> , <NUM_LIT:4> ) <EOL> SS . mergeComps ( <NUM_LIT:0> , <NUM_LIT:1> ) <EOL> assert SS . K == K - <NUM_LIT:2> <EOL> assert SS . _ELBOTerms . K == K - <NUM_LIT:2> <EOL> assert SS . _MergeTerms . K == K - <NUM_LIT:2> </s>
