Ejemplo n.º 1
0
def test_rand():
    n = 5
    m = 4
    d = 2
    atmp = numpy.random.random_sample((n, n))
    row_sums = atmp.sum(axis=1)
    a = numpy.array(atmp / row_sums[:, numpy.newaxis], dtype=numpy.double)    

    wtmp = numpy.random.random_sample((n, m))
    row_sums = wtmp.sum(axis=1)
    w = numpy.array(wtmp / row_sums[:, numpy.newaxis], dtype=numpy.double)
    
    means = numpy.array((0.6 * numpy.random.random_sample((n, m, d)) - 0.3), dtype=numpy.double)
    covars = numpy.zeros( (n,m,d,d) )
    
    for i in range(n):
        for j in range(m):
            for k in range(d):
                covars[i][j][k][k] = 1    
    
    pitmp = numpy.random.random_sample((n))
    pi = numpy.array(pitmp / sum(pitmp), dtype=numpy.double)

    gmmhmm = GMHMM(n,m,d,a,means,covars,w,pi,init_type='user',verbose=True)
    
    obs = numpy.array((0.6 * numpy.random.random_sample((40,d)) - 0.3), dtype=numpy.double)
    
    print("Doing Baum-welch")
    gmmhmm.train(obs,1000)
    print()
    print("Pi",gmmhmm.pi)
    print("A",gmmhmm.A)
    print("weights", gmmhmm.w)
    print("means", gmmhmm.means)
    print("covars", gmmhmm.covars)
Ejemplo n.º 2
0
Archivo: tests.py Proyecto: MTG/HMM
def test_rand():
    n = 5
    m = 9
    d = 2
    atmp = numpy.random.random_sample((n, n))
    row_sums = atmp.sum(axis=1)
    a = numpy.array(atmp / row_sums[:, numpy.newaxis], dtype=numpy.double)    

    wtmp = numpy.random.random_sample((n, m))
    row_sums = wtmp.sum(axis=1)
    w = numpy.array(wtmp / row_sums[:, numpy.newaxis], dtype=numpy.double)
    
    means = numpy.array((0.6 * numpy.random.random_sample((n, m, d)) - 0.3), dtype=numpy.double)
    covars = numpy.zeros( (n,m,d,d) )
    
    for i in xrange(n):
        for j in xrange(m):
            for k in xrange(d):
                covars[i][j][k][k] = 1    
    
    pitmp = numpy.random.random_sample((n))
    pi = numpy.array(pitmp / sum(pitmp), dtype=numpy.double)

    gmmhmm = GMHMM(n,m,d,a,means,covars,w,pi,init_type='user',verbose=True)
    
    obs = numpy.array((0.6 * numpy.random.random_sample((40,d)) - 0.3), dtype=numpy.double)
    
    print "Doing Baum-welch"
    gmmhmm.train(obs,1000)
    print
    print "Pi",gmmhmm.pi
    print "A",gmmhmm.A
    print "weights", gmmhmm.w
    print "means", gmmhmm.means
    print "covars", gmmhmm.covars
Ejemplo n.º 3
0
    def _constructHmmNetwork(self,  numStates, ALPHA, withModels ):
        '''
        top level-function: costruct self.hmmNEtwork that confirms to guyz's code 
        '''
        
    #     sequencePhonemes = sequencePhonemes[0:4]
        
        
        ######## construct transition matrix
        #######
        if not WITH_DURATIONS:
            transMAtrix = self._constructTransMatrixHMMNetwork(self.lyricsWithModels.phonemesNetwork)

#        DEBUG
#  writeListOfListToTextFile(transMAtrix, None , '/Users/joro/Documents/Phd/UPF/voxforge/myScripts/AlignmentStep/transMatrix')
        
        # construct means, covars, and all the rest params
        #########
       
        if numStates == None:
            numStates = len(self.lyricsWithModels.statesNetwork) 
        
        means, covars, weights, pi = self._constructHMMNetworkParameters(numStates,  withModels)
        
        if  WITH_DURATIONS:
            self.hmmNetwork = GMHMM(numStates,numMixtures,numDimensions,None,means,covars,weights,pi,init_type='user',verbose=True)
            self.hmmNetwork.setALPHA(ALPHA)
        else:
            self.hmmNetwork = GMHMM(numStates,numMixtures,numDimensions,transMAtrix,means,covars,weights,pi,init_type='user',verbose=True)
Ejemplo n.º 4
0
 def __init__(self,
              n,
              m,
              d=1,
              A=None,
              means=None,
              covars=None,
              w=None,
              pi=None,
              min_std=0.01,
              init_type='uniform',
              precision=numpy.double,
              verbose=False):
     GMHMM.__init__(self, n, m, d, A, means, covars, w, pi, min_std,
                    init_type, precision, verbose)
Ejemplo n.º 5
0
    def _constructHmmNetwork(self, numStates, ALPHA, withModels):
        '''
        top level-function: costruct self.hmmNEtwork that confirms to guyz's code 
        '''

        #     sequencePhonemes = sequencePhonemes[0:4]

        ######## construct transition matrix
        #######
        if not WITH_DURATIONS:
            transMAtrix = self._constructTransMatrixHMMNetwork(
                self.lyricsWithModels.phonemesNetwork)

#        DEBUG
#  writeListOfListToTextFile(transMAtrix, None , '/Users/joro/Documents/Phd/UPF/voxforge/myScripts/AlignmentStep/transMatrix')

# construct means, covars, and all the rest params
#########

        if numStates == None:
            numStates = len(self.lyricsWithModels.statesNetwork)

        means, covars, weights, pi = self._constructHMMNetworkParameters(
            numStates, withModels)

        if WITH_DURATIONS:
            self.hmmNetwork = GMHMM(numStates,
                                    numMixtures,
                                    numDimensions,
                                    None,
                                    means,
                                    covars,
                                    weights,
                                    pi,
                                    init_type='user',
                                    verbose=True)
            self.hmmNetwork.setALPHA(ALPHA)
        else:
            self.hmmNetwork = GMHMM(numStates,
                                    numMixtures,
                                    numDimensions,
                                    transMAtrix,
                                    means,
                                    covars,
                                    weights,
                                    pi,
                                    init_type='user',
                                    verbose=True)
Ejemplo n.º 6
0
def test_simple():
    n = 2
    m = 2
    d = 2
    pi = numpy.array([0.5, 0.5])
    A = numpy.ones((n, n), dtype=numpy.double) / float(n)

    w = numpy.ones((n, m), dtype=numpy.double)
    means = numpy.ones((n, m, d), dtype=numpy.double)
    covars = [[numpy.matrix(numpy.eye(d, d)) for j in xrange(m)]
              for i in xrange(n)]

    w[0][0] = 0.5
    w[0][1] = 0.5
    w[1][0] = 0.5
    w[1][1] = 0.5
    means[0][0][0] = 0.5
    means[0][0][1] = 0.5
    means[0][1][0] = 0.5
    means[0][1][1] = 0.5
    means[1][0][0] = 0.5
    means[1][0][1] = 0.5
    means[1][1][0] = 0.5
    means[1][1][1] = 0.5

    gmmhmm = GMHMM(n,
                   m,
                   d,
                   A,
                   means,
                   covars,
                   w,
                   pi,
                   init_type='user',
                   verbose=True)

    obs = numpy.array([[0.3, 0.3], [0.1, 0.1], [0.2, 0.2]])

    print "Doing Baum-welch"
    gmmhmm.train(obs, 10)
    print
    print "Pi", gmmhmm.pi
    print "A", gmmhmm.A
    print "weights", gmmhmm.w
    print "means", gmmhmm.means
    print "covars", gmmhmm.covars
Ejemplo n.º 7
0
    def __init__(self,
                 n,
                 m,
                 d=1,
                 A=None,
                 means=None,
                 covars=None,
                 w=None,
                 pi=None,
                 min_std=0.01,
                 init_type='uniform',
                 precision=numpy.double,
                 verbose=False):
        print(
            "Warning: weighted EMs may not converge to local optima, since the log-likelihood function may decrease."
        )
        print()

        GMHMM.__init__(self, n, m, d, A, means, covars, w, pi, min_std,
                       init_type, precision, verbose)  #@UndefinedVariable
        Linear.__init__(self)
Ejemplo n.º 8
0
Archivo: tests.py Proyecto: MTG/HMM
def test_simple():
    n = 2
    m = 2
    d = 2
    pi = numpy.array([0.5, 0.5])
    A = numpy.ones((n,n),dtype=numpy.double)/float(n)
    
    w = numpy.ones((n,m),dtype=numpy.double)
    means = numpy.ones((n,m,d),dtype=numpy.double)
    covars = [[ numpy.matrix(numpy.eye(d,d)) for j in xrange(m)] for i in xrange(n)]
    
    w[0][0] = 0.5
    w[0][1] = 0.5
    w[1][0] = 0.5
    w[1][1] = 0.5    
    means[0][0][0] = 0.5
    means[0][0][1] = 0.5
    means[0][1][0] = 0.5    
    means[0][1][1] = 0.5
    means[1][0][0] = 0.5
    means[1][0][1] = 0.5
    means[1][1][0] = 0.5    
    means[1][1][1] = 0.5    

    gmmhmm = GMHMM(n,m,d,A,means,covars,w,pi,init_type='user',verbose=True)
    
    obs = numpy.array([ [0.3,0.3], [0.1,0.1], [0.2,0.2]])
    
    print "Doing Baum-welch"
    gmmhmm.train(obs,10)
    print
    print "Pi",gmmhmm.pi
    print "A",gmmhmm.A
    print "weights", gmmhmm.w
    print "means", gmmhmm.means
    print "covars", gmmhmm.covars
Ejemplo n.º 9
0
    def trainModel(self, obs):
        pi = numpy.array([0.2, 0.2, 0.2, 0.2, 0.2])
        A = numpy.ones((self.n, self.n), dtype=numpy.double) / float(self.n)

        w = numpy.ones((self.n, self.m), dtype=numpy.double)
        means = numpy.ones((self.n, self.m, self.d), dtype=numpy.double)
        covars = [[
            numpy.matrix(numpy.eye(self.d, self.d)) for j in xrange(self.m)
        ] for i in xrange(self.n)]
        n_iter = 20
        '''w[0][0] = 0.5
        w[0][1] = 0.5
        w[1][0] = 0.5
        w[1][1] = 0.5    
        means[0][0][0] = 0.5
        means[0][0][1] = 0.5
        means[0][1][0] = 0.5    
        means[0][1][1] = 0.5
        means[1][0][0] = 0.5
        means[1][0][1] = 0.5
        means[1][1][0] = 0.5    
        means[1][1][1] = 0.5 '''

        gmmhmm = GMHMM(self.n,
                       self.m,
                       self.d,
                       A,
                       means,
                       covars,
                       w,
                       pi,
                       init_type='user',
                       verbose=True)

        print "Doing Baum-welch"
        #gmmhmm.train(obs,10)
        if len(obs.shape) == 2:
            gmmhmm.train(obs)
            return self

        elif len(obs.shape) == 3:
            count = obs.shape[0]
            for n in range(count):
                gmmhmm.train(obs[n, :, :])
                return self
Ejemplo n.º 10
0
# w[1][0] = 0.5
# w[1][1] = 0.5

# btmp = np.random.random_sample((N, M))
# row_sums = btmp.sum(axis=1)
# B = btmp / row_sums[:, np.newaxis]
# print("Initial B: ", B)

print("HMM node started... Call a service")
#print("--- Check rows sum up to 1 ---")

