File tree Expand file tree Collapse file tree 4 files changed +69
-0
lines changed
neurosynth_compose/scripts Expand file tree Collapse file tree 4 files changed +69
-0
lines changed Original file line number Diff line number Diff line change 1414from neurosynth_compose .database import db
1515from neurosynth_compose import models
1616from neurosynth_compose .ingest import neurostore as ingest_nstore
17+ from neurosynth_compose .backfill_extraction_metadata import (
18+ add_missing_extraction_ids ,
19+ )
1720
1821
1922app .config .from_object (os .environ ["APP_SETTINGS" ])
@@ -44,3 +47,12 @@ def create_meta_analyses(n_studysets, neurostore_url):
4447 if n_studysets is not None :
4548 n_studysets = int (n_studysets )
4649 ingest_nstore .create_meta_analyses (url = neurostore_url , n_studysets = n_studysets )
50+
51+
52+ @app .cli .command ("backfill-extraction-metadata" )
53+ def backfill_extraction_metadata ():
54+ """Add missing extractionMetadata ids to project provenance."""
55+ updated , skipped = add_missing_extraction_ids ()
56+ click .echo (
57+ f"Updated { updated } project(s); skipped { skipped } project(s) with no changes."
58+ )
File renamed without changes.
Original file line number Diff line number Diff line change 1+ import logging
2+ from typing import Tuple
3+
4+ from sqlalchemy import select
5+
6+ from neurosynth_compose .database import db
7+ from neurosynth_compose .models .analysis import Project
8+
9+ logger = logging .getLogger (__name__ )
10+
11+
12+ def add_missing_extraction_ids (session = None ) -> Tuple [int , int ]:
13+ """Add null studysetId/annotationId keys to extractionMetadata when absent."""
14+ sess = session or db .session
15+ updated = 0
16+ skipped = 0
17+
18+ projects = sess .scalars (select (Project )).all ()
19+
20+ for project in projects :
21+ provenance = project .provenance or {}
22+ extraction_metadata = provenance .get ("extractionMetadata" )
23+
24+ if not isinstance (extraction_metadata , dict ):
25+ skipped += 1
26+ continue
27+
28+ changed = False
29+
30+ if "studysetId" not in extraction_metadata :
31+ extraction_metadata ["studysetId" ] = None
32+ changed = True
33+
34+ if "annotationId" not in extraction_metadata :
35+ extraction_metadata ["annotationId" ] = None
36+ changed = True
37+
38+ if changed :
39+ provenance ["extractionMetadata" ] = extraction_metadata
40+ project .provenance = provenance
41+ updated += 1
42+ else :
43+ skipped += 1
44+
45+ if updated :
46+ try :
47+ sess .commit ()
48+ except Exception :
49+ sess .rollback ()
50+ logger .exception (
51+ "Failed to commit extractionMetadata backfill for projects."
52+ )
53+ raise
54+ else :
55+ sess .rollback ()
56+
57+ return updated , skipped
You can’t perform that action at this time.
0 commit comments