def _make_metadata_tsv()

in python/mxboard/utils.py [0:0]


def _make_metadata_tsv(metadata, save_path):
    """Given an `NDArray` or a `numpy.ndarray` or a list as metadata e.g. labels, save the
    flattened array into the file metadata.tsv under the path provided by the user. The
    labels can be 1D or 2D with multiple labels per data point.
    Made to satisfy the requirement in the following link:
    https://www.tensorflow.org/programmers_guide/embedding#metadata"""
    if isinstance(metadata, NDArray):
        metadata = metadata.asnumpy()
    elif isinstance(metadata, list):
        metadata = np.array(metadata)
    elif not isinstance(metadata, np.ndarray):
        raise TypeError('expected NDArray or np.ndarray or 1D/2D list, while received '
                        'type {}'.format(str(type(metadata))))

    if len(metadata.shape) > 2:
        raise TypeError('expected a 1D/2D NDArray, np.ndarray or list, while received '
                        'shape {}'.format(str(metadata.shape)))

    if len(metadata.shape) == 1:
        metadata = metadata.reshape(-1, 1)
    with open(os.path.join(save_path, 'metadata.tsv'), 'w') as f:
        for row in metadata:
            f.write('\t'.join([str(x) for x in row]) + '\n')