Skip to content

Instantly share code, notes, and snippets.

@mattsouth
Created July 9, 2019 15:52
Show Gist options
  • Save mattsouth/12eaaab1aa6c3a6568b6d3905e98ec33 to your computer and use it in GitHub Desktop.
Save mattsouth/12eaaab1aa6c3a6568b6d3905e98ec33 to your computer and use it in GitHub Desktop.
A python 2.7 / pyxnat script for downloading the resources from an xnat project
import pyxnat
import os
import time
# A python 2.7 / pyxnat script for downloading the resources from an xnat project
# gather source xnat details
print '** DOWNLOAD XNAT PROJECT RESOURCES **'
xnat_url=raw_input("Enter the xnat url: ")
project_name=raw_input("Enter the xnat project id: ")
print 'Enter xnat credentials'
interface = pyxnat.Interface(xnat_url)
project = interface.select.project(project_name)
if project.exists():
filepath=raw_input("Project found. Where do you want the files?: ")
project_filepath = os.path.join(filepath, project_name)
if os.path.isdir(filepath):
time_start_script=time.time()
for subject_id in interface.select.project(project_name).subjects().get():
subject = project.subject(subject_id)
subject_filepath = os.path.join(project_filepath, subject.label())
print "{}".format(subject.label())
time_start_subject=time.time()
for experiment_id in subject.experiments().get():
experiment = subject.experiment(experiment_id)
experiment_filepath = os.path.join(subject_filepath, experiment.label())
if experiment.scans():
os.makedirs(experiment_filepath)
print "\t{}".format(experiment.label())
experiment.scans().download(experiment_filepath)
print '\nDownload completed in {} seconds'.format(time.time()-time_start_script)
else:
print 'cannot find dir: {}'.format(filepath)
else:
print 'cannot attach to project, {} on {}'.format(project_name, xnat_url)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment