Skip to content

Commit

Permalink
[FIX] Address performance issues in SPARQL query (#308)
Browse files Browse the repository at this point in the history
* Print out the response durations

* [FIX] Correct typo in query template

Fixes #306

* [REF] SPARQL OPTIONAL inline

For improved readability

* [ENH] Explicit imaging session type

Removes need to traverse entire graph
Improves query performance by a lot

* Revert "Print out the response durations"

it served its purpose

This reverts commit ffc36ae.

* [MNT] Ran black

also addressed flake8 complaints

* [DOC] Add updated query template
  • Loading branch information
surchs authored Apr 16, 2024
1 parent 4c26b3e commit d276155
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 75 deletions.
52 changes: 15 additions & 37 deletions app/api/utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,50 +206,27 @@ def create_query(
nb:hasLabel ?session_id.
OPTIONAL {{
?session nb:hasAcquisition/nb:hasContrastType ?image_modal.
OPTIONAL {{
?session nb:hasFilePath ?session_file_path.
}}
}}
OPTIONAL {{
?dataset_uuid nb:hasPortalURI ?dataset_portal_uri.
}}
OPTIONAL {{
?session nb:hasAge ?age.
}}
OPTIONAL {{
?session nb:hasSex ?sex.
}}
OPTIONAL {{
?session nb:hasDiagnosis ?diagnosis.
}}
OPTIONAL {{
?session nb:isSubjectGroup ?subject_group.
}}
OPTIONAL {{
?session nb:hasAssessment ?assessment.
OPTIONAL {{?session nb:hasFilePath ?session_file_path.}}
}}
OPTIONAL {{?dataset_uuid nb:hasPortalURI ?dataset_portal_uri.}}
OPTIONAL {{?session nb:hasAge ?age.}}
OPTIONAL {{?session nb:hasSex ?sex.}}
OPTIONAL {{?session nb:hasDiagnosis ?diagnosis.}}
OPTIONAL {{?session nb:isSubjectGroup ?subject_group.}}
OPTIONAL {{?session nb:hasAssessment ?assessment.}}
{{
SELECT ?subject (count(distinct ?phenotypic_session) as ?num_matching_phenotypic_sessions)
WHERE {{
?subject a nb:Subject.
OPTIONAL {{
?subject nb:hasSession ?phenotypic_session.
?phenotypic_session a nb:PhenotypicSession.
OPTIONAL {{
?phenotypic_session nb:hasAge ?age.
}}
OPTIONAL {{
?phenotypic_session nb:hasSex ?sex.
}}
OPTIONAL {{
?phenoypic_session nb:hasDiagnosis ?diagnosis.
}}
OPTIONAL {{
?phenotypic_session nb:isSubjectGroup ?subject_group.
}}
OPTIONAL {{
?phenotypic_session nb:hasAssessment ?assessment.
}}
OPTIONAL {{?phenotypic_session nb:hasAge ?age.}}
OPTIONAL {{?phenotypic_session nb:hasSex ?sex.}}
OPTIONAL {{?phenotypic_session nb:hasDiagnosis ?diagnosis.}}
OPTIONAL {{?phenotypic_session nb:isSubjectGroup ?subject_group.}}
OPTIONAL {{?phenotypic_session nb:hasAssessment ?assessment.}}
}}
{phenotypic_session_level_filters}
}} GROUP BY ?subject
Expand All @@ -260,7 +237,8 @@ def create_query(
?subject a nb:Subject.
OPTIONAL {{
?subject nb:hasSession ?imaging_session.
?imaging_session nb:hasAcquisition/nb:hasContrastType ?image_modal.
?imaging_session a nb:ImagingSession;
nb:hasAcquisition/nb:hasContrastType ?image_modal.
}}
{imaging_session_level_filters}
}} GROUP BY ?subject
Expand Down
54 changes: 16 additions & 38 deletions docs/default_neurobagel_query.rq
Original file line number Diff line number Diff line change
Expand Up @@ -18,50 +18,27 @@ WHERE {
nb:hasLabel ?session_id.
OPTIONAL {
?session nb:hasAcquisition/nb:hasContrastType ?image_modal.
OPTIONAL {
?session nb:hasFilePath ?session_file_path.
}
}
OPTIONAL {
?dataset_uuid nb:hasPortalURI ?dataset_portal_uri.
}
OPTIONAL {
?session nb:hasAge ?age.
}
OPTIONAL {
?session nb:hasSex ?sex.
}
OPTIONAL {
?session nb:hasDiagnosis ?diagnosis.
}
OPTIONAL {
?session nb:isSubjectGroup ?subject_group.
}
OPTIONAL {
?session nb:hasAssessment ?assessment.
}
OPTIONAL {?session nb:hasFilePath ?session_file_path.}
}
OPTIONAL {?dataset_uuid nb:hasPortalURI ?dataset_portal_uri.}
OPTIONAL {?session nb:hasAge ?age.}
OPTIONAL {?session nb:hasSex ?sex.}
OPTIONAL {?session nb:hasDiagnosis ?diagnosis.}
OPTIONAL {?session nb:isSubjectGroup ?subject_group.}
OPTIONAL {?session nb:hasAssessment ?assessment.}
{
SELECT ?subject (count(distinct ?phenotypic_session) as ?num_matching_phenotypic_sessions)
WHERE {
?subject a nb:Subject.
OPTIONAL {
?subject nb:hasSession ?phenotypic_session.
?phenotypic_session a nb:PhenotypicSession.
OPTIONAL {
?phenotypic_session nb:hasAge ?age.
}
OPTIONAL {
?phenotypic_session nb:hasSex ?sex.
}
OPTIONAL {
?phenoypic_session nb:hasDiagnosis ?diagnosis.
}
OPTIONAL {
?phenotypic_session nb:isSubjectGroup ?subject_group.
}
OPTIONAL {
?phenotypic_session nb:hasAssessment ?assessment.
}

OPTIONAL {?phenotypic_session nb:hasAge ?age.}
OPTIONAL {?phenotypic_session nb:hasSex ?sex.}
OPTIONAL {?phenotypic_session nb:hasDiagnosis ?diagnosis.}
OPTIONAL {?phenotypic_session nb:isSubjectGroup ?subject_group.}
OPTIONAL {?phenotypic_session nb:hasAssessment ?assessment.}
}

} GROUP BY ?subject
Expand All @@ -72,7 +49,8 @@ WHERE {
?subject a nb:Subject.
OPTIONAL {
?subject nb:hasSession ?imaging_session.
?imaging_session nb:hasAcquisition/nb:hasContrastType ?image_modal.
?imaging_session a nb:ImagingSession;
nb:hasAcquisition/nb:hasContrastType ?image_modal.
}

} GROUP BY ?subject
Expand Down

0 comments on commit d276155

Please sign in to comment.