def test_query_dates(): """Date parameters should be passed in proper format""" query = MintQuery( minStudyDate=datetime(year=2019, month=1, day=2), maxStudyDate=datetime(year=2020, month=3, day=1), patientBirthDate=date(year=1983, month=8, day=11), ) assert query.as_parameters()["minStudyDate"] == "20190102" assert query.as_parameters()["maxStudyDate"] == "20200301" assert query.as_parameters()["patientBirthDate"] == "19830811"
def test_search_error_500(a_mint, requests_mock): """Internal errors in the mint server might be communicated by plain html instead mint format. This should be handled """ set_mock_response(requests_mock, MINT_SEARCH_STUDY_LEVEL_ERROR_500) with pytest.raises(DICOMTrolleyError) as e: a_mint.find_studies(query=MintQuery(patientName="B*")) assert "Could not parse" in str(e)
from datetime import datetime from os import environ from dicomtrolley.mint import Mint, MintQuery, QueryLevels from dicomtrolley.servers import VitreaConnection # Log in to get an authenticated session session = VitreaConnection(environ["LOGIN_URL"]).log_in( environ["USER"], environ["PASSWORD"], environ["REALM"]) # Using mint for search mint = Mint(session, environ["MINT_URL"]) print("Quick search for some studies") studies = mint.find_studies( MintQuery(patientName="B*", includeFields=["PatientBirthDate"])) print(f"Found {len(studies)} studies") print("More extensive search") studies = mint.find_studies( MintQuery( patientName="B*", # wildcards can be used modalitiesInStudy="CT*", # more parameters can be found patientSex="M", # in dicomtrolley.mint.MintQuery minStudyDate=datetime(year=2015, month=3, day=1), maxStudyDate=datetime(year=2020, month=3, day=1), includeFields=[ "PatientBirthDate", # which fields to get back. "SOPClassesInStudy", ], # see dicomtrolley.fields for options queryLevel=QueryLevels.
from dicomtrolley.trolley import Trolley from dicomtrolley.wado import Wado print("logging in") session = VitreaConnection(environ["LOGIN_URL"]).log_in( environ["USER"], environ["PASSWORD"], environ["REALM"]) trolley = Trolley( searcher=Mint(session, environ["MINT_URL"]), wado=Wado(session, environ["WADO_URL"]), ) print("Quick search for studies") studies = trolley.find_studies( MintQuery(patientName="B*", includeFields=["NumberOfStudyRelatedInstances"])) print(f"Found {len(studies)} studies. Taking one with least instances") studies.sort(key=lambda x: int(x.data.NumberOfStudyRelatedInstances)) study = studies[1] print(f"Getting slice info for {study}") studies_full = trolley.find_studies( MintQuery(studyInstanceUID=study.uid, queryLevel=QueryLevels.INSTANCE) ) # queryLevel=INSTANCE will return all instances inside each study print(f"Saving datasets to {environ['DOWNLOAD_PATH']}") trolley.download(studies_full, environ["DOWNLOAD_PATH"], use_async=False) print("Done")
def test_trolley_find(a_trolley, some_mint_studies): a_trolley.searcher.find_studies = Mock(return_value=some_mint_studies) assert a_trolley.find_studies(query=MintQuery()) == some_mint_studies
def test_query_validation_error(query_params): """These queries should fail validation""" with pytest.raises(ValueError): MintQuery(**query_params)
def test_find_study_exception(a_mint, some_mint_studies): """Using a find_study query that returns multiple studies is not allowed""" a_mint.find_studies = Mock(return_value=some_mint_studies) with pytest.raises(DICOMTrolleyError): a_mint.find_study(MintQuery())
def test_search_instance_level(a_mint, mock_mint_responses): """Instance query level returns series per study and also instances per study""" response = a_mint.find_studies( query=MintQuery(patientName="B*", queryLevel=QueryLevels.INSTANCE)) assert len(response[0].series[1].instances) == 13
def test_search_series_level(a_mint, mock_mint_responses): """Series query level will populate studies with series""" response = a_mint.find_studies( query=MintQuery(patientName="B*", queryLevel=QueryLevels.SERIES)) assert len(response[1].series) == 2
def test_search_study_level(a_mint, mock_mint_responses): """Default query level receives info in study only""" response = a_mint.find_studies(query=MintQuery(patientName="B*")) assert response[2].series == []
def test_query_should_pass(query_params): """These queries should pass validation""" MintQuery(**query_params)