Created
May 21, 2018 19:57
-
-
Save manthey/14446f97b88b00d3a9e347f0254685ac to your computer and use it in GitHub Desktop.
Identify and preprocess dicom items in Girder
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import argparse | |
import girder_client | |
import girder_client.cli | |
import os | |
import re | |
def get_girder_client(opts): | |
""" | |
Log in to Girder and return a reference to the client. | |
:param opts: options that include the username, password, and girder api | |
url. | |
:returns: the girder client. | |
""" | |
gcopts = {k: v for k, v in opts.iteritems() if k in { | |
'username', 'password', 'host', 'port', 'apiRoot', 'scheme', 'apiUrl', | |
'apiKey', 'sslVerify'}} | |
gcopts['username'] = gcopts.get('username') or None | |
gcopts['password'] = gcopts.get('password') or None | |
return girder_client.cli.GirderCli(**gcopts) | |
def check_for_dicom(client, regex='.*', simulate=False, **kwargs): | |
""" | |
Enumerate all items in girder. For any that match a specified regex, check | |
if they have been checked if they are DICOM images. If not, do so. | |
""" | |
pattern = re.compile(regex, re.IGNORECASE | re.UNICODE) | |
limit = 100 | |
offset = 0 | |
while True: | |
items = client.get('resource/search', parameters={ | |
'q': '', 'mode': 'prefix', 'types': '["item"]', 'level': 0, | |
'limit': limit, 'offset': offset}) | |
if not len(items) or not len(items.get('item', [])): | |
break | |
offset += limit | |
for item in items['item']: | |
if not pattern.search(item['name']): | |
continue | |
if 'dicom' in item: | |
continue | |
print('Processing %s' % item['name']) | |
if simulate: | |
result = 'Simulated -- did nothing' | |
else: | |
try: | |
client.post('item/%s/parseDicom' % item['_id']) | |
item = client.getItem(item['_id']) | |
result = 'Set to DICOM' if 'dicom' in item else 'Not a DICOM' | |
except Exception: | |
result = 'Failed to process' | |
print(' %s' % result) | |
if __name__ == '__main__': | |
parser = argparse.ArgumentParser( | |
description='Check and mark DICOM files.') | |
# Standard girder_client CLI options | |
parser.add_argument( | |
'--apiurl', '--api-url', '--api', '--url', '-a', dest='apiUrl', | |
help='The Girder api url (e.g., http://127.0.0.1:8080/api/v1).') | |
parser.add_argument( | |
'--apikey', '--api-key', '--key', dest='apiKey', | |
default=os.environ.get('GIRDER_API_KEY', None), | |
help='An API key, defaults to GIRDER_API_KEY environment variable.') | |
parser.add_argument( | |
'--username', '--user', | |
help='The Girder admin username. If not specified, a prompt is given.') | |
parser.add_argument( | |
'--password', '--pass', '--passwd', '--pw', | |
help='The Girder admin password. If not specified, a prompt is given.') | |
parser.add_argument('--host', help='The Girder API host.') | |
parser.add_argument('--scheme', help='The Girder API scheme.') | |
parser.add_argument('--port', type=int, help='The Girder API port.') | |
parser.add_argument( | |
'--apiroot', '--api-root', '--root', dest='apiRoot', | |
help='The Girder API root.') | |
parser.add_argument( | |
'--no-ssl-verify', action='store_false', dest='sslVerify', | |
help='Disable SSL verification.') | |
parser.add_argument( | |
'--certificate', dest='sslVerify', help='A path to SSL certificate') | |
# Generic verbose option | |
parser.add_argument('--verbose', '-v', action='count', default=0) | |
# This program's options | |
parser.add_argument( | |
'regex', help='A regex used to check items. Only items whose names ' | |
'match will be processed.') | |
parser.add_argument( | |
'--rename', action='store_true', | |
help='Rename DICOM items based on their series number and description') | |
parser.add_argument( | |
'--simulate', '--dry-run', '-n', action='store_true', dest='simulate', | |
help='Report what would be done, but don\'t actually make any changes.') | |
args = parser.parse_args() | |
if args.verbose >= 2: | |
print('Parsed arguments: %r' % args) | |
client = get_girder_client(vars(args)) | |
check_for_dicom(client, **vars(args)) | |
# if args.rename: | |
# rename_dicom_items(client, vars(args)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment