Spaces:
Sleeping
Sleeping
File size: 4,592 Bytes
e0ef700 |
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 |
#!/usr/bin/env python3
"""
Test the self-contained volume_threshold module.
"""
import numpy as np
from ideal_poly_volume_toolkit.volume_threshold import (
volume_exceeds_threshold,
get_volume,
compute_volume
)
def test_basic_tetrahedron():
"""Test with basic tetrahedron: 0, 1, i, ∞"""
print("=" * 70)
print("Test 1: Basic tetrahedron (0, 1, i, ∞)")
print("=" * 70)
vertices = [0+0j, 1+0j, 1j, np.inf]
volume = get_volume(vertices)
print(f"Vertices: {vertices}")
print(f"Volume: {volume:.8f}")
# Test threshold comparisons
thresholds = [0.5, 1.0, 1.5, 2.0]
for threshold in thresholds:
result = volume_exceeds_threshold(vertices, threshold)
print(f" Volume > {threshold}? {result}")
print()
def test_seven_vertex_config():
"""Test with 7-vertex configuration (4 random + 0, 1, i)"""
print("=" * 70)
print("Test 2: Seven-vertex polyhedron (0, 1, i, ∞ + 3 random)")
print("=" * 70)
# Use a known configuration from the examples
np.random.seed(42)
random_vertices = [
0.5 + 0.5j,
-0.3 + 0.7j,
0.8 - 0.2j
]
vertices = [0+0j, 1+0j, 1j, np.inf] + random_vertices
volume = get_volume(vertices)
print(f"Fixed vertices: 0, 1, i, ∞")
print(f"Random vertices: {random_vertices}")
print(f"Total vertices: {len(vertices)}")
print(f"Volume: {volume:.8f}")
# Test threshold comparisons
thresholds = [2.0, 3.0, 4.0, 5.0]
for threshold in thresholds:
result = volume_exceeds_threshold(vertices, threshold)
print(f" Volume > {threshold}? {result}")
print()
def test_optimized_config():
"""Test with a known optimized configuration"""
print("=" * 70)
print("Test 3: Optimized configuration from previous runs")
print("=" * 70)
# This is an example - replace with actual optimized values if available
# Format: real parts, imaginary parts for the 4 free vertices
# (0, 1, i are fixed, infinity is implicit)
real_parts = [0.234, -0.456, 0.789, -0.123]
imag_parts = [0.567, 0.890, -0.234, 0.456]
free_vertices = [complex(r, i) for r, i in zip(real_parts, imag_parts)]
vertices = [0+0j, 1+0j, 1j, np.inf] + free_vertices
volume = get_volume(vertices)
print(f"Free vertices (4):")
for i, v in enumerate(free_vertices, 1):
print(f" v{i}: {v:.4f}")
print(f"Total vertices: {len(vertices)}")
print(f"Volume: {volume:.8f}")
# Test threshold comparisons
thresholds = [3.0, 4.0, 5.0, 6.0]
for threshold in thresholds:
result = volume_exceeds_threshold(vertices, threshold)
print(f" Volume > {threshold}? {result}")
print()
def test_method_comparison():
"""Compare hull vs Delaunay methods"""
print("=" * 70)
print("Test 4: Method comparison (hull vs Delaunay)")
print("=" * 70)
vertices = [0+0j, 1+0j, 1j, np.inf, 0.5+0.5j]
vol_hull = compute_volume(vertices, method="hull")
vol_delaunay = compute_volume(vertices, method="delaunay")
vol_auto = compute_volume(vertices, method="auto")
print(f"Vertices: {vertices}")
print(f"Volume (hull method): {vol_hull:.8f}")
print(f"Volume (Delaunay method): {vol_delaunay:.8f}")
print(f"Volume (auto method): {vol_auto:.8f}")
print(f"Difference (hull-Delaunay): {abs(vol_hull - vol_delaunay):.2e}")
print()
def test_performance():
"""Test performance with larger vertex sets"""
print("=" * 70)
print("Test 5: Performance test")
print("=" * 70)
import time
for n_random in [5, 10, 15, 20]:
np.random.seed(42)
# Generate random vertices in unit disk
random_vertices = []
for _ in range(n_random):
r = np.random.uniform(0, 0.9)
theta = np.random.uniform(0, 2*np.pi)
random_vertices.append(r * np.exp(1j * theta))
vertices = [0+0j, 1+0j, 1j, np.inf] + random_vertices
start = time.time()
volume = get_volume(vertices)
elapsed = time.time() - start
print(f" {len(vertices)} vertices: volume = {volume:.6f}, time = {elapsed*1000:.2f} ms")
print()
def main():
"""Run all tests"""
print("\n" + "=" * 70)
print("VOLUME THRESHOLD MODULE TEST SUITE")
print("=" * 70)
print()
test_basic_tetrahedron()
test_seven_vertex_config()
test_optimized_config()
test_method_comparison()
test_performance()
print("=" * 70)
print("All tests completed!")
print("=" * 70)
if __name__ == "__main__":
main()
|