Skip to content

create_partitions

Splits a vector of a specified length into nearly equal partitions.

This function creates a partition vector where each partition is of equal size, except the last partition which may be smaller depending on the vector length and the number of equal parts specified. Each element in the partition vector represents the partition number.

Parameters:

Name Type Description Default
vector_length int

The total length of the vector to be partitioned.

required
equal_parts int

The number of equal parts to divide the vector. Defaults to 100.

100

Returns:

Type Description

numpy.ndarray: An array where each element represents the partition number for each element in the original vector.

Examples:

>>> create_partitions(10, 3)
array([1, 1, 1, 2, 2, 2, 3, 3, 3, 3])
Source code in callingcardstools/Analysis/yeast/rank_response.py
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
def create_partitions(vector_length, equal_parts=100):
    """
    Splits a vector of a specified length into nearly equal partitions.

    This function creates a partition vector where each partition is of equal
    size, except the last partition which may be smaller depending on the
    vector length and the number of equal parts specified. Each element in
    the partition vector represents the partition number.

    Args:
        vector_length (int): The total length of the vector to be partitioned.
        equal_parts (int, optional): The number of equal parts to divide the
            vector. Defaults to 100.

    Returns:
        numpy.ndarray: An array where each element represents the partition
            number for each element in the original vector.

    Examples:
        >>> create_partitions(10, 3)
        array([1, 1, 1, 2, 2, 2, 3, 3, 3, 3])
    """
    quotient, remainder = divmod(vector_length, equal_parts)
    return np.concatenate(
        [
            np.repeat(np.arange(1, quotient + 1), equal_parts),
            np.repeat(quotient + 1, remainder),
        ]
    )