Python itertools 模块,combinations() 实例源码

我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用itertools.combinations()

项目:treecat    作者:posterior    | 项目源码 | 文件源码
def test_quantize_from_probs2(size, resolution):
    set_random_seed(make_seed(size, resolution))
    probs = np.exp(np.random.random(size)).astype(np.float32)
    probs2 = probs.reshape((1, size))
    quantized = quantize_from_probs2(probs2, resolution)
    assert quantized.shape == probs2.shape
    assert quantized.dtype == np.int8
    assert np.all(quantized.sum(axis=1) == resolution)

    # Check that quantized result is closer to target than any other value.
    quantized = quantized.reshape((size, ))
    target = resolution * probs / probs.sum()
    distance = np.abs(quantized - target).sum()
    for combo in itertools.combinations(range(size), resolution):
        other = np.zeros(size, np.int8)
        for i in combo:
            other[i] += 1
        assert other.sum() == resolution
        other_distance = np.abs(other - target).sum()
        assert distance <= other_distance
项目:hsarchetypes    作者:HearthSim    | 项目源码 | 文件源码
def _most_similar_pair(clusters, distance_function):
    result = []

    for c1, c2 in combinations(clusters, 2):
        if not c1.can_merge(c2):
            continue

        if c1.must_merge(c2):
            logger.info("External IDs Match.\n%s\n%s\nMust Merge" % (c1, c2))
            return c1, c2, 1.0

        sim_score = distance_function(c1, c2)
        result.append((c1, c2, sim_score))

    if result:
        sorted_result = sorted(result, key=lambda t: t[2], reverse=True)
        return sorted_result[0]
项目:health-mosconi    作者:GNUHealth-Mosconi    | 项目源码 | 文件源码
def _default_lines(self):
        'Return the larger list of lines which can be reconciled'
        currency = self.show.account.company.currency
        chunk = config.getint('account', 'reconciliation_chunk', default=10)
        # Combination is exponential so it must be limited to small number
        default = []
        for lines in grouped_slice(self._all_lines(), chunk):
            lines = list(lines)
            best = None
            for n in xrange(len(lines), 1, -1):
                for comb_lines in combinations(lines, n):
                    amount = sum((l.debit - l.credit) for l in comb_lines)
                    if currency.is_zero(amount):
                        best = [l.id for l in comb_lines]
                        break
                if best:
                    break
            if best:
                default.extend(best)
        return default
项目:ConceptualSpaces    作者:lbechberger    | 项目源码 | 文件源码
def size(self):
        """Computes the hypervolume of this concept."""

        hypervolume = 0.0
        num_cuboids = len(self._core._cuboids)

        # use the inclusion-exclusion formula over all the cuboids
        for l in range(1, num_cuboids + 1):
            inner_sum = 0.0

            subsets = list(itertools.combinations(self._core._cuboids, l))           
            for subset in subsets:
                intersection = subset[0]
                for cuboid in subset:
                    intersection = intersection.intersect_with(cuboid)
                inner_sum += self._hypervolume_couboid(intersection)

            hypervolume += inner_sum * (-1.0)**(l+1)

        return hypervolume
项目:matasano    作者:shainer    | 项目源码 | 文件源码
def GenerateCollisionTree(k, stateLen):
    nodesByLevel = collections.defaultdict(list)

    allBytes = range(2 ** 8)
    allPossibleBlocks = []
    for comb in itertools.combinations(allBytes, stateLen):
        byteString = b''.join(x.to_bytes(1, 'little') for x in comb)
        allPossibleBlocks.append(byteString)

    for level in range(k+1):
        nodes = []

        if level == 0:
            nodes = RandomUniqueStates(2 ** k, stateLen)
        else:
            nodes = FindCollisions(nodesByLevel[level-1], allPossibleBlocks, stateLen)
        print('Generated nodes for level %d: %s' % (level, str(nodes)))
        nodesByLevel[level] = nodes

    return nodesByLevel

# Now I generate a meaningful message, crafted so that it's exactly 1 block
# length, but there is no need to do so. Nobody should be seeing this
# message, only the hash.
项目:xenoGI    作者:ecbush    | 项目源码 | 文件源码
def scoreGeneSet(geneT,scoresO,geneNames,scoreType):
    '''Given a tuple of genes in geneT, calculate the average score
between them. If there are some genes that lack a score, we give it
the minimum possible. The kind of score we use is indicated by
scoreType, which corresponds to the type of score in the scores
graph.
    '''

    geneNumsL = [geneNames.nameToNum(name) for name in geneT]
    edgeL = getInternalEdges(geneNumsL,scoresO)

    scSum = sum((scoresO.getScoreByEdge(edge,scoreType) for edge in edgeL))

    # if there was an edge between every node, there would be
    # len(geneT) choose 2.
    maxPossibleNumEdges = len(list(itertools.combinations(geneT,2)))
    actualNumEdges = len(edgeL)

    numMissEdge=maxPossibleNumEdges-actualNumEdges

    scSum += numMissEdge * min(scoresO.scoreD[scoreType])

    avSc = scSum / maxPossibleNumEdges

    return avSc,maxPossibleNumEdges,actualNumEdges
项目:singlecell-dash    作者:czbiohub    | 项目源码 | 文件源码
def tenx_diff_exp_all(tenx_data, communities):
    diff_expr_dfs = []

    for c1, c2 in itertools.combinations(np.unique(communities), 2):
        group1 = (communities == c1)
        group2 = (communities == c2)
        diff_expr_df = sparse_diff_exp(tenx_data.genes.matrix,
                                       group1, group2, tenx_data.genes.columns).sort_values('p')
        diff_expr_df['community1'] = c1
        diff_expr_df['community2'] = c2
        diff_expr_dfs.append(diff_expr_df)

    diff_expr = pd.concat(diff_expr_dfs)
    print(diff_expr.shape)
    print(diff_expr.head())
    return diff_expr
项目:gae-dataflow    作者:amygdala    | 项目源码 | 文件源码
def process(self, element):
    content_value = element.properties.get('text', None)
    text_line = ''
    if content_value:
      text_line = content_value.string_value

    words = set([x.lower() for x in re.findall(r'[A-Za-z\']+', text_line)])
    # You can add more stopwords if you want.  These words are not included
    # in the analysis.
    stopwords = [
        'a', 'amp', 'an', 'and', 'are', 'as', 'at', 'be', 'been',
        'but', 'by', 'co', 'do', 'for', 'has', 'have', 'he', 'her', 'his',
        'https', 'if', 'in', 'is', 'it', 'me', 'my', 'no', 'not', 'of', 'on',
        'or', 'rt', 's', 'she', 'so', 't', 'than', 'that', 'the', 'they',
        'this', 'to', 'us', 'was', 'we', 'what', 'with', 'you', 'your',
        'who', 'when', 'via']
    stopwords += list(map(chr, range(97, 123)))
    pruned_words = list(words - set(stopwords))
    pruned_words.sort()
    import itertools
    return list(itertools.combinations(pruned_words, 2))
项目:gae-dataflow    作者:amygdala    | 项目源码 | 文件源码
def process(self, element):
    content_value = element.properties.get('text', None)
    text_line = ''
    if content_value:
      text_line = content_value.string_value

    words = set([x.lower() for x in re.findall(r'[A-Za-z\']+', text_line)])
    # You can add more stopwords if you want.  These words are not included
    # in the analysis.
    stopwords = [
        'a', 'amp', 'an', 'and', 'are', 'as', 'at', 'be', 'been',
        'but', 'by', 'co', 'do', 'for', 'has', 'have', 'he', 'her', 'his',
        'https', 'if', 'in', 'is', 'it', 'me', 'my', 'no', 'not', 'of', 'on',
        'or', 'rt', 's', 'she', 'so', 't', 'than', 'that', 'the', 'they',
        'this', 'to', 'us', 'was', 'we', 'what', 'with', 'you', 'your',
        'who', 'when', 'via']
    stopwords += list(map(chr, range(97, 123)))
    pruned_words = list(words - set(stopwords))
    pruned_words.sort()
    import itertools
    return list(itertools.combinations(pruned_words, 2))
项目:deep_stack    作者:lyn716    | 项目源码 | 文件源码
def get_ones_max_pattern(self, player):
        """ ?????5-7???????? """
        temp_cards = []
        temp_cards.extend(self.public_pot_cards)
        temp_cards.extend(player.hands)
        print('temp_cards: ', temp_cards)

        if len(temp_cards) < 5:
            raise LookupError('Cards < 5')
        all_cards = list(combinations(
            iterable=temp_cards,
            r=5
        ))
        max_cards = all_cards[0]
        for cards in all_cards:
            # 5-7????????python????????
            self.dealer.get(
                five_cards_A=cards,
                five_cards_B=max_cards
            )
            if self.dealer.A_stronger_than_B:
                max_cards = cards
        return max_cards
项目:ababe    作者:unkcpz    | 项目源码 | 文件源码
def _gen_dup_trinary_alloy(self, sp1, n1, sp2, n2):
        init_numbers = self.init_cell.numbers
        isp1 = sp1.Z
        isp2 = sp2.Z

        sp_ind_origin = [i for i, s in enumerate(init_numbers)]
        for sp1_comb_index in combinations(sp_ind_origin, n1):
            sp_ind_bin = [x for x in sp_ind_origin if x not in sp1_comb_index]
            for sp2_comb_index in combinations(sp_ind_bin, n2):
                numbers = init_numbers.copy()
                for i1, i2 in zip_longest(sp1_comb_index, sp2_comb_index):
                    if i1 is not None:
                        numbers[i1] = isp1
                    if i2 is not None:
                        numbers[i2] = isp2
                yield GeneralCell(self.lattice, self.positions, numbers)
                # pdb.set_trace()
项目:TCP-IP    作者:JackZ0    | 项目源码 | 文件源码
def test_equivalent_any_addresses(self):
        from certbot_nginx.obj import Addr
        any_addresses = ("0.0.0.0:80 default_server ssl",
                         "80 default_server ssl",
                         "*:80 default_server ssl")
        for first, second in itertools.combinations(any_addresses, 2):
            self.assertEqual(Addr.fromstring(first), Addr.fromstring(second))

        # Also, make sure ports are checked.
        self.assertNotEqual(Addr.fromstring(any_addresses[0]),
                            Addr.fromstring("0.0.0.0:443 default_server ssl"))

        # And they aren't equivalent to a specified address.
        for any_address in any_addresses:
            self.assertNotEqual(
                Addr.fromstring("192.168.1.2:80 default_server ssl"),
                Addr.fromstring(any_address))
项目:Visualization-of-popular-algorithms-in-Python    作者:MUSoC    | 项目源码 | 文件源码
def EgocentricNetwork(G,v):

    egocentric_network_edge_list = []
    egocentric_network_node_list = [v]
    for i in G.neighbors(v):
        egocentric_network_node_list.append(i)
        egocentric_network_edge_list.append((v,i))
    egocentric_network_node_list.sort()
    egocentric_network_edge_list = list(tuple(sorted(p)) for p in egocentric_network_edge_list)

    for i in list(itertools.combinations(egocentric_network_node_list, 2)): #generates all possible pairs of nodes
        if i in G.edges() and i not in egocentric_network_edge_list:
            egocentric_network_edge_list.append(i)

    return egocentric_network_edge_list,egocentric_network_node_list



#takes input from the file and creates a graph
项目:johnson-county-ddj-public    作者:dssg    | 项目源码 | 文件源码
def generate_feature_group_combinations(self, feature_groups):
        combination_unflattened = sum([map(list,
                                      combinations(feature_groups, i)) for i in range(len(feature_groups) + 1)],
                                      [])

        combinations_flattened = []
        for combination in combination_unflattened:
            flattened_combination = {'feature_column_names':[],
                                            'feature_groups': []}
            for feature_group in combination:
                flattened_combination['feature_column_names'].extend(
                        feature_group['feature_column_names'])
                flattened_combination['feature_groups'].extend(
                        feature_group['feature_groups'])
            combinations_flattened.append(flattened_combination)
        return combinations_flattened[1:]
项目:AshsSDK    作者:thehappydinoa    | 项目源码 | 文件源码
def normalize_language_tag(tag):
    """Return a list of normalized combinations for a `BCP 47` language tag.

    Example:

    >>> from docutils.utils import normalize_language_tag
    >>> normalize_language_tag('de_AT-1901')
    ['de-at-1901', 'de-at', 'de-1901', 'de']
    >>> normalize_language_tag('de-CH-x_altquot')
    ['de-ch-x-altquot', 'de-ch', 'de-x-altquot', 'de']

    """
    # normalize:
    tag = tag.lower().replace('-','_')
    # split (except singletons, which mark the following tag as non-standard):
    tag = re.sub(r'_([a-zA-Z0-9])_', r'_\1-', tag)
    subtags = [subtag for subtag in tag.split('_')]
    base_tag = [subtags.pop(0)]
    # find all combinations of subtags
    taglist = []
    for n in range(len(subtags), 0, -1):
        for tags in unique_combinations(subtags, n):
            taglist.append('-'.join(base_tag+tags))
    taglist += base_tag
    return taglist
项目:raiden    作者:raiden-network    | 项目源码 | 文件源码
def test_channelmanager_graph_building(
        raiden_network,
        token_addresses,
        settle_timeout):

    token_address = token_addresses[0]

    total_pairs = 0
    pairs = itertools.combinations(raiden_network, 2)
    for app0, app1 in pairs:
        manager = app0.raiden.default_registry.manager_by_token(token_address)
        manager.new_netting_channel(
            app0.raiden.address,
            app1.raiden.address,
            settle_timeout,
        )
        total_pairs += 1
        assert total_pairs == len(manager.channels_addresses())
项目:freehand-zxcalculus-recognition    作者:emmacaort    | 项目源码 | 文件源码
def get_ref_centre(nodelist):
    """Choose a node as the diagram centre and find the unit distance.

    Find the shortest distance between two nodes and use it as the unit distance of the 
    diagram grid. Choose one of these two nodes as the diagram centre.

    Args:
        nodelist (list): A list of nodes

    Returns:
        The absolute position of the centre node and the unit distance of the diagram.
    """    
    centres = [node.centre for node in nodelist]
    min_d = sp.dist(centres[0], centres[1])    
    min_p0,min_p1 = centres[0], centres[1]
    for p0, p1 in itertools.combinations(centres, 2):
        d = sp.dist(p0,p1)
        if d < min_d:
            min_d = d
            min_p0,min_p1 = p0,p1   
    min_x = abs(min_p0[0] - min_p1[0])
    min_y = abs(min_p0[1] - min_p1[1])
    unit_d = max(min_x, min_y)
    return [min_p0,unit_d]
项目:Intranet-Penetration    作者:yuxiaokui    | 项目源码 | 文件源码
def _GivePropertiesFromGeneralToSpecific(handler_list):
  """Makes sure that handlers have all properties of more general ones.

  Ex. Since "*" matches everything "admin/*" matches, we want everything
  matched by "admin/*" to have all the properties specified to "*".
  Therefore we give properties from the "*" handler to the "admin/*" handler.
  If the "*" handler is a SimpleHandler, it carries its own properties, so it
  becomes a child of the "admin/*" handler. Otherwise, its properties are
  define by its children, so its children are copied to the "admin/*"
  handler.

  This is an in-place mutation of the list.

  Args:
    handler_list: List of ordered Handlers.
  """
  for i, j in itertools.combinations(xrange(len(handler_list)), 2):
    if handler_list[j].MatchesAll(handler_list[i]):
      if isinstance(handler_list[i], SimpleHandler):
        handler_list[i] = handler_list[i].CreateOverlappedHandler()
      handler_list[i].AddMatchingHandler(handler_list[j])
