2026-03-29 23:47:20 +08:00

47 lines
1.4 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""
医疗数据因果推断分析示例v2
本脚本演示如何使用 causal_agent 主包对医疗数据进行完整的因果推断分析。
"""
import os
import sys
# 将项目根目录加入路径
project_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, project_root)
from causal_agent.agent import CausalInferenceAgent
from causal_agent.core.config import AgentConfig
def main():
data_path = "examples/medical_v2/data.xlsx"
# 如果数据不存在,自动生成示例数据
if not os.path.exists(data_path):
print("数据文件不存在,正在生成示例医疗数据...")
from examples.medical_v2.data_generator import generate_medical_data
df = generate_medical_data(n_samples=500, treatment_ratio=2 / 3, seed=42)
df.to_excel(data_path, index=False)
print(f"数据已保存到:{data_path}")
# 配置 Agent可选自定义 LLM 参数、日志路径等)
config = AgentConfig.from_env()
config.log_path = "examples/medical_v2/log.md"
# 创建 Agent 并执行分析
agent = CausalInferenceAgent(config)
result = agent.analyze(data_path)
print("\n" + "=" * 60)
print("示例运行完成!")
print(f"分析 ID{result['id']}")
print(f"日志已保存到:{agent.logger.log_path}")
print("=" * 60)
if __name__ == "__main__":
main()