Skip to content

utils

Class with helper functions.

MIT License Copyright © 2021-2022, Daniel Nagel All rights reserved.

savetxt(filename, array, fmt, submodule=None, header=None)

Save ndarray with user runtime information.

Source code in src/mosaic/utils.py
45
46
47
48
49
50
51
52
53
54
55
56
def savetxt(filename, array, fmt, submodule=None, header=None):
    """Save ndarray with user runtime information."""
    header_generic = _get_rui(submodule)
    if header:
        header_generic = f'{header_generic}\n\n{header}'

    np.savetxt(
        filename,
        array,
        fmt=fmt,
        header=header_generic,
    )

load_clusters(filename)

Load clusters stored from cli.

Parameters:

  • filename (str) –

    Filename of cluster file.

Returns:

  • clusters ( ndarray of shape (n_clusters, ) ) –

    A list of arrays, each containing all indices (features) for each cluster.

Source code in src/mosaic/utils.py
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
@beartype
def load_clusters(filename: str) -> Object1DArray:
    """Load clusters stored from cli.

    Parameters
    ----------
    filename : str
        Filename of cluster file.

    Returns
    -------
    clusters : ndarray of shape (n_clusters, )
        A list of arrays, each containing all indices (features) for each
        cluster.

    """
    comment = '#'
    with open(filename) as clusters:
        clusters_list = [
            np.array(
                cluster.split()
            ).astype(int).tolist()
            for cluster in clusters if not cluster.startswith(comment)
        ]

    # In case of clusters of same length, numpy casted it as a 2D array.
    # To ensure that the result is an numpy array of list, we need to
    # create an empty list, adding the values in the second step
    clusters: Object1DArray = np.empty(len(clusters_list), dtype=object)
    clusters[:] = clusters_list  # noqa: WPS362
    return clusters

save_clusters(filename, clusters)

Save clusters from mosaic.Clustering.clusters_ to txt file.

Parameters:

  • filename (str) –

    Filename of cluster file.

  • clusters (ndarray of shape (n_clusters, )) –

    A list of arrays, each containing all indices (features) for each cluster.

Source code in src/mosaic/utils.py
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
@beartype
def save_clusters(filename: str, clusters: Object1DArray):
    """Save clusters from `mosaic.Clustering.clusters_` to txt file.

    Parameters
    ----------
    filename : str
        Filename of cluster file.
    clusters : ndarray of shape (n_clusters, )
        A list of arrays, each containing all indices (features) for each
        cluster.

    """
    clusters_string = np.array(
        [
            ' '.join([str(state) for state in cluster])
            for cluster in clusters
        ],
        dtype=str,
    )
    savetxt(
        filename,
        clusters_string,
        fmt='%s',
        submodule='clustering',
        header=(
            'In ith row are the indices listed (zero-indexed) corresponding '
            'to cluster i.'
        ),
    )