Example #1
0
def tcia_tests():
    # The TCIA interface is very slow, so uncomment to skip this one
    #raise SkipTest

    logger = logging.getLogger(tcia_tests.__name__)

    # Test TCIA Instantiate
    from tithonus import read_yaml
    repos = read_yaml('repos.yaml')

    source = Interface.factory('tcia', repos)

    # source = TCIAInterface(address='https://services.cancerimagingarchive.net/services/v3/TCIA',
    #                        api_key=os.environ['TCIA_API_KEY'])

    # Apparently API key doesn't work for NLST b/c it is a private collection
    # series = source.get_series_from_id('1.2.840.113654.2.55.4303894980888172655039251025765147023')
    # source.download_archive(series, 'nlst_tmp_archive')

    # Test TCIA Download
    series = source.get_series_from_id('1.3.6.1.4.1.9328.50.4.15567')
    source.download_archive(series, 'tcia_tmp_archive')
    assert os.path.getsize('tcia_tmp_archive.zip') == 266582
    os.remove('tcia_tmp_archive.zip')

    # Test TCIA Copy
    source.copy(series, source, 'tcia_tmp_archive')
    assert os.path.getsize('tcia_tmp_archive.zip') == 266582
    os.remove('tcia_tmp_archive.zip')
Example #2
0
def xnat_bulk_edit():
    # Example of bulk editing from spreadsheet

    logger = logging.getLogger(xnat_bulk_edit.__name__)

    from tithonus import read_yaml
    repos = read_yaml('repos.yaml')

    source = Interface.factory('xnat-dev', repos)

    import csv
    with open('/Users/derek/Desktop/protect3d.variables.csv', 'rbU') as csvfile:
        propreader = csv.reader(csvfile)
        propreader.next()
        for row in propreader:
            logger.info(', '.join(row))
            subject_id = row[0]
            new_subject_id = '{num:04d}'.format(num=int(subject_id))
            logger.info(new_subject_id)

            params = {'gender': row[1],
                      #'age': row[2],
                      'src': row[3],
                      'label': new_subject_id}
            source.do_put('data/archive/projects/protect3d/subjects', subject_id, params=params)
Example #3
0
def tcia_tests():
    # The TCIA interface is very slow, so uncomment to skip this one
    #raise SkipTest

    logger = logging.getLogger(tcia_tests.__name__)

    # Test TCIA Instantiate
    from tithonus import read_yaml
    repos = read_yaml('repos.yaml')

    source = Interface.factory('tcia', repos)

    # source = TCIAInterface(address='https://services.cancerimagingarchive.net/services/v3/TCIA',
    #                        api_key=os.environ['TCIA_API_KEY'])

    # Apparently API key doesn't work for NLST b/c it is a private collection
    # series = source.get_series_from_id('1.2.840.113654.2.55.4303894980888172655039251025765147023')
    # source.download_archive(series, 'nlst_tmp_archive')

    # Test TCIA Download
    series = source.get_series_from_id('1.3.6.1.4.1.9328.50.4.15567')
    source.download_archive(series, 'tcia_tmp_archive')
    assert os.path.getsize('tcia_tmp_archive.zip') == 266582
    os.remove('tcia_tmp_archive.zip')

    # Test TCIA Copy
    source.copy(series, source, 'tcia_tmp_archive')
    assert os.path.getsize('tcia_tmp_archive.zip') == 266582
    os.remove('tcia_tmp_archive.zip')
Example #4
0
def xnat_bulk_edit():
    # Example of bulk editing from spreadsheet

    logger = logging.getLogger(xnat_bulk_edit.__name__)

    from tithonus import read_yaml
    repos = read_yaml('repos.yaml')

    source = Interface.factory('xnat-dev', repos)

    import csv
    with open('/Users/derek/Desktop/protect3d.variables.csv',
              'rbU') as csvfile:
        propreader = csv.reader(csvfile)
        propreader.next()
        for row in propreader:
            logger.info(', '.join(row))
            subject_id = row[0]
            new_subject_id = '{num:04d}'.format(num=int(subject_id))
            logger.info(new_subject_id)

            params = {
                'gender': row[1],
                #'age': row[2],
                'src': row[3],
                'label': new_subject_id
            }
            source.do_put('data/archive/projects/protect3d/subjects',
                          subject_id,
                          params=params)
Example #5
0
def test_montage_adv():
    logger = logging.getLogger(test_montage_adv.__name__)
    from tithonus import read_yaml
    repos = read_yaml('repos.yaml')
    source = Interface.factory('montage', repos)

    mrn = '000019477504'
    q = '16397558'

    r = source.do_get('api/v1/index/rad/search/rad',
                      params={
                          'q': '',
                          'patient_mrn': mrn
                      })
    logger.debug(pprint(r))

    exit()

    r = source.find('study', q, 'rad')
    #    logger.debug(pprint(r))

    qq = r.get('objects')[0].get('resource_uri')
    logger.debug(qq)

    r = source.do_get(qq)
    logger.debug(pprint(r))
Example #6
0
def dicom_tests():

    logger = logging.getLogger(dicom_tests.__name__)

    # Test DICOM Instantiate
    from OrthancInterface import OrthancInterface
    proxy = OrthancInterface(address='http://localhost:8043', name='3dlab-dev1')
    source = DICOMInterface(proxy=proxy, name='3dlab-dev0+dcm')

    # Test DICOM Subject Query
    r = source.find('subject', {'PatientName': 'ZNE*'})[0]
    assert r.subject_id == 'ZA4VSDAUSJQA6'

    # Test DICOM Q/R/DL
    from tithonus import read_yaml
    repos = read_yaml('repos.yaml')

    source = Interface.factory('3dlab-dev0+dcm', repos)
    target = Interface.factory('3dlab-dev1', repos)

    w = source.find('series', {'SeriesInstanceUID': '1.2.840.113654.2.55.4303894980888172655039251025765147023'})[0]
    u = target.retrieve(w, source)[0]
    target.copy(u, target, 'nlst_tmp_archive')
    assert os.path.getsize('nlst_tmp_archive.zip') == 176070
    os.remove('nlst_tmp_archive.zip')
Example #7
0
def dicom_tests():

    logger = logging.getLogger(dicom_tests.__name__)

    # Test DICOM Instantiate
    from OrthancInterface import OrthancInterface
    proxy = OrthancInterface(address='http://localhost:8043',
                             name='3dlab-dev1')
    source = DICOMInterface(proxy=proxy, name='3dlab-dev0+dcm')

    # Test DICOM Subject Query
    r = source.find('subject', {'PatientName': 'ZNE*'})[0]
    assert r.subject_id == 'ZA4VSDAUSJQA6'

    # Test DICOM Q/R/DL
    from tithonus import read_yaml
    repos = read_yaml('repos.yaml')

    source = Interface.factory('3dlab-dev0+dcm', repos)
    target = Interface.factory('3dlab-dev1', repos)

    w = source.find(
        'series', {
            'SeriesInstanceUID':
            '1.2.840.113654.2.55.4303894980888172655039251025765147023'
        })[0]
    u = target.retrieve(w, source)[0]
    target.copy(u, target, 'nlst_tmp_archive')
    assert os.path.getsize('nlst_tmp_archive.zip') == 176070
    os.remove('nlst_tmp_archive.zip')
Example #8
0
def test_orthanc_juniper():

    logger = logging.getLogger(orthanc_tests2.__name__)
    from tithonus import read_yaml

    repos = read_yaml('repos.yaml')
    source = Interface.factory('deathstar+lsmaster', repos)

    source.all_studies()
