|
| 1 | +from sqlalchemy import exists, or_ |
| 2 | +from neurostore.models.data import Study, Analysis, Point, Image |
| 3 | +from neurostore.database import db |
| 4 | + |
| 5 | +# Get studies without base_study_id that have analyses with points or images |
| 6 | +orphans_with_analyses = Study.query.filter( |
| 7 | + Study.base_study_id == None, # Studies without base_study_id |
| 8 | + or_( |
| 9 | + # Has analysis with points |
| 10 | + exists().where( |
| 11 | + Analysis.study_id == Study.id, |
| 12 | + exists().where(Point.analysis_id == Analysis.id) |
| 13 | + ), |
| 14 | + # Has analysis with images |
| 15 | + exists().where( |
| 16 | + Analysis.study_id == Study.id, |
| 17 | + exists().where(Image.analysis_id == Analysis.id) |
| 18 | + ) |
| 19 | + ) |
| 20 | +).all() |
| 21 | + |
| 22 | +print(f"Found {len(orphans_with_analyses)} orphan studies with analyses containing points or images:") |
| 23 | +for study in orphans_with_analyses: |
| 24 | + print(f"\nStudy ID: {study.id}") |
| 25 | + print(f"Name: {study.name}") |
| 26 | + |
| 27 | + # Count points and images for this study |
| 28 | + point_count = db.session.query(Point)\ |
| 29 | + .join(Analysis)\ |
| 30 | + .filter(Analysis.study_id == study.id)\ |
| 31 | + .count() |
| 32 | + |
| 33 | + image_count = db.session.query(Image)\ |
| 34 | + .join(Analysis)\ |
| 35 | + .filter(Analysis.study_id == study.id)\ |
| 36 | + .count() |
| 37 | + |
| 38 | + print(f"Points: {point_count}") |
| 39 | + print(f"Images: {image_count}") |
| 40 | + |
| 41 | + # Print identifiers if available |
| 42 | + if study.pmid: |
| 43 | + print(f"PMID: {study.pmid}") |
| 44 | + if study.doi: |
| 45 | + print(f"DOI: {study.doi}") |
| 46 | + if study.pmcid: |
| 47 | + print(f"PMCID: {study.pmcid}") |
0 commit comments