Coverage for NeuralTSNE/NeuralTSNE/Plotter/plot.py: 97%
27 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
1from typing import List
2import matplotlib.pyplot as plt
3import numpy as np
6def plot(
7 data: np.ndarray,
8 labels: np.ndarray | None,
9 step: int,
10 marker_size: int,
11 alpha: float,
12 are_neural_labels: bool = False,
13 img_file: str | None = None,
14 kwargs: dict | None = None,
15) -> None:
16 """
17 Plot t-SNE results.
19 Parameters
20 ----------
21 `data` : `np.ndarray`
22 t-SNE data to be plotted.
23 `labels` : `np.ndarray`, optional
24 Labels corresponding to the data points.
25 `step` : `int`
26 Step size for subsampling the data.
27 `marker_size` : `int`
28 Marker size for the scatter plot.
29 `alpha` : `float`
30 Alpha value for transparency in the scatter plot.
31 `are_neural_labels` : `bool`, optional
32 Flag indicating whether the labels are neural network predictions.
33 `img_file` : `str`, optional
34 File path to save the plot as an image.
35 `**kwargs` : `dict`, optional
36 Additional keyword arguments.
38 Important
39 ---------
40 The following additional keyword arguments are available:
42 `file_step` : `int`, optional
43 Step size for subsampling labels. Defaults to `1`.
45 Note
46 ----
47 This function plots the t-SNE results with scatter plot, allowing customization of various plot parameters.
48 """
49 if kwargs is None:
50 kwargs = {}
51 f_step = kwargs.get("file_step", 1)
53 plt.subplots(1, 1)
55 (
56 plt.scatter(
57 data[::step, 0],
58 data[::step, 1],
59 marker_size,
60 alpha=alpha,
61 marker=".",
62 )
63 if labels is None
64 else plt.scatter(
65 data[::step, 0],
66 data[::step, 1],
67 marker_size,
68 labels[:: f_step * step] if not are_neural_labels else labels[::step],
69 alpha=alpha,
70 marker=".",
71 )
72 )
74 plt.ylabel("t-SNE 2")
75 plt.xlabel("t-SNE 1")
77 if img_file: 77 ↛ 80line 77 didn't jump to line 80 because the condition on line 77 was always true
78 new_name = img_file
79 plt.savefig(new_name)
80 plt.show()
83def plot_from_file(
84 file: str,
85 labels_file: str,
86 columns: List[int],
87 step: int,
88 marker_size: int,
89 alpha: float,
90 are_neural_labels: bool = False,
91) -> None:
92 """
93 Plot t-SNE results from file.
95 Parameters
96 ----------
97 `file` : `str`
98 File path containing t-SNE data.
99 `labels_file` : `str`
100 File path containing labels data.
101 `columns` : `List[int]`
102 Column indices to load from the labels file.
103 `step` : `int`
104 Step size for subsampling the data.
105 `marker_size` : `int`
106 Marker size for the scatter plot.
107 `alpha` : `float`
108 Alpha value for transparency in the scatter plot.
109 `are_neural_labels` : `bool`, optional
110 Flag indicating whether the labels are neural network predictions.
112 Note
113 ----
114 This function reads t-SNE data and labels from files, applies subsampling, and plots the results using the `plot` function.
115 """
116 data = None
117 file_step = None
119 with open(file, "r") as f:
120 file_step = int(f.readline())
121 data = np.loadtxt(f)
123 labels = None
124 if labels_file:
125 with open(labels_file, "r") as f:
126 labels = np.loadtxt(f, usecols=columns, dtype="int")
127 data = data[: len(labels)]
129 plot(
130 data,
131 labels,
132 step,
133 marker_size,
134 alpha,
135 are_neural_labels,
136 file.rsplit(".", 1)[0] + ".png",
137 {"file_step": file_step},
138 )