项目:brainpipe    作者:EtienneCmb    | 项目源码 | 文件源码
def _getCombi(stuff, grplen=[], kind=int, sep=''):
    """Function which return combinations of string/integer objects.
    -> (start, stop) : number of combine elements from initial list
    """
    allComb = []
    if not grplen:
        grplen = list(n.arange(1, len(idxGp)+1))
    for L in grplen:
        for subset in combinations(stuff, L):
            if kind == str:
                allComb.extend([sep.join(subset)])
            elif kind == int:
                t = []
                [t.extend(k) for k in subset]
                allComb.extend([t])
    return allComb
项目:brainpipe    作者:EtienneCmb    | 项目源码 | 文件源码
def _seqcombination(cst, dyn, direction, grp):
    """Generate combi for forward/backward/exhaustive sequence.

    cst : list containing the index of all the features
    dyn : features to add or remove
    direction : direction of the sequence
    grp : group features
    """
    if direction == 'forward':
        combi = [cst + [y]
                 for y in dyn if not list(set(cst).intersection([y]))]
    elif direction == 'backward':
        combi = [list(set(cst).difference([x])) for x in dyn]
    elif direction == 'exhaustive':
        combi = [list(k) for i in range(1, len(cst)+1)
                 for k in combinations(cst, i)]

    return [[j for i in k for j in grp['g'+str(i)]] for k in combi]
项目:MKFQ    作者:maojingios    | 项目源码 | 文件源码
def _GivePropertiesFromGeneralToSpecific(handler_list):
  """Makes sure that handlers have all properties of more general ones.

  Ex. Since "*" matches everything "admin/*" matches, we want everything
  matched by "admin/*" to have all the properties specified to "*".
  Therefore we give properties from the "*" handler to the "admin/*" handler.
  If the "*" handler is a SimpleHandler, it carries its own properties, so it
  becomes a child of the "admin/*" handler. Otherwise, its properties are
  define by its children, so its children are copied to the "admin/*"
  handler.

  This is an in-place mutation of the list.

  Args:
    handler_list: List of ordered Handlers.
  """
  for i, j in itertools.combinations(xrange(len(handler_list)), 2):
    if handler_list[j].MatchesAll(handler_list[i]):
      if isinstance(handler_list[i], SimpleHandler):
        handler_list[i] = handler_list[i].CreateOverlappedHandler()
      handler_list[i].AddMatchingHandler(handler_list[j])
项目:gpam_stats    作者:ricoms    | 项目源码 | 文件源码
def random_combinations(points_in_class):
    n_cl = len(points_in_class)
    max_points = 2 * n_cl # as used by by Orriols-Puig et al., 2010
    all_combinations = []
    for i, j in itertools.combinations(points_in_class, r = 2):
        all_combinations.append((i, j))

    points_i = 0
    n = len(all_combinations)
    for i in range(n):

        point = np.random.choice(len(all_combinations), 1)[0]

        yield all_combinations[point]

        del all_combinations[point]
        if points_i > max_points or len(all_combinations) == 0:
            break
        points_i = points_i + 1
项目:directed-probe-matching    作者:rc1035    | 项目源码 | 文件源码
def get_similar_ssid_sets(ssid_sets, threshold):
    """Return a mapping of ssid set to similar ssid sets.
    :param ssid_sets: Iterable of SSID sets
    :param threshold: Minimum Jaccard index for two sets to be matched as similar.
    """
    ssid_set_to_matches = defaultdict(set)
    ssid_pairs = combinations(ssid_sets, r=2)

    # Distribute calulcations to worker processes
    # Significant speed-up over single process
    with multiprocessing.Pool() as pool:
        task = partial(jaccard_worker, threshold=threshold)

        # Immediately returns an iterable
        similar_ssids = pool.imap_unordered(task, ssid_pairs, chunksize=300000)

        # Consumes the iterable whenever a worker process yields
        for match in similar_ssids:
            if match:
                ssid_set_to_matches[match[0]].add(match[1])
                ssid_set_to_matches[match[1]].add(match[0])

    return ssid_set_to_matches
项目:apyori    作者:ymoch    | 项目源码 | 文件源码
def gen_ordered_statistics(transaction_manager, record):
    """
    Returns a generator of ordered statistics as OrderedStatistic instances.

    Arguments:
        transaction_manager -- Transactions as a TransactionManager instance.
        record -- A support record as a SupportRecord instance.
    """
    items = record.items
    for combination_set in combinations(sorted(items), len(items) - 1):
        items_base = frozenset(combination_set)
        items_add = frozenset(items.difference(items_base))
        confidence = (
            record.support / transaction_manager.calc_support(items_base))
        lift = confidence / transaction_manager.calc_support(items_add)
        yield OrderedStatistic(
            frozenset(items_base), frozenset(items_add), confidence, lift)
项目:pyannote-audio    作者:pyannote    | 项目源码 | 文件源码
def compute_similarity_matrix(self, parent=None):

        clusters = list(self._models)
        n_clusters = len(clusters)

        X = np.vstack([self[cluster][0] for cluster in clusters])

        nX = l2_normalize(X)
        similarities = -squareform(pdist(nX, metric=self.distance))

        matrix = ValueSortedDict()
        for i, j in itertools.combinations(range(n_clusters), 2):
            matrix[clusters[i], clusters[j]] = similarities[i, j]
            matrix[clusters[j], clusters[i]] = similarities[j, i]

        return matrix
