-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathrun_analysis.py
More file actions
executable file
·257 lines (224 loc) · 10.4 KB
/
Copy pathrun_analysis.py
File metadata and controls
executable file
·257 lines (224 loc) · 10.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
#!/usr/bin/env python3
"""
CLIF Table One Analysis CLI
Command-line interface for running validation and summary analysis on CLIF tables.
Usage Examples:
# Single table with both validation and summary
python run_analysis.py --patient --validate --summary
# Multiple tables with validation only
python run_analysis.py --patient --hospitalization --validate
# All implemented tables
python run_analysis.py --all --validate --summary
# Specify custom config file
python run_analysis.py --config path/to/config.json --patient --validate
# Verbose output for debugging
python run_analysis.py --patient --validate --summary --verbose
# Quiet mode (minimal output)
python run_analysis.py --all --validate --summary --quiet
"""
import os
import sys
import io
# Force UTF-8 encoding for Windows compatibility
# This ensures emojis and Unicode characters display correctly
if sys.platform == 'win32':
try:
# Set console code page to UTF-8 so emojis/unicode render correctly
os.system('chcp 65001 >nul 2>&1')
sys.stdout.reconfigure(encoding='utf-8', errors='replace')
sys.stderr.reconfigure(encoding='utf-8', errors='replace')
except (AttributeError, TypeError):
# Fallback: wrap the buffer directly
try:
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8', errors='replace')
sys.stderr = io.TextIOWrapper(sys.stderr.buffer, encoding='utf-8', errors='replace')
except (AttributeError, TypeError):
pass
import argparse
import json
from pathlib import Path
# Add modules to path
sys.path.insert(0, os.path.dirname(__file__))
# Add code directory for MCIDE import
sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'code'))
from modules.cli import CLIAnalysisRunner, ConsoleFormatter
def load_config(config_path: str) -> dict:
"""Load configuration from JSON file."""
try:
with open(config_path, 'r', encoding='utf-8') as f:
return json.load(f)
except FileNotFoundError:
print(f"[ERROR] Configuration file not found: {config_path}")
print("\nPlease ensure the config file exists and contains:")
print("""{
"site_name": "Your Hospital Name",
"site_id": "YOUR_ID",
"tables_path": "/path/to/clif/tables",
"filetype": "parquet",
"timezone": "UTC",
"output_dir": "output"
}""")
sys.exit(1)
except json.JSONDecodeError as e:
print(f"[ERROR] Invalid JSON in configuration file: {e}")
sys.exit(1)
def main():
"""Main CLI entry point."""
parser = argparse.ArgumentParser(
description='CLIF Table One Analysis - CLI Tool',
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
Examples:
%(prog)s --patient --validate --summary
%(prog)s --patient --hospitalization --validate
%(prog)s --all --validate --summary --verbose
%(prog)s --config custom/config.json --patient --validate
"""
)
# Configuration
parser.add_argument('--config', default='config/config.json',
help='Path to configuration JSON file (default: config/config.json)')
parser.add_argument('--output-dir', help='Override output directory from config')
# Table selection
table_group = parser.add_argument_group('Table Selection')
table_group.add_argument('--patient', action='store_true',
help='Analyze patient table')
table_group.add_argument('--hospitalization', action='store_true',
help='Analyze hospitalization table')
table_group.add_argument('--adt', action='store_true',
help='Analyze ADT table')
table_group.add_argument('--code_status', action='store_true',
help='Analyze code status table')
table_group.add_argument('--crrt_therapy', action='store_true',
help='Analyze CRRT therapy table')
table_group.add_argument('--hospital_diagnosis', action='store_true',
help='Analyze hospital diagnosis table')
table_group.add_argument('--labs', action='store_true',
help='Analyze labs table')
table_group.add_argument('--medication_admin_continuous', action='store_true',
help='Analyze medication admin continuous table')
table_group.add_argument('--medication_admin_intermittent', action='store_true',
help='Analyze medication admin intermittent table')
table_group.add_argument('--microbiology_culture', action='store_true',
help='Analyze microbiology culture table')
table_group.add_argument('--microbiology_susceptibility', action='store_true',
help='Analyze microbiology susceptibility table')
table_group.add_argument('--patient_assessments', action='store_true',
help='Analyze patient assessments table')
table_group.add_argument('--patient_procedures', action='store_true',
help='Analyze patient procedures table')
table_group.add_argument('--position', action='store_true',
help='Analyze position table')
table_group.add_argument('--respiratory_support', action='store_true',
help='Analyze respiratory support table')
table_group.add_argument('--vitals', action='store_true',
help='Analyze vitals table')
table_group.add_argument('--all', action='store_true',
help='Analyze all implemented tables')
# Operations
ops_group = parser.add_argument_group('Operations')
ops_group.add_argument('--validate', action='store_true',
help='Run validation using clifpy')
ops_group.add_argument('--summary', action='store_true',
help='Generate summary statistics')
# Output control
output_group = parser.add_argument_group('Output Control')
output_group.add_argument('--verbose', '-v', action='store_true',
help='Enable verbose output')
output_group.add_argument('--quiet', '-q', action='store_true',
help='Minimize output (only show errors and final summary)')
output_group.add_argument('--no-pdf', action='store_true',
help='Disable PDF report generation (only generate JSON)')
args = parser.parse_args()
# Validate arguments
has_table = (args.patient or args.hospitalization or args.adt or args.code_status or args.crrt_therapy or
args.hospital_diagnosis or args.labs or args.medication_admin_continuous or
args.medication_admin_intermittent or args.microbiology_culture or
args.microbiology_susceptibility or args.patient_assessments or args.patient_procedures or
args.position or args.respiratory_support or args.vitals or args.all)
if not has_table:
parser.error('Please specify at least one table or use --all for all tables')
if not (args.validate or args.summary):
parser.error('Please specify at least one operation: --validate and/or --summary')
# Determine which tables to analyze
tables = []
if args.all:
tables = ['patient', 'hospitalization', 'adt', 'code_status', 'crrt_therapy',
'hospital_diagnosis', 'labs', 'medication_admin_continuous', 'medication_admin_intermittent',
'microbiology_culture', 'microbiology_susceptibility',
'patient_assessments', 'patient_procedures', 'position', 'respiratory_support', 'vitals']
else:
if args.patient:
tables.append('patient')
if args.hospitalization:
tables.append('hospitalization')
if args.adt:
tables.append('adt')
if args.code_status:
tables.append('code_status')
if args.crrt_therapy:
tables.append('crrt_therapy')
if args.hospital_diagnosis:
tables.append('hospital_diagnosis')
if args.labs:
tables.append('labs')
if args.medication_admin_continuous:
tables.append('medication_admin_continuous')
if args.medication_admin_intermittent:
tables.append('medication_admin_intermittent')
if args.microbiology_culture:
tables.append('microbiology_culture')
if args.microbiology_susceptibility:
tables.append('microbiology_susceptibility')
if args.patient_assessments:
tables.append('patient_assessments')
if args.patient_procedures:
tables.append('patient_procedures')
if args.position:
tables.append('position')
if args.respiratory_support:
tables.append('respiratory_support')
if args.vitals:
tables.append('vitals')
# Load configuration
try:
config = load_config(args.config)
except Exception as e:
print(f"[ERROR] Error loading configuration: {e}")
sys.exit(1)
# Override output directory if specified
if args.output_dir:
config['output_dir'] = args.output_dir
# Validate configuration
if 'tables_path' not in config:
print("❌ Error: 'tables_path' not found in configuration file")
sys.exit(1)
data_path = config.get('tables_path', '')
if not os.path.exists(data_path):
print(f"[ERROR] Error: Data directory not found: {data_path}")
print("Please check your configuration and ensure the tables_path is correct.")
sys.exit(1)
# Initialize runner
generate_pdf = not args.no_pdf
runner = CLIAnalysisRunner(config, verbose=args.verbose, quiet=args.quiet, generate_pdf=generate_pdf)
# Run analysis
try:
results = runner.run_analysis(tables, args.validate, args.summary)
# Exit with appropriate code
if results['total_failed'] == 0:
sys.exit(0) # Success
elif results['total_success'] > 0:
sys.exit(2) # Partial success
else:
sys.exit(1) # Complete failure
except KeyboardInterrupt:
print("\n\n[WARNING] Analysis interrupted by user")
sys.exit(130)
except Exception as e:
print(f"\n[ERROR] Unexpected error: {e}")
if args.verbose:
import traceback
traceback.print_exc()
sys.exit(1)
if __name__ == "__main__":
main()