def _get_round_winner_message(self, black_text, white_cards_text): def format_cards(cards_iter, class_iter): message_parts = list(cards_iter) if message_parts[-1] == '.': message_parts = message_parts[:-1] last = len(message_parts) - 1 return [{ "class": class_iter.next(), "text": x.rstrip('.') if i < last else x } for i, x in enumerate(message_parts)] if not "{}" in black_text: css_class = cycle(["black", "white"]) white_cards_text[0] = ' ' + white_cards_text[0] return format_cards(roundrobin([black_text], white_cards_text), css_class) elif black_text.startswith("{}"): black_parts = black_text.split("{}") black_parts.pop(0) css_class = cycle(["white", "black"]) return format_cards(roundrobin(white_cards_text, black_parts), css_class) else: black_parts = black_text.split("{}") css_class = cycle(["black", "white"]) return format_cards(roundrobin(black_parts, white_cards_text), css_class)
def merge_hits(hits_positive_strand, hits_negative_strand, only_best): """ Merges the hits from both strands. :arg hits_positive_strand: The list of hits on the positive strand. :type hits_positive_strand: list of :class:`HIT` :arg hits_negative_strand: The list of hits on the negative strand. :type hits_negative_strand: list of :class:`HIT` :arg only_best: Boolean set to True only if the best TFBS hit in the sequence is to be kept. :type only_best: bool :returns: A list containing the TFBS hits (empty if no hit). :rtype: list :note: The two input lists are required to be ordered following the positions on the sequence. The best hit per position is given. When no hit has been found at a position, the constant None is used. """ if only_best: return [best_hit(hits_positive_strand, hits_negative_strand)] else: return [hit for hit in utils.roundrobin(hits_positive_strand, hits_negative_strand)]
def order_pred(self, query, docs_scores, cluster_order='relevance'): docs_to_cluster = [doc_id for (doc_id, _) in docs_scores[:self.N]] clusters = self.clustering.cluster_documents(docs_to_cluster) docs_ranked = [] if cluster_order == 'docs_count': ordered_clusters = self.order_cluster_by_docs_count( clusters, docs_scores) elif cluster_order == 'relevance': ordered_clusters = self.order_cluster_by_relevance( clusters, docs_scores) else: print('implement cluster order!!') exit() docs_grouped_by_clusters = [] for cluster_id in ordered_clusters: clusters_docs = [d for d, c in clusters.items() if c == cluster_id] clusters_docs_ordered = self.order_docs_by_relevance( clusters_docs, docs_scores) docs_grouped_by_clusters.append(clusters_docs_ordered) ordered_docs = list(roundrobin(*docs_grouped_by_clusters)) return [(doc_id, rank) for rank, doc_id in enumerate(ordered_docs)]
def warp(self): if not self.convexOnly: pairs = [pair for pair in pairwise(self.hullVertices)] for p1, p2 in pairs: # TODO: implement min distance check? mdpt = midpoint(p1, p2) angle = random.randint(self.minWarpAngle, self.maxWarpAngle) newPoint = endpoint(p1, mdpt, angle, 'd') self.addedPoints.append(newPoint) # interleave self.hullVertices and newPoints self.hullVertices = [point for point in roundrobin(self.hullVertices, self.addedPoints)]
def criterium_balanced(uncertainty, correct, keys, labels, num_samples_to_choose): classes = {} for label, key, unc in zip(labels, keys, uncertainty): if label not in classes: classes[label] = [] if unc > config.UNCERTAINTY_THRESOLD: classes[label].append((unc, key)) for key in classes.keys(): classes[key].sort(key=lambda x: x[0], reverse=True) classes[key] = [x[1] for x in classes[key]] samples_per_class = 2 * num_samples_to_choose / len(classes.keys()) for key in classes.keys(): classes[key] = classes[key][:samples_per_class] chosen_keys = list(utils.roundrobin(*classes.values())) print 'Chosen {0} keys with threshold {1}'.format(len(chosen_keys), config.UNCERTAINTY_THRESOLD) return chosen_keys[:num_samples_to_choose]
def _decode(self, s): # rails = OrderedDict() # keys = range(self.key) # z = utils.zigzag(keys) # rail_counts = Counter(itertools.islice(z, len(s))) # print(rail_counts) # could easily start at zero, again [TODO] to test use of counter # and dict as being robust and superior over array/list rails_in_order = list(range(1, self.key+1)) z = utils.zigzag(rails_in_order) #### using str here to ensure that OrderedCounter below actually works # as opposed to typical Counter, which will preserve order of int keys ## [TODO] remove str() before final version rail_slices = list(str(n) for n in itertools.islice(z, len(s))) # rail_slices = list(itertools.islice(z, len(s))) # z = utils.zigzag(rails_in_order) # zippy = (list(zip(z, s))) #### c is not guaranteed to store keys in order period = 2 * (self.key - 1) one_period_of_zigzags = rail_slices[:period] #### combine with OrderedDict # c = Counter(rail_slices) # how many characters are in each row? d = OrderedCounter(rail_slices) # print(d) # rail_lengths = { rail: rail_len for rail, rail_len in d.items() } # rail_counts = [ (rail,c[rail]) for rail in rails_in_order] # rail_counts2 = [(rail, crail) for rail,crail in d.items()] # print('---') # print(rail_counts) # print(rail_counts2) # print('===') t = iter(s) # rails = [iter(take(c[rail], t)) for rail in rails_in_order] fence = {k: iter(take(v, t)) for k, v in d.items()} # rail: count # rail_iters = (rails[rail] for rail in rail_slices) rail_iters2 = (fence[rail] for rail in one_period_of_zigzags) # out = map(next, rail_iters) # out = [next(rail_iter) for rail_iter in rail_iters] out = utils.roundrobin(*rail_iters2) # x = [take(c[rail], t) for rail in rails_in_order] # y = itertools.tee(t, self.key) # [take(c[rail], m) for m in r # ends = itertools.accumulate(c[rail] for rail in rails_in_order) # rails = { rail: iter(list(itertools.islice(t, c[rail]))) # for rail in rails_in_order } # rails = { rail: iter(x[rail]) for rail in rails_in_order } # rails = [iter(x[rail]) for rail in rails_in_order] # rails = { rail: iter( list( next(t) for __ in range(count) )) # for rail, count in c.items() } # out = map(next, (rails[rail] for rail in rail_slices)) ### use roundrobin? # out = map(next, map(rails.__getitem__, rail_slices)) # out = [next(rail_contents) for rail_contents in # (rails[rail] for rail in rail_slices)] return ''.join(out)
def _decode(self, s): groups = grouper(s, len(s) // self.key, fillvalue=NULLCHAR) return roundrobin(*groups)
def _encode(self, s): groups = grouper(s, self.key, fillvalue=NULLCHAR) return roundrobin(*groups)