test_system.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. """
  2. 系统测试脚本
  3. System Test Script
  4. 用于验证Cd预测集成系统的各个模块是否正常工作
  5. """
  6. import os
  7. import sys
  8. import logging
  9. # 添加项目根目录到Python路径
  10. sys.path.append(os.path.dirname(os.path.abspath(__file__)))
  11. def test_imports():
  12. """测试模块导入"""
  13. print("🔍 测试模块导入...")
  14. try:
  15. import config
  16. print("✅ config 模块导入成功")
  17. from models.crop_cd_model.predict import CropCdPredictor
  18. print("✅ CropCdPredictor 导入成功")
  19. from models.effective_cd_model.predict import EffectiveCdPredictor
  20. print("✅ EffectiveCdPredictor 导入成功")
  21. from analysis.data_processing import DataProcessor
  22. print("✅ DataProcessor 导入成功")
  23. from analysis.mapping import RasterMapper
  24. print("✅ RasterMapper 导入成功")
  25. from analysis.visualization import Visualizer
  26. print("✅ Visualizer 导入成功")
  27. from utils.common import setup_logging
  28. print("✅ utils.common 导入成功")
  29. return True
  30. except Exception as e:
  31. print(f"❌ 模块导入失败: {str(e)}")
  32. return False
  33. def test_file_structure():
  34. """测试文件结构"""
  35. print("\n🔍 测试文件结构...")
  36. required_files = [
  37. "config.py",
  38. "main.py",
  39. "models/crop_cd_model/model_files/cropCdNN.pth",
  40. "models/crop_cd_model/model_files/cropCd_mean.npy",
  41. "models/crop_cd_model/model_files/cropCd_scale.npy",
  42. "models/crop_cd_model/data/areatest.csv",
  43. "models/effective_cd_model/model_files/EffCdNN6C.pth",
  44. "models/effective_cd_model/model_files/EffCd_mean.npy",
  45. "models/effective_cd_model/model_files/EffCd_scale.npy",
  46. "models/effective_cd_model/data/areatest.csv",
  47. "data/coordinates/坐标.csv",
  48. "output/raster/meanTemp.tif",
  49. "output/raster/lechang.shp"
  50. ]
  51. missing_files = []
  52. for file_path in required_files:
  53. if os.path.exists(file_path):
  54. print(f"✅ {file_path}")
  55. else:
  56. print(f"❌ {file_path} - 文件不存在")
  57. missing_files.append(file_path)
  58. if missing_files:
  59. print(f"\n⚠️ 缺少 {len(missing_files)} 个必要文件")
  60. return False
  61. else:
  62. print("\n✅ 所有必要文件都存在")
  63. return True
  64. def test_configuration():
  65. """测试配置"""
  66. print("\n🔍 测试配置...")
  67. try:
  68. import config
  69. # 测试配置项
  70. assert hasattr(config, 'CROP_CD_MODEL'), "缺少 CROP_CD_MODEL 配置"
  71. assert hasattr(config, 'EFFECTIVE_CD_MODEL'), "缺少 EFFECTIVE_CD_MODEL 配置"
  72. assert hasattr(config, 'DATA_PATHS'), "缺少 DATA_PATHS 配置"
  73. assert hasattr(config, 'WORKFLOW_CONFIG'), "缺少 WORKFLOW_CONFIG 配置"
  74. print("✅ 配置项检查通过")
  75. # 创建目录
  76. config.ensure_directories()
  77. print("✅ 目录创建成功")
  78. return True
  79. except Exception as e:
  80. print(f"❌ 配置测试失败: {str(e)}")
  81. return False
  82. def test_data_loading():
  83. """测试数据加载"""
  84. print("\n🔍 测试数据加载...")
  85. try:
  86. import pandas as pd
  87. # 测试坐标数据
  88. coord_file = "data/coordinates/坐标.csv"
  89. if os.path.exists(coord_file):
  90. coord_data = pd.read_csv(coord_file)
  91. print(f"✅ 坐标数据加载成功: {coord_data.shape}")
  92. else:
  93. print("❌ 坐标数据文件不存在")
  94. return False
  95. # 测试作物Cd模型数据
  96. crop_data_file = "models/crop_cd_model/data/areatest.csv"
  97. if os.path.exists(crop_data_file):
  98. crop_data = pd.read_csv(crop_data_file)
  99. print(f"✅ 作物Cd模型数据加载成功: {crop_data.shape}")
  100. else:
  101. print("❌ 作物Cd模型数据文件不存在")
  102. return False
  103. # 测试有效态Cd模型数据
  104. eff_data_file = "models/effective_cd_model/data/areatest.csv"
  105. if os.path.exists(eff_data_file):
  106. eff_data = pd.read_csv(eff_data_file)
  107. print(f"✅ 有效态Cd模型数据加载成功: {eff_data.shape}")
  108. else:
  109. print("❌ 有效态Cd模型数据文件不存在")
  110. return False
  111. return True
  112. except Exception as e:
  113. print(f"❌ 数据加载测试失败: {str(e)}")
  114. return False
  115. def main():
  116. """主测试函数"""
  117. print("=" * 60)
  118. print("🧪 Cd预测集成系统测试")
  119. print("=" * 60)
  120. tests = [
  121. ("模块导入", test_imports),
  122. ("文件结构", test_file_structure),
  123. ("配置", test_configuration),
  124. ("数据加载", test_data_loading)
  125. ]
  126. passed = 0
  127. total = len(tests)
  128. for test_name, test_func in tests:
  129. print(f"\n{'='*20} {test_name} {'='*20}")
  130. if test_func():
  131. passed += 1
  132. print(f"✅ {test_name} 测试通过")
  133. else:
  134. print(f"❌ {test_name} 测试失败")
  135. print("\n" + "=" * 60)
  136. print(f"📊 测试结果: {passed}/{total} 通过")
  137. if passed == total:
  138. print("🎉 所有测试通过!系统准备就绪。")
  139. print("\n下一步:运行 python main.py 开始完整的预测流程")
  140. else:
  141. print("⚠️ 部分测试失败,请检查上述错误信息")
  142. print("\n建议:")
  143. print("1. 确保运行了 python setup_data.py")
  144. print("2. 检查原始项目文件夹是否在正确位置")
  145. print("3. 安装必要的依赖包:pip install -r requirements.txt")
  146. print("=" * 60)
  147. if __name__ == "__main__":
  148. main()