项目:zippy    作者:securesystemslab    | 项目源码 | 文件源码
def ksubsets(superset, k):
    """
    Finds the subsets of size k in lexicographic order.

    This uses the itertools generator.

    Examples
    ========

    >>> from sympy.combinatorics.subsets import ksubsets
    >>> list(ksubsets([1,2,3], 2))
    [(1, 2), (1, 3), (2, 3)]
    >>> list(ksubsets([1,2,3,4,5], 2))
    [(1, 2), (1, 3), (1, 4), (1, 5), (2, 3), (2, 4), \
    (2, 5), (3, 4), (3, 5), (4, 5)]

    See Also
    ========
    class:Subset
    """
    return combinations(superset, k)
项目:DaNet-Tensorflow    作者:khaotik    | 项目源码 | 文件源码
def combinations(s_data, subset_size, total_size=None, name=None):
    assert isinstance(subset_size, int)
    assert subset_size > 0
    if total_size is None:
        total_size = s_data.get_shape().as_list()[0]

    if total_size is None:
        raise ValueError(
            "tensor size on axis 0 is unknown,"
            " please supply 'total_size'")
    else:
        assert isinstance(total_size, int)
        assert subset_size <= total_size

    c_combs = tf.constant(
        list(itertools.combinations(range(total_size), subset_size)),
        dtype=hparams.INTX,
        name=('combs' if name is None else name))

    return tf.gather(s_data, c_combs)
项目:cfbets    作者:joshpelkey    | 项目源码 | 文件源码
def get_bettingest_couples():
    # get list of users
    users = User.objects.values('id', 'first_name', 'last_name')

    # use itertools to get all combinations
    global_bettingest_couples = []
    for combo in combinations(users, 2):
        # for each combo, check how many bets they have with eachother 
        num_bets = get_couple_bet_number(combo[0]['id'], combo[1]['id'])

        # append to list of dictionaries
        # e.g. L = [{num_bets: 5, users: ['John Doe', 'Jane Doe']}]
        user1_name = combo[0]['first_name'] + ' ' + combo[0]['last_name']
        user2_name = combo[1]['first_name'] + ' ' + combo[1]['last_name']
        users_names = [user1_name, user2_name]
        entry = {'num_bets': num_bets, 'users': users_names}
        global_bettingest_couples.append(entry)

    # pare down to top 5
    pared_global_bettingest_couples = sorted(global_bettingest_couples, key=lambda k: k['num_bets'], reverse=True)[:10]

    return pared_global_bettingest_couples
项目:albion    作者:Oslandia    | 项目源码 | 文件源码
def find_4_cycles(edges):
    """return all unique four-cycles in graph"""
    # for each node, add a list of reachable nodes
    # for all pairs of reachable node test if they share a reachable node -> cycle
    reachables = defaultdict(set)
    for edge in edges:
        reachables[edge[0]].add(edge[1])
        reachables[edge[1]].add(edge[0])

    loops = {}
    for a, reachable in reachables.iteritems():
        for b, c in itertools.combinations(reachable, 2):
            for d in reachables[b].intersection(reachables[c]).difference(set([a])):
                loops[tuple(sorted([a, b, d, c]))] = [a, b, d, c]

    return loops.values()
项目:deepmodels    作者:learningsociety    | 项目源码 | 文件源码
def gen_cls_combs(labels):
  """Generate exhaustive label pairs.

  Args:
    labels: a set of labels.
  Returns:
    a list of unique label pairs.
  """
  unique_labels = list(set(labels))
  # generate class combinations to cover all classes at least once
  cls_combs = []
  for idx in range(len(unique_labels)):
    cls1 = unique_labels[idx]
    for id2 in range(idx + 1, len(unique_labels)):
      cls2 = unique_labels[id2]
      cls_combs.append((cls1, cls2))
  return cls_combs
项目:elfi    作者:elfi-dev    | 项目源码 | 文件源码
def _combine_ss(self, list_ss, max_cardinality):
        """Create all combinations of the initialised summary statistics up till the maximum cardinality.

        Parameters
        ----------
        list_ss : List of callable functions
            List of candidate summary statistics.
        max_cardinality : int
            Maximum cardinality of a candidate summary-statistics combination.

        Returns
        -------
        List
            Combinations of candidate summary statistics.

        """
        if max_cardinality > len(list_ss):
            max_cardinality = len(list_ss)

        # Combine the candidate summary statistics.
        combinations_ss = []
        for i in range(max_cardinality):
            for combination in combinations(list_ss, i + 1):
                combinations_ss.append(combination)
        return combinations_ss
项目:gold-digger    作者:business-factory    | 项目源码 | 文件源码
def pick_the_best(rates_records):
        """
        Compare rates to each other and group then by absolute difference.
        If there is group with minimal difference of two rates, choose one of them according the order of providers.
        If there is group with minimal difference with more than two rates, choose rate in the middle / aka most common rate in the list.

        :type rates_records: list[gold_digger.database.db_model.ExchangeRate]
        :rtype: gold_digger.database.db_model.ExchangeRate
        """
        if len(rates_records) in (1, 2):
            return rates_records[0]

        differences = defaultdict(list)
        for a, b in combinations(rates_records, 2):
            differences[abs(a.rate - b.rate)].extend((a, b))  # if (a,b)=1 and (b,c)=1 then differences[1]=[a,b,b,c]

        minimal_difference, rates = min(differences.items())
        if len(rates) == 2:
            return rates[0]
        else:
            return Counter(rates).most_common(1)[0][0]  # [(ExchangeRate, occurrences)]