HMM_model = GMHMM(N,
                  M,
                  D,
                  A,
                  means,
                  covars,
                  w,
                  pi,
                  init_type='user',
                  verbose=True)


def get_training_set_srv(req):
    print("Start training requested")
    print("Run a bag...")
    global train_hmm
    train_hmm = True
    return


def start_training_srv(req):
Ejemplo n.º 11
0
            model.transmat_prior = model.transmat_

            model.fit(obs)

            pi = model.startprob_
            A = model.transmat_
            w = numpy.ones((n, m), dtype=numpy.double)
            hmm_means = numpy.ones((n, m, d), dtype=numpy.double)
            hmm_means[0][0] = model.means_[0]
            hmm_means[1][0] = model.means_[1]
            hmm_means[2][0] = model.means_[2]
            hmm_covars = numpy.array([[ numpy.matrix(numpy.eye(d,d)) for j in xrange(m)] for i in xrange(n)])
            hmm_covars[0][0] = model.covars_[0]
            hmm_covars[1][0] = model.covars_[1]
            hmm_covars[2][0] = model.covars_[2]
            gmmhmm = GMHMM(n,m,d,A,hmm_means,hmm_covars,w,pi,init_type='user',verbose=False)

            # hidden_state = model.predict(obs)
            hidden_state = gmmhmm.decode(obs)

            mean_sequence = [None] * len(obs)
            var_sequence = [None] * len(obs)
            for i in range(len(obs)):
                mean_sequence[i] = model.means_[hidden_state[i]]
                var_sequence[i] = numpy.diag(model.covars_[hidden_state[i]])

            means.append(mean_sequence)
            vars.append(var_sequence)
            hiddens.append(hidden_state)
        except:
            means.append(None)
Ejemplo n.º 12
0
            w = numpy.ones((n, m), dtype=numpy.double)
            hmm_means = numpy.ones((n, m, d), dtype=numpy.double)
            hmm_means[0][0] = model.means_[0]
            hmm_means[1][0] = model.means_[1]
            hmm_means[2][0] = model.means_[2]
            hmm_covars = numpy.array(
                [[numpy.matrix(numpy.eye(d, d)) for j in xrange(m)]
                 for i in xrange(n)])
            hmm_covars[0][0] = model.covars_[0]
            hmm_covars[1][0] = model.covars_[1]
            hmm_covars[2][0] = model.covars_[2]
            gmmhmm = GMHMM(n,
                           m,
                           d,
                           A,
                           hmm_means,
                           hmm_covars,
                           w,
                           pi,
                           init_type='user',
                           verbose=False)

            # hidden_state = model.predict(obs)
            hidden_state = gmmhmm.decode(obs)

            mean_sequence = [None] * len(obs)
            var_sequence = [None] * len(obs)
            for i in range(len(obs)):
                mean_sequence[i] = model.means_[hidden_state[i]]
                var_sequence[i] = numpy.diag(model.covars_[hidden_state[i]])

            means.append(mean_sequence)
Ejemplo n.º 13
0
class Decoder(object):
    '''
    holds structures used in decoding and decoding result
    '''
    def __init__(self,
                 lyricsWithModels,
                 ALPHA,
                 numStates=None,
                 withModels=True):
        '''
        Constructor
        '''
        self.lyricsWithModels = lyricsWithModels
        '''
        of class HMM
        '''
        self.hmmNetwork = []

        self._constructHmmNetwork(numStates, float(ALPHA), withModels)
        self.hmmNetwork.logger.setLevel(loggingLevel)

        # Path class object
        self.path = None

    def decodeAudio(self, observationFeatures, usePersistentFiles,
                    URI_recording_noExt, listDurations):
        ''' decode path for given exatrcted features for audio
        HERE is decided which decoding scheme: with or without duration (based on WITH_DURATION parameter)
        '''
        if self.lyricsWithModels.ONLY_MIDDLE_STATE:
            URI_bmap = URI_recording_noExt + '.bmap_onlyMiddleState'
        else:
            URI_bmap = URI_recording_noExt + '.bmap'

        self.hmmNetwork.setPersitentFiles(usePersistentFiles, URI_bmap)

        # double check that features are in same dimension as model
        if observationFeatures.shape[1] != numDimensions:
            sys.exit(
                "dimension of feature vector should be {} but is {} ".format(
                    numDimensions, observationFeatures.shape[1]))
