-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopenpiv_client.py
More file actions
164 lines (138 loc) · 4.8 KB
/
openpiv_client.py
File metadata and controls
164 lines (138 loc) · 4.8 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
#!/usr/bin/env python3
"""
OpenPIV MCP Client - Use PIV analysis directly in your Python code.
This module provides a simple interface to use the OpenPIV MCP server
from your Python applications.
"""
import asyncio
import sys
import os
from typing import Optional
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "src"))
from mcp.client.stdio import stdio_client, StdioServerParameters
from mcp import ClientSession
async def compute_piv(
image_a_path: str,
image_b_path: str,
window_size: int = 32,
overlap: int = 16,
dt: float = 1.0,
output_dir: str = ""
) -> str:
"""
Compute PIV velocity field from two images.
Args:
image_a_path: Path to first image
image_b_path: Path to second image
window_size: Interrogation window size (default 32)
overlap: Overlap between windows (default 16)
dt: Time delay between frames (default 1.0)
output_dir: Output directory for results
Returns:
Result message with output path and statistics
"""
server_params = StdioServerParameters(
command="python",
args=[os.path.join(os.path.dirname(__file__), "src", "openpiv_mcp.py")],
)
async with stdio_client(server_params) as (read, write):
async with ClientSession(read, write) as session:
await session.initialize()
result = await session.call_tool(
"compute_piv",
arguments={
"image_a_path": image_a_path,
"image_b_path": image_b_path,
"window_size": window_size,
"overlap": overlap,
"dt": dt,
"output_dir": output_dir
}
)
return result.content[0].text if result.content else "No result"
async def create_quiver_plot(
csv_path: str,
output_path: str = "",
title: str = "PIV Velocity Field",
scale: int = 50,
width: float = 0.003,
cmap: str = "viridis"
) -> str:
"""
Create a quiver plot from PIV results.
Args:
csv_path: Path to PIV results CSV
output_path: Output path for PNG
title: Plot title
scale: Arrow scale factor
width: Arrow width
cmap: Colormap
Returns:
Result message with output path
"""
server_params = StdioServerParameters(
command="python",
args=[os.path.join(os.path.dirname(__file__), "src", "openpiv_mcp.py")],
)
async with stdio_client(server_params) as (read, write):
async with ClientSession(read, write) as session:
await session.initialize()
result = await session.call_tool(
"create_quiver_plot",
arguments={
"csv_path": csv_path,
"output_path": output_path,
"title": title,
"scale": scale,
"width": width,
"cmap": cmap
}
)
return result.content[0].text if result.content else "No result"
async def main():
"""Example usage."""
print("=" * 60)
print("OpenPIV MCP Client Demo")
print("=" * 60)
# Example: Compute PIV (using demo images if they exist)
demo_dir = os.path.join(os.path.dirname(__file__), "demo", "test1")
img_a = os.path.join(demo_dir, "exp1_001_a.tiff")
img_b = os.path.join(demo_dir, "exp1_001_b.tiff")
if os.path.exists(img_a) and os.path.exists(img_b):
print(f"\n1. Computing PIV on demo images...")
result = await compute_piv(img_a, img_b)
print(f"\n Result:\n{result}")
# Extract CSV path from result
if "piv_results.csv" in result:
import re
csv_match = re.search(r'/[\w/.]+/piv_results\.csv', result)
if csv_match:
csv_path = csv_match.group()
print(f"\n2. Creating quiver plot from {csv_path}...")
plot_result = await create_quiver_plot(csv_path)
print(f"\n Result:\n{plot_result}")
else:
print(f"Demo images not found at {demo_dir}")
print("\nTo use PIV analysis, call:")
print("""
import asyncio
from openpiv_client import compute_piv, create_quiver_plot
async def analyze():
# Compute velocity field
result = await compute_piv(
image_a_path="/path/to/image1.tiff",
image_b_path="/path/to/image2.tiff",
window_size=32,
overlap=16
)
print(result)
# Create visualization
plot_result = await create_quiver_plot(
csv_path="/path/to/piv_results.csv",
title="My Flow Field"
)
print(plot_result)
asyncio.run(analyze())
""")
if __name__ == "__main__":
asyncio.run(main())