Skip to content

Commit 6287390

Browse files
committed
Don't export excluded annotations if not in config
1 parent 51f7bf7 commit 6287390

3 files changed

Lines changed: 92 additions & 6 deletions

File tree

autonima.egg-info/SOURCES.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ tests/test_multi_annotation.py
6060
tests/test_objective_in_prompt.py
6161
tests/test_parallel_screening.py
6262
tests/test_pipeline_retrieval.py
63+
tests/test_pipeline_stage_cutoff.py
6364
tests/test_pubmed.py
6465
tests/test_retrieval.py
6566
tests/test_screening.py

autonima/coordinates/nimads_models.py

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -340,16 +340,32 @@ def create_annotations_from_results(
340340
"""
341341
if not annotation_results:
342342
return []
343-
343+
344+
# Keep annotations aligned to the exported NiMADS studyset.
345+
# This avoids emitting notes for analyses that were filtered out
346+
# (e.g., excluded studies when export_excluded_studies is false).
347+
studyset_analysis_ids = {
348+
analysis.id
349+
for study in studyset.studies
350+
for analysis in study.analyses
351+
}
352+
344353
# Group annotation results by annotation name and analysis_id
345354
annotations_by_name = {}
346-
all_analysis_ids = set()
355+
annotation_names = set()
347356

348357
for result in annotation_results:
358+
annotation_names.add(result.annotation_name)
359+
if result.analysis_id not in studyset_analysis_ids:
360+
continue
349361
if result.annotation_name not in annotations_by_name:
350362
annotations_by_name[result.annotation_name] = {}
351363
annotations_by_name[result.annotation_name][result.analysis_id] = result.include
352-
all_analysis_ids.add(result.analysis_id)
364+
365+
# Ensure all annotation columns are represented in note_keys, even if
366+
# some analyses were filtered out.
367+
for annotation_name in annotation_names:
368+
annotations_by_name.setdefault(annotation_name, {})
353369

354370
# Create note_keys with all annotation names as boolean columns
355371
note_keys = {}
@@ -366,8 +382,8 @@ def create_annotations_from_results(
366382
studyset_id=studyset_id
367383
)
368384

369-
# Create notes for each analysis with all annotation decisions
370-
for analysis_id in sorted(all_analysis_ids):
385+
# Create notes for each exported analysis with all annotation decisions.
386+
for analysis_id in sorted(studyset_analysis_ids):
371387
note_data = {}
372388

373389
# For each annotation type, check if this analysis was included
@@ -511,4 +527,4 @@ def sanitize_annotation_dict(annotation_dict: dict) -> dict:
511527
if 'analysis' in note and note['analysis']:
512528
# Sanitize the analysis ID (which may contain quotes from the name)
513529
note['analysis'] = sanitize_analysis_name(note['analysis'])
514-
return annotation_dict
530+
return annotation_dict

tests/test_nimads_annotations.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
from autonima.annotation.schema import AnnotationDecision
2+
from autonima.coordinates.nimads_models import (
3+
convert_to_nimads_studyset,
4+
create_annotations_from_results,
5+
)
6+
from autonima.coordinates.schema import Analysis, CoordinatePoint
7+
from autonima.models.types import Study, StudyStatus
8+
9+
10+
def _make_study(pmid: str, status: StudyStatus) -> Study:
11+
return Study(
12+
pmid=pmid,
13+
title=f"Study {pmid}",
14+
abstract="Abstract",
15+
authors=["Doe"],
16+
journal="Journal",
17+
publication_date="2020",
18+
status=status,
19+
analyses=[
20+
Analysis(
21+
name="contrast",
22+
description="desc",
23+
table_id="table_1",
24+
points=[
25+
CoordinatePoint(
26+
coordinates=[1.0, 2.0, 3.0],
27+
space="MNI",
28+
values=[],
29+
)
30+
],
31+
)
32+
],
33+
)
34+
35+
36+
def test_create_annotations_from_results_filters_out_non_studyset_analyses():
37+
included = _make_study("100", StudyStatus.INCLUDED_FULLTEXT)
38+
excluded = _make_study("200", StudyStatus.EXCLUDED_FULLTEXT)
39+
40+
# Simulate export_excluded_studies: false by building the studyset from
41+
# only included studies.
42+
studyset = convert_to_nimads_studyset("studyset_1", [included])
43+
44+
annotation_results = [
45+
AnnotationDecision(
46+
annotation_name="risky_dm",
47+
analysis_id="100_analysis_0",
48+
study_id="100",
49+
include=True,
50+
reasoning="included study analysis",
51+
model_used="test-model",
52+
),
53+
AnnotationDecision(
54+
annotation_name="risky_dm",
55+
analysis_id="200_analysis_0",
56+
study_id="200",
57+
include=True,
58+
reasoning="excluded study analysis",
59+
model_used="test-model",
60+
),
61+
]
62+
63+
annotation = create_annotations_from_results(
64+
"studyset_1", studyset, annotation_results
65+
)
66+
67+
exported_analysis_ids = {note.analysis_id for note in annotation.notes}
68+
assert exported_analysis_ids == {"100_analysis_0"}
69+
assert annotation.notes[0].note["risky_dm"] is True

0 commit comments

Comments
 (0)