Skip to content

utils module

This module contains some utility programs used by the package.

change_indices(nests)

subtracts 1 from the indices within the nest structure

Parameters:

Name Type Description Default
nests NestsList

the nest structure

required

Returns:

Type Description
NestsList

a similar list

Source code in cupid_matching/utils.py
55
56
57
58
59
60
61
62
63
64
def change_indices(nests: NestsList) -> NestsList:
    """subtracts 1 from the indices within the nest structure

    Args:
        nests: the nest structure

    Returns:
        a similar list
    """
    return [[nest_i - 1 for nest_i in nest] for nest in nests]

find_nest_of(nests, y)

find the index of the nest that contains y, or return -1

Parameters:

Name Type Description Default
nests NestsList

a nest structure

required
y int

the type we are looking for

required

Returns:

Type Description
int

the nest of y, or -1 if not found

Source code in cupid_matching/utils.py
67
68
69
70
71
72
73
74
75
76
77
78
79
80
def find_nest_of(nests: NestsList, y: int) -> int:
    """find the index of the nest that contains y, or return -1

    Args:
        nests: a nest structure
        y: the type we are looking for

    Returns:
        the nest of y, or -1 if not found
    """
    for i_n, nest in enumerate(nests):
        if y in nest:
            return i_n
    return -1  # if not found

make_XY_K_mat(xyk_array)

Reshapes an (X,Y,K) array to an (XY,K) matrix.

Parameters:

Name Type Description Default
xyk_array np.ndarray

an (X, Y, K) array of bases

required

Returns:

Type Description
np.ndarray

the same, (XY, K)-reshaped

Source code in cupid_matching/utils.py
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
def make_XY_K_mat(xyk_array: np.ndarray) -> np.ndarray:
    """Reshapes an (X,Y,K) array to an (XY,K) matrix.

    Args:
        xyk_array: an (X, Y, K) array of bases

    Returns:
        the same,  (XY, K)-reshaped
    """
    X, Y, K = xyk_array.shape
    XY = X * Y
    xy_k_mat = np.zeros((XY, K))
    for k in range(K):
        xy_k_mat[:, k] = xyk_array[:, :, k].ravel()
    return xy_k_mat

reshape4_to2(array4)

Reshapes an array (X,Y,Z,T) to a matrix (XY,ZT).

Parameters:

Name Type Description Default
array4 np.ndarray

an (X, Y, Z, T) array

required

Returns:

Type Description
np.ndarray

the same, (XY, ZT)-reshaped

Source code in cupid_matching/utils.py
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
def reshape4_to2(array4: np.ndarray) -> np.ndarray:
    """Reshapes an array (X,Y,Z,T) to a matrix (XY,ZT).

    Args:
        array4: an (X, Y, Z, T) array

    Returns:
        the same,  (XY, ZT)-reshaped
    """
    if array4.ndim != 4:
        bs_error_abort(f"array4 should have 4 dimensions not {array4.ndim}")
    X, Y, Z, T = array4.shape
    XY, ZT = X * Y, Z * T
    array2 = np.zeros((XY, ZT))
    xy = 0
    for x in range(X):
        for y in range(Y):
            array2[xy, :] = array4[x, y, :, :].ravel()
            xy += 1
    return array2