-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmatplotex_test.exs
More file actions
320 lines (277 loc) · 9.82 KB
/
matplotex_test.exs
File metadata and controls
320 lines (277 loc) · 9.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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
90
91
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
defmodule MatplotexTest do
alias Matplotex.Figure.Dataset
alias Matplotex.InputError
use Matplotex.PlotCase
alias Matplotex.Figure
setup do
x = [1, 3, 7, 4, 2, 5, 6]
y = [1, 3, 7, 4, 2, 5, 6]
figure = Matplotex.plot(x, y)
{:ok, %{figure: figure}}
end
test "plot can create a figure with axes by data and setup tick and limits" do
x = [1, 3, 7, 4, 2, 5, 6]
y = [1, 3, 7, 4, 2, 5, 6]
assert figure = Matplotex.plot(x, y)
assert figure.axes.dataset |> List.first() |> Map.get(:x) == x
assert figure.axes.dataset |> List.first() |> Map.get(:y) == y
end
test "raise error for invalid input" do
assert_raise InputError, "Invalid x and y values for plot, x and y should be in list", fn ->
Matplotex.plot("ser", "ser")
end
end
test "adds xlabel to the figure ", %{figure: figure} do
x_label = "Xlabel"
figure = Matplotex.set_xlabel(figure, x_label)
assert figure.axes.label.x == x_label
end
test "adds ylabel to the figure ", %{figure: figure} do
y_label = "Ylabel"
figure = Matplotex.set_ylabel(figure, y_label)
assert figure.axes.label.y == y_label
end
test "adds title to the figure ", %{figure: figure} do
title = "My Plot"
figure = Matplotex.set_title(figure, title)
assert figure.axes.title == title
end
# test "adds legend to the figure ", %{figure: figure} do
# labels = ["Data 1", "Data 2"]
# figure = Matplotex.legend(figure, %{labels: labels})
# assert figure.axes.legend.labels == labels
# end
test "adds xlim to the figure ", %{figure: figure} do
xlim = {1, 10}
figure = Matplotex.set_xlim(figure, xlim)
assert figure.axes.limit.x == xlim
end
test "adds ylim to the figure ", %{figure: figure} do
ylim = {1, 10}
figure = Matplotex.set_ylim(figure, ylim)
assert figure.axes.limit.y == ylim
end
test "adds rc_params to the figure ", %{figure: figure} do
rc_params = %{
"x_tick_font_size" => 12,
"y_tick_font_size" => 16,
"x_label_font_size" => 14,
"y_label_font_size" => 10,
"line_width" => 2,
"line_style" => "_"
}
figure = Matplotex.set_rc_params(figure, rc_params)
assert figure.rc_params.line_width == 2
assert figure.rc_params.line_style == "_"
assert figure.rc_params.x_tick_font_size == 12
assert figure.rc_params.y_tick_font_size == 16
end
test "figure updates the params of the figure", %{figure: figure} do
size = {5, 4}
figure = Matplotex.figure(figure, %{figsize: size})
assert figure.figsize == size
end
test "raise error if invalid params passed", %{figure: figure} do
assert_raise Matplotex.InputError, "Invalid keys", fn ->
Matplotex.figure(figure, %{invalid: "im not valid"})
end
end
test "svg string with border lines", %{figure: figure} do
frame_width = 8
frame_height = 6
size = {frame_width, frame_height}
margin = 0.1
font_size = 0
title_font_size = 0
ticks = [1, 2, 3, 4, 5, 6, 7]
figure_mater =
figure
|> Matplotex.figure(%{figsize: size, margin: margin})
|> Matplotex.set_title("The Plot Title")
|> Matplotex.set_xlabel("X Axis")
|> Matplotex.set_ylabel("Y Axis")
|> Matplotex.set_xticks(ticks)
|> Matplotex.set_yticks(ticks)
|> Matplotex.set_rc_params(
x_tick_font_size: font_size,
y_tick_font_size: font_size,
title_font_size: title_font_size,
x_label_font_size: font_size,
y_label_font_size: font_size,
title_font_size: title_font_size
)
assert Matplotex.show(figure_mater) |> is_binary()
end
describe "set_figure_size" do
test "updates the frame size based on margin also", %{figure: figure} do
fwidth = 10
fheight = 8
assert %Figure{margin: margin, axes: %{size: {width, height}}} =
Matplotex.set_figure_size(figure, {fwidth, fheight})
assert width == fwidth - fwidth * margin * 2
assert height == fheight - fheight * margin * 2
end
end
describe "set_margin" do
test "updates the frame size according to margin", %{figure: figure} do
margin = 0.01
assert %Figure{figsize: {fwidth, fheight}, axes: %{size: {width, height}}} =
Matplotex.set_margin(figure, margin)
assert width == fwidth - fwidth * margin * 2
assert height == fheight - fheight * margin * 2
end
end
describe "with plot options" do
test "updates the values according to the plot options in line_plot" do
x = [1, 3, 7, 4, 2, 5, 6]
y = [1, 3, 7, 4, 2, 5, 6]
options = [
x_label: "X Axis",
y_label: "Y Axis",
title: "The Plot Title",
x_ticks: [1, 2, 3, 4, 5, 6, 7],
y_ticks: [1, 2, 3, 4, 5, 6, 7],
x_tick_font_size: 12,
y_tick_font_size: 16,
title_font_size: 14,
x_label_font_size: 10,
y_label_font_size: 10,
title_font_size: 14,
line_width: 2,
figsize: {10, 10},
x_limit: [1, 7],
y_limit: [1, 7],
line_style: "_"
]
figure = Matplotex.plot(x, y, options)
assert figure.axes.label.x == "X Axis"
assert figure.axes.label.y == "Y Axis"
assert figure.axes.title == "The Plot Title"
assert figure.axes.dataset |> List.first() |> Map.get(:x) == x
assert figure.axes.dataset |> List.first() |> Map.get(:y) == y
assert figure.axes.limit.x == [1, 7]
assert figure.axes.limit.y == [1, 7]
assert figure.rc_params.line_width == 2
assert figure.figsize == {10, 10}
assert figure.rc_params.line_style == "_"
assert figure.rc_params.x_tick_font.font_size == 12
assert figure.rc_params.y_tick_font.font_size == 16
assert figure.rc_params.title_font.font_size == 14
assert figure.rc_params.x_label_font.font_size == 10
assert figure.rc_params.y_label_font.font_size == 10
end
end
test "updates the values according to the plot options in bar_chart" do
values = [1, 3, 7, 4, 2, 5, 6]
width = 0.22
options = [
x_label: "X Axis",
y_label: "Y Axis",
title: "The Plot Title",
x_ticks: [1, 2, 3, 4, 5, 6, 7],
y_ticks: [1, 2, 3, 4, 5, 6, 7],
x_tick_font_size: 12,
y_tick_font_size: 16,
title_font_size: 14,
x_label_font_size: 10,
y_label_font_size: 10,
title_font_size: 14,
line_width: 2,
figsize: {10, 10},
x_limit: [1, 7],
y_limit: [1, 7],
line_style: "_"
]
figure = Matplotex.bar(values, width, options)
assert figure.axes.label.x == "X Axis"
assert figure.axes.label.y == "Y Axis"
assert figure.axes.title == "The Plot Title"
assert figure.axes.dataset |> List.first() |> Map.get(:y) == values
assert figure.axes.limit.x == [1, 7]
assert figure.axes.limit.y == [1, 7]
assert figure.rc_params.line_width == 2
assert figure.figsize == {10, 10}
assert figure.rc_params.line_style == "_"
assert figure.rc_params.x_tick_font.font_size == 12
assert figure.rc_params.y_tick_font.font_size == 16
assert figure.rc_params.title_font.font_size == 14
assert figure.rc_params.x_label_font.font_size == 10
assert figure.rc_params.y_label_font.font_size == 10
end
test "updates the values according to the plot options in scatter plot" do
x = [1, 3, 7, 4, 2, 5, 6]
y = [1, 3, 7, 4, 2, 5, 6]
options = [
x_label: "X Axis",
y_label: "Y Axis",
title: "The Plot Title",
x_ticks: [1, 2, 3, 4, 5, 6, 7],
y_ticks: [1, 2, 3, 4, 5, 6, 7],
x_tick_font_size: 12,
y_tick_font_size: 16,
title_font_size: 14,
x_label_font_size: 10,
y_label_font_size: 10,
title_font_size: 14,
line_width: 2,
figsize: {10, 10},
x_limit: [1, 7],
y_limit: [1, 7],
line_style: "_"
]
figure = Matplotex.scatter(x, y, options)
assert figure.axes.label.x == "X Axis"
assert figure.axes.label.y == "Y Axis"
assert figure.axes.title == "The Plot Title"
assert figure.axes.dataset |> List.first() |> Map.get(:x) == x
assert figure.axes.dataset |> List.first() |> Map.get(:y) == y
assert figure.axes.limit.x == [1, 7]
assert figure.axes.limit.y == [1, 7]
assert figure.rc_params.line_width == 2
assert figure.figsize == {10, 10}
assert figure.rc_params.line_style == "_"
assert figure.rc_params.x_tick_font.font_size == 12
assert figure.rc_params.y_tick_font.font_size == 16
assert figure.rc_params.title_font.font_size == 14
assert figure.rc_params.x_label_font.font_size == 10
assert figure.rc_params.y_label_font.font_size == 10
end
describe "hist" do
test "creates a figure for histogram" do
values =
Nx.Random.key(12) |> Nx.Random.normal(0, 1, shape: {1000}) |> elem(0) |> Nx.to_list()
bins = 30
figure = Matplotex.hist(values, bins, x_label: "Value", y_label: "Frequency")
assert %Dataset{x: x, y: y} = hd(figure.axes.dataset)
assert length(x) == bins
assert length(y) == bins
assert figure.axes.label.x == "Value"
assert figure.axes.label.y == "Frequency"
end
end
describe "spline" do
test "creates a figure for spline" do
x_nx = Nx.linspace(0, 10, n: 100)
x = Nx.to_list(x_nx)
y = x_nx |> Nx.sin() |> Nx.to_list()
assert %Figure{axes: %{dataset: [data1], label: label}} =
Matplotex.spline(x, y, x_label: "X", y_label: "Y")
assert label.x == "X"
assert label.y == "Y"
assert data1.x == x
assert data1.y == y
end
end
describe "step" do
test "creates a figure for step plot" do
x = [1, 2, 3, 4, 5]
y = [1,4,9,16,25]
assert %Figure{axes: %{dataset: [data1], label: label}} =
Matplotex.step(x, y, x_label: "X", y_label: "Y")
assert label.x == "X"
assert label.y == "Y"
assert data1.x|>Enum.uniq() == x
assert data1.y|>Enum.uniq() == y
end
end
end