Ticket #3726: sage-3726-part9.patch

File sage-3726-part9.patch, 4.9 KB (added by was, 11 years ago)
  • sage/stats/hmm/chmm.pyx

    # 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 
    220220        self.m.class_change = NULL
    221221
    222222        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())
    223236
    224237    def __dealloc__(self):
    225238        """
    cdef ghmm_cseq* to_cseq(seq) except NULL 
    688701        memcpy(sqd.seq[i], T._values , sizeof(double)*T._length)
    689702
    690703    return sqd
     704
     705def 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)
  • sage/stats/hmm/hmm.pyx

    diff -r bc0e714d457e -r 1dfe749d4931 sage/stats/hmm/hmm.pyx
    a b cdef class DiscreteHiddenMarkovModel(Hid 
    140140        # Assign model identifier if specified
    141141        if name is not None:
    142142            name = str(name)
    143             self.m.name = name
     143            self.m.name = <char*> safe_malloc(len(name))
     144            strcpy(self.m.name, name)
    144145        else:
    145146            self.m.name = NULL
    146147
    cdef class DiscreteHiddenMarkovModel(Hid 
    270271            elif self.m.s[i].pi > o.m.s[i].pi:
    271272                return 1
    272273
    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())
    274289
    275290    def __dealloc__(self):
    276291        """
    cdef class DiscreteHiddenMarkovModel(Hid 
    304319        if self._emission_symbols_dict:
    305320            s += '\nEmission symbols: %s'%self._emission_symbols
    306321        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
    307345
    308346    def initial_probabilities(self):
    309347        """
    cdef ghmm_dseq* malloc_ghmm_dseq(seqs) e 
    683721        d.seq_w[i] = 1
    684722    d.flags = 0
    685723    return d
     724
     725
     726def 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)