Created
July 13, 2017 12:19
-
-
Save mattsouth/bd6f7f9a7840dfe2c9745c2245556d6e to your computer and use it in GitHub Desktop.
Output a summary of an xnat project
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
# outputs a summary of the data in an xnat project, e.g.: | |
# 55 subjects, 65 scans, 400 sequences (SNAPSHOTS: 400, DICOM: 400, NIFTI: 200) | |
# Note that in our lab we talk about "scans" and "sequences" which correspond | |
# to xnat's "MR Sessions" and "Scans" respectively. | |
# Parameters: | |
# 1. url - xnat url | |
# 2. username - xnat username | |
# 3. password - xnat password | |
# 4. project - xnat project | |
import sys | |
import xnat # see https://xnat.readthedocs.io/en/latest/ | |
from requests.exceptions import ConnectionError | |
if len(sys.argv) != 5: | |
print "Incorrect number of arguments - expected 4" | |
print "Usage: python summary.py xnat_url xnat_username xnat_password project_id" | |
else: | |
host = sys.argv[1] | |
user = sys.argv[2] | |
password = sys.argv[3] | |
project = sys.argv[4] | |
try: | |
session = xnat.connect(host, user=user, password=password) | |
xnatproject = session.projects[project] | |
if xnatproject: | |
summ = "{} subjects, {} scans, ".format(len(xnatproject.subjects.keys()),len(xnatproject.experiments.keys())) | |
tot = 0 | |
cnt = {} | |
for experiment_id in xnatproject.experiments.keys(): | |
scans = xnatproject.experiments[experiment_id].scans.keys() | |
tot = tot + len(scans) | |
for scan_id in scans: | |
scan = xnatproject.experiments[experiment_id].scans[scan_id] | |
for resource_id in scan.resources.keys(): | |
resource = scan.resources[resource_id] | |
if resource.label in cnt.keys(): | |
cnt[resource.label]=cnt[resource.label]+1 | |
else: | |
cnt[resource.label]=1 | |
summ = summ + "{} sequences ".format(tot) | |
if tot>0: | |
summ = summ + "(" + ', '.join('{}: {}'.format(scantype, cnt[scantype]) for scantype in cnt) + ")" | |
print summ | |
else: | |
print 'Unable to find {} project. Does the user you supplied have collaborator access?'.format(project) | |
except ConnectionError: | |
print "Unable to connect to specified xnat instance, {}".format(host) | |
print " - is the url valid?" | |
except: | |
print "Unable to authenticate with specified credientials" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment