Skip to content

Commit ab9e602

Browse files
authored
[FIX] project metadata (#1224)
* add script to fix project metadata * fix path to manage.py * fix path of import
1 parent 678b350 commit ab9e602

File tree

4 files changed

+69
-0
lines changed

4 files changed

+69
-0
lines changed

compose/backend/manage.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
from neurosynth_compose.database import db
1515
from neurosynth_compose import models
1616
from neurosynth_compose.ingest import neurostore as ingest_nstore
17+
from neurosynth_compose.scripts.backfill_extraction_metadata import (
18+
add_missing_extraction_ids,
19+
)
1720

1821

1922
app.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+
)

compose/backend/neurosynth_compose/scripts/__init__.py

Whitespace-only changes.
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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

0 commit comments

Comments
 (0)