Starters Tutorial

This is a short tutorial going through the main features of this API. Depending on the policy of the XNAT server you are using, and your user level, you can have read and write access to a specific set of resources. During this tutorial, we will use a fake standard user account on the XNAT central repository, where you will have limited access to a number of projects and full access to the projects you own.


XNAT Central is a public XNAT repository managed by the XNAT team and updated regularly with the latest improvements of the development branch.

Getting started

Connecting to an XNAT server requires valid credentials so you might want to start by requesting those on the web interface of your server.

>>> from pyxnat import Interface
>>> central = Interface(

It is also possible to define an Interface object without specifying all the connection settings. In that case pyxnat switches to interactive mode and prompts the user for the missing information.

>>> central = Interface(server='')
>>> User:my_login
>>> Password:

You can also use a configuration file. The best way to create the file is to use the save_config() method on an existing interface.

>>> central.save_config('central.cfg')
>>> central2 = Interface(config='central.cfg')


Depending on the server configuration, you may have to include the port in the server URL, as well as the name of the XNAT tomcat application. You might end up with something like: http://server_ip:port/xnat

The main interface class is now divided into logical subinterfaces:
  • data selection

  • general management

  • cache management

  • server instrospection

Data selection

Now that we have an Interface object, we can start browsing the server with the select subinterface which can be used, either with explicit Python objects and methods, or through a path describing the data.

Simple requests:


Nested requests:

['IMAGEN_000000001274', 'IMAGEN_000000075717', ...,'IMAGEN_000099954902']

Filtered requests:


['IMAGEN_000055203542', 'IMAGEN_000055982442', 'IMAGEN_000097555742']

Resource paths

The resource paths that can be passed as an argument to select is a powerful tool but can easily generate thousands of queries so one has to be careful when using it.

Absolute paths

A full path to a resource is a sequence of resource level and resource_id pairs:


A full path to a resource listing is a sequence of resource level and resource_id pairs finishing by a plural resource level (i.e. with an ‘s’):


The first nice thing here is that you actually don’t have to worry about resource level to be plural or singular within the path:



Relative paths and shortcuts

When browsing resources, some levels are often left without any IDs and filled with * filters instead, which leads to paths like:


That can instead be written:

/projects/subjects/experiments OR //experiments

To have all the experiments from a specific project:


The double slash syntax can be used anywhere in the path and any number of time:


Sometimes, a path will generate more than one path because it can be interpreted in different way:





If you try //files, it will generate all the possible descendant paths:


If the server has decent amount a data it will take ages to go through all the resources.

Resources operations

Several operations are accessible for every resource level. The most important are for creating new resources, deleting existing ones and testing whether a given resource exists or not:

>>> my_project ='my_project')
>>> my_project.exists()
>>> my_project.create()
>>> my_project.exists()
>>> subject = my_project.subject('first_subject')
>>> subject.create()
>>> subject.delete()
>>> subject.exists()

An optional keyword argument is available to specify the datatype from the XNAT schema. The keyword must match the name of the REST level.

>>> subject.create()
>>> subject.experiment('pet_session'

It is also possible to create resources without having to create the parent resources first. For example:


Specifiy the datatype on multiple levels:

                  ).create(experiments='xnat:mrSessionData', scans='xnat:mrScanData')

Use default datatypes:


Additional fields can be configured at the resource creation. It can be especially useful for datatypes that have some mandatory fields, and thus would not be created if not specified (this is not a best practice for XML Schema writers though). It also enables users to set the resource ID through the REST API instead of just the label (the ID in this case is generated automatically).

Custom ID example:

>>> experiment.create(experiments='xnat:mrSessionData',

With additional fields:

>>> experiment.create(**{'experiments':'xnat:mrSessionData',


When using xpath syntax to declare fields, it is mandatory to pass the arguments using a dictionnary because of the / and : characters. And do not forget to expand the dict with the **.

Since you can create different resource levels in a single create call in pyxnat, it is also possible to configure those levels in a single call. For example if the subject for that experiment was not created, you could have specified:

>>> experiment.create(

File support

It is possible to upload and then download files at every REST resource level:

>>> my_project.files()
>>> my_project.file('image.nii').put('/tmp/image.nii')
>>> # you can add any of the following arguments to give additional
>>> # information on the file you are uploading
>>> my_project.file('image.nii').put( '/tmp/image.nii',
                                      tags='image test'
>>> my_project.resource('NIFTI').file('image.nii').size()
>>> my_project.resource('NIFTI').file('image.nii').content()
>>> my_project.resource('NIFTI').file('image.nii').format()
>>> my_project.resource('NIFTI').file('image.nii').tags()
'image test'
>>> my_project.resource('NIFTI').file('image.nii').get()
>>> my_project.file('image.nii').get_copy()
>>> my_project.file('image.nii').get_copy('/tmp/test.nii')
>>> # the resource level can be used to group files
>>> my_project.resource('ANALYZE').file('image.hdr').put('/tmp/image.hdr')
>>> my_project.resource('ANALYZE').file('image.img').put('/tmp/image.img')
>>> my_project.resources()
>>> my_project.resource('ANALYZE').files()
['image.hdr', 'image.img']


New since 0.7, the default get() method on a file can be given a custom path. It will still be handled and tracked by the cache in the same way as other files.

Attributes support

Each resource level also has a set of metadata fields that can be informed. This set of fields depends on the resource level and on its type in the XNAT schema.

>>> # use hard-coded shortcuts from the REST API
>>> my_project.attrs.set('secondary_ID', 'myproject')
>>> my_project.attrs.get('secondary_ID')
>>> # use XPATH from standard or custom XNAT Schema
>>> my_project.attrs.set('xnat:projectData/keywords', 'test project')
>>> my_project.attrs.get('xnat:projectData/keywords')
'test project'
>>> # get or set multiple attributes in a single request to improve performance
>>> my_project.attrs.mset({'xnat:projectData/keywords':'test project', 'secondary_ID':'myproject'})
>>> my_project.attrs.mget(['xnat:projectData/keywords', 'secondary_ID'])
['test porject', 'myproject']

The search engine

The XNAT search engine can be queried via the REST model. It can be used to retrieve a specific subset of REST resources or a table containing the relevant values. The following queries find all the subjects that are within my_project older than 14:

>>> constraints = [('xnat:subjectData/SUBJECT_ID','LIKE','%'),
                  ('xnat:subjectData/PROJECT', '=', 'my_project'),
>>> # retrieve experiments
>>> # retrieve table with one subject per row and the columns SUBJECT_ID and AGE
>>>'xnat:subjectData', ['xnat:subjectData/SUBJECT_ID', 'xnat:subjectData/AGE']).where(constraints)

See the Search, SeachManager and CObject classes reference documentation for further details.

To get the searchable types and fields to put in the constraints, rows and columns parameters, use the Interface.inspect.datatypes method:

>>> central.inspect.datatypes(optional_filter)
[..., 'xnat:subjectData', 'xnat:projectData', 'xnat:mrSessionData',  ...]
>>> central.inspect.datatypes('xnat:subjectData', optional_filter)


How to get all the results in a query?


How to get all the columns from a datatype?
>>> table ='xnat:subjectData').where(...)


Then to get everything:
>>> table ='xnat:subjectData').all()