Skip to content

Instantly share code, notes, and snippets.

@mattsouth
Created August 11, 2017 08:48
Show Gist options
  • Save mattsouth/0bd96a6154eb6d3061100c87af3879f3 to your computer and use it in GitHub Desktop.
Save mattsouth/0bd96a6154eb6d3061100c87af3879f3 to your computer and use it in GitHub Desktop.
A python script that re-runs an updated anonymisation script on all MR sessions in an XNAT project
# Renames every MR experiment, thus kicking off the anonymisation script.
# see https://groups.google.com/d/msg/xnat_discussion/vvLyLvyo2uQ/yMPHgkrOEwAJ
# used in XNAT 1.6.5
# also see https://groups.google.com/d/msg/xnat_discussion/fBI33j36D3Y/VEwh-HWez70J for discussion about changing experiment label
import xnat # see https://xnat.readthedocs.io/en/latest/
url = raw_input("Enter XNAT url: ")
user = raw_input("Enter username: ")
with xnat.connect(url, user=user) as session:
name = raw_input("Enter project: ")
project = session.projects[name]
print 'subject, session'
for subject_id in project.subjects.keys():
for experiment_id in project.subjects[subject_id].experiments.keys():
experiment = project.subjects[subject_id].experiments[experiment_id]
# dir(experiment) = ['SECONDARY_LOOKUP_FIELD', '_CONTAINED_IN', '_HAS_FIELDS', '_XSI_TYPE', '__abstractmethods__', '__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__unicode__', '__weakref__', '_abc_cache', '_abc_negative_cache', '_abc_negative_cache_version', '_abc_registry', '_cache', '_caching', '_fieldname', '_fields', '_parent', '_uri', '_xnat_session', 'acquisition_site', 'age', 'assessors', 'caching', 'clearcache', 'coil', 'create_assessor', 'data', 'date', 'dcmaccessionnumber', 'dcmpatientbirthdate', 'dcmpatientid', 'dcmpatientname', 'delay', 'delete', 'download', 'download_dir', 'duration', 'fieldname', 'fields', 'fieldstrength', 'files', 'fulldata', 'fulluri', 'get', 'get_object', 'id', 'investigator', 'label', 'logger', 'marker', 'modality', 'note', 'operator', 'original', 'parent', 'prearchivepath', 'project', 'protocol', 'query', 'reconstructions', 'regions', 'resources', 'scanner', 'scans', 'session_type', 'set', 'sharing', 'stabilization', 'subject', 'subject_id', 'time', 'uid', 'uri', 'validation', 'version', 'visit', 'visit_id', 'xnat_session', 'xsi_type']
if experiment.xsi_type == 'xnat:mrSessionData' and experiment.label[-3:]!='_up' :
uri = url + '/data/archive/experiments/'+experiment.id+'?label='+experiment.label+'_up'
session.interface.put(uri)
print 'updated: ', experiment.label
else:
print 'ignored: ', experiment.label
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment