""" 系统测试脚本 System Test Script 用于验证Cd预测集成系统的各个模块是否正常工作 """ import os import sys import logging # 添加项目根目录到Python路径 sys.path.append(os.path.dirname(os.path.abspath(__file__))) def test_imports(): """测试模块导入""" print("🔍 测试模块导入...") try: import config print("✅ config 模块导入成功") from models.crop_cd_model.predict import CropCdPredictor print("✅ CropCdPredictor 导入成功") from models.effective_cd_model.predict import EffectiveCdPredictor print("✅ EffectiveCdPredictor 导入成功") from analysis.data_processing import DataProcessor print("✅ DataProcessor 导入成功") from analysis.mapping import RasterMapper print("✅ RasterMapper 导入成功") from analysis.visualization import Visualizer print("✅ Visualizer 导入成功") from utils.common import setup_logging print("✅ utils.common 导入成功") return True except Exception as e: print(f"❌ 模块导入失败: {str(e)}") return False def test_file_structure(): """测试文件结构""" print("\n🔍 测试文件结构...") required_files = [ "config.py", "main.py", "models/crop_cd_model/model_files/cropCdNN.pth", "models/crop_cd_model/model_files/cropCd_mean.npy", "models/crop_cd_model/model_files/cropCd_scale.npy", "models/crop_cd_model/data/areatest.csv", "models/effective_cd_model/model_files/EffCdNN6C.pth", "models/effective_cd_model/model_files/EffCd_mean.npy", "models/effective_cd_model/model_files/EffCd_scale.npy", "models/effective_cd_model/data/areatest.csv", "data/coordinates/坐标.csv", "output/raster/meanTemp.tif", "output/raster/lechang.shp" ] missing_files = [] for file_path in required_files: if os.path.exists(file_path): print(f"✅ {file_path}") else: print(f"❌ {file_path} - 文件不存在") missing_files.append(file_path) if missing_files: print(f"\n⚠️ 缺少 {len(missing_files)} 个必要文件") return False else: print("\n✅ 所有必要文件都存在") return True def test_configuration(): """测试配置""" print("\n🔍 测试配置...") try: import config # 测试配置项 assert hasattr(config, 'CROP_CD_MODEL'), "缺少 CROP_CD_MODEL 配置" assert hasattr(config, 'EFFECTIVE_CD_MODEL'), "缺少 EFFECTIVE_CD_MODEL 配置" assert hasattr(config, 'DATA_PATHS'), "缺少 DATA_PATHS 配置" assert hasattr(config, 'WORKFLOW_CONFIG'), "缺少 WORKFLOW_CONFIG 配置" print("✅ 配置项检查通过") # 创建目录 config.ensure_directories() print("✅ 目录创建成功") return True except Exception as e: print(f"❌ 配置测试失败: {str(e)}") return False def test_data_loading(): """测试数据加载""" print("\n🔍 测试数据加载...") try: import pandas as pd # 测试坐标数据 coord_file = "data/coordinates/坐标.csv" if os.path.exists(coord_file): coord_data = pd.read_csv(coord_file) print(f"✅ 坐标数据加载成功: {coord_data.shape}") else: print("❌ 坐标数据文件不存在") return False # 测试作物Cd模型数据 crop_data_file = "models/crop_cd_model/data/areatest.csv" if os.path.exists(crop_data_file): crop_data = pd.read_csv(crop_data_file) print(f"✅ 作物Cd模型数据加载成功: {crop_data.shape}") else: print("❌ 作物Cd模型数据文件不存在") return False # 测试有效态Cd模型数据 eff_data_file = "models/effective_cd_model/data/areatest.csv" if os.path.exists(eff_data_file): eff_data = pd.read_csv(eff_data_file) print(f"✅ 有效态Cd模型数据加载成功: {eff_data.shape}") else: print("❌ 有效态Cd模型数据文件不存在") return False return True except Exception as e: print(f"❌ 数据加载测试失败: {str(e)}") return False def main(): """主测试函数""" print("=" * 60) print("🧪 Cd预测集成系统测试") print("=" * 60) tests = [ ("模块导入", test_imports), ("文件结构", test_file_structure), ("配置", test_configuration), ("数据加载", test_data_loading) ] passed = 0 total = len(tests) for test_name, test_func in tests: print(f"\n{'='*20} {test_name} {'='*20}") if test_func(): passed += 1 print(f"✅ {test_name} 测试通过") else: print(f"❌ {test_name} 测试失败") print("\n" + "=" * 60) print(f"📊 测试结果: {passed}/{total} 通过") if passed == total: print("🎉 所有测试通过!系统准备就绪。") print("\n下一步:运行 python main.py 开始完整的预测流程") else: print("⚠️ 部分测试失败,请检查上述错误信息") print("\n建议:") print("1. 确保运行了 python setup_data.py") print("2. 检查原始项目文件夹是否在正确位置") print("3. 安装必要的依赖包:pip install -r requirements.txt") print("=" * 60) if __name__ == "__main__": main()