-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathread_xnat_files.py
More file actions
52 lines (39 loc) · 2.07 KB
/
read_xnat_files.py
File metadata and controls
52 lines (39 loc) · 2.07 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
#!/usr/bin/env python
# Script written by Lorena Escudero (Department of Radiology, University of Cambridge)
# to read DICOM files directly from XNAT - April 2020
#
# Usage: edit xnathost/project/user and just run normally: python3 read_xnat_files.py
# Note: This reads 'all DICOM files' in a project, for only a set of them, create a list or filter and modify this script
# As an example, it prints to screen the values of the slice thickness of each file
#
# Based on examples in xnatpy/examples
import xnat
import os
import getpass
import pydicom as pydcm
# ---------------------------------------------------------------------------------------------------------------------
def read_dicom_files(connection, project_id):
project = connection.projects[project_id]
subjects = project.subjects
for subject in subjects:
experiments = project.subjects[subject].experiments
for experiment in experiments:
scans = project.subjects[subject].experiments[experiment].scans
for scan in scans:
n_files = len(project.subjects[subject].experiments[experiment].scans[scan].resources['DICOM'].files)
for i in range(n_files):
file = project.subjects[subject].experiments[experiment].scans[scan].resources['DICOM'].files[i]
with file.open() as dicomfile:
process_dicom(dicomfile)
# ---------------------------------------------------------------------------------------------------------------------
def process_dicom(input_dicom):
data = pydcm.dcmread(input_dicom)
print (data.SliceThickness)
# ---------------------------------------------------------------------------------------------------------------------
if __name__ == '__main__':
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 connection:
read_dicom_files(connection, project_id)