项目:pyprocessmacro    作者:QuentinAndre    | 项目源码 | 文件源码
def _simple_ind_effects_wrapper(self):
        """
        A wrapper for the indirect effects (and for total/contrast effects if specified)
        :return: pd.DataFrame
            A DataFrame of effects, se, llci, and ulci, for the simple/total/constrasts of indirect effects.
        """
        symb_to_var = self._symb_to_var
        results = self.estimation_results
        rows_stats = np.array([results["effect"], results["se"], results["llci"], results["ulci"]]).T

        med_names = [symb_to_var.get('m{}'.format(i + 1), 'm{}'.format(i + 1)) for i in range(self._n_meds)]
        rows_levels = []
        if self._options["total"]:
            rows_levels += ["TOTAL"]
        rows_levels += med_names
        if self._options["contrast"]:
            contrasts = ["Contrast: {} vs. {}".format(a, b) for a, b in combinations(med_names, 2)]
            rows_levels += contrasts
        rows_levels = np.array(rows_levels).reshape(-1, 1)

        rows = np.concatenate([rows_levels, rows_stats], axis=1)
        cols = ["", "Effect", "Boot SE", "BootLLCI", "BootULCI"]
        df = pd.DataFrame(rows, columns=cols, index=[""] * rows.shape[0])
        return df.apply(pd.to_numeric, args=["ignore"])
项目:frisbeer-backend    作者:Moetto    | 项目源码 | 文件源码
def create_teams(self):
        def calculate_team_elo(team):
            return int(sum([player.elo for player in team]) / len(team))

        elo_list = []
        players = set(self.players.all())
        possibilities = itertools.combinations(players, 3)
        for possibility in possibilities:
            team1 = possibility
            team2 = players - set(team1)
            elo1 = calculate_team_elo(team1)
            elo2 = calculate_team_elo(team2)
            elo_list.append((abs(elo1 - elo2), team1, team2))
        ideal_teams = sorted(elo_list, key=itemgetter(0))[0]
        self.gameplayerrelation_set\
            .filter(player__id__in=[player.id for player in ideal_teams[1]]).update(team=GamePlayerRelation.Team1)
        self.gameplayerrelation_set \
            .filter(player__id__in=[player.id for player in ideal_teams[2]]).update(team=GamePlayerRelation.Team2)
        print(ideal_teams[0])
        self.save()
项目:leven-squash    作者:dwcoates    | 项目源码 | 文件源码
def read_files(directory, length, num):
    files = [join(directory, f)
             for f in listdir(directory) if isfile(join(directory, f))]

    # make sure there are enough text chunks to make num combinations
    # of them.
    txt = ""
    count = 0
    for f in files:
        print("reading %s..." % (f))
        txt += read(f)
        num_chunks = len(txt) / length
        count = count + 1
        if num < nCr(num_chunks, 2):
            break

    print("Read %s/%s files in '%s'" % (count, len(files), directory))

    chunks = [txt[x:x + length] for x in range(0, len(txt), length)]

    print("Calculating distance average of %s measurements of text " +
          "strings length %s...") % (num, length)

    return list(islice(combinations(chunks, 2), 0, num))
项目:dalila    作者:slipguru    | 项目源码 | 文件源码
def apply_prox_operator(self, x, gamma):
        if self._lambda < 0:
            logging.error("A negative regularisation parameter was used")
            raise ValueError("A negative regularization parameter was used")

        l = list(set().union(*self._groups))
        if not (l == list(np.arange(x.shape[1]))):
            logging.error("The groups in group lasso must cover all the "
                          "features")
            raise ValueError("The groups in group lasso must cover all the "
                          "features")

        for pair in combinations(self._groups, r=2):
            if len(set(pair[0]) & set(pair[1])) > 0:
                logging.error("There are overlapping groups")
                raise ValueError("There are overlapping groups")

        new_x = np.zeros_like(x)
        for r in range(0, x.shape[0]):
            for g in self._groups:
                new_x[r, g] = self.prox_operator(x[r, g], gamma)
        return new_x
项目:beans    作者:Yelp    | 项目源码 | 文件源码
def test_no_re_matches(minimal_database):
    pref_1 = SubscriptionDateTime(datetime=datetime.now() - timedelta(weeks=MEETING_COOLDOWN_WEEKS - 1)).put()
    subscription = MeetingSubscription(title='all engineering weekly', datetime=[pref_1]).put()
    user_pref = UserSubscriptionPreferences(preference=pref_1, subscription=subscription).put()
    meeting_spec = MeetingSpec(meeting_subscription=subscription, datetime=pref_1.get().datetime)
    meeting_spec.put()

    users = []
    num_users = 20
    for i in range(0, num_users):
        user = User(email='{}@yelp.com'.format(i), metadata={
                    'department': 'dept{}'.format(i)}, subscription_preferences=[user_pref])
        user.put()
        MeetingRequest(user=user.key, meeting_spec=meeting_spec.key).put()
        users.append(user)

    previous_meetings = {pair for pair in itertools.combinations([user.key.id() for user in users], 2)}
    previous_meetings = previous_meetings - {(users[0].key.id(), users[1].key.id())}
    matches, unmatched = generate_meetings(users, meeting_spec, previous_meetings)
    assert len(unmatched) == num_users - 2
    assert [(match[0].key.id(), match[1].key.id()) for match in matches] == [(users[0].key.id(), users[1].key.id())]
