-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathupload_resources_files.py
More file actions
executable file
·70 lines (52 loc) · 3.12 KB
/
upload_resources_files.py
File metadata and controls
executable file
·70 lines (52 loc) · 3.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/env python
# Script written by Lorena Escudero (Department of Radiology, University of Cambridge)
# to upload in batch files as resources as well as scans - March 2020
#
# Usage: edit xnathost/project/user and give as input a .txt file containing a line per patient+session
# in the form 'subject_id:session_id:/path/to/scans.zip:/path/to/resources_file1:/path/to/resources_file2..'
# Note: Writen for CT session and to store in Resources/RAW
#
# Based on xnatpy/examples/upload_data.py
import xnat
import os
import getpass
# ---------------------------------------------------------------------------------------------------------------------
def upload_scans(session, project_id, subject_id, experiment_id, scans_file):
xnat_project = session.projects[project_id]
# We create the subject with the following line:
xnat_subject = session.classes.SubjectData(parent=xnat_project, label=subject_id)
# And now we upload with the import_ method - the experiment will be created in this step
session.services.import_(scans_file,overwrite=None, quarantine=False, destination='/archive', trigger_pipelines=None,project=project_id, subject=subject_id, experiment=experiment_id, content_type=None)
# ---------------------------------------------------------------------------------------------------------------------
def upload_resources(session, project_id, subject_id, experiment_id, resources):
xnat_project = session.projects[project_id]
xnat_subject = session.classes.SubjectData(parent=xnat_project, label=subject_id)
xnat_experiment = session.classes.CtSessionData(parent=xnat_subject, label=experiment_id)
# Create the resources folder called 'RAW'
xnat_resources = session.classes.ResourceCatalog(parent=xnat_experiment, label='RAW')
# And upload the resource files there
for resource_file in resources:
resources_file_name = resource_file.split(os.sep)[-1]
print(resources_file_name)
xnat_resources.upload(resource_file,resources_file_name)
# ---------------------------------------------------------------------------------------------------------------------
if __name__ == '__main__':
input_list = 'test_file.txt'
xnathost = 'XNATURL'
project_id = 'PROJECT'
user_id = 'USER'
pwd = getpass.getpass("Password for user name : %s = " % user_id)
with xnat.connect(xnathost, user=user_id, password=pwd) as session:
with open(input_list, 'r') as fp:
line = fp.readline()
while line:
line = line.strip()
subject_id = line.split(':')[0]
experiment_id = line.split(':')[1]
scans_file = line.split(':')[2]
resources = []
for i in range(3,len(line.split(':'))):
resources.append(line.split(':')[i])
upload_scans(session, project_id, subject_id, experiment_id, scans_file)
upload_resources(session, project_id, subject_id, experiment_id, resources)
line = fp.readline()