-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtransparency_analyzer_cli.py
More file actions
677 lines (577 loc) · 24.3 KB
/
transparency_analyzer_cli.py
File metadata and controls
677 lines (577 loc) · 24.3 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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
#!/usr/bin/env python3
"""
Transparency Analyzer CLI - Detect undisclosed bias risk in biomedical papers
Command-line interface for assessing transparency and disclosure completeness
of biomedical research publications. Uses local LLM (Ollama) for offline analysis.
Usage:
# Assess a single document by database ID
python transparency_analyzer_cli.py assess --doc-id 12345
# Assess a batch of documents with full text
python transparency_analyzer_cli.py assess --has-fulltext --limit 50
# Assess documents matching a search query
python transparency_analyzer_cli.py assess --query "cardiovascular exercise"
# Show statistics from previous assessments
python transparency_analyzer_cli.py stats
# Show detailed assessment for a document
python transparency_analyzer_cli.py show --doc-id 12345
# Export assessments to JSON or CSV
python transparency_analyzer_cli.py export --output results.json
python transparency_analyzer_cli.py export --output results.csv --format csv
"""
import argparse
import json
import logging
import sys
from datetime import datetime, timezone
from pathlib import Path
from typing import Any, Dict, List, Optional
import psycopg
from dotenv import load_dotenv
from src.bmlibrarian.agents import TransparencyAgent, TransparencyAssessment
from src.bmlibrarian.agents.transparency_data import RiskLevel
from src.bmlibrarian.config import BMLibrarianConfig
logger = logging.getLogger(__name__)
def setup_logging(verbose: bool = False) -> None:
"""Configure logging for the CLI.
Args:
verbose: Enable debug-level logging.
"""
level = logging.DEBUG if verbose else logging.INFO
logging.basicConfig(
level=level,
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
)
def get_db_connection() -> psycopg.Connection:
"""Get database connection from environment.
Returns:
Active database connection.
Raises:
SystemExit: If connection fails.
"""
user_env_path = Path.home() / ".bmlibrarian" / ".env"
if user_env_path.exists():
load_dotenv(user_env_path)
else:
load_dotenv()
try:
import os
conn = psycopg.connect(
dbname=os.environ.get("POSTGRES_DB", "knowledgebase"),
user=os.environ.get("POSTGRES_USER", ""),
password=os.environ.get("POSTGRES_PASSWORD", ""),
host=os.environ.get("POSTGRES_HOST", "localhost"),
port=os.environ.get("POSTGRES_PORT", "5432"),
)
return conn
except Exception as e:
print(f"Error: Could not connect to database: {e}", file=sys.stderr)
sys.exit(1)
def create_agent(config: Optional[BMLibrarianConfig] = None) -> TransparencyAgent:
"""Create a TransparencyAgent from configuration.
Args:
config: Optional BMLibrarianConfig instance.
Returns:
Configured TransparencyAgent.
"""
if config is None:
config = BMLibrarianConfig()
model = config.get_model("transparency")
host = config.get("ollama", {}).get("host", "http://localhost:11434")
agent_config = config.get_agent_config("transparency")
temperature = agent_config.get("temperature", 0.1)
top_p = agent_config.get("top_p", 0.9)
return TransparencyAgent(
model=model,
host=host,
temperature=temperature,
top_p=top_p,
)
def fetch_documents_for_assessment(
conn: psycopg.Connection,
doc_ids: Optional[List[int]] = None,
has_fulltext: bool = False,
query: Optional[str] = None,
limit: int = 100,
) -> List[Dict[str, Any]]:
"""Fetch documents from database for transparency assessment.
Args:
conn: Database connection.
doc_ids: Specific document IDs to fetch.
has_fulltext: Only fetch documents with full text.
query: Search query to filter documents.
limit: Maximum number of documents.
Returns:
List of document dictionaries.
"""
documents = []
with conn.cursor() as cur:
if doc_ids:
cur.execute(
"""
SELECT id, title, abstract, full_text, doi,
array_to_string(authors, ', ') as authors_str
FROM public.document
WHERE id = ANY(%s)
""",
(doc_ids,),
)
elif query:
cur.execute(
"""
SELECT id, title, abstract, full_text, doi,
array_to_string(authors, ', ') as authors_str
FROM public.document
WHERE (title ILIKE %s OR abstract ILIKE %s)
AND (abstract IS NOT NULL AND abstract != '')
ORDER BY publication_date DESC NULLS LAST
LIMIT %s
""",
(f"%{query}%", f"%{query}%", limit),
)
elif has_fulltext:
cur.execute(
"""
SELECT id, title, abstract, full_text, doi,
array_to_string(authors, ', ') as authors_str
FROM public.document
WHERE full_text IS NOT NULL AND full_text != ''
AND id NOT IN (SELECT document_id FROM transparency.assessments)
ORDER BY publication_date DESC NULLS LAST
LIMIT %s
""",
(limit,),
)
else:
cur.execute(
"""
SELECT id, title, abstract, full_text, doi,
array_to_string(authors, ', ') as authors_str
FROM public.document
WHERE (abstract IS NOT NULL AND abstract != '')
AND id NOT IN (SELECT document_id FROM transparency.assessments)
ORDER BY publication_date DESC NULLS LAST
LIMIT %s
""",
(limit,),
)
for row in cur.fetchall():
doc_id, title, abstract, full_text, doi, authors_str = row
documents.append({
"id": doc_id,
"title": title or "Untitled",
"abstract": abstract or "",
"full_text": full_text or "",
"doi": doi,
"authors": authors_str,
})
return documents
def save_assessment(
conn: psycopg.Connection,
assessment: TransparencyAssessment,
) -> None:
"""Save a transparency assessment to the database.
Args:
conn: Database connection.
assessment: TransparencyAssessment to save.
"""
try:
doc_id = int(assessment.document_id)
except (ValueError, TypeError):
logger.warning(f"Cannot save assessment - non-numeric document_id: {assessment.document_id}")
return
with conn.cursor() as cur:
cur.execute(
"""
INSERT INTO transparency.assessments (
document_id, has_funding_disclosure, funding_statement,
funding_sources, is_industry_funded, industry_funding_confidence,
funding_disclosure_quality,
has_coi_disclosure, coi_statement, conflicts_identified,
coi_disclosure_quality,
data_availability, data_availability_statement,
has_author_contributions, contributions_statement,
has_trial_registration, trial_registry_ids,
transparency_score, overall_confidence, risk_level,
risk_indicators, strengths, weaknesses,
is_retracted, retraction_reason, trial_sponsor_class,
model_used, agent_version
) VALUES (
%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s,
%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s
)
ON CONFLICT (document_id)
DO UPDATE SET
has_funding_disclosure = EXCLUDED.has_funding_disclosure,
funding_statement = EXCLUDED.funding_statement,
funding_sources = EXCLUDED.funding_sources,
is_industry_funded = EXCLUDED.is_industry_funded,
industry_funding_confidence = EXCLUDED.industry_funding_confidence,
funding_disclosure_quality = EXCLUDED.funding_disclosure_quality,
has_coi_disclosure = EXCLUDED.has_coi_disclosure,
coi_statement = EXCLUDED.coi_statement,
conflicts_identified = EXCLUDED.conflicts_identified,
coi_disclosure_quality = EXCLUDED.coi_disclosure_quality,
data_availability = EXCLUDED.data_availability,
data_availability_statement = EXCLUDED.data_availability_statement,
has_author_contributions = EXCLUDED.has_author_contributions,
contributions_statement = EXCLUDED.contributions_statement,
has_trial_registration = EXCLUDED.has_trial_registration,
trial_registry_ids = EXCLUDED.trial_registry_ids,
transparency_score = EXCLUDED.transparency_score,
overall_confidence = EXCLUDED.overall_confidence,
risk_level = EXCLUDED.risk_level,
risk_indicators = EXCLUDED.risk_indicators,
strengths = EXCLUDED.strengths,
weaknesses = EXCLUDED.weaknesses,
is_retracted = EXCLUDED.is_retracted,
retraction_reason = EXCLUDED.retraction_reason,
trial_sponsor_class = EXCLUDED.trial_sponsor_class,
model_used = EXCLUDED.model_used,
agent_version = EXCLUDED.agent_version,
assessed_at = NOW()
""",
(
doc_id,
assessment.has_funding_disclosure,
assessment.funding_statement,
assessment.funding_sources,
assessment.is_industry_funded,
assessment.industry_funding_confidence,
assessment.funding_disclosure_quality,
assessment.has_coi_disclosure,
assessment.coi_statement,
assessment.conflicts_identified,
assessment.coi_disclosure_quality,
assessment.data_availability,
assessment.data_availability_statement,
assessment.has_author_contributions,
assessment.contributions_statement,
assessment.has_trial_registration,
assessment.trial_registry_ids,
assessment.transparency_score,
assessment.overall_confidence,
assessment.risk_level,
assessment.risk_indicators,
assessment.strengths,
assessment.weaknesses,
assessment.is_retracted,
assessment.retraction_reason,
assessment.trial_sponsor_class,
assessment.model_used,
assessment.agent_version,
),
)
conn.commit()
# ──────────────────────────────────────────────────────────────────────────────
# CLI Commands
# ──────────────────────────────────────────────────────────────────────────────
def cmd_assess(args: argparse.Namespace) -> None:
"""Run transparency assessment on documents.
Args:
args: Parsed command line arguments.
"""
conn = get_db_connection()
agent = create_agent()
# Determine which documents to assess
doc_ids = None
if args.doc_id:
doc_ids = [args.doc_id]
documents = fetch_documents_for_assessment(
conn,
doc_ids=doc_ids,
has_fulltext=args.has_fulltext,
query=args.query,
limit=args.limit,
)
if not documents:
print("No documents found matching the criteria.")
return
print(f"\nAssessing transparency for {len(documents)} document(s)...\n")
def progress_callback(current: int, total: int, title: str) -> None:
"""Display progress during batch assessment."""
print(f" [{current}/{total}] {title[:60]}...")
assessments = agent.assess_batch(
documents,
progress_callback=progress_callback,
)
# Optionally enrich with bulk metadata
for assessment in assessments:
agent.enrich_with_metadata(assessment)
# Save to database
saved_count = 0
for assessment in assessments:
try:
save_assessment(conn, assessment)
saved_count += 1
except Exception as e:
logger.error(f"Failed to save assessment for document {assessment.document_id}: {e}")
conn.close()
# Print results
print(f"\n{'='*60}")
print(f"Assessment Complete: {len(assessments)} of {len(documents)} documents assessed")
print(f"Saved to database: {saved_count}")
print(f"{'='*60}\n")
for assessment in assessments:
print(agent.format_assessment_summary(assessment))
# Print aggregate stats
if len(assessments) > 1:
risk_dist = agent.get_risk_distribution(assessments)
print("\n--- RISK DISTRIBUTION ---")
for level, count in risk_dist.items():
print(f" {level.upper()}: {count}")
# Export if requested
if args.output:
output_path = Path(args.output)
if output_path.suffix == ".csv":
agent.export_to_csv(assessments, args.output)
else:
agent.export_to_json(assessments, args.output)
print(f"\nResults exported to: {args.output}")
def cmd_stats(args: argparse.Namespace) -> None:
"""Show transparency assessment statistics.
Args:
args: Parsed command line arguments.
"""
conn = get_db_connection()
with conn.cursor() as cur:
# Try the view first, fall back to direct query
try:
cur.execute("SELECT * FROM transparency.v_statistics")
row = cur.fetchone()
except Exception:
conn.rollback()
print("No transparency assessments found. Run 'assess' first.")
conn.close()
return
if not row:
print("No transparency assessments found. Run 'assess' first.")
conn.close()
return
(total, low, medium, high, unknown, avg_score,
industry_count, funding_count, coi_count, trial_count, retracted_count) = row
print(f"\n{'='*60}")
print(f"TRANSPARENCY ASSESSMENT STATISTICS")
print(f"{'='*60}")
print(f"\nTotal Assessed: {total}")
print(f"Average Score: {avg_score:.1f}/10" if avg_score else "Average Score: N/A")
print(f"\n--- Risk Distribution ---")
print(f" Low Risk: {low}")
print(f" Medium Risk: {medium}")
print(f" High Risk: {high}")
print(f" Unknown: {unknown}")
print(f"\n--- Disclosure Rates ---")
if total > 0:
print(f" With Funding Disclosure: {funding_count} ({funding_count/total:.0%})")
print(f" With COI Disclosure: {coi_count} ({coi_count/total:.0%})")
print(f" With Trial Registration: {trial_count} ({trial_count/total:.0%})")
print(f" Industry Funded: {industry_count} ({industry_count/total:.0%})")
print(f" Retracted: {retracted_count} ({retracted_count/total:.0%})")
print(f"{'='*60}\n")
conn.close()
def cmd_show(args: argparse.Namespace) -> None:
"""Show detailed assessment for a document.
Args:
args: Parsed command line arguments.
"""
conn = get_db_connection()
with conn.cursor() as cur:
cur.execute(
"""
SELECT a.*, d.title, d.doi, d.abstract
FROM transparency.assessments a
JOIN public.document d ON d.id = a.document_id
WHERE a.document_id = %s
""",
(args.doc_id,),
)
row = cur.fetchone()
if not row:
print(f"No transparency assessment found for document ID {args.doc_id}.")
print("Run 'assess --doc-id {id}' to create one.")
conn.close()
return
# Build assessment from database row (use column names)
colnames = [desc[0] for desc in conn.cursor().description] if hasattr(conn, 'cursor') else []
# Simpler approach: just fetch and display using the agent formatter
agent = TransparencyAgent(show_model_info=False)
with conn.cursor() as cur:
cur.execute(
"""
SELECT document_id, transparency_score, risk_level, overall_confidence,
has_funding_disclosure, funding_statement, funding_sources,
is_industry_funded, industry_funding_confidence, funding_disclosure_quality,
has_coi_disclosure, coi_statement, conflicts_identified, coi_disclosure_quality,
data_availability, data_availability_statement,
has_author_contributions, contributions_statement,
has_trial_registration, trial_registry_ids,
risk_indicators, strengths, weaknesses,
is_retracted, retraction_reason, trial_sponsor_class,
model_used, agent_version, assessed_at
FROM transparency.assessments
WHERE document_id = %s
""",
(args.doc_id,),
)
row = cur.fetchone()
if not row:
print(f"No assessment found for document {args.doc_id}")
conn.close()
return
# Fetch title
with conn.cursor() as cur:
cur.execute("SELECT title FROM public.document WHERE id = %s", (args.doc_id,))
title_row = cur.fetchone()
title = title_row[0] if title_row else "Unknown"
assessment = TransparencyAssessment(
document_id=str(row[0]),
document_title=title,
transparency_score=row[1] or 0.0,
risk_level=row[2] or "unknown",
overall_confidence=row[3] or 0.0,
has_funding_disclosure=row[4] or False,
funding_statement=row[5],
funding_sources=row[6] or [],
is_industry_funded=row[7],
industry_funding_confidence=row[8] or 0.0,
funding_disclosure_quality=row[9] or 0.0,
has_coi_disclosure=row[10] or False,
coi_statement=row[11],
conflicts_identified=row[12] or [],
coi_disclosure_quality=row[13] or 0.0,
data_availability=row[14] or "not_stated",
data_availability_statement=row[15],
has_author_contributions=row[16] or False,
contributions_statement=row[17],
has_trial_registration=row[18] or False,
trial_registry_ids=row[19] or [],
risk_indicators=row[20] or [],
strengths=row[21] or [],
weaknesses=row[22] or [],
is_retracted=row[23],
retraction_reason=row[24],
trial_sponsor_class=row[25],
model_used=row[26],
agent_version=row[27],
)
print(agent.format_assessment_summary(assessment))
conn.close()
def cmd_export(args: argparse.Namespace) -> None:
"""Export assessments to file.
Args:
args: Parsed command line arguments.
"""
conn = get_db_connection()
agent = TransparencyAgent(show_model_info=False)
with conn.cursor() as cur:
cur.execute(
"""
SELECT a.document_id, d.title, d.doi,
a.transparency_score, a.risk_level, a.overall_confidence,
a.has_funding_disclosure, a.funding_sources,
a.is_industry_funded, a.funding_disclosure_quality,
a.has_coi_disclosure, a.coi_disclosure_quality,
a.data_availability, a.has_author_contributions,
a.has_trial_registration, a.trial_registry_ids,
a.risk_indicators, a.strengths, a.weaknesses,
a.is_retracted, a.trial_sponsor_class
FROM transparency.assessments a
JOIN public.document d ON d.id = a.document_id
ORDER BY a.transparency_score ASC
"""
)
rows = cur.fetchall()
if not rows:
print("No assessments to export.")
conn.close()
return
assessments = []
for row in rows:
assessment = TransparencyAssessment(
document_id=str(row[0]),
document_title=row[1] or "Unknown",
doi=row[2],
transparency_score=row[3] or 0.0,
risk_level=row[4] or "unknown",
overall_confidence=row[5] or 0.0,
has_funding_disclosure=row[6] or False,
funding_sources=row[7] or [],
is_industry_funded=row[8],
funding_disclosure_quality=row[9] or 0.0,
has_coi_disclosure=row[10] or False,
coi_disclosure_quality=row[11] or 0.0,
data_availability=row[12] or "not_stated",
has_author_contributions=row[13] or False,
has_trial_registration=row[14] or False,
trial_registry_ids=row[15] or [],
risk_indicators=row[16] or [],
strengths=row[17] or [],
weaknesses=row[18] or [],
is_retracted=row[19],
trial_sponsor_class=row[20],
)
assessments.append(assessment)
output_path = Path(args.output)
if args.format == "csv" or output_path.suffix == ".csv":
agent.export_to_csv(assessments, args.output)
else:
agent.export_to_json(assessments, args.output)
print(f"Exported {len(assessments)} assessments to {args.output}")
conn.close()
# ──────────────────────────────────────────────────────────────────────────────
# Argument Parser
# ──────────────────────────────────────────────────────────────────────────────
def build_parser() -> argparse.ArgumentParser:
"""Build the argument parser.
Returns:
Configured ArgumentParser.
"""
parser = argparse.ArgumentParser(
description="Transparency Analyzer - Detect undisclosed bias risk in biomedical papers",
formatter_class=argparse.RawDescriptionHelpFormatter,
)
parser.add_argument("-v", "--verbose", action="store_true", help="Enable verbose logging")
subparsers = parser.add_subparsers(dest="command", help="Available commands")
# assess command
assess_parser = subparsers.add_parser("assess", help="Run transparency assessment")
assess_parser.add_argument("--doc-id", type=int, help="Assess a specific document by ID")
assess_parser.add_argument("--query", type=str, help="Search query to filter documents")
assess_parser.add_argument("--has-fulltext", action="store_true",
help="Only assess documents with full text")
assess_parser.add_argument("--limit", type=int, default=100,
help="Maximum documents to assess (default: 100)")
assess_parser.add_argument("-o", "--output", type=str,
help="Export results to file (.json or .csv)")
# stats command
subparsers.add_parser("stats", help="Show assessment statistics")
# show command
show_parser = subparsers.add_parser("show", help="Show assessment for a document")
show_parser.add_argument("--doc-id", type=int, required=True, help="Document ID")
# export command
export_parser = subparsers.add_parser("export", help="Export all assessments")
export_parser.add_argument("-o", "--output", type=str, required=True,
help="Output file path (.json or .csv)")
export_parser.add_argument("--format", choices=["json", "csv"], default="json",
help="Export format (default: json)")
return parser
def main() -> None:
"""Main entry point for the CLI."""
parser = build_parser()
args = parser.parse_args()
setup_logging(args.verbose)
if not args.command:
parser.print_help()
sys.exit(0)
commands = {
"assess": cmd_assess,
"stats": cmd_stats,
"show": cmd_show,
"export": cmd_export,
}
cmd_func = commands.get(args.command)
if cmd_func:
cmd_func(args)
else:
parser.print_help()
if __name__ == "__main__":
main()