-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrun_agent.py
More file actions
380 lines (289 loc) · 13.4 KB
/
Copy pathrun_agent.py
File metadata and controls
380 lines (289 loc) · 13.4 KB
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
#!/usr/bin/env python3
"""
Unified Agent Runner for DDR_Bench.
Single entry point for running data analysis agents across all scenarios:
- MIMIC: Patient data analysis using MIMIC-IV database
- 10-K: Financial report analysis using SEC 10-K filings
- GLOBEM: Behavioral data analysis using GLOBEM dataset
Usage:
python run_agent.py --scenario mimic --db-path /path/to/mimic_iv.db --input /path/to/notes.json
python run_agent.py --scenario 10k --db-path /path/to/10k.db
python run_agent.py --scenario globem --data-path /path/to/globem/data
See README.md for detailed usage instructions.
"""
import argparse
import asyncio
import json
import os
import sys
from pathlib import Path
from typing import Any, Dict, List, Optional, Set
from config import Config, get_config
from base_batch_analyzer import BaseBatchAnalyzer
class PatientBatchAnalyzer(BaseBatchAnalyzer):
"""Batch analyzer for MIMIC patient data."""
def __init__(self, base_log_dir: str, target_ids: Optional[Set[str]] = None, overwrite: bool = False):
super().__init__(base_log_dir, target_ids, overwrite)
def extract_identifiers(self, source_file: Path) -> List[Dict[str, Any]]:
"""Extract patient identifiers from pre-defined ID list file."""
try:
print(f"Reading patient IDs from: {source_file}")
with open(source_file, 'r', encoding='utf-8') as f:
patient_ids = json.load(f)
if not isinstance(patient_ids, list):
print(f" Error: Expected a list of patient IDs")
return []
patients_list = []
for pid in patient_ids:
subject_id = str(pid)
patients_list.append({
"patient_id": f"patient_{subject_id}",
"subject_id": subject_id,
"identifier": subject_id,
"data": {}
})
print(f" Found {len(patients_list)} patients")
return patients_list
except Exception as e:
print(f" Error reading file: {e}")
return []
def _prepare_analysis_command(self, identifier_info: Dict[str, Any], source_file: Path,
subdir_name: str, **kwargs) -> tuple:
"""Prepare the command for patient analysis."""
subject_id = identifier_info["subject_id"]
patient_log_dir = self.base_log_dir / subdir_name
patient_log_dir.mkdir(parents=True, exist_ok=True)
task = f"Analyze patient {subject_id}"
cmd = [
sys.executable,
"agent/data_agent.py",
"--task", task,
"--log-dir", str(patient_log_dir)
]
if not kwargs.get("auto_finish", True):
cmd.append("--no-auto-finish")
# Pass config info
if kwargs.get("config_path"):
cmd.extend(["--config", kwargs.get("config_path")])
if kwargs.get("scenario"):
cmd.extend(["--scenario", kwargs.get("scenario")])
# MCP arguments: Only pass server script, agent will load config for DB path
cmd.extend(["--sql-server", "tool_server/sqlite_mcp.py"])
# We do NOT pass --data-path override unless specific need, but user said NO overrides.
# So we trust config.yaml loaded by sqlite_mcp.py via --config
# Pass max_turns if provided
if kwargs.get("max_turns"):
cmd.extend(["--max-turns", str(kwargs.get("max_turns"))])
env = os.environ.copy()
env['CUSTOM_LOG_DIR'] = str(patient_log_dir)
return cmd, env, f"Patient {subject_id}"
def get_subdir_name(self, identifier: str) -> str:
return f"patient_{identifier}"
def _create_identifier_from_logs(self, identifier: str, dirname: str) -> Optional[Dict[str, Any]]:
return {
"patient_id": f"patient_{identifier}",
"subject_id": identifier,
"identifier": identifier,
"data": {}
}
class CompanyBatchAnalyzer(BaseBatchAnalyzer):
"""Batch analyzer for 10-K company data."""
def __init__(self, base_log_dir: str, target_ids: Optional[Set[str]] = None, overwrite: bool = False):
super().__init__(base_log_dir, target_ids, overwrite)
def extract_identifiers(self, source_file: Path) -> List[Dict[str, Any]]:
"""Extract company identifiers (CIKs) from pre-defined ID list file."""
companies = []
try:
print(f"Reading company CIKs from: {source_file}")
with open(source_file, 'r', encoding='utf-8') as f:
ciks = json.load(f)
if not isinstance(ciks, list):
print(f" Error: Expected a list of company CIKs")
return []
for cik in ciks:
companies.append({
"cik": str(cik),
"identifier": str(cik)
})
print(f"Found {len(companies)} companies")
except Exception as e:
print(f"Error reading ID file: {e}")
return companies
def _prepare_analysis_command(self, identifier_info: Dict[str, Any], source_file: Path,
subdir_name: str, **kwargs) -> tuple:
"""Prepare the command for company analysis."""
cik = identifier_info["cik"]
company_log_dir = self.base_log_dir / subdir_name
company_log_dir.mkdir(parents=True, exist_ok=True)
task = f"Analyze company with CIK {cik}"
cmd = [
sys.executable,
"agent/data_agent.py",
"--task", task,
"--log-dir", str(company_log_dir)
]
if not kwargs.get("auto_finish", True):
cmd.append("--no-auto-finish")
if kwargs.get("config_path"):
cmd.extend(["--config", kwargs.get("config_path")])
if kwargs.get("scenario"):
cmd.extend(["--scenario", kwargs.get("scenario")])
# Setup MCP arguments
cmd.extend(["--sql-server", "tool_server/sqlite_mcp.py"])
# Pass max_turns if provided
if kwargs.get("max_turns"):
cmd.extend(["--max-turns", str(kwargs.get("max_turns"))])
env = os.environ.copy()
env['CUSTOM_LOG_DIR'] = str(company_log_dir)
return cmd, env, f"Company CIK {cik}"
def get_subdir_name(self, identifier: str) -> str:
return f"company_{identifier}"
def _create_identifier_from_logs(self, identifier: str, dirname: str) -> Optional[Dict[str, Any]]:
return {
"cik": identifier,
"identifier": identifier
}
class UserBatchAnalyzer(BaseBatchAnalyzer):
"""Batch analyzer for GLOBEM user data."""
def __init__(self, base_log_dir: str, target_ids: Optional[Set[str]] = None, overwrite: bool = False):
super().__init__(base_log_dir, target_ids, overwrite)
def extract_identifiers(self, source_file: Path) -> List[Dict[str, Any]]:
"""Extract user identifiers from pre-defined ID list file."""
users = []
try:
print(f"Reading user IDs from: {source_file}")
with open(source_file, 'r', encoding='utf-8') as f:
user_ids = json.load(f)
if not isinstance(user_ids, list):
print(f" Error: Expected a list of user IDs")
return []
for pid in user_ids:
users.append({
"pid": str(pid),
"identifier": str(pid)
})
print(f"Found {len(users)} users")
except Exception as e:
print(f"Error reading ID file: {e}")
return users
def _prepare_analysis_command(self, identifier_info: Dict[str, Any], source_file: Path,
subdir_name: str, **kwargs) -> tuple:
"""Prepare the command for user analysis."""
pid = identifier_info["pid"]
user_log_dir = self.base_log_dir / subdir_name
user_log_dir.mkdir(parents=True, exist_ok=True)
task = f"Analyze user {pid}"
cmd = [
sys.executable,
"agent/data_agent.py",
"--task", task,
"--log-dir", str(user_log_dir)
]
if not kwargs.get("auto_finish", True):
cmd.append("--no-auto-finish")
if kwargs.get("config_path"):
cmd.extend(["--config", kwargs.get("config_path")])
if kwargs.get("scenario"):
cmd.extend(["--scenario", kwargs.get("scenario")])
# Setup MCP arguments
cmd.extend(["--code-server", "tool_server/code_mcp.py"])
# Pass max_turns if provided
if kwargs.get("max_turns"):
cmd.extend(["--max-turns", str(kwargs.get("max_turns"))])
env = os.environ.copy()
env['CUSTOM_LOG_DIR'] = str(user_log_dir)
return cmd, env, f"User {pid}"
def get_subdir_name(self, identifier: str) -> str:
return f"user_{identifier}"
def _create_identifier_from_logs(self, identifier: str, dirname: str) -> Optional[Dict[str, Any]]:
return {
"pid": identifier,
"identifier": identifier
}
def main():
"""Main entry point for running the agent."""
parser = argparse.ArgumentParser(
description="DDR_Bench Unified Agent Runner",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
Examples:
# Run MIMIC patient analysis (configured via config.yaml)
python run_agent.py --scenario mimic
# Run 10-K company analysis
python run_agent.py --scenario 10k
# Run GLOBEM user analysis
python run_agent.py --scenario globem
"""
)
# Required arguments
parser.add_argument("--scenario", required=True, choices=["mimic", "10k", "globem"],
help="Analysis scenario to run")
# Configuration file
parser.add_argument("--config", help="Path to config.yaml file")
# Execution options
parser.add_argument("--target-ids", help="Comma-separated list of specific IDs to process")
parser.add_argument("--overwrite", action="store_true", help="Overwrite existing results")
parser.add_argument("--retry-only", action="store_true", help="Only retry failed analyses")
args = parser.parse_args()
# Resolve config path
config_path = args.config
if not config_path and Path("config.yaml").exists():
config_path = str(Path("config.yaml").resolve())
# Load configuration
config = get_config(args.config) # get_config handles default loading too
scenario_config = config.get_scenario(args.scenario)
# Get settings from config
log_dir = scenario_config.log_dir
id_file = scenario_config.id_file
auto_finish = config.agent.auto_finish if hasattr(config.agent, 'auto_finish') else True
max_retries = config.agent.max_retries or 2
max_turns = config.agent.max_turns or 100
log_level = config.agent.log_level or "INFO"
# Set log level for subprocesses and current process
os.environ["DDR_LOG_LEVEL"] = log_level
# Process target IDs
target_ids = None
if args.target_ids:
target_ids = set(id.strip() for id in args.target_ids.split(',') if id.strip())
print(f"Target IDs: {sorted(target_ids)}")
# Validate id_file exists
if not id_file or not Path(id_file).exists():
parser.error(f"ID file not found: {id_file}. Please check config.yaml.")
# Validate scenario paths are configured (just valid check, not passed via args)
if args.scenario == "mimic" and not scenario_config.db_path:
parser.error("db_path for mimic not found in config.yaml")
if args.scenario == "10k" and not scenario_config.db_path:
parser.error("db_path for 10k not found in config.yaml")
if args.scenario == "globem" and not scenario_config.data_path:
parser.error("data_path for globem not found in config.yaml")
# Create analyzer based on scenario
if args.scenario == "mimic":
analyzer = PatientBatchAnalyzer(log_dir, target_ids, args.overwrite)
elif args.scenario == "10k":
analyzer = CompanyBatchAnalyzer(log_dir, target_ids, args.overwrite)
elif args.scenario == "globem":
analyzer = UserBatchAnalyzer(log_dir, target_ids, args.overwrite)
source_file = Path(id_file)
run_kwargs = {
"max_turns": max_turns,
"auto_finish": auto_finish
}
# Run analysis
print(f"\n{'='*60}")
print(f"DDR_Bench Agent Runner")
print(f"Scenario: {args.scenario}")
print(f"Provider: {config.provider.default_provider}")
print(f"Model: {config.provider.default_model}")
print(f"Log Directory: {log_dir}")
print(f"Config File: {config_path}")
print(f"{'='*60}\n")
# Add config_path and scenario to run_kwargs
if config_path:
run_kwargs["config_path"] = config_path
run_kwargs["scenario"] = args.scenario
if args.retry_only:
analyzer.retry_failed_analyses(max_retries=max_retries, **run_kwargs)
else:
analyzer.run_batch_analysis(source_file, max_retries=max_retries, **run_kwargs)
if __name__ == "__main__":
main()