Developing Python Rest API for downloading and uploading the Data into XNAT

254 views Asked by At

I am developing a python Rest API to XNAT for transferring the data locally or to server. I am trying to download all experiments(data format eg:CT,MR,ECOG,MEEG) those are associated with one subject of Project from XNAT to my local file system. But I am able to download only the experiment with data format of MR &CT sessions(Dicom files) of that subject. Below you can see my python code and please let me know for the changes I have to make.

import xnat
import os
#creating a session
session = xnat.connect('https://xnat.prj.ae.mpg.de', user='xxxx', password='xxxx')
test_project = session.projects["Test"] #loading a project Test
test_subject = test_project.subjects #loading subjects of project
download_folder = os.path.expanduser(r"C:\Users\rkls\Documents\Python-Restapi") # download path
print('Your data is downladed in this directory {} '.format(download_folder))
if not os.path.exists(download_folder):
    os.makedirs(download_folder)
test_project.subjects['S2_RS'].download_dir(download_folder) # using download_dir() to download subject S2_RS
session.disconnect()

Now the above code gives me only experiment with CT, MR session data (DICOM files) but not ECOG and MEEG session data(EDF & FIF files). How do I download all the experiments associated within single subject of a project?

1

There are 1 answers

0
Spanky Quigman On

What kind of experiments are the ECOG and MEEG session data contained in, i.e. what is the data type? Or are they just resources associated with the subject?

I created a subject with an associated MR session as well as some resources (e.g. open Manage Files on the subject page, create a resource folder, and upload a couple of PDFs to the new resource folder). The download_dir() function on the SubjectData class only downloads the experiments and not the resources associated with the subject. For that you need to iterate the resources directly:

# Set subject to variable for convenience
s2_rs = test_project.subjects['S2_RS']
s2_rs.download_dir(download_folder) # Download experiment data
for resource_id in s2_rs.resources: # Iterate subject resources
    # Download resources for 
    s2_rs.resources.data[resource_id].download_dir(download_folder)

I created a resource folder named FOO on my subject then uploaded a couple of PDFs there. In my download folder, I got the following:

S2_RS/resources/FOO/files/foo-1.pdf
S2_RS/resources/FOO/files/foo-2.pdf
S2_RS/S2_RS_MR01/...

S2_RS_MR01 is the MR session I uploaded so there are a bunch of files under that folder.

It would be nice if the download_dir() function included the resources as well but for now this should work around that issue for you.