From bdc0b7fbe10aa9b8787d9dcd348d3b7d829c0763 Mon Sep 17 00:00:00 2001 From: Chris MacLellan Date: Thu, 3 Jan 2019 14:26:11 -0500 Subject: [PATCH 1/4] fixed the iteration print counter and cases where 0 / 0 should just be 0 rather than NaN. --- hmms/cthmm.pyx | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/hmms/cthmm.pyx b/hmms/cthmm.pyx index 06667b9..a7b7acc 100755 --- a/hmms/cthmm.pyx +++ b/hmms/cthmm.pyx @@ -814,16 +814,15 @@ cdef class CtHMM(hmm.HMM): if i == j: - tA /= self._pt[ ix ] + tA = numpy.divide(tA, self._pt[ ix ], out=numpy.zeros_like(tA), where=tA!=0) tau[i] += numpy.exp( self.log_sum( (ksi_sum[ix] + numpy.log( tA ) ).flatten() ) ) #tau is not in log prob anymore. else: tA *= self._q[i,j] - tA /= self._pt[ ix ] + tA = numpy.divide(tA, self._pt[ ix ], out=numpy.zeros_like(tA), where=tA!=0) eta[i,j] += numpy.exp( self.log_sum( (ksi_sum[ix] + numpy.log( tA ) ).flatten() ) ) #eta is not in log prob anymore. #Update parameter Q: - self._q = ( eta.T / tau ).T self._q = numpy.nan_to_num(self._q) # nan can appear, when some of the states is not reachable @@ -934,8 +933,7 @@ cdef class CtHMM(hmm.HMM): for it in range( iterations ): - print("iteration ", i+1, "/", iterations ) - + print("iteration ", it+1, "/", iterations ) self._prepare_matrices_pt( times ) @@ -1033,14 +1031,15 @@ cdef class CtHMM(hmm.HMM): if i == j: - tA /= self._pt[ ix ] + tA = numpy.divide(tA, self._pt[ ix ], out=numpy.zeros_like(tA), where=tA!=0) tau[i] += numpy.exp( self.log_sum( (ksi_sum[ix] + numpy.log( tA ) ).flatten() ) ) #tau is not in log prob anymore. else: tA *= self._q[i,j] - tA /= self._pt[ ix ] + tA = numpy.divide(tA, self._pt[ ix ], out=numpy.zeros_like(tA), where=tA!=0) + eta[i,j] += numpy.exp( self.log_sum( (ksi_sum[ix] + numpy.log( tA ) ).flatten() ) ) #eta is not in log prob anymore. #Update parameters: @@ -1052,12 +1051,10 @@ cdef class CtHMM(hmm.HMM): #jump rates matrice self._q = ( eta.T / tau ).T - - self._q = numpy.nan_to_num(self._q) # nan can appear, when some of the states is not reachable - if sum( self._q.flatten() ) == 0: - raise ValueError("Parameter error! Matrix Q can't contain unreachable states.") + # if sum( self._q.flatten() ) == 0: + # raise ValueError("Parameter error! Matrix Q can't contain unreachable states.") for i in range( s_num ): self._q[i,i] = - numpy.sum( self._q[i,:] ) From c37acb45ab2c6aa88082c25de2a1475929deb796 Mon Sep 17 00:00:00 2001 From: cmaclell Date: Thu, 3 Jan 2019 14:34:22 -0500 Subject: [PATCH 2/4] removed value error messages, which are not an issue now that we sometimes don't have transitions out of states. --- hmms/cthmm.pyx | 4 ---- 1 file changed, 4 deletions(-) diff --git a/hmms/cthmm.pyx b/hmms/cthmm.pyx index a7b7acc..6d11dfb 100755 --- a/hmms/cthmm.pyx +++ b/hmms/cthmm.pyx @@ -826,10 +826,6 @@ cdef class CtHMM(hmm.HMM): self._q = ( eta.T / tau ).T self._q = numpy.nan_to_num(self._q) # nan can appear, when some of the states is not reachable - - if sum( self._q.flatten() ) == 0: - raise ValueError("Parameter error! Matrix Q can't contain unreachable states.") - for i in range( s_num ): self._q[i,i] = - numpy.sum( self._q[i,:] ) From d239b65f9b7bf8da2924178d7c5d7bc487526fc8 Mon Sep 17 00:00:00 2001 From: cmaclell Date: Thu, 3 Jan 2019 14:49:29 -0500 Subject: [PATCH 3/4] removed commented out lines. --- hmms/cthmm.pyx | 3 --- 1 file changed, 3 deletions(-) diff --git a/hmms/cthmm.pyx b/hmms/cthmm.pyx index 6d11dfb..97185a5 100755 --- a/hmms/cthmm.pyx +++ b/hmms/cthmm.pyx @@ -1049,9 +1049,6 @@ cdef class CtHMM(hmm.HMM): self._q = numpy.nan_to_num(self._q) # nan can appear, when some of the states is not reachable - # if sum( self._q.flatten() ) == 0: - # raise ValueError("Parameter error! Matrix Q can't contain unreachable states.") - for i in range( s_num ): self._q[i,i] = - numpy.sum( self._q[i,:] ) From 1f3c112406910a80dbba8aa61ccbc551c1685b57 Mon Sep 17 00:00:00 2001 From: cmaclell Date: Thu, 3 Jan 2019 15:41:11 -0500 Subject: [PATCH 4/4] removed all the printing --- hmms/cthmm.pyx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/hmms/cthmm.pyx b/hmms/cthmm.pyx index 97185a5..c2c4559 100755 --- a/hmms/cthmm.pyx +++ b/hmms/cthmm.pyx @@ -739,7 +739,7 @@ cdef class CtHMM(hmm.HMM): for it in range( iterations ): - print("it",it) + # print("it",it) self._prepare_matrices_pt( t_seqs ) @@ -929,7 +929,7 @@ cdef class CtHMM(hmm.HMM): for it in range( iterations ): - print("iteration ", it+1, "/", iterations ) + # print("iteration ", it+1, "/", iterations ) self._prepare_matrices_pt( times )