#         observationFeatures = observationFeatures[0:100,:]

        if WITH_DURATIONS:

            # transMatrix for 0 state which is silence
            transMatrix = self.lyricsWithModels.phonemesNetwork[
                0].getTransMatrix()
            self.hmmNetwork.setWaitProbSilState(transMatrix[2, 2])

            self.hmmNetwork.setDurForStates(listDurations)

#         if os.path.exists(PATH_CHI) and os.path.exists(PATH_PSI):
#             chiBackPointer = numpy.loadtxt(PATH_CHI)
#             psiBackPointer = numpy.loadtxt(PATH_PSI)
#
#         else:

# standard viterbi forced alignment
        if not WITH_DURATIONS:
            path_, psi, delta = self.hmmNetwork._viterbiForced(
                observationFeatures)
            self.path = Path(None, None)
            self.path.setPatRaw(path_)

        # duration-HMM
        else:

            chiBackPointer, psiBackPointer = self.hmmNetwork._viterbiForcedDur(
                observationFeatures)

            #             writeListOfListToTextFile(chiBackPointer, None , PATH_CHI)
            #             writeListOfListToTextFile(psiBackPointer, None , PATH_PSI)

            self.path = Path(chiBackPointer, psiBackPointer)
            print "\n"
        # DEBUG
#         self.path.printDurations()
#         writeListToTextFile(self.path.pathRaw, None , '/tmp/path')

    def _constructHmmNetwork(self, numStates, ALPHA, withModels):
        '''
        top level-function: costruct self.hmmNEtwork that confirms to guyz's code 
        '''

        #     sequencePhonemes = sequencePhonemes[0:4]

        ######## construct transition matrix
        #######
        if not WITH_DURATIONS:
            transMAtrix = self._constructTransMatrixHMMNetwork(
                self.lyricsWithModels.phonemesNetwork)

#        DEBUG
#  writeListOfListToTextFile(transMAtrix, None , '/Users/joro/Documents/Phd/UPF/voxforge/myScripts/AlignmentStep/transMatrix')

# construct means, covars, and all the rest params
#########

        if numStates == None:
            numStates = len(self.lyricsWithModels.statesNetwork)

        means, covars, weights, pi = self._constructHMMNetworkParameters(
            numStates, withModels)

        if WITH_DURATIONS:
            self.hmmNetwork = GMHMM(numStates,
                                    numMixtures,
                                    numDimensions,
                                    None,
                                    means,
                                    covars,
                                    weights,
                                    pi,
                                    init_type='user',
                                    verbose=True)
            self.hmmNetwork.setALPHA(ALPHA)
        else:
            self.hmmNetwork = GMHMM(numStates,
                                    numMixtures,
                                    numDimensions,
                                    transMAtrix,
                                    means,
                                    covars,
                                    weights,
                                    pi,
                                    init_type='user',
                                    verbose=True)

    def _constructTransMatrixHMMNetwork(self, sequencePhonemes):
        '''
        tranform other htkModel params to  format of gyuz's hmm class
        take from sequencePhonemes' attached htk models the transprobs.
        '''
        # just for initialization totalNumPhonemes
        totalNumStates = 0
        for phoneme in sequencePhonemes:
            currNumStates = phoneme.htkModel.tmat.numStates - 2
            totalNumStates += currNumStates

        transMAtrix = numpy.zeros((totalNumStates, totalNumStates),
                                  dtype=numpy.double)

        counterOverallStateNum = 0

        for phoneme in sequencePhonemes:
            currNumStates = phoneme.htkModel.tmat.numStates - 2

            #         disregard 1st and last states from transMat because they are the non-emitting states
            currTransMat = phoneme.getTransMatrix()

            transMAtrix[counterOverallStateNum:counterOverallStateNum +
                        currNumStates,
                        counterOverallStateNum:counterOverallStateNum +
                        currNumStates] = currTransMat[1:-1, 1:-1]

            # transition probability to next state
            #         TODO: here multiply by [0,1] matrix next state. check if it exists
            nextStateTransition = 1

            if (counterOverallStateNum + currNumStates) < transMAtrix.shape[1]:
                val = currTransMat[-2, -1] * nextStateTransition
                transMAtrix[counterOverallStateNum + currNumStates - 1,
                            counterOverallStateNum + currNumStates] = val

            # increment in final trans matrix
            counterOverallStateNum += currNumStates

        return transMAtrix

    def _constructHMMNetworkParameters(self,
                                       numStates,
                                       withModels=True,
                                       sequenceStates=None):
        '''
        tranform other htkModel params to  format of gyuz's hmm class
        '''

        means = numpy.empty((numStates, numMixtures, numDimensions))

        # init covars
        covars = [[
            numpy.matrix(numpy.eye(numDimensions, numDimensions))
            for j in xrange(numMixtures)
        ] for i in xrange(numStates)]

        weights = numpy.ones((numStates, numMixtures), dtype=numpy.double)

        # start probs :
        pi = numpy.zeros((numStates), dtype=numpy.double)

        # avoid log(0)
        pi.fill(sys.float_info.min)
        #          allow to start only at first state
        pi[0] = 1

        # equal prob. for states to start
        #         pi = numpy.ones( (numStates)) *(1.0/numStates)

        if not withModels:
            return None, None, None, pi

        sequenceStates = self.lyricsWithModels.statesNetwork

        if sequenceStates == None:
            sys.exit('no state sequence')

        for i in range(len(sequenceStates)):
            state = sequenceStates[i]

            for (numMixture, weight, mixture) in state.mixtures:

                weights[i, numMixture - 1] = weight

                means[i, numMixture - 1, :] = mixture.mean.vector

                variance_ = mixture.var.vector
                for k in range(len(variance_)):
                    covars[i][numMixture - 1][k, k] = variance_[k]
        return means, covars, weights, pi

    def path2ResultWordList(self):
        '''
        makes sense of path indices : maps numbers to states and phonemes, uses self.lyricsWithModels.statesNetwork and self.lyricsWithModels.listWords) 
        to be called after decoding
        '''
        # indices in pathRaw
        self.path._path2stateIndices()

        #sanity check
        numStates = len(self.lyricsWithModels.statesNetwork)
        numdecodedStates = len(self.path.indicesStateStarts)

        if numStates != numdecodedStates:
            logging.warn(
                "detected path has {} states, but stateNetwork transcript has {} states"
                .format(numdecodedStates, numStates))
            # num misssed states in the beginning of the path
            howManyMissedStates = numStates - numdecodedStates
            # WORKAROUND: assume missed states start at time 0
            for i in range(howManyMissedStates):
                self.path.indicesStateStarts[:0] = [0]
        dummy = 0
        detectedWordList = expandlyrics2Words(
            self.lyricsWithModels, self.path, dummy,
            _constructTimeStampsForWordDetected)

        return detectedWordList
Ejemplo n.º 14
0
class Decoder(object):
    '''
    holds structures used in decoding and decoding result
    '''


    def __init__(self, lyricsWithModels, ALPHA,  numStates=None, withModels=True):
        '''
        Constructor
        '''
        self.lyricsWithModels = lyricsWithModels
        
        '''
        of class HMM
        '''
        self.hmmNetwork = []
                
        
        self._constructHmmNetwork(numStates, float(ALPHA), withModels)
        self.hmmNetwork.logger.setLevel(loggingLevel)
        
        # Path class object
        self.path = None
    
    def decodeAudio( self, observationFeatures, usePersistentFiles, URI_recording_noExt, listDurations):
        ''' decode path for given exatrcted features for audio
        HERE is decided which decoding scheme: with or without duration (based on WITH_DURATION parameter)
        '''
        if self.lyricsWithModels.ONLY_MIDDLE_STATE:
            URI_bmap = URI_recording_noExt + '.bmap_onlyMiddleState'
        else:             
            URI_bmap = URI_recording_noExt + '.bmap'
        
        
        
        self.hmmNetwork.setPersitentFiles( usePersistentFiles, URI_bmap )
        
        # double check that features are in same dimension as model
        if observationFeatures.shape[1] != numDimensions:
            sys.exit("dimension of feature vector should be {} but is {} ".format(numDimensions, observationFeatures.shape[1]) )
#         observationFeatures = observationFeatures[0:100,:]
        
        if  WITH_DURATIONS:
            
            # transMatrix for 0 state which is silence
            transMatrix = self.lyricsWithModels.phonemesNetwork[0].getTransMatrix()
            self.hmmNetwork.setWaitProbSilState(transMatrix[2,2])
            
            self.hmmNetwork.setDurForStates(listDurations)
        
#         if os.path.exists(PATH_CHI) and os.path.exists(PATH_PSI): 
#             chiBackPointer = numpy.loadtxt(PATH_CHI)
#             psiBackPointer = numpy.loadtxt(PATH_PSI)
#                
#         else:

        # standard viterbi forced alignment
        if not WITH_DURATIONS:
            path_, psi, delta = self.hmmNetwork._viterbiForced(observationFeatures)
            self.path =  Path(None, None)
            self.path.setPatRaw(path_)
            
        # duration-HMM
        else:
        
            chiBackPointer, psiBackPointer = self.hmmNetwork._viterbiForcedDur(observationFeatures)
            
#             writeListOfListToTextFile(chiBackPointer, None , PATH_CHI)
#             writeListOfListToTextFile(psiBackPointer, None , PATH_PSI)
                
            self.path =  Path(chiBackPointer, psiBackPointer)
            print "\n"
         # DEBUG
#         self.path.printDurations()
#         writeListToTextFile(self.path.pathRaw, None , '/tmp/path')
        
    
    
    
    
        
    def _constructHmmNetwork(self,  numStates, ALPHA, withModels ):
        '''
        top level-function: costruct self.hmmNEtwork that confirms to guyz's code 
        '''
        
    #     sequencePhonemes = sequencePhonemes[0:4]
        
        
        ######## construct transition matrix
        #######
        if not WITH_DURATIONS:
            transMAtrix = self._constructTransMatrixHMMNetwork(self.lyricsWithModels.phonemesNetwork)