Example #9
0
def test_orthanc_juniper():

    logger = logging.getLogger(orthanc_tests2.__name__)
    from tithonus import read_yaml

    repos = read_yaml('repos.yaml')
    source = Interface.factory('deathstar+lsmaster', repos)

    source.all_studies()
Example #10
0
def bulk_age_deduction():

    logger = logging.getLogger(test_xnat.__name__)

    from tithonus import read_yaml
    repos = read_yaml('repos.yaml')

    source = Interface.factory('xnat-dev', repos)

    s = source.subject_from_id('UOCA0008A', project_id='ava')
    r = source.do_get('data/services/dicomdump?src=/archive/projects', s.project_id, 'subjects', s.subject_id, 'experiments', 'UOCA0008A&field=PatientAge', params={'field1': 'PatientAge', 'field': 'StudyDate'})
Example #11
0
def bulk_age_deduction():

    logger = logging.getLogger(test_xnat.__name__)

    from tithonus import read_yaml
    repos = read_yaml('repos.yaml')

    source = Interface.factory('xnat-dev', repos)

    s = source.subject_from_id('UOCA0008A', project_id='ava')
    r = source.do_get('data/services/dicomdump?src=/archive/projects', s.project_id, 'subjects', s.subject_id, 'experiments', 'UOCA0008A&field=PatientAge', params={'field1': 'PatientAge', 'field': 'StudyDate'})
Example #12
0
def bulk_upload():

    logger = logging.getLogger(test_xnat.__name__)

    from tithonus import read_yaml
    repos = read_yaml('repos.yaml')

    source = Interface.factory('xnat-dev', repos)

    from glob import glob
    worklist = glob('/Users/derek/Data/hydrocephalus_dicom/*.zip')

    for fn in worklist[1:]:
        source.upload_archive(None, fn, project_id='ava')
Example #13
0
def bulk_upload():

    logger = logging.getLogger(test_xnat.__name__)

    from tithonus import read_yaml
    repos = read_yaml('repos.yaml')

    source = Interface.factory('xnat-dev', repos)

    from glob import glob
    worklist = glob('/Users/derek/Data/hydrocephalus_dicom/*.zip')

    for fn in worklist[1:]:
        source.upload_archive(None, fn, project_id='ava')
Example #14
0
def tcia_mirror():

    logger = logging.getLogger(tcia_tests.__name__)

    # Test TCIA Instantiate
    from tithonus import read_yaml
    repos = read_yaml('repos.yaml')

    source = Interface.factory('tcia', repos)
    target = Interface.factory('xnat-dev', repos)

    fn = '/Users/derek/Desktop/tcia-all-seriesInstanceUids1438444078500.csv'

    import csv
    with open(fn, 'rbU') as csvfile:
        seriesreader = csv.reader(csvfile)
        seriesreader.next()

        i = 0
        for row in seriesreader:
            i = i + 1

            if i <= 0: continue
            if i > 5: break

            logger.info(', '.join(row))
            group = row[0]
            # subject_id = row[1]
            series_id = row[9]

            item = source.series_from_id(series_id)
            #source.download_data(item)

            # Change vars for XNAT
            item['collection'] = group
            item.subject.project_id = 'tcia'
            #target.upload_data(item)

            target.do_put('data/archive/projects',
                          item.subject.project_id,
                          'subjects',
                          item.subject.subject_id.replace('.', '_'),
                          params={
                              'group': item['collection'],
                              'yob': item.subject['yob'],
                              'gender': item.subject['gender']
                          })

    pass
Example #15
0
def test_xnat():

    logger = logging.getLogger(bulk_folder_upload.__name__)

    from tithonus import read_yaml
    repos = read_yaml('repos.yaml')

    source = Interface.factory('xnat-dev', repos)

    count = source.all_studies()

    slack_addr = repos['rih3dlab']['address']

    payload = {"text": "<http://xnat-dev.cloudapp.net:8080/xnat|xnat> currently contains {0} studies.".format(count)}
    requests.post(slack_addr, json=payload)
