This repository has been archived by the owner on Jun 18, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
mirror_demo.py
109 lines (84 loc) · 3.36 KB
/
mirror_demo.py
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
from pyxnat import Interface
# Change this for different projects
P = '2096'
# Passing no arguments, this will ask for values at the commandline
xnat = Interface()
# Or, you can pass installation URL, username, password
# xnat = Interface('http://masi.vuse.vanderbilt.edu', 'sburns', MYPASSWORD)
"""
xnat.select is the most useful function
You can select by full paths:
project = xnat.select('/project/MYPROJ')
subject = xnat.select('/project/MYPROJ/subject/MYSUBJ')
all_subjects = xnat.select('/project/MYPROJ/subject/*')
"""
project = xnat.select.project(P)
all_subjects = xnat.select('/project/%s/subjects/*' % P)
"""
Subject Attributes
sub.id() -----> XNAT's internal name
sub.label() -----> Exposed name that can be changed
"""
for subject in all_subjects:
print subject.id(), subject.label()
"""
Drilling down the Heirarchy
project/
MYPROJ
subject/
MYSUBJ
experiment/
MYEXP
scan/
MYSCAN
resource/
MYRES
file
MYFILE
All objects have a .get()/.fetchone(),.fetchall()/.first()
Any of these accessor functions can take 'label', 'id', 'obj' or any combination:
- 'label' is the user defined name for object
- 'id' is XNAT's name for the object
- 'obj' is a python object
You could also specify more than one, e.g.:
stuff = experiment.scans().fetchall('id', 'obj'))
for scan_id, scan_obj in stuff:
<do stuff with each scan>
"""
# A real basic tree traversal
subject = all_subjects.fetchone()
for experiment in subject.experiments().fetchall('obj'):
for scan in experiment.scans().fetchall('obj'):
for resource in scan.resources().fetchall('obj'):
for file in resource.files().fetchall('obj'):
print subject.label(), experiment.label(), scan.label(), resource.label(), file.label()
"""
We can do better and weed out stuff we don't want...
Couple bits of information...
- scan.attrs.get('type') is the protocol name
- We're really only interested in the 'DICOM' resources (these are the actual images from the scanner)
- secondary resources are generated by the scanner, but not really useful
- SNAPSHOTS are created by XNAT, they're gifs, not very useful (but nice to look at through browser)
Maybe we could (re) construct DICOM names like we get from getstudy...
"""
pi_lname = project.attrs.get('pi_lastname')
experiment = subject.experiments().fetchone()
label = experiment.label()
for scan in experiment.scans().fetchall('obj'):
protocol = scan.attrs.get('type').replace(' ', '_')
scan_num = '%02d' % (int(scan.label()) / 100)
subscan_num = '%02d' % (int(scan.label()) % 100)
good_fname = '_'.join([pi_lname, label, scan_num, subscan_num, protocol]) + '.DCM'
print good_fname
try:
good_resource = [res for res in scan.resources().fetchall('obj') if res.label() == 'DICOM'][0]
files = good_resource.files().get()
good_file = good_resource.file(files[0])
# Download the file under TOP_DIR
local_fname = os.path.join('TOP_DIR', good_fname)
# good_file.get(local_fname)
except IndexError:
print("No DICOM resources for scan %s :(" % scan.label())