-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtest_basic.py
More file actions
282 lines (227 loc) · 7.6 KB
/
test_basic.py
File metadata and controls
282 lines (227 loc) · 7.6 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
#!/usr/bin/env python3
"""
Basic test script for plotnine MCP server functionality.
"""
import sys
import pandas as pd
from pathlib import Path
# Add src to path
sys.path.insert(0, str(Path(__file__).parent / "src"))
from plotnine_mcp.data_loader import load_data, DataSource
from plotnine_mcp.plot_builder import build_plot, save_plot
from plotnine_mcp.schemas import (
Aesthetics,
GeomConfig,
ThemeConfig,
LabelsConfig,
OutputConfig,
)
def test_inline_data_scatter_plot():
"""Test creating a simple scatter plot with inline data."""
print("Test 1: Simple scatter plot with inline data...")
# Create data source
data_source = DataSource(
type="inline",
data=[
{"x": 1, "y": 2, "category": "A"},
{"x": 2, "y": 4, "category": "A"},
{"x": 3, "y": 3, "category": "B"},
{"x": 4, "y": 5, "category": "B"},
{"x": 5, "y": 6, "category": "C"},
],
)
# Load data
data = load_data(data_source)
print(f" ✓ Data loaded: {len(data)} rows")
# Create plot configuration
aes_config = Aesthetics(x="x", y="y", color="category")
geom_config = GeomConfig(type="point", params={"size": 3})
theme_config = ThemeConfig(base="minimal")
labels_config = LabelsConfig(
title="Test Scatter Plot", x="X Values", y="Y Values"
)
output_config = OutputConfig(
format="png", filename="test_scatter.png", directory="./output"
)
# Build plot
plot = build_plot(
data=data,
aes_config=aes_config,
geom_config=geom_config,
theme_config=theme_config,
labels_config=labels_config,
)
print(" ✓ Plot built successfully")
# Save plot
result = save_plot(plot, output_config)
print(f" ✓ Plot saved to: {result['path']}")
return result
def test_file_data_line_plot():
"""Test creating a line plot from CSV file."""
print("\nTest 2: Line plot from CSV file...")
# Create data source
data_source = DataSource(
type="file", path="./examples/sample_data.csv", format="csv"
)
# Load data
data = load_data(data_source)
print(f" ✓ Data loaded: {len(data)} rows, columns: {list(data.columns)}")
# Create plot configuration
aes_config = Aesthetics(x="x", y="y", color="category")
geom_config = GeomConfig(type="line", params={"size": 1.5})
theme_config = ThemeConfig(
base="bw",
customizations={"figure_size": [10, 6], "legend_position": "bottom"},
)
labels_config = LabelsConfig(
title="Sample Data Line Plot", x="X Axis", y="Y Axis"
)
output_config = OutputConfig(
format="png", filename="test_line.png", directory="./output"
)
# Build plot
plot = build_plot(
data=data,
aes_config=aes_config,
geom_config=geom_config,
theme_config=theme_config,
labels_config=labels_config,
)
print(" ✓ Plot built successfully")
# Save plot
result = save_plot(plot, output_config)
print(f" ✓ Plot saved to: {result['path']}")
return result
def test_bar_plot():
"""Test creating a bar plot."""
print("\nTest 3: Bar plot...")
# Create data
data_source = DataSource(
type="inline",
data=[
{"category": "A", "value": 10},
{"category": "B", "value": 15},
{"category": "C", "value": 12},
{"category": "D", "value": 18},
],
)
data = load_data(data_source)
print(f" ✓ Data loaded: {len(data)} rows")
# Create plot
aes_config = Aesthetics(x="category", y="value", fill="category")
geom_config = GeomConfig(type="col")
theme_config = ThemeConfig(base="classic")
labels_config = LabelsConfig(title="Bar Chart Example", x="Category", y="Value")
output_config = OutputConfig(
format="png", filename="test_bar.png", directory="./output"
)
plot = build_plot(
data=data,
aes_config=aes_config,
geom_config=geom_config,
theme_config=theme_config,
labels_config=labels_config,
)
print(" ✓ Plot built successfully")
result = save_plot(plot, output_config)
print(f" ✓ Plot saved to: {result['path']}")
return result
def test_multi_layer_plot():
"""Test creating a multi-layer plot (scatter + smooth)."""
print("\nTest 4: Multi-layer plot (scatter + smooth)...")
# Create data source
data_source = DataSource(
type="file", path="./examples/sample_data.csv", format="csv"
)
data = load_data(data_source)
print(f" ✓ Data loaded: {len(data)} rows")
# Create plot with multiple geoms
aes_config = Aesthetics(x="x", y="y", color="category")
geom_configs = [
GeomConfig(type="point", params={"size": 3, "alpha": 0.7}),
GeomConfig(type="smooth", params={"method": "lm", "se": False}),
]
theme_config = ThemeConfig(
base="minimal", customizations={"figure_size": [10, 6]}
)
labels_config = LabelsConfig(
title="Multi-Layer Plot: Points + Smooth Trend",
x="X Values",
y="Y Values",
)
output_config = OutputConfig(
format="png", filename="test_multi_layer.png", directory="./output"
)
plot = build_plot(
data=data,
aes_config=aes_config,
geom_configs=geom_configs,
theme_config=theme_config,
labels_config=labels_config,
)
print(" ✓ Multi-layer plot built successfully")
result = save_plot(plot, output_config)
print(f" ✓ Plot saved to: {result['path']}")
return result
def test_boxplot_with_jitter():
"""Test boxplot with jittered points overlay."""
print("\nTest 5: Boxplot with jittered points...")
# Create data
data_source = DataSource(
type="file", path="./examples/sample_data.csv", format="csv"
)
data = load_data(data_source)
print(f" ✓ Data loaded: {len(data)} rows")
# Create layered plot
aes_config = Aesthetics(x="category", y="y", fill="category")
geom_configs = [
GeomConfig(type="boxplot", params={"alpha": 0.7}),
GeomConfig(type="jitter", params={"width": 0.2, "alpha": 0.5}),
]
theme_config = ThemeConfig(base="bw")
labels_config = LabelsConfig(
title="Boxplot with Individual Points",
x="Category",
y="Values",
)
output_config = OutputConfig(
format="png", filename="test_boxplot_jitter.png", directory="./output"
)
plot = build_plot(
data=data,
aes_config=aes_config,
geom_configs=geom_configs,
theme_config=theme_config,
labels_config=labels_config,
)
print(" ✓ Boxplot with jitter built successfully")
result = save_plot(plot, output_config)
print(f" ✓ Plot saved to: {result['path']}")
return result
def main():
"""Run all tests."""
print("=" * 60)
print("Running Plotnine MCP Basic Tests")
print("=" * 60)
try:
# Test 1: Scatter plot with inline data
test_inline_data_scatter_plot()
# Test 2: Line plot from file
test_file_data_line_plot()
# Test 3: Bar plot
test_bar_plot()
# Test 4: Multi-layer plot
test_multi_layer_plot()
# Test 5: Boxplot with jitter
test_boxplot_with_jitter()
print("\n" + "=" * 60)
print("All tests passed! ✓")
print("=" * 60)
print("\nCheck the ./output directory for generated plots.")
except Exception as e:
print(f"\n✗ Test failed with error: {e}")
import traceback
traceback.print_exc()
sys.exit(1)
if __name__ == "__main__":
main()