#        DEBUG
#  writeListOfListToTextFile(transMAtrix, None , '/Users/joro/Documents/Phd/UPF/voxforge/myScripts/AlignmentStep/transMatrix')
        
        # construct means, covars, and all the rest params
        #########
       
        if numStates == None:
            numStates = len(self.lyricsWithModels.statesNetwork) 
        
        means, covars, weights, pi = self._constructHMMNetworkParameters(numStates,  withModels)
        
        if  WITH_DURATIONS:
            self.hmmNetwork = GMHMM(numStates,numMixtures,numDimensions,None,means,covars,weights,pi,init_type='user',verbose=True)
            self.hmmNetwork.setALPHA(ALPHA)
        else:
            self.hmmNetwork = GMHMM(numStates,numMixtures,numDimensions,transMAtrix,means,covars,weights,pi,init_type='user',verbose=True)


        
    def  _constructTransMatrixHMMNetwork(self, sequencePhonemes):
        '''
        tranform other htkModel params to  format of gyuz's hmm class
        take from sequencePhonemes' attached htk models the transprobs.
        '''
        # just for initialization totalNumPhonemes
        totalNumStates = 0
        for phoneme in sequencePhonemes:
            currNumStates = phoneme.htkModel.tmat.numStates - 2
            totalNumStates += currNumStates
            
        transMAtrix = numpy.zeros((totalNumStates, totalNumStates), dtype=numpy.double)
        
        
        counterOverallStateNum = 0 
        
        for phoneme in sequencePhonemes:
            currNumStates =   phoneme.htkModel.tmat.numStates - 2
            
    #         disregard 1st and last states from transMat because they are the non-emitting states
            currTransMat = phoneme.getTransMatrix()
            
            transMAtrix[counterOverallStateNum : counterOverallStateNum + currNumStates, counterOverallStateNum : counterOverallStateNum + currNumStates ] = currTransMat[1:-1,1:-1]
           
            # transition probability to next state
            #         TODO: here multiply by [0,1] matrix next state. check if it exists
            nextStateTransition = 1
            
            if (counterOverallStateNum + currNumStates) < transMAtrix.shape[1]:
                val = currTransMat[-2,-1] * nextStateTransition
                transMAtrix[counterOverallStateNum + currNumStates -1, counterOverallStateNum + currNumStates] =  val
    
    
            # increment in final trans matrix  
            counterOverallStateNum +=currNumStates
            
            
        return transMAtrix
    

    
    def _constructHMMNetworkParameters(self,  numStates,  withModels=True, sequenceStates=None):
        '''
        tranform other htkModel params to  format of gyuz's hmm class
        '''
        
       
        
        means = numpy.empty((numStates, numMixtures, numDimensions))
        
        # init covars
        covars = [[ numpy.matrix(numpy.eye(numDimensions,numDimensions)) for j in xrange(numMixtures)] for i in xrange(numStates)]
        
        weights = numpy.ones((numStates,numMixtures),dtype=numpy.double)
        
        # start probs :
        pi = numpy.zeros((numStates), dtype=numpy.double)
        
        # avoid log(0) 
        pi.fill(sys.float_info.min)
#          allow to start only at first state
        pi[0] = 1
        
        # equal prob. for states to start
#         pi = numpy.ones( (numStates)) *(1.0/numStates)
        
        if not withModels:
            return None, None, None, pi

        
        sequenceStates = self.lyricsWithModels.statesNetwork
         
        if sequenceStates==None:
            sys.exit('no state sequence')
               
        for i in range(len(sequenceStates) ):
            state  = sequenceStates[i] 
            
            for (numMixture, weight, mixture) in state.mixtures:
                
                weights[i,numMixture-1] = weight
                
                means[i,numMixture-1,:] = mixture.mean.vector
                
                variance_ = mixture.var.vector
                for k in  range(len( variance_) ):
                    covars[i][numMixture-1][k,k] = variance_[k]
        return means, covars, weights, pi
    
            
      
        
        
    def path2ResultWordList(self):
        '''
        makes sense of path indices : maps numbers to states and phonemes, uses self.lyricsWithModels.statesNetwork and self.lyricsWithModels.listWords) 
        to be called after decoding
        '''
        # indices in pathRaw
        self.path._path2stateIndices()
        
        #sanity check
        numStates = len(self.lyricsWithModels.statesNetwork)
        numdecodedStates = len(self.path.indicesStateStarts)
        
        if numStates != numdecodedStates:
            logging.warn("detected path has {} states, but stateNetwork transcript has {} states".format( numdecodedStates, numStates ) )
            # num misssed states in the beginning of the path
            howManyMissedStates = numStates - numdecodedStates
            # WORKAROUND: assume missed states start at time 0
            for i in range(howManyMissedStates):
                self.path.indicesStateStarts[:0] = [0]
        dummy= 0
        detectedWordList = expandlyrics2Words (self.lyricsWithModels, self.path, dummy, _constructTimeStampsForWordDetected)
        
        return detectedWordList