def put_dicoms(self, project, session, experiment, filename, retries=3): """Upload an archive of dicoms to XNAT filename: archive to upload""" headers = {'Content-Type': 'application/zip'} upload_url = "{server}/data/services/import?project={project}" \ "&subject={subject}&session={session}&overwrite=delete" \ "&prearchive=false&inbody=true" upload_url = upload_url.format(server=self.server, project=project, subject=session, session=experiment) try: with open(filename) as data: self._make_xnat_post(upload_url, data, retries, headers) except XnatException as e: e.study = project e.session = session raise e except IOError as e: logger.error('Failed to open file:{} with excuse:' .format(filename, e.strerror)) err = XnatException("Error in file:{}". format(filename)) err.study = project err.session = session raise err except requests.exceptions.RequestException as e: err = XnatException("Error uploading data with url:{}" .format(upload_url)) err.study = project err.session = session raise err
def get_dicom(self, project, session, experiment, scan, filename=None, retries=3): """Downloads a dicom file from xnat to filename If filename is not specified creates a temporary file and returns the path to that, user needs to be responsible for cleaning up any created tempfiles""" url = '{}/data/archive/projects/{}/' \ 'subjects/{}/experiments/{}/' \ 'scans/{}/resources/DICOM/files?format=zip' \ .format(self.server, project, session, experiment, scan) if not filename: filename = tempfile.mkstemp(prefix="dm2_xnat_extract_") # mkstemp returns a filename and a file object # dealing with the filename in future so close the file object os.close(filename[0]) try: self._get_xnat_stream(url, filename, retries) return(filename) except: try: os.remove(filename[1]) except OSError as e: logger.warning('Failed to delete tempfile:{} with excuse:{}' .format(filename, str(e))) err = XnatException("Failed getting dicom with url:{}".format(url)) err.study = project err.session = session raise err
def get_scan_list(self, study, session, experiment): """The list of dicom scans in an experiment""" url = '{}/data/archive/projects/{}' \ '/subjects/{}/experiments/{}' \ '/scans/?format=json'.format(self.server, study, session, experiment) try: result = self._make_xnat_query(url) except: return XnatException('Failed getting scans with url:{}' .format(url)) if result is None: e = XnatException('Scan not found for experiment:{}' .format(experiment)) e.study = study e.session = session raise e return(result['ResultSet']['Result'])
def put_resource(self, project, session, experiment, filename, data, folder, retries=3): """POST a resource file to the xnat server filename: string to store filename as data: string containing data (such as produced by zipfile.ZipFile.read())""" resource_id = self.get_resource_ids(project, session, experiment, folderName=folder) attach_url = "{server}/data/archive/projects/{project}/" \ "subjects/{subject}/experiments/{experiment}/" \ "resources/{resource_id}/" \ "files/{filename}?inbody=true" uploadname = urllib.quote(filename) url = attach_url.format(server=self.server, project=project, subject=session, experiment=experiment, resource_id=resource_id, filename=uploadname) try: self._make_xnat_post(url, data) except XnatException as err: err.study = project err.session = session raise err except: logger.warning("Failed adding resource to xnat with url:{}" .format(url)) err = XnatException("Failed adding resource to xnat") err.study = project err.session = session
def get_scan_info(self, study, session, experiment, scanid): """Returns info about an xnat scan""" url = '{}/data/archive/projects/{}' \ '/subjects/{}/experiments/{}' \ '/scans/{}/?format=json'.format(self.server, study, session, experiment, scanid) try: result = self._make_xnat_query(url) except: return XnatException('Failed getting scan with url:{}' .format(url)) if result is None: e = XnatException('Scan:{} not found for experiment:{}' .format(scanid, experiment)) e.study = study e.session = session raise e return(result['items'][0])