Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ENH] Implemented pipeline_version and pipeline_name query fields #345

Merged
merged 17 commits into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions app/api/crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ async def get(
min_num_phenotypic_sessions: int,
assessment: str,
image_modal: str,
pipeline_version: str,
pipeline_name: str,
rmanaem marked this conversation as resolved.
Show resolved Hide resolved
) -> list[CohortQueryResponse]:
"""
Sends SPARQL queries to the graph API via httpx POST requests for subject-session or dataset metadata
Expand All @@ -125,6 +127,10 @@ async def get(
Non-imaging assessment completed by subjects.
image_modal : str
Imaging modality of subject scans.
pipeline_version : str
Pipeline version of subject scans.
pipeline_name : str
Pipeline name of subject scans.
rmanaem marked this conversation as resolved.
Show resolved Hide resolved

Returns
-------
Expand All @@ -142,6 +148,8 @@ async def get(
min_num_imaging_sessions=min_num_imaging_sessions,
assessment=assessment,
image_modal=image_modal,
pipeline_version=pipeline_version,
pipeline_name=pipeline_name,
)
)

Expand Down Expand Up @@ -184,6 +192,8 @@ async def get(
"subject_group": "first",
"assessment": lambda x: list(x.unique()),
"image_modal": lambda x: list(x.unique()),
"pipeline_version": lambda x: list(x.unique()),
rmanaem marked this conversation as resolved.
Show resolved Hide resolved
"pipeline_name": lambda x: list(x.unique()),
"session_file_path": "first",
}
)
Expand Down Expand Up @@ -224,6 +234,16 @@ async def get(
group["image_modal"].notna()
].unique()
),
pipeline_version=list(
group["pipeline_version"][
group["pipeline_version"].notna()
].unique()
),
pipeline_name=list(
group["pipeline_name"][
group["pipeline_name"].notna()
].unique()
),
)
)

Expand Down
7 changes: 7 additions & 0 deletions app/api/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from pydantic import BaseModel, constr, root_validator

CONTROLLED_TERM_REGEX = r"^[a-zA-Z]+[:]\S+$"
VERSION_REGEX = r"^\d+\.\d+\.\d+$"
rmanaem marked this conversation as resolved.
Show resolved Hide resolved


class QueryModel(BaseModel):
Expand All @@ -22,6 +23,8 @@ class QueryModel(BaseModel):
min_num_phenotypic_sessions: int = Query(default=None, ge=0)
assessment: constr(regex=CONTROLLED_TERM_REGEX) = None
image_modal: constr(regex=CONTROLLED_TERM_REGEX) = None
pipeline_version: constr(regex=VERSION_REGEX) = None
pipeline_name: constr(regex=CONTROLLED_TERM_REGEX) = None
rmanaem marked this conversation as resolved.
Show resolved Hide resolved

@root_validator()
def check_maxage_ge_minage(cls, values):
Expand Down Expand Up @@ -67,6 +70,8 @@ class SessionResponse(BaseModel):
assessment: list
image_modal: list
session_file_path: Optional[str]
pipeline_version: list
pipeline_name: list


class CohortQueryResponse(BaseModel):
Expand All @@ -81,6 +86,8 @@ class CohortQueryResponse(BaseModel):
num_matching_subjects: int
subject_data: Union[list[SessionResponse], str]
image_modals: list
pipeline_version: list
pipeline_name: list


class DataElementURI(str, Enum):
Expand Down
2 changes: 2 additions & 0 deletions app/api/routers/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ async def get_query(
query.min_num_phenotypic_sessions,
query.assessment,
query.image_modal,
query.pipeline_version,
query.pipeline_name,
rmanaem marked this conversation as resolved.
Show resolved Hide resolved
)

return response
34 changes: 33 additions & 1 deletion app/api/utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"ncit": "http://ncicb.nci.nih.gov/xml/owl/EVS/Thesaurus.owl#",
"nidm": "http://purl.org/nidash/nidm#",
"snomed": "http://purl.bioontology.org/ontology/SNOMEDCT/",
"np": "https://github.com/nipoppy/pipeline-catalog/tree/main/processing",
}

# Store domains in named tuples
Expand All @@ -61,6 +62,8 @@
IS_CONTROL = Domain("subject_group", "nb:isSubjectGroup")
ASSESSMENT = Domain("assessment", "nb:hasAssessment")
IMAGE_MODAL = Domain("image_modal", "nb:hasContrastType")
PIPELINE_VERSION = Domain("pipeline_version", "nb:hasPipelineVersion")
PIPELINE_NAME = Domain("pipeline_name", "nb:hasPipelineName")
PROJECT = Domain("project", "nb:hasSamples")


Expand Down Expand Up @@ -115,6 +118,8 @@ def create_query(
min_num_phenotypic_sessions: Optional[int] = None,
assessment: Optional[str] = None,
image_modal: Optional[str] = None,
pipeline_version: Optional[str] = None,
pipeline_name: Optional[str] = None,
) -> str:
"""
Creates a SPARQL query using a query template and filters it using the input parameters.
Expand All @@ -139,6 +144,10 @@ def create_query(
Non-imaging assessment completed by subjects, by default None.
image_modal : str, optional
Imaging modality of subject scans, by default None.
pipeline_version : str, optional
Pipeline version of subject scans, by default None.
pipeline_name : str, optional
Pipeline name of subject scans, by default None.
rmanaem marked this conversation as resolved.
Show resolved Hide resolved

Returns
-------
Expand Down Expand Up @@ -206,10 +215,22 @@ def create_query(
"\n" + f"FILTER (?{IMAGE_MODAL.var} = {image_modal})."
)

if pipeline_version is not None:
imaging_session_level_filters += (
"\n"
+ f'FILTER (?{PIPELINE_VERSION.var} = "{pipeline_version}").' # Wrap with quotes
rmanaem marked this conversation as resolved.
Show resolved Hide resolved
)

if pipeline_name is not None:
rmanaem marked this conversation as resolved.
Show resolved Hide resolved
imaging_session_level_filters += (
"\n" + f"FILTER (?{PIPELINE_NAME.var} = {pipeline_name})."
)

query_string = textwrap.dedent(
f"""
SELECT DISTINCT ?dataset_uuid ?dataset_name ?dataset_portal_uri ?sub_id ?age ?sex
?diagnosis ?subject_group ?num_matching_phenotypic_sessions ?num_matching_imaging_sessions ?session_id ?session_type ?assessment ?image_modal ?session_file_path
?diagnosis ?subject_group ?num_matching_phenotypic_sessions ?num_matching_imaging_sessions
?session_id ?session_type ?assessment ?image_modal ?session_file_path ?pipeline_version ?pipeline_name
WHERE {{
?dataset_uuid a nb:Dataset;
nb:hasLabel ?dataset_name;
Expand Down Expand Up @@ -244,6 +265,12 @@ def create_query(
{phenotypic_session_level_filters}
}} GROUP BY ?subject
}}

OPTIONAL {{
?session nb:hasCompletedPipeline ?pipeline.
?pipeline nb:hasPipelineVersion ?pipeline_version.
?pipeline nb:hasPipelineName ?pipeline_name.
}}
{{
SELECT ?subject (count(distinct ?imaging_session) as ?num_matching_imaging_sessions)
WHERE {{
Expand All @@ -253,6 +280,11 @@ def create_query(
?imaging_session a nb:ImagingSession;
nb:hasAcquisition/nb:hasContrastType ?image_modal.
rmanaem marked this conversation as resolved.
Show resolved Hide resolved
}}
OPTIONAL {{
?imaging_session nb:hasCompletedPipeline ?pipeline.
?pipeline nb:hasPipelineVersion ?pipeline_version.
?pipeline nb:hasPipelineName ?pipeline_name.
}}
{imaging_session_level_filters}
}} GROUP BY ?subject
}}
Expand Down
12 changes: 11 additions & 1 deletion docs/default_neurobagel_query.rq
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ PREFIX nidm: <http://purl.org/nidash/nidm#>
PREFIX snomed: <http://purl.bioontology.org/ontology/SNOMEDCT/>

SELECT DISTINCT ?dataset_uuid ?dataset_name ?dataset_portal_uri ?sub_id ?age ?sex
?diagnosis ?subject_group ?num_matching_phenotypic_sessions ?num_matching_imaging_sessions ?session_id ?session_type ?assessment ?image_modal ?session_file_path
?diagnosis ?subject_group ?num_matching_phenotypic_sessions ?num_matching_imaging_sessions ?session_id ?session_type ?assessment ?image_modal ?session_file_path ?pipeline_name ?pipeline_version
WHERE {
?dataset_uuid a nb:Dataset;
nb:hasLabel ?dataset_name;
Expand Down Expand Up @@ -41,6 +41,11 @@ WHERE {

} GROUP BY ?subject
}
OPTIONAL {
?session nb:hasCompletedPipeline ?pipeline.
?pipeline nb:hasPipelineVersion ?pipeline_version.
?pipeline nb:hasPipelineName ?pipeline_name.
}
{
SELECT ?subject (count(distinct ?imaging_session) as ?num_matching_imaging_sessions)
WHERE {
Expand All @@ -50,6 +55,11 @@ WHERE {
?imaging_session a nb:ImagingSession;
nb:hasAcquisition/nb:hasContrastType ?image_modal.
}
OPTIONAL {
?imaging_session nb:hasCompletedPipeline ?pipeline.
?pipeline nb:hasPipelineVersion ?pipeline_version.
?pipeline nb:hasPipelineName ?pipeline_name.
}

} GROUP BY ?subject
}
Expand Down
10 changes: 10 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ def test_data():
"http://purl.org/nidash/nidm#T1Weighted",
"http://purl.org/nidash/nidm#T2Weighted",
],
"pipeline_version": ["7.3.2", "23.1.3"],
"pipeline_name": ["freesurfer", "fmriprep"],
},
{
"dataset_uuid": "http://neurobagel.org/vocab/67890",
Expand All @@ -86,6 +88,8 @@ def test_data():
"http://purl.org/nidash/nidm#FlowWeighted",
"http://purl.org/nidash/nidm#T1Weighted",
],
"pipeline_version": ["7.3.2"],
"pipeline_name": ["freesurfer"],
},
]

Expand Down Expand Up @@ -178,6 +182,8 @@ async def _mock_get_with_exception(
min_num_phenotypic_sessions,
assessment,
image_modal,
pipeline_version,
pipeline_name,
):
raise request.param

Expand Down Expand Up @@ -206,6 +212,8 @@ async def _mock_get(
min_num_phenotypic_sessions,
assessment,
image_modal,
pipeline_version,
pipeline_name,
):
return request.param

Expand All @@ -226,6 +234,8 @@ async def _mock_successful_get(
min_num_phenotypic_sessions,
assessment,
image_modal,
pipeline_version,
pipeline_name,
):
return test_data

Expand Down
84 changes: 84 additions & 0 deletions tests/test_query.py
rmanaem marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,90 @@ def test_get_undefined_prefix_image_modal(
assert response.status_code == 500


@pytest.mark.parametrize("valid_pipeline_version", ["7.3.2", "23.1.3"])
rmanaem marked this conversation as resolved.
Show resolved Hide resolved
def test_get_valid_pipeline_version(
test_app,
mock_successful_get,
monkeypatch,
mock_auth_header,
set_mock_verify_token,
valid_pipeline_version,
):
"""Given a valid pipeline version, returns a 200 status code and a non-empty list of results."""

monkeypatch.setattr(crud, "get", mock_successful_get)
response = test_app.get(
f"{ROUTE}?pipeline_version={valid_pipeline_version}",
headers=mock_auth_header,
)
assert response.status_code == 200
assert response.json() != []


@pytest.mark.parametrize("mock_get", [None], indirect=True)
@pytest.mark.parametrize("invalid_pipeline_version", ["latest", "7.2", "23"])
def test_get_invalid_pipeline_version(
test_app,
mock_get,
monkeypatch,
mock_auth_header,
set_mock_verify_token,
invalid_pipeline_version,
):
"""Given an invalid pipeline version, returns a 422 status code."""

monkeypatch.setattr(crud, "get", mock_get)
response = test_app.get(
f"{ROUTE}?pipeline_version={invalid_pipeline_version}",
headers=mock_auth_header,
)
assert response.status_code == 422


@pytest.mark.parametrize(
"valid_pipeline_name", ["np:fmriprep", "np:freesurfer"]
)
def test_get_valid_pipeline_name(
test_app,
mock_successful_get,
monkeypatch,
mock_auth_header,
set_mock_verify_token,
valid_pipeline_name,
):
"""Given a valid pipeline name, returns a 200 status code and a non-empty list of results."""

monkeypatch.setattr(crud, "get", mock_successful_get)
response = test_app.get(
f"{ROUTE}?pipeline_name={valid_pipeline_name}",
headers=mock_auth_header,
)
assert response.status_code == 200
assert response.json() != []


@pytest.mark.parametrize("mock_get", [None], indirect=True)
@pytest.mark.parametrize(
"invalid_pipeline_name", ["n2p:coolpipeline", "apple", "some_thing:cool"]
)
def test_get_invalid_pipeline_name(
test_app,
mock_get,
monkeypatch,
mock_auth_header,
set_mock_verify_token,
invalid_pipeline_name,
):
"""Given an invalid pipeline name, returns a 422 status code."""

monkeypatch.setattr(crud, "get", mock_get)
response = test_app.get(
f"{ROUTE}?pipeline_name={invalid_pipeline_name}",
headers=mock_auth_header,
)
assert response.status_code == 422


def test_aggregate_query_response_structure(
test_app,
set_test_credentials,
Expand Down