项目:beans    作者:Yelp    | 项目源码 | 文件源码
def get_disallowed_meetings(users, prev_meeting_tuples, spec):
    """Returns set of matches that are not allowed
    Returns:
        Set of tuples
    """
    # don't match users with previous meetings
    pairs = prev_meeting_tuples

    userids = sorted([user.key.id() for user in users])
    id_to_user = {user.key.id(): user for user in users}
    all_pairs = {pair for pair in itertools.combinations(userids, 2)}

    for rule in spec.meeting_subscription.get().dept_rules:
        rule = rule.get()
        pairs = pairs.union({pair for pair in all_pairs if is_same(rule.name, pair, id_to_user)})
    return pairs
项目:pyBN    作者:ncullen93    | 项目源码 | 文件源码
def orient_edges_gs2(edge_dict, Mb, data, alpha):
    """
    Similar algorithm as above, but slightly modified for speed?
    Need to test.
    """
    d_edge_dict = dict([(rv,[]) for rv in edge_dict])
    for X in edge_dict.keys():
        for Y in edge_dict[X]:
            nxy = set(edge_dict[X]) - set(edge_dict[Y]) - {Y}
            for Z in nxy:
                if Y not in d_edge_dict[X]:
                    d_edge_dict[X].append(Y) # SET Y -> X
                B = min(set(Mb[Y]) - {X} - {Z},set(Mb[Z]) - {X} - {Y})
                for i in range(len(B)):
                    for S in itertools.combinations(B,i):
                        cols = (Y,Z,X) + tuple(S)
                        pval = mi_test(data[:,cols])
                        if pval < alpha and X in d_edge_dict[Y]: # Y IS independent of Z given S+X
                            d_edge_dict[Y].remove(X)
                if X in d_edge_dict[Y]:
                    break
    return d_edge_dict
项目:Splipy    作者:sintefmath    | 项目源码 | 文件源码
def sections(src_dim, tgt_dim):
    """Generate all boundary sections from a source dimension to a target
    dimension. For example, `sections(3,1)` generates all edges on a volume.

    The return values are lists of length `src_dim` with each element either 0,
    --1 or `None`, which are suitable for passing to
    :func:`splipy.SplineObject.section`.
    """
    # Enumerate all combinations of fixed directions
    nfixed = src_dim - tgt_dim
    for fixed in combinations(range(src_dim), r=nfixed):
        # Enumerate all {0,-1}^n over the fixed directions
        for indices in product([0, -1], repeat=nfixed):
            args = [None] * src_dim
            for f, i in zip(fixed, indices[::-1]):
                args[f] = i
            yield args
项目:Causality    作者:vcla    | 项目源码 | 文件源码
def powerset(iterable):
    "powerset([1,2,3]) --> () (1,) (2,) (3,) (1,2) (1,3) (2,3) (1,2,3)"
    s = list(iterable)
    return itertools.chain.from_iterable(itertools.combinations(s, r) for r in range(len(s)+1))
项目:NS_Proj    作者:drstarry    | 项目源码 | 文件源码
def get_count(l, comb):
    result = 0
    for n in range(1, comb+1):
        for sub_char_set in itertools.combinations(char_set, n):
            char_n = sum(sub_char_set)
            for m in range(n, 0, -1):
                sign = (-1) ** (n - m)
                for sets in itertools.combinations(sub_char_set, m):
                    result += sign * sum(sets) ** l
    return result
项目:zellij    作者:nedbat    | 项目源码 | 文件源码
def all_pairs(seq):
    """Produce all pairs from seq, but not (a, a)"""
    return itertools.combinations(seq, 2)
项目:blender-scripting    作者:njanakiev    | 项目源码 | 文件源码
def recursiveTetrahedron(bm, points, level=0):
    subTetras = []
    for i in range(len(points)):
        p0 = points[i]
        pK = points[:i] + points[i + 1:]
        subTetras.append([p0] + [(p0 + p)/2 for p in pK])

    if 0 < level:
        for subTetra in subTetras:
            recursiveTetrahedron(bm, subTetra, level-1)
    else:
        for subTetra in subTetras:
            verts = [bm.verts.new(p) for p in subTetra]
            faces = [bm.faces.new(face) for face in itertools.combinations(verts, 3)]
            bmesh.ops.recalc_face_normals(bm, faces=faces)
项目:scikit-dataaccess    作者:MITHaystack    | 项目源码 | 文件源码
def dateMismatch(dates, days=10):
    '''
    Check if dates are not within a certain number of days of each other

    @param dates: Iterable container of pandas timestamps
    @param days: Number of days

    @return true if they are not with 10 days, false otherwise
    '''
    for combo in combinations(dates,2):
        if np.abs(combo[0] - combo[1]) > pd.to_timedelta(days, 'D'):
            return True
    return False
项目:speccer    作者:bensimner    | 项目源码 | 文件源码
def generate(self, depth, t, *args, **kwargs):
            alls = list(Strategy[t](depth, *args, **kwargs))

            for n in range(depth):
                for p in itertools.combinations(alls, n):
                    yield set(p)
