def adjust_for_precedence(self, weights): prec = {pitch.pc(p):w for p,w in self.precedences.items()} for k in weights.keys(): if k not in prec: prec[k] = 1 for p in self.prev_picks: pcp = pitch.pc(p) if pcp in prec: prec[pcp] *= 0.5 mult = {k: prec[pitch.pc(k)] for k in weights.keys()} mult = w.scale(mult, float(self.precedence) / self.left_to_select) return w.normalize(w.apply(mult, weights))
def choose(weights): '''Choose one item out of a weighted list.''' weights = w.probabilitize(weights) rand = random.random() choice = None for k, v in weights.items(): if rand <= v: choice = k break; rand = rand - v return choice ### Utility functions @staticmethod def identity(x, y): # to be overrided when dealing with pitch classes return x == y ### Cleaning operations def clean(self): self.max_length = len(self.weighted_items) * 2 - 1 self.normalize() def normalize(self): self.weighted_items = w.normalize(self.weighted_items) if __name__ == '__main__': dm = DataMap({1:0.001, 2:1, 3:5}) weights = dm.weighted_items print weights print w.scale(weights, 0.1)