python.md 2.87 KB
Newer Older
Cassandra Gould van Praag's avatar
Cassandra Gould van Praag committed
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
---
title: Python libraries
parent: Open Data
grand_parent: Open WIN Tools
has_children: false
nav_order: 4
---


# Python libraries
{: .fs-9 }

Python libraries for working with XNAT
{: .fs-6 .fw-300 }

---


There are several python libraries that can be used to interrogate the XNAT API.  This page has notes on the libraries that have been used in WIN.

## PyXNAT

https://pyxnat.github.io/pyxnat/ is the original python library.  It's API is a little long winded but does allow you to create objects in XNAT.  

TODO: examples, gotchyas, version woes

## XNATPy

https://xnat.readthedocs.io/en/latest/static/tutorial.html is a newer python library.  It's api is more concise and easier to use than pyXNAT by is readonly.

## DAX

https://dax.readthedocs.io/en/latest/index.html is a wrapper for pyxnat that knows about clusers, pipelines and BIDS.

---

# Examples

## Example PyXNAT usage:

For an example use case see the following page (under the "Using Python" section): https://info.dpuk.org/2018/01/10/uploading-niftis/

## Example XNATPy usage:

```
# Connecting to an XNAT instance
>>> import xnat
>>> session = xnat.connect('xnat_website_address', user='xnat_username')
# Creating a subject (the project will need to be created via the XNAT UI if it doesn't already exist)
>>> project = session.projects['projectID']
>>> subject = session.classes.SubjectData(parent=project, label='new_subject_label')
# Creating an MR experiment
>>> experiment = session.classes.MrSessionData(parent=subject, label='new_scan_label')
 # Creating an MR scan
>>> scan = session.classes.MrScanData(parent=experiment, id='unique_scan_ID', type='new_scan_type')
 # Creating a resource folder
>>> resource = session.classes.ResourceCatalog(parent=scan, label='NIFTI')
# Uploading a NIFTI file
>>> resource.upload('file_path', 'uploaded_filename')
```

Where you see a label or type input, this can be changed to something more appropriate to your data (except the NIFTI label for the resource folder). The scan id will need to be unique within the experiment, XNAT by default will count from 1 for this but it shouldn't be required so long as it is unique. The parent attribute requires a particular XNAT type depending on what is being created, but it follows the XNAT hierarchy (project > subject > experiment > scan > resource).

## PyXNAT vs XNATPy subject creation:

### PyXNAT:

```python
from pyxnat import Interface

url = ""
user = ""
project_id = ""

# establish pyxnat session and connect to project
project = Interface(server=url,user=user).select.project(project_id)

# Create a new subject
subject = project.subject('TestSubject')
subject.create()
```

### XNATPy:

```python
import xnat

url = ""
user = ""
project_id = ""

# Connect to an XNAT instance
session = xnat.connect(url, user=user)

# Create a subject
project = session.projects[project_id]
subject = session.classes.SubjectData(parent=project, label='TestSubject')
```