项目:Google-FooBar    作者:FoxHub    | 项目源码 | 文件源码
def answer(num_buns, num_required):
    """
    To start this problem, we calculate the number of ways we can arrange bunnies. This is:
        num_buns choose num_required

    This number determines how many distinct keys we have. So we then need to lexicographically hand off keys to each
    bunny and deputize them with the powers to open locks.

    At that point, this problem comes down to deciding how many keys to hand to each bunny. I had to think about that
    by hand.

    :param num_buns: The number of bunnies to distribute keys to.
    :param num_required: The "choose" part of our combinatorial.
    :return: bunny_keyrings, the enumerated keys belonging to each bunny in num_buns.
    """
    # One keyring per bunny.
    bunny_keyrings = [[] for num in range(num_buns)]
    # The number of keys each bunny requires is described by this formula, which I noticed by doing some napkin math.
    # If N == 4 and M == 4, bunnies_per_key == 1.
    # If N == 2 and M == 1, bunnies_per_key == 2.
    # If N == 5 and M == 3, bunnies_per_key == 3.
    # This generally fit the formula bunnies_per_key = N - M + 1.
    bunnies_per_key = num_buns - num_required + 1

    # itertools' enumerate function:
    # def enumerate(sequence, start=0):
    #     n = start
    #     for elem in sequence:
    #         yield n, elem
    #         n += 1
    # This yields a generator! So by generating combinations one at a time, we will get num_buns choose num_required
    # different permutations of num_buns, and we can assign each one as a bunny's keyring. Since this covers all
    # combinations, it should also assure that in all situations, if we pick num_required bunnies, they will all have
    # enough keys to open a cell.
    for keynum, keyholders in enumerate(combinations(range(num_buns), bunnies_per_key)):
        # print(keynum, keyholders)
        for index in keyholders:
            bunny_keyrings[index].append(keynum)

    return bunny_keyrings
项目:rosie    作者:datasciencebr    | 项目源码 | 文件源码
def __calculate_sum_distances(self, X):
        coordinate_list = X[['latitude', 'longitude']].values
        edges = list(combinations(coordinate_list, 2))
        return np.sum([distance(edge[0][1:], edge[1][1:]).km for edge in edges])
项目:ssbio    作者:SBRG    | 项目源码 | 文件源码
def run_fatcat_all_by_all(list_of_structure_paths, fatcat_sh, outdir='', silent=True, force_rerun=False):
    """Run FATCAT on all pairs of structures given a list of structures.

    Args:
        list_of_structure_paths (list): List of PDB file paths 
        fatcat_sh (str): Path to "runFATCAT.sh" executable script
        outdir (str): Path to where FATCAT XML output files will be saved 
        silent (bool): If command to run FATCAT should be printed to stdout
        force_rerun (bool): If FATCAT should be run even if XML output files already exist 

    Returns:
        Pandas DataFrame: TM-scores (similarity) between all structures

    """
    structure_ids = {x: i for i, x in enumerate(list_of_structure_paths)}

    comps = itertools.combinations(list_of_structure_paths, 2)
    tm_score_matrix = np.eye(len(list_of_structure_paths))

    for pdb1, pdb2 in tqdm(comps):
        fatcat_file = run_fatcat(pdb1, pdb2, fatcat_sh, outdir=outdir, silent=silent, force_rerun=force_rerun)
        tm_score_matrix[structure_ids[pdb1], structure_ids[pdb2]] = parse_fatcat(fatcat_file)['tm_score']
        tm_score_matrix[structure_ids[pdb2], structure_ids[pdb1]] = parse_fatcat(fatcat_file)['tm_score']

    # Convert to dataframe with filenames
    filenames = [op.splitext(op.basename(x))[0] for x in list_of_structure_paths]
    tm_score_matrix_annotated = pd.DataFrame(data=tm_score_matrix, columns=filenames, index=filenames)

    return tm_score_matrix_annotated
项目:TADPOLE    作者:noxtoby    | 项目源码 | 文件源码
def MAUC(data, num_classes):
    """
    Calculates the MAUC over a set of multi-class probabilities and
    their labels. This is equation 7 in Hand and Till's 2001 paper.
    NB: The class labels should be in the set [0,n-1] where n = # of classes.
    The class probability should be at the index of its label in the
    probability list.
    I.e. With 3 classes the labels should be 0, 1, 2. The class probability
    for class '1' will be found in index 1 in the class probability list
    wrapped inside the zipped list with the labels.
    Args:
        data (list): A zipped list (NOT A GENERATOR) of the labels and the
            class probabilities in the form (m = # data instances):
             [(label1, [p(x1c1), p(x1c2), ... p(x1cn)]),
              (label2, [p(x2c1), p(x2c2), ... p(x2cn)])
                             ...
              (labelm, [p(xmc1), p(xmc2), ... (pxmcn)])
             ]
        num_classes (int): The number of classes in the dataset.
    Returns:
        The MAUC as a floating point value.
    """
    # Find all pairwise comparisons of labels
    class_pairs = [x for x in itertools.combinations(range(num_classes), 2)]

    # Have to take average of A value with both classes acting as label 0 as this
    # gives different outputs for more than 2 classes
    sum_avals = 0
    for pairing in class_pairs:
        sum_avals += (a_value(data, zero_label=pairing[0], one_label=pairing[1]) +
                      a_value(data, zero_label=pairing[1], one_label=pairing[0])) / 2.0

    return sum_avals * (2 / float(num_classes * (num_classes-1)))  # Eqn 7