Coverage for NeuralTSNE/NeuralTSNE/Utils/Loaders/LabelLoaders/label_loaders.py: 100%
11 statements
« prev ^ index » next coverage.py v7.8.0, created at 2025-05-18 16:32 +0000
« prev ^ index » next coverage.py v7.8.0, created at 2025-05-18 16:32 +0000
1import io
2from typing import Union
4import numpy as np
5import torch
8def load_labels(labels: io.TextIOWrapper) -> Union[torch.Tensor, None]:
9 """
10 Load labels from a text file into a `torch.Tensor`.
12 The function reads labels from the provided text file and converts them into a `torch.Tensor` of type `float`.
13 If the `labels` parameter is not provided or the file is empty, the function returns `None`.
15 Parameters
16 ----------
17 `labels` : `io.TextIOWrapper`
18 The `file` object containing labels to be loaded.
20 Returns
21 -------
22 `torch.Tensor` | `None`
23 A `torch.Tensor` containing loaded labels or `None` if no labels are available.
25 Note
26 ----
27 - The function expects the `labels` parameter to be a file object (`io.TextIOWrapper`) with labels in text format.
28 - If the file is not provided or is empty, the function returns `None`.
29 - The labels are read from the file using `numpy` and then converted to a `torch.Tensor` of type `float`.
30 """
31 read_labels = None
32 if labels:
33 read_labels = np.loadtxt(labels)
34 read_labels = torch.from_numpy(read_labels).float()
35 labels.close()
36 return read_labels