<s> '''<STR_LIT>''' <EOL> import scipy . linalg <EOL> import numpy as np <EOL> from bnpy . data import XData , MinibatchIterator <EOL> def get_short_name ( ) : <EOL> return '<STR_LIT>' % ( D ) <EOL> def get_data_info ( ) : <EOL> return '<STR_LIT>' % ( K , D ) <EOL> def get_data ( seed = <NUM_LIT> , nObsTotal = <NUM_LIT> , ** kwargs ) : <EOL> X , TrueZ = generateData ( seed , nObsTotal ) <EOL> Data = XData ( X = X , TrueZ = TrueZ ) <EOL> Data . summary = get_data_info ( ) <EOL> return Data <EOL> def get_minibatch_iterator ( seed = <NUM_LIT> , nObsTotal = <NUM_LIT> , ** kwargs ) : <EOL> X , TrueZ = generateData ( seed , nObsTotal ) <EOL> Data = XData ( X = X , TrueZ = TrueZ ) <EOL> DataIterator = MinibatchIterator ( Data , ** kwargs ) <EOL> DataIterator . summary = get_data_info ( ) <EOL> return DataIterator <EOL> def makeTrueParams ( Din ) : <EOL> '''<STR_LIT>''' <EOL> global K , D <EOL> D = Din <EOL> K = <NUM_LIT:8> <EOL> global w <EOL> global Mu <EOL> global Sigma <EOL> global cholSigma <EOL> w = np . ones ( K ) <EOL> w = w / np . sum ( w ) <EOL> Mu = np . zeros ( ( K , D ) ) <EOL> Sigma = np . zeros ( ( K , D , D ) ) <EOL> cholSigma = np . zeros ( Sigma . shape ) <EOL> for k in xrange ( K ) : <EOL> Sigma [ k ] = makeImgPatchCovMatForComp ( D , k ) <EOL> cholSigma [ k ] = scipy . linalg . cholesky ( Sigma [ k ] ) <EOL> def makeImgPatchPrototype ( D , compID ) : <EOL> '''<STR_LIT>''' <EOL> P = np . sqrt ( D ) <EOL> Xprototype = np . zeros ( ( P , P ) ) <EOL> if compID % <NUM_LIT:4> == <NUM_LIT:0> : <EOL> Xprototype [ : P / <NUM_LIT:2> ] = <NUM_LIT:1.0> <EOL> Xprototype = np . rot90 ( Xprototype , compID / <NUM_LIT:4> ) <EOL> if compID % <NUM_LIT:4> == <NUM_LIT:1> : <EOL> Xprototype [ np . tril_indices ( P ) ] = <NUM_LIT:1> <EOL> Xprototype = np . rot90 ( Xprototype , ( compID - <NUM_LIT:1> ) / <NUM_LIT:4> ) <EOL> if compID % <NUM_LIT:4> == <NUM_LIT:2> : <EOL> Xprototype [ np . tril_indices ( P , <NUM_LIT:2> ) ] = <NUM_LIT:1> <EOL> Xprototype = np . rot90 ( Xprototype , ( compID - <NUM_LIT:2> ) / <NUM_LIT:4> ) <EOL> if compID % <NUM_LIT:4> == <NUM_LIT:3> : <EOL> Xprototype [ np . tril_indices ( P , - <NUM_LIT:2> ) ] = <NUM_LIT:1> <EOL> Xprototype = np . rot90 ( Xprototype , ( compID - <NUM_LIT:3> ) / <NUM_LIT:4> ) <EOL> return Xprototype <EOL> def makeImgPatchCovMatForComp ( D , compID , sig = <NUM_LIT> ) : <EOL> '''<STR_LIT>''' <EOL> Xprototype = makeImgPatchPrototype ( D , compID ) <EOL> N = <NUM_LIT:100> * D <EOL> PRNG = np . random . RandomState ( compID ) <EOL> Xnoise = sig * PRNG . randn ( N , D ) <EOL> Xsignal = np . tile ( Xprototype . flatten ( ) , ( N , <NUM_LIT:1> ) ) <EOL> Xsignal *= PRNG . randn ( N , <NUM_LIT:1> ) <EOL> X = Xnoise + Xsignal <EOL> Sigma = np . cov ( X . T ) <EOL> return Sigma + <NUM_LIT> * np . eye ( D ) <EOL> def sample_data_from_comp ( k , Nk ) : <EOL> PRNG = np . random . RandomState ( k ) <EOL> return Mu [ k , : ] + np . dot ( cholSigma [ k ] . T , PRNG . randn ( D , Nk ) ) . T <EOL> def generateData ( seed , nObsTotal ) : <EOL> PRNG = np . random . RandomState ( seed ) <EOL> trueList = list ( ) <EOL> Npercomp = PRNG . multinomial ( nObsTotal , w ) <EOL> X = list ( ) <EOL> for k in range ( K ) : <EOL> X . append ( sample_data_from_comp ( k , Npercomp [ k ] ) ) <EOL> trueList . append ( k * np . ones ( Npercomp [ k ] ) ) <EOL> X = np . vstack ( X ) <EOL> TrueZ = np . hstack ( trueList ) <EOL> permIDs = PRNG . permutation ( X . shape [ <NUM_LIT:0> ] ) <EOL> X = X [ permIDs ] <EOL> TrueZ = TrueZ [ permIDs ] <EOL> return X , TrueZ <EOL> def plotImgPatchPrototypes ( doShowNow = True ) : <EOL> from matplotlib import pylab <EOL> pylab . figure ( ) <EOL> for kk in range ( K ) : <EOL> pylab . subplot ( <NUM_LIT:2> , <NUM_LIT:4> , kk + <NUM_LIT:1> ) <EOL> Xp = makeImgPatchPrototype ( D , kk ) <EOL> pylab . imshow ( Xp , interpolation = '<STR_LIT>' ) <EOL> if doShowNow : <EOL> pylab . show ( ) <EOL> def plotTrueCovMats ( doShowNow = True ) : <EOL> from matplotlib import pylab <EOL> pylab . figure ( ) <EOL> for kk in range ( K ) : <EOL> pylab . subplot ( <NUM_LIT:2> , <NUM_LIT:4> , kk + <NUM_LIT:1> ) <EOL> pylab . imshow ( Sigma [ kk ] , interpolation = '<STR_LIT>' ) <EOL> if doShowNow : <EOL> pylab . show ( ) </s>
