# HG changeset patch
# User William Stein <wstein@gmail.com>
# Date 1217853030 25200
# Node ID 1dfe749d493139b7326fb0e18d10466cd9e7b04b
# Parent bc0e714d457ea08162fa5a4e036b8ee42bf555ce
trac #3726  add pickling for HMM's and fix a segfault with name for discrete HMM.
diff r bc0e714d457e r 1dfe749d4931 sage/stats/hmm/chmm.pyx
a

b

cdef class GaussianHiddenMarkovModel(Con 
220  220  self.m.class_change = NULL 
221  221  
222  222  self.initialized = True 
 223  
 224  def __reduce__(self): 
 225  """ 
 226  Used in pickling. 
 227  
 228  EXAMPLES: 
 229  sage: m = hmm.GaussianHiddenMarkovModel([[1]], [(0,1)], [1], 'test') 
 230  sage: f,g = m.__reduce__() 
 231  sage: f(*g) == m 
 232  True 
 233  """ 
 234  return unpickle_gaussian_hmm_v0, (self.transition_matrix(), self.emission_parameters(), 
 235  self.initial_probabilities(), self.name()) 
223  236  
224  237  def __dealloc__(self): 
225  238  """ 
… 
… 
cdef ghmm_cseq* to_cseq(seq) except NULL 
688  701  memcpy(sqd.seq[i], T._values , sizeof(double)*T._length) 
689  702  
690  703  return sqd 
 704  
 705  def unpickle_gaussian_hmm_v0(A, B, pi, name): 
 706  """ 
 707  EXAMPLES: 
 708  sage: m = hmm.GaussianHiddenMarkovModel([[1]], [(0,1)], [1], 'test') 
 709  sage: loads(dumps(m)) == m 
 710  True 
 711  sage: sage.stats.hmm.chmm.unpickle_gaussian_hmm_v0(m.transition_matrix(), m.emission_parameters(), m.initial_probabilities(), 'test') 
 712  Gaussian Hidden Markov Model test with 1 States 
 713  Transition matrix: 
 714  [1.0] 
 715  Emission parameters: 
 716  [(0.0, 1.0)] 
 717  Initial probabilities: [1.0] 
 718  """ 
 719  return GaussianHiddenMarkovModel(A,B,pi,name) 
diff r bc0e714d457e r 1dfe749d4931 sage/stats/hmm/hmm.pyx
a

b

cdef class DiscreteHiddenMarkovModel(Hid 
140  140  # Assign model identifier if specified 
141  141  if name is not None: 
142  142  name = str(name) 
143   self.m.name = name 
 143  self.m.name = <char*> safe_malloc(len(name)) 
 144  strcpy(self.m.name, name) 
144  145  else: 
145  146  self.m.name = NULL 
146  147  
… 
… 
cdef class DiscreteHiddenMarkovModel(Hid 
270  271  elif self.m.s[i].pi > o.m.s[i].pi: 
271  272  return 1 
272  273  
273   return 0 
 274  # Compare emission symbols 
 275  return cmp(self._emission_symbols, o._emission_symbols) 
 276  
 277  def __reduce__(self): 
 278  """ 
 279  Used in pickling. 
 280  
 281  EXAMPLES: 
 282  sage: m = hmm.DiscreteHiddenMarkovModel([[0.4,0.6],[0.1,0.9]], [[0.0,1.0],[1,1]], [0,1], ['a','b'], name='test model') 
 283  sage: f,g = m.__reduce__() 
 284  sage: f(*g) == m 
 285  True 
 286  """ 
 287  return unpickle_discrete_hmm_v0, (self.transition_matrix(), self.emission_matrix(), 
 288  self.initial_probabilities(), self._emission_symbols, self.name()) 
274  289  
275  290  def __dealloc__(self): 
276  291  """ 
… 
… 
cdef class DiscreteHiddenMarkovModel(Hid 
304  319  if self._emission_symbols_dict: 
305  320  s += '\nEmission symbols: %s'%self._emission_symbols 
306  321  return s 
 322  
 323  def name(self): 
 324  """ 
 325  Return the name of this model. 
 326  
 327  OUTPUT: 
 328  string or None 
 329  
 330  EXAMPLES: 
 331  sage: m = hmm.DiscreteHiddenMarkovModel([[0.4,0.6],[0.1,0.9]], [[0.0,1.0],[1,1]], [1,2], name='test model') 
 332  sage: m.name() 
 333  'test model' 
 334  
 335  If the model is not explicitly named then this function returns None: 
 336  sage: m = hmm.DiscreteHiddenMarkovModel([[0.4,0.6],[0.1,0.9]], [[0.0,1.0],[1,1]], [1,2]) 
 337  sage: m.name() is None 
 338  True 
 339  """ 
 340  if self.m.name: 
 341  s = str(self.m.name) 
 342  return s 
 343  else: 
 344  return None 
307  345  
308  346  def initial_probabilities(self): 
309  347  """ 
… 
… 
cdef ghmm_dseq* malloc_ghmm_dseq(seqs) e 
683  721  d.seq_w[i] = 1 
684  722  d.flags = 0 
685  723  return d 
 724  
 725  
 726  def unpickle_discrete_hmm_v0(A, B, pi, emission_symbols,name): 
 727  """ 
 728  TESTS: 
 729  sage: m = hmm.DiscreteHiddenMarkovModel([[0.4,0.6],[0.1,0.9]], [[0.0,1.0],[1,1]], [1,0], name='test model') 
 730  sage: loads(dumps(m)) == m 
 731  True 
 732  sage: loads(dumps(m)).name() 
 733  'test model' 
 734  sage: sage.stats.hmm.hmm.unpickle_discrete_hmm_v0(m.transition_matrix(), m.emission_matrix(), m.initial_probabilities(), ['a','b'], m.name()) 
 735  Discrete Hidden Markov Model test model with 2 States and 2 Emissions 
 736  Transition matrix: 
 737  [0.4 0.6] 
 738  [0.1 0.9] 
 739  Emission matrix: 
 740  [0.0 1.0] 
 741  [1.0 1.0] 
 742  Initial probabilities: [1.0, 0.0] 
 743  Emission symbols: ['a', 'b'] 
 744  """ 
 745  return DiscreteHiddenMarkovModel(A,B,pi,emission_symbols,name) 