def next(self): #if self.nsteps == self.length: raise StopIteration() nexts= {} intersections= {} nexts_distr= self._get_state_distr(self.actual_state) for state, prob in nexts_distr.iteritems(): # si las notas que vengo tocando interseccion con las que debo tocar no es vacio intersections[state]= self.now_pitches.intersection(self.must_dict[self.length-self.nsteps][state]) if len(intersections[state]) > 0: nexts[state]= prob if len(nexts) == 0: import ipdb;ipdb.set_trace() raise Exception('Impossible phrase: actual_state= %(actual_state)s, start_pitch: %(n0)s, %(n1)s end_pitch: %(nfs)s, length:%(length)s' % self.__dict__) s= sum(nexts.itervalues()) for state, prob in nexts.iteritems(): nexts[state]= prob/s next= RandomPicker(values=nexts).get_value() self.actual_state= next now_pitches= set() for p in intersections[next]: now_pitches.update(next.related_notes(p[0], p[1], self.available_notes, reverse=False)) self.now_pitches= now_pitches self.history.append((self.now_pitches, self.actual_state)) self.nsteps+=1 return next
def next(self): #if self.nsteps == self.length: raise StopIteration() nexts= {} intersections= {} nexts_distr= self._get_state_distr(self.actual_state) #for state, prob in self.hmm.nexts(self.actual_state).iteritems(): for state, prob in nexts_distr.iteritems(): # si las notas que vengo tocando interseccion con las que debo tocar hay intersections[state]= self.now_pitches.intersection(self.must_dict[self.length-self.nsteps][state]) if len(intersections[state]) > 0: nexts[state]= prob if len(nexts) == 0: raise ImpossiblePhraseException('Impossible phrase: start_pitch: %(n0)s, end_pitch: %(nf)s, length:%(length)s' % self.__dict__) s= sum(nexts.itervalues()) for state, prob in nexts.iteritems(): nexts[state]= prob/s next= RandomPicker(values=nexts, random=self.random).get_value() self.actual_state= next now_pitches= set() for p in intersections[next]: now_pitches.update(next.related_notes(p, reverse=False)) self.now_pitches= now_pitches self.history.append((self.now_pitches, self.actual_state)) self.nsteps+=1 return next
def next(self, available_states): distr= {} state_counters= self.states_counters[self.actual_state] n= sum(state_counters.itervalues()) alpha= self.alpha # solo esta parte cambia, donde uso available_states nexts= self.hmm.nexts(self.actual_state) nexts= dict(((k, v) for (k,v) in nexts.iteritems() if k in available_states)) s= sum(nexts.itervalues()) for k, v in nexts.iteritems(): nexts[k]= v/s for state, prob in nexts.iteritems(): distr[state]= alpha/(alpha+n)*prob + n/(alpha+n)*state_counters[state] rnd_picker= RandomPicker("",distr) self.actual_state= rnd_picker.get_value() state_counters[self.actual_state]+=1 res= {} for random_variable in self.hmm.observators(self.actual_state): res[random_variable]= random_variable.get_value() return res
def next(self, input, result, prev_notes): available_notes= self.next_candidates(input.notes_distr, input.min_pitch, input.max_pitch) if len(available_notes) == 0: result.pitch= -1 return actual_note= RandomPicker(values=available_notes, random=self.random).get_value() self.ec.last_note= actual_note self.ec.last_pitch= actual_note.get_canonical_note() result.pitch= actual_note.pitch