Example #16
0
def test_montage():

    logger = logging.getLogger(test_montage.__name__)

    # Test DICOM Q/R/DL
    from tithonus import read_yaml
    repos = read_yaml('repos.yaml')

    source = Interface.factory('montage', repos)

    # Look in collection "rad" for query string "fracture"
    r = source.find('study', 'fracture', 'rad')
    assert(r['meta']['total_count'] > 1400000)

    # Test shared juniper session cookies
    source2 = Interface.factory('montage', repos)

    # Look in collection "rad" for query string "fracture"
    r = source2.find('study', 'fracture', 'rad')
    assert(r['meta']['total_count'] > 1400000)
Example #17
0
def test_montage():

    logger = logging.getLogger(test_montage.__name__)

    # Test DICOM Q/R/DL
    from tithonus import read_yaml
    repos = read_yaml('repos.yaml')

    source = Interface.factory('montage', repos)

    # Look in collection "rad" for query string "fracture"
    r = source.find('study', 'fracture', 'rad')
    assert (r['meta']['total_count'] > 1400000)

    # Test shared juniper session cookies
    source2 = Interface.factory('montage', repos)

    # Look in collection "rad" for query string "fracture"
    r = source2.find('study', 'fracture', 'rad')
    assert (r['meta']['total_count'] > 1400000)
Example #18
0
def bulk_folder_upload():

    logger = logging.getLogger(bulk_folder_upload.__name__)

    from tithonus import read_yaml
    repos = read_yaml('repos.yaml')

    source = Interface.factory('xnat-dev', repos)

    source.all_studies()

    base_dir = '/Users/derek/Data/ATACH_I'
    subject_worklist = glob('%s/*' % base_dir)
    logger.info(subject_worklist)

    i = 0

    for subject_dir in subject_worklist:
        subsubject_worklist = glob('%s/*' % subject_dir)
        for subsubject_dir in subsubject_worklist:
            study_worklist = glob('%s/*' % subsubject_dir)
            #logger.info(study_worklist)
            for study_dir in study_worklist:

                i = i+1

                #logger.info(study_dir)
                if 'scene' in study_dir.lower(): continue

                # get a dcm file to figure out the subject name, study id, and session type...
                session_worklist = glob('%s/*' % study_dir)
                session_dir = session_worklist[0]
                instance_worklist = glob('%s/*' % session_dir)
                instance_file = instance_worklist[0]

                ds = dicom.read_file(instance_file)

                #logger.info(ds)

                orig_subject_id = ds.PatientID
                num = int(re.search(r'\d+', orig_subject_id).group())
                suffix_res = re.search(r'[A-Za-z]+', orig_subject_id)

                if suffix_res:
                    suffix = suffix_res.group().upper()
                    if suffix == 'TIER':
                        # Special case for silly ATACH mis-labelling
                        if orig_subject_id.endswith('1'):
                            suffix = 'T1'
                        elif orig_subject_id.endswith('2'):
                            num = 172
                            suffix = 'T2'
                else:
                    suffix = ''

                subject_id = '{num:04d}{suffix}'.format(num=int(num), suffix=str(suffix))

                study_id = ds.AccessionNumber

                study_date_str = ds.StudyDate
                age_str = ds.get('PatientsAge')

                if study_date_str and age_str:
                    yob = deduce_yob(study_date_str, age_str)
                else:
                    yob = None

                logger.info('%s : %s : %s' % (subject_id, study_id, yob))

                if i<=1:
                    continue

                # source.upload_archive(None, study_dir, project_id='atach3d', study_id=study_id, subject_id=subject_id)
                source.upload_archive(None, 'tmp.zip', project_id='atach3d', study_id=study_id, subject_id=subject_id)
                if yob:
                    source.do_put('data/archive/projects/atach3d/subjects', subject_id, params={'yob': yob})


    # TODO: Need to consider 'asserts' for xnat testing

    s = source.subject_from_id('my_patient2', 'mtp01')
    source.do_get('data/archive/projects/mtp01/subjects/my_patient2', params={'format': 'json', 'yob': '1971'})

    t = DicomStudy(subject_id='my_patient3',
                   project_id='mtp01')
    source.upload_archive(t, 'tcia_tmp_archive1.zip')

    logger.info(s)

    # Need to test:
    # 1. xnat-dev is up
    # 2. mtp01 is empty
    # 3. can upload a study to mtp01 w correct subject name, study name, etc.
    # 4. can download the study from mtp01
    pass
Example #19
0
def bulk_folder_upload():

    logger = logging.getLogger(bulk_folder_upload.__name__)

    from tithonus import read_yaml
    repos = read_yaml('repos.yaml')

    source = Interface.factory('xnat-dev', repos)

    source.all_studies()

    base_dir = '/Users/derek/Data/ATACH_I'
    subject_worklist = glob('%s/*' % base_dir)
    logger.info(subject_worklist)

    i = 0

    for subject_dir in subject_worklist:
        subsubject_worklist = glob('%s/*' % subject_dir)
        for subsubject_dir in subsubject_worklist:
            study_worklist = glob('%s/*' % subsubject_dir)
            #logger.info(study_worklist)
            for study_dir in study_worklist:

                i = i + 1

                #logger.info(study_dir)
                if 'scene' in study_dir.lower(): continue

                # get a dcm file to figure out the subject name, study id, and session type...
                session_worklist = glob('%s/*' % study_dir)
                session_dir = session_worklist[0]
                instance_worklist = glob('%s/*' % session_dir)
                instance_file = instance_worklist[0]

                ds = pydicom.read_file(instance_file)

                #logger.info(ds)

                orig_subject_id = ds.PatientID
                num = int(re.search(r'\d+', orig_subject_id).group())
                suffix_res = re.search(r'[A-Za-z]+', orig_subject_id)

                if suffix_res:
                    suffix = suffix_res.group().upper()
                    if suffix == 'TIER':
                        # Special case for silly ATACH mis-labelling
                        if orig_subject_id.endswith('1'):
                            suffix = 'T1'
                        elif orig_subject_id.endswith('2'):
                            num = 172
                            suffix = 'T2'
                else:
                    suffix = ''

                subject_id = '{num:04d}{suffix}'.format(num=int(num),
                                                        suffix=str(suffix))

                study_id = ds.AccessionNumber

                study_date_str = ds.StudyDate
                age_str = ds.get('PatientsAge')

                if study_date_str and age_str:
                    yob = deduce_yob(study_date_str, age_str)
                else:
                    yob = None

                logger.info('%s : %s : %s' % (subject_id, study_id, yob))

                if i <= 1:
                    continue

                # source.upload_archive(None, study_dir, project_id='atach3d', study_id=study_id, subject_id=subject_id)
                source.upload_archive(None,
                                      'tmp.zip',
                                      project_id='atach3d',
                                      study_id=study_id,
                                      subject_id=subject_id)
                if yob:
                    source.do_put('data/archive/projects/atach3d/subjects',
                                  subject_id,
                                  params={'yob': yob})

    # TODO: Need to consider 'asserts' for xnat testing

    s = source.subject_from_id('my_patient2', 'mtp01')
    source.do_get('data/archive/projects/mtp01/subjects/my_patient2',
                  params={
                      'format': 'json',
                      'yob': '1971'
                  })

    t = DicomStudy(subject_id='my_patient3', project_id='mtp01')
    source.upload_archive(t, 'tcia_tmp_archive1.zip')

    logger.info(s)

    # Need to test:
    # 1. xnat-dev is up
    # 2. mtp01 is empty
    # 3. can upload a study to mtp01 w correct subject name, study name, etc.
    # 4. can download the study from mtp01
    pass