tmp
This commit is contained in:
170
demo/example_jupyter.py
Normal file
170
demo/example_jupyter.py
Normal file
@@ -0,0 +1,170 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
STT Jupyter Notebook使用示例
|
||||
|
||||
这个脚本展示了如何在Jupyter notebook中使用STT的Python绑定,
|
||||
包括进度条适配。
|
||||
"""
|
||||
|
||||
import pystt as stt
|
||||
import sys
|
||||
import os
|
||||
|
||||
def basic_jupyter_example():
|
||||
"""Jupyter notebook基本使用示例"""
|
||||
print("=== Jupyter Notebook基本示例 ===")
|
||||
|
||||
# 创建生成器
|
||||
generator = stt.SttGenerator()
|
||||
|
||||
# 设置参数
|
||||
generator.set_tree_depth(3, 6)
|
||||
generator.set_reference_system("WGS84")
|
||||
|
||||
# 创建简单的进度回调
|
||||
def simple_progress(description, percentage):
|
||||
print(f"{description}: {percentage:.1f}%")
|
||||
|
||||
generator.set_progress_callback(simple_progress)
|
||||
|
||||
# 运行生成
|
||||
result = generator.run("jupyter_output.msh")
|
||||
|
||||
print(f"生成结果: {'成功' if result == 0 else '失败'}")
|
||||
print()
|
||||
|
||||
def tqdm_jupyter_example():
|
||||
"""使用tqdm的Jupyter示例"""
|
||||
print("=== 使用tqdm的Jupyter示例 ===")
|
||||
|
||||
if not stt.HAS_TQDM:
|
||||
print("tqdm未安装,使用简单进度条")
|
||||
progress_cb = stt.create_simple_callback("STT生成")
|
||||
else:
|
||||
print("使用tqdm进度条")
|
||||
progress_cb = stt.create_tqdm_callback("STT生成")
|
||||
|
||||
generator = stt.SttGenerator()
|
||||
generator.set_progress_callback(progress_cb)
|
||||
|
||||
generator.set_tree_depth(2, 5)
|
||||
generator.set_reference_system("Earth")
|
||||
|
||||
result = generator.run("tqdm_output.msh")
|
||||
|
||||
print(f"生成结果: {'成功' if result == 0 else '失败'}")
|
||||
print()
|
||||
|
||||
def auto_progress_example():
|
||||
"""自动选择最佳进度条"""
|
||||
print("=== 自动选择进度条示例 ===")
|
||||
|
||||
# 自动创建最适合的进度回调
|
||||
progress_cb = stt.create_progress_callback("自动进度")
|
||||
|
||||
generator = stt.SttGenerator()
|
||||
generator.set_progress_callback(progress_cb)
|
||||
|
||||
generator.set_tree_depth(2, 4)
|
||||
generator.set_reference_system("Moon")
|
||||
|
||||
result = generator.run("auto_output.msh")
|
||||
|
||||
print(f"生成结果: {'成功' if result == 0 else '失败'}")
|
||||
print()
|
||||
|
||||
def advanced_jupyter_example():
|
||||
"""高级Jupyter示例"""
|
||||
print("=== 高级Jupyter示例 ===")
|
||||
|
||||
# 创建tqdm进度回调
|
||||
if stt.HAS_TQDM:
|
||||
progress_cb = stt.TqdmProgressCallback("高级STT生成")
|
||||
else:
|
||||
progress_cb = stt.SimpleProgressCallback("高级STT生成")
|
||||
|
||||
generator = stt.SttGenerator()
|
||||
generator.set_progress_callback(progress_cb)
|
||||
|
||||
# 设置自定义参考系统
|
||||
generator.set_pole_equator_radius(3396200.0, 3376200.0) # 火星
|
||||
|
||||
# 设置二十面体方向
|
||||
generator.set_icosahedron_orient(0.0, 90.0)
|
||||
|
||||
# 使用完整参数
|
||||
params = {
|
||||
"output_msh": "mars_grid.msh",
|
||||
"output_vertex": "mars_vertices.txt",
|
||||
"output_triangle_center": "mars_centers.txt"
|
||||
}
|
||||
|
||||
result = generator.run_full(params)
|
||||
|
||||
print(f"火星网格生成: {'成功' if result == 0 else '失败'}")
|
||||
print()
|
||||
|
||||
def notebook_integration_demo():
|
||||
"""Notebook集成演示"""
|
||||
print("=== Notebook集成演示 ===")
|
||||
|
||||
# 检查是否在notebook环境中
|
||||
try:
|
||||
from IPython.display import display, HTML
|
||||
in_notebook = True
|
||||
except ImportError:
|
||||
in_notebook = False
|
||||
|
||||
if in_notebook:
|
||||
print("检测到Jupyter环境,使用增强进度显示")
|
||||
display(HTML("<h4>STT生成进度</h4>"))
|
||||
|
||||
def notebook_progress(description, percentage):
|
||||
display(HTML(f"""
|
||||
<div style='border: 1px solid #ccc; padding: 10px; margin: 5px;'>
|
||||
<strong>{description}</strong>: {percentage:.1f}%
|
||||
<div style='background: #f0f0f0; height: 20px; margin-top: 5px;'>
|
||||
<div style='background: #4CAF50; height: 100%; width: {percentage}%;'></div>
|
||||
</div>
|
||||
</div>
|
||||
"""))
|
||||
|
||||
generator = stt.SttGenerator()
|
||||
generator.set_progress_callback(notebook_progress)
|
||||
else:
|
||||
print("普通Python环境,使用标准进度条")
|
||||
progress_cb = stt.create_progress_callback("Notebook演示")
|
||||
generator = stt.SttGenerator()
|
||||
generator.set_progress_callback(progress_cb)
|
||||
|
||||
generator.set_tree_depth(2, 4)
|
||||
generator.set_reference_system("WGS84")
|
||||
|
||||
result = generator.run("notebook_output.msh")
|
||||
|
||||
print(f"生成结果: {'成功' if result == 0 else '失败'}")
|
||||
print()
|
||||
|
||||
def main():
|
||||
"""主函数"""
|
||||
print("STT Jupyter Notebook使用示例")
|
||||
print("=" * 40)
|
||||
|
||||
# 运行各种示例
|
||||
try:
|
||||
basic_jupyter_example()
|
||||
tqdm_jupyter_example()
|
||||
auto_progress_example()
|
||||
advanced_jupyter_example()
|
||||
notebook_integration_demo()
|
||||
|
||||
print("所有Jupyter示例运行完成!")
|
||||
print("检查生成的文件以查看结果。")
|
||||
|
||||
except Exception as e:
|
||||
print(f"运行示例时出错: {e}")
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
138
demo/example_usage.py
Normal file
138
demo/example_usage.py
Normal file
@@ -0,0 +1,138 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
STT Python绑定使用示例
|
||||
|
||||
这个脚本展示了如何使用STT的Python绑定来生成球面三角网格。
|
||||
"""
|
||||
|
||||
import pystt as stt
|
||||
import os
|
||||
import sys
|
||||
|
||||
def basic_example():
|
||||
"""基本使用示例"""
|
||||
print("=== 基本使用示例 ===")
|
||||
|
||||
# 创建生成器
|
||||
generator = stt.SttGenerator()
|
||||
|
||||
# 设置参数
|
||||
generator.set_tree_depth(3, 6) # 最小深度3,最大深度6
|
||||
generator.set_reference_system("WGS84") # 使用WGS84参考系统
|
||||
|
||||
# 运行生成
|
||||
result = generator.run("basic_output.msh")
|
||||
|
||||
print(f"生成结果: {'成功' if result == 0 else '失败'}")
|
||||
print(f"输出文件: basic_output.msh")
|
||||
print()
|
||||
|
||||
def quick_creation_example():
|
||||
"""快速创建示例"""
|
||||
print("=== 快速创建示例 ===")
|
||||
|
||||
# 使用便利函数快速创建
|
||||
info = stt.create_stt(
|
||||
min_depth=2,
|
||||
max_depth=5,
|
||||
reference_system="Earth",
|
||||
output_file="quick_output.msh"
|
||||
)
|
||||
|
||||
print("生成信息:")
|
||||
for key, value in info.items():
|
||||
print(f" {key}: {value}")
|
||||
print()
|
||||
|
||||
def advanced_example():
|
||||
"""高级使用示例"""
|
||||
print("=== 高级使用示例 ===")
|
||||
|
||||
generator = stt.SttGenerator()
|
||||
|
||||
# 设置自定义参考系统(月球)
|
||||
generator.set_pole_equator_radius(1738000.0, 1738000.0) # 月球半径
|
||||
|
||||
# 设置二十面体方向
|
||||
generator.set_icosahedron_orient(0.0, 90.0) # 北极方向
|
||||
|
||||
# 使用完整参数运行
|
||||
params = {
|
||||
"output_msh": "advanced_output.msh",
|
||||
"output_vertex": "vertices.txt",
|
||||
"output_triangle_center": "triangle_centers.txt",
|
||||
"output_neighbor": "neighbors.txt"
|
||||
}
|
||||
|
||||
result = generator.run_full(params)
|
||||
|
||||
print(f"高级生成结果: {'成功' if result == 0 else '失败'}")
|
||||
print("输出文件:")
|
||||
for key, file in params.items():
|
||||
if os.path.exists(file):
|
||||
size = os.path.getsize(file)
|
||||
print(f" {file}: {size} bytes")
|
||||
else:
|
||||
print(f" {file}: 未生成")
|
||||
print()
|
||||
|
||||
def custom_reference_system_example():
|
||||
"""自定义参考系统示例"""
|
||||
print("=== 自定义参考系统示例 ===")
|
||||
|
||||
generator = stt.SttGenerator()
|
||||
|
||||
# 设置自定义椭球参数(例如火星)
|
||||
# 火星: 赤道半径 3396.2 km, 极半径 3376.2 km
|
||||
generator.set_pole_equator_radius(3396200.0, 3376200.0)
|
||||
generator.set_tree_depth(3, 7)
|
||||
|
||||
result = generator.run("mars_grid.msh")
|
||||
|
||||
print(f"火星网格生成: {'成功' if result == 0 else '失败'}")
|
||||
print()
|
||||
|
||||
def check_module_info():
|
||||
"""检查模块信息"""
|
||||
print("=== 模块信息 ===")
|
||||
|
||||
info = stt.get_info()
|
||||
for key, value in info.items():
|
||||
print(f"{key}: {value}")
|
||||
|
||||
print(f"可用常量:")
|
||||
print(f" stt.WGS84: {stt.WGS84}")
|
||||
print(f" stt.EARTH: {stt.EARTH}")
|
||||
print(f" stt.MOON: {stt.MOON}")
|
||||
print()
|
||||
|
||||
def main():
|
||||
"""主函数"""
|
||||
print("STT Python绑定使用示例")
|
||||
print("=" * 30)
|
||||
|
||||
# 检查模块是否可用
|
||||
try:
|
||||
check_module_info()
|
||||
except Exception as e:
|
||||
print(f"错误: 无法加载STT模块 - {e}")
|
||||
print("请确保已经正确安装STT Python绑定")
|
||||
sys.exit(1)
|
||||
|
||||
# 运行各种示例
|
||||
try:
|
||||
basic_example()
|
||||
quick_creation_example()
|
||||
advanced_example()
|
||||
custom_reference_system_example()
|
||||
|
||||
print("所有示例运行完成!")
|
||||
print("检查生成的文件以查看结果。")
|
||||
|
||||
except Exception as e:
|
||||
print(f"运行示例时出错: {e}")
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
239
demo/test_binding.py
Normal file
239
demo/test_binding.py
Normal file
@@ -0,0 +1,239 @@
|
||||
"""
|
||||
STT Python绑定测试脚本
|
||||
|
||||
这个脚本测试STT Python绑定的基本功能。
|
||||
"""
|
||||
|
||||
import sys
|
||||
import os
|
||||
import tempfile
|
||||
import unittest
|
||||
|
||||
# 尝试导入STT模块
|
||||
try:
|
||||
import stt
|
||||
STT_AVAILABLE = True
|
||||
except ImportError as e:
|
||||
print(f"警告: 无法导入STT模块 - {e}")
|
||||
print("请确保已经构建并安装了STT Python绑定")
|
||||
STT_AVAILABLE = False
|
||||
stt = None
|
||||
|
||||
class TestSttBinding(unittest.TestCase):
|
||||
"""STT绑定测试类"""
|
||||
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
"""测试类设置"""
|
||||
if not STT_AVAILABLE:
|
||||
raise unittest.SkipTest("STT模块不可用")
|
||||
|
||||
# 创建临时目录用于测试输出
|
||||
cls.temp_dir = tempfile.mkdtemp()
|
||||
print(f"测试临时目录: {cls.temp_dir}")
|
||||
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
"""测试类清理"""
|
||||
if hasattr(cls, 'temp_dir') and os.path.exists(cls.temp_dir):
|
||||
# 清理临时文件
|
||||
import shutil
|
||||
shutil.rmtree(cls.temp_dir)
|
||||
print(f"清理临时目录: {cls.temp_dir}")
|
||||
|
||||
def test_module_import(self):
|
||||
"""测试模块导入"""
|
||||
self.assertIsNotNone(stt, "STT模块应该可用")
|
||||
self.assertTrue(hasattr(stt, 'SttGenerator'), "应该包含SttGenerator类")
|
||||
self.assertTrue(hasattr(stt, 'create_stt'), "应该包含create_stt函数")
|
||||
self.assertTrue(hasattr(stt, 'WGS84'), "应该包含WGS84常量")
|
||||
|
||||
def test_generator_creation(self):
|
||||
"""测试生成器创建"""
|
||||
generator = stt.SttGenerator()
|
||||
self.assertIsNotNone(generator, "应该能够创建生成器实例")
|
||||
|
||||
# 测试基本方法存在
|
||||
self.assertTrue(hasattr(generator, 'set_tree_depth'), "应该有set_tree_depth方法")
|
||||
self.assertTrue(hasattr(generator, 'set_reference_system'), "应该有set_reference_system方法")
|
||||
self.assertTrue(hasattr(generator, 'run'), "应该有run方法")
|
||||
self.assertTrue(hasattr(generator, 'run_full'), "应该有run_full方法")
|
||||
|
||||
def test_basic_generation(self):
|
||||
"""测试基本生成功能"""
|
||||
generator = stt.SttGenerator()
|
||||
|
||||
# 设置基本参数
|
||||
generator.set_tree_depth(2, 4) # 使用较小的深度进行快速测试
|
||||
generator.set_reference_system("WGS84")
|
||||
|
||||
# 创建输出文件路径
|
||||
output_file = os.path.join(self.temp_dir, "test_basic.msh")
|
||||
|
||||
# 运行生成
|
||||
result = generator.run(output_file)
|
||||
|
||||
# 检查结果
|
||||
self.assertEqual(result, 0, "生成应该成功返回0")
|
||||
|
||||
# 检查输出文件是否存在
|
||||
if os.path.exists(output_file):
|
||||
file_size = os.path.getsize(output_file)
|
||||
print(f"生成文件大小: {file_size} 字节")
|
||||
self.assertGreater(file_size, 0, "输出文件应该非空")
|
||||
else:
|
||||
print("警告: 输出文件未生成,这可能是因为生成器需要更多参数")
|
||||
|
||||
def test_quick_creation(self):
|
||||
"""测试快速创建功能"""
|
||||
output_file = os.path.join(self.temp_dir, "test_quick.msh")
|
||||
|
||||
# 使用便利函数
|
||||
info = stt.create_stt(
|
||||
min_depth=2,
|
||||
max_depth=4,
|
||||
reference_system="Earth",
|
||||
output_file=output_file
|
||||
)
|
||||
|
||||
# 检查返回信息
|
||||
self.assertIsInstance(info, dict, "应该返回字典信息")
|
||||
self.assertEqual(info['min_depth'], 2, "最小深度应该正确")
|
||||
self.assertEqual(info['max_depth'], 4, "最大深度应该正确")
|
||||
self.assertEqual(info['reference_system'], "Earth", "参考系统应该正确")
|
||||
self.assertEqual(info['output_file'], output_file, "输出文件应该正确")
|
||||
|
||||
# 检查文件生成
|
||||
if os.path.exists(output_file):
|
||||
file_size = os.path.getsize(output_file)
|
||||
print(f"快速创建文件大小: {file_size} 字节")
|
||||
self.assertGreater(file_size, 0, "输出文件应该非空")
|
||||
|
||||
def test_custom_reference_system(self):
|
||||
"""测试自定义参考系统"""
|
||||
generator = stt.SttGenerator()
|
||||
|
||||
# 测试预设系统
|
||||
generator.set_reference_system("WGS84")
|
||||
generator.set_reference_system("Earth")
|
||||
generator.set_reference_system("Moon")
|
||||
|
||||
# 测试自定义半径
|
||||
generator.set_pole_equator_radius(6378137.0, 6356752.3) # WGS84参数
|
||||
|
||||
# 设置其他参数
|
||||
generator.set_tree_depth(2, 3)
|
||||
generator.set_icosahedron_orient(0.0, 90.0)
|
||||
|
||||
# 运行测试
|
||||
output_file = os.path.join(self.temp_dir, "test_custom.msh")
|
||||
result = generator.run(output_file)
|
||||
|
||||
self.assertEqual(result, 0, "自定义参考系统生成应该成功")
|
||||
|
||||
def test_module_constants(self):
|
||||
"""测试模块常量"""
|
||||
self.assertEqual(stt.WGS84, "WGS84", "WGS84常量应该正确")
|
||||
self.assertEqual(stt.EARTH, "Earth", "Earth常量应该正确")
|
||||
self.assertEqual(stt.MOON, "Moon", "Moon常量应该正确")
|
||||
|
||||
def test_module_info(self):
|
||||
"""测试模块信息"""
|
||||
info = stt.get_info()
|
||||
self.assertIsInstance(info, dict, "应该返回字典信息")
|
||||
self.assertIn('version', info, "应该包含版本信息")
|
||||
self.assertIn('author', info, "应该包含作者信息")
|
||||
|
||||
print("模块信息:")
|
||||
for key, value in info.items():
|
||||
print(f" {key}: {value}")
|
||||
|
||||
def test_advanced_parameters(self):
|
||||
"""测试高级参数"""
|
||||
generator = stt.SttGenerator()
|
||||
|
||||
# 设置参数
|
||||
generator.set_tree_depth(2, 4)
|
||||
generator.set_reference_system("WGS84")
|
||||
generator.set_icosahedron_orient(0.0, 90.0)
|
||||
|
||||
# 使用完整参数运行
|
||||
params = {
|
||||
"output_msh": os.path.join(self.temp_dir, "test_advanced.msh"),
|
||||
"output_vertex": os.path.join(self.temp_dir, "vertices.txt"),
|
||||
"output_triangle_center": os.path.join(self.temp_dir, "centers.txt"),
|
||||
"output_neighbor": os.path.join(self.temp_dir, "neighbors.txt")
|
||||
}
|
||||
|
||||
result = generator.run_full(params)
|
||||
self.assertEqual(result, 0, "高级参数生成应该成功")
|
||||
|
||||
# 检查生成的文件
|
||||
for key, filename in params.items():
|
||||
if os.path.exists(filename):
|
||||
file_size = os.path.getsize(filename)
|
||||
print(f"{key} 文件大小: {file_size} 字节")
|
||||
|
||||
def test_error_handling(self):
|
||||
"""测试错误处理"""
|
||||
generator = stt.SttGenerator()
|
||||
|
||||
# 测试无效参数(应该不会崩溃)
|
||||
try:
|
||||
generator.set_tree_depth(-1, 10) # 无效深度
|
||||
generator.set_reference_system("InvalidSystem")
|
||||
# 这些调用应该能够处理而不会崩溃
|
||||
except Exception as e:
|
||||
print(f"错误处理测试: {e}")
|
||||
|
||||
def run_basic_tests():
|
||||
"""运行基本测试"""
|
||||
print("运行STT Python绑定基本测试...")
|
||||
|
||||
if not STT_AVAILABLE:
|
||||
print("错误: STT模块不可用,无法运行测试")
|
||||
return False
|
||||
|
||||
# 创建简单的测试
|
||||
try:
|
||||
print("1. 测试模块导入...")
|
||||
generator = stt.SttGenerator()
|
||||
print(" ✓ 模块导入成功")
|
||||
|
||||
print("2. 测试基本功能...")
|
||||
generator.set_tree_depth(2, 3)
|
||||
generator.set_reference_system("WGS84")
|
||||
print(" ✓ 基本设置成功")
|
||||
|
||||
print("3. 测试快速创建...")
|
||||
info = stt.create_stt(2, 3, "Earth", "test.msh")
|
||||
print(f" ✓ 快速创建成功: {info}")
|
||||
|
||||
print("4. 测试模块信息...")
|
||||
info = stt.get_info()
|
||||
print(f" ✓ 模块信息: {info['version']}")
|
||||
|
||||
print("基本测试通过!")
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
print(f"测试失败: {e}")
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
return False
|
||||
|
||||
def main():
|
||||
"""主函数"""
|
||||
print("STT Python绑定测试")
|
||||
print("=" * 30)
|
||||
|
||||
# 运行基本测试
|
||||
if run_basic_tests():
|
||||
print("\n运行完整单元测试...")
|
||||
unittest.main(argv=[''], exit=False, verbosity=2)
|
||||
else:
|
||||
print("\n基本测试失败,跳过完整测试")
|
||||
sys.exit(1)
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user