@@ -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
0 commit comments