-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_samples.py
More file actions
186 lines (143 loc) · 5.46 KB
/
generate_samples.py
File metadata and controls
186 lines (143 loc) · 5.46 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
#!/usr/bin/env python3
"""
Generate sample VTI files for the examples directory.
"""
import numpy as np
from pathlib import Path
try:
import vtk
from vtk.util import numpy_support
VTK_AVAILABLE = True
except ImportError:
VTK_AVAILABLE = False
def create_gaussian_vti(output_path: Path, nx=30, ny=25, nz=20):
"""Create a simple Gaussian VTI file."""
if not VTK_AVAILABLE:
print("VTK not available")
return False
# Create image data
image_data = vtk.vtkImageData()
image_data.SetDimensions(nx, ny, nz)
image_data.SetSpacing(0.1, 0.1, 0.1)
image_data.SetOrigin(0.0, 0.0, 0.0)
# Create coordinate arrays
x = np.linspace(0, 3, nx)
y = np.linspace(0, 2.5, ny)
z = np.linspace(0, 2, nz)
# Create meshgrid
X, Y, Z = np.meshgrid(x, y, z, indexing='ij')
# Create scalar data - 3D Gaussian
center_x, center_y, center_z = 1.5, 1.25, 1.0
sigma = 0.8
gaussian = np.exp(-((X - center_x)**2 + (Y - center_y)**2 + (Z - center_z)**2) / (2 * sigma**2))
# Create distance field
distance = np.sqrt((X - center_x)**2 + (Y - center_y)**2 + (Z - center_z)**2)
# Create simple vector field (gradient)
vector_x = np.gradient(gaussian, axis=0)
vector_y = np.gradient(gaussian, axis=1)
vector_z = np.gradient(gaussian, axis=2)
# Flatten arrays for VTK
gaussian_flat = gaussian.flatten(order='F')
distance_flat = distance.flatten(order='F')
vector_flat = np.column_stack([
vector_x.flatten(order='F'),
vector_y.flatten(order='F'),
vector_z.flatten(order='F')
])
# Add scalar data
gaussian_array = numpy_support.numpy_to_vtk(gaussian_flat)
gaussian_array.SetName("temperature")
image_data.GetPointData().AddArray(gaussian_array)
image_data.GetPointData().SetActiveScalars("temperature")
# Add distance field
distance_array = numpy_support.numpy_to_vtk(distance_flat)
distance_array.SetName("distance")
image_data.GetPointData().AddArray(distance_array)
# Add vector field
vector_array = numpy_support.numpy_to_vtk(vector_flat)
vector_array.SetName("velocity")
image_data.GetPointData().AddArray(vector_array)
# Write to file
writer = vtk.vtkXMLImageDataWriter()
writer.SetFileName(str(output_path))
writer.SetInputData(image_data)
writer.Write()
return True
def create_wave_vti(output_path: Path, nx=40, ny=30, nz=25):
"""Create a wave pattern VTI file."""
if not VTK_AVAILABLE:
print("VTK not available")
return False
# Create image data
image_data = vtk.vtkImageData()
image_data.SetDimensions(nx, ny, nz)
image_data.SetSpacing(0.05, 0.05, 0.05)
image_data.SetOrigin(0.0, 0.0, 0.0)
# Create coordinate arrays
x = np.linspace(0, 2*np.pi, nx)
y = np.linspace(0, 2*np.pi, ny)
z = np.linspace(0, np.pi, nz)
# Create meshgrid
X, Y, Z = np.meshgrid(x, y, z, indexing='ij')
# Create wave patterns
wave1 = np.sin(X) * np.cos(Y) * np.sin(Z)
wave2 = np.cos(X + Y) * np.sin(Z)
pressure = wave1 + 0.5 * wave2
# Create magnitude field
magnitude = np.sqrt(X**2 + Y**2 + Z**2)
# Flatten arrays for VTK
pressure_flat = pressure.flatten(order='F')
magnitude_flat = magnitude.flatten(order='F')
# Add scalar data
pressure_array = numpy_support.numpy_to_vtk(pressure_flat)
pressure_array.SetName("pressure")
image_data.GetPointData().AddArray(pressure_array)
image_data.GetPointData().SetActiveScalars("pressure")
# Add magnitude field
magnitude_array = numpy_support.numpy_to_vtk(magnitude_flat)
magnitude_array.SetName("magnitude")
image_data.GetPointData().AddArray(magnitude_array)
# Write to file
writer = vtk.vtkXMLImageDataWriter()
writer.SetFileName(str(output_path))
writer.SetInputData(image_data)
writer.Write()
return True
def main():
"""Generate sample VTI files."""
if not VTK_AVAILABLE:
print("VTK not available. Cannot generate sample files.")
return False
# Create output directories
examples_dir = Path("examples/sample_data")
tests_dir = Path("tests/sample_data")
examples_dir.mkdir(parents=True, exist_ok=True)
tests_dir.mkdir(parents=True, exist_ok=True)
print("Generating sample VTI files...")
# Generate files
files_created = []
# Simple Gaussian (small file)
gaussian_file = examples_dir / "gaussian_simple.vti"
if create_gaussian_vti(gaussian_file, nx=20, ny=15, nz=12):
files_created.append(gaussian_file)
print(f"✅ Created: {gaussian_file}")
# Larger Gaussian for tests
gaussian_test = tests_dir / "sample.vti"
if create_gaussian_vti(gaussian_test, nx=30, ny=25, nz=20):
files_created.append(gaussian_test)
print(f"✅ Created: {gaussian_test}")
# Wave pattern
wave_file = examples_dir / "wave_pattern.vti"
if create_wave_vti(wave_file):
files_created.append(wave_file)
print(f"✅ Created: {wave_file}")
print(f"\n🎉 Generated {len(files_created)} sample VTI files!")
# Show file info
for file_path in files_created:
if file_path.exists():
size_mb = file_path.stat().st_size / (1024 * 1024)
print(f" 📁 {file_path.name}: {size_mb:.2f} MB")
return True
if __name__ == "__main__":
success = main()
exit(0 if success else 1)