pylib.xnat package

Module contents

exception pylib.xnat.AccessionError

Bases: exceptions.Exception

exception pylib.xnat.AuthError

Bases: exceptions.Exception

exception pylib.xnat.AutoboxError

Bases: exceptions.Exception

exception pylib.xnat.DownloadError

Bases: exceptions.Exception

class pylib.xnat.Experiment(uri, label, id, project, subject_id, subject_label)

Bases: tuple

Container to hold XNAT Experiment information. Fields include the URI (uri), Accession ID (id), Project (project), and Label (label).

id

Alias for field number 2

label

Alias for field number 1

project

Alias for field number 3

subject_id

Alias for field number 4

subject_label

Alias for field number 5

uri

Alias for field number 0

class pylib.xnat.Format

Bases: object

CSV = 'csv'
JSON = 'json'
SQLDB = 'sqldb'
XML = 'xml'
exception pylib.xnat.MultipleAccessionError

Bases: exceptions.Exception

exception pylib.xnat.NullAccessionError

Bases: exceptions.Exception

exception pylib.xnat.RestApiError

Bases: exceptions.Exception

exception pylib.xnat.ResultSetError

Bases: exceptions.Exception

exception pylib.xnat.SavedSearchError

Bases: exceptions.Exception

exception pylib.xnat.SavedSearchIDError

Bases: exceptions.Exception

exception pylib.xnat.ScanSearchError

Bases: exceptions.Exception

exception pylib.xnat.StoreXARError

Bases: exceptions.Exception

class pylib.xnat.Subject(uri, label, id, project, experiments)

Bases: tuple

experiments

Alias for field number 4

id

Alias for field number 2

label

Alias for field number 1

project

Alias for field number 3

uri

Alias for field number 0

class pylib.xnat.XnatAuth(url, username, password)

Bases: tuple

Container to hold XNAT authentication information. Fields include the url, username, and password.

password

Alias for field number 2

url

Alias for field number 0

username

Alias for field number 1

pylib.xnat.accession(auth, label, project=None)

Get the Accession ID for any experiment label. If label is tied to multiple Accession IDs, a Project must be supplied.

Example usage::
>>> from pylib.xnat import auth, accession
>>> info = auth("gspcentral")
>>> accession(info, "AB1234C")
'GSPCentral_E00001_MR_1'
Parameters:
  • auth (pylib.xnat.XnatAuth) – XNAT authentication
  • label (str) – XNAT Experiment label
  • project (str) – XNAT Experiment Project
Returns:

Accession ID

Return type:

str

pylib.xnat.diffqc(auth, fmt, autobox=True, accession=None, project=None)

Get DiffQC information

Example usage::
>>> from pylib.xnat import diffqc
>>> diffqc(auth, Format.CSV)
Parameters:
  • auth (pylib.xnat.XnatAuth) – XNAT authentication
  • accession (str) – XNAT Accession ID
  • fmt (pylib.xnat.Format) – Saved search format
  • autobox (bool) – Autobox response data into a reader or data structure
  • accession – XNAT Accession ID
  • project (str) – XNAT Project ID
Returns:

Requested DiffQC data

Return type:

dict | ElementTree | csv.reader | sqlite3.Connection

pylib.xnat.download(auth, accession, ids, out_dir='.', in_mem=True, progress=False, attempts=1)

Download scan data from XNAT.

Example usage::
>>> from pylib.xnat import auth, accession, download
>>> info = auth("gspcentral")
>>> aid = accession(info, "AB1234C")
>>> download(info, aid, [1, 2], out_dir="./data"})
Parameters:
  • auth (pylib.xnat.XnatAuth) – XNAT authentication
  • accession (str) – XNAT Accession ID
  • ids (list) – Scan IDs (i.e., scan numbers)
  • out_dir (str) – Output directory
  • in_mem (bool) – Keep downloaded file content in memory
  • progress (int) – Show download progress every N bytes
  • attempts – Number of download attempts
pylib.xnat.experiment(auth, label, project=None)

Get the URI, Accession ID, Subject ID, and Project for any experiment label. If label is tied to multiple Accession IDs, a Project must be specified.

Example usage::
>>> from pylib.xnat import auth, experiment
>>> info = auth("gspcentral")
>>> experiment(info, "AB1234C")
Experiment(uri=u'/data/experiments/XNAT_E0001', label=u'AB1234C', id=u'XNAT_E0001', 
    project=u'MyProject', subject_id=u'XNAT_S0001', subject_label='ABC')
Parameters:
  • auth (pylib.xnat.XnatAuth) – XNAT authentication
  • label (str) – XNAT Experiment label
  • project (str) – XNAT Experiment Project
Returns:

Experiment named tuple

Return type:

Experiment

pylib.xnat.extendedboldqc(auth, fmt, autobox=True, accession=None, project=None)

Get ExtendedBOLDQC information

Example usage::
>>> from pylib.xnat import extendedboldqc
>>> extendedboldqc(auth, Format.CSV)
Parameters:
  • auth (pylib.xnat.XnatAuth) – XNAT authentication
  • accession (str) – XNAT Accession ID
  • fmt (pylib.xnat.Format) – Saved search format
  • autobox (bool) – Autobox response data into a reader or data structure
  • accession – XNAT Accession ID
  • project (str) – XNAT Project ID
Returns:

Requested ExtendedBOLDQC data

Return type:

dict | ElementTree | csv.reader | sqlite3.Connection

pylib.xnat.extqc(auth, fmt, autobox=True, accession=None, project=None)

Get ExtendedBOLDQC information

Example usage::
>>> from pylib.xnat import extendedboldqc
>>> extendedboldqc(auth, Format.CSV)
Parameters:
  • auth (pylib.xnat.XnatAuth) – XNAT authentication
  • accession (str) – XNAT Accession ID
  • fmt (pylib.xnat.Format) – Saved search format
  • autobox (bool) – Autobox response data into a reader or data structure
  • accession – XNAT Accession ID
  • project (str) – XNAT Project ID
Returns:

Requested ExtendedBOLDQC data

Return type:

dict | ElementTree | csv.reader | sqlite3.Connection

pylib.xnat.extract(zf, content, out_dir='.')

Extract a Java 1.6/XNAT ZIP archive

Parameters:
  • zf (zipfile.ZipFile) – ZipFile object
  • out_dir (str) – Output directory
pylib.xnat.get(auth, path, fmt, autobox=True, params=None)

Issue a GET request to the XNAT REST API and autobox the response content.

Example usage::
>>> from pylib.xnat import auth, get, Format
>>> info = auth("gspcentral")
>>> get(info, "/data/experiments", Format.JSON)
Parameters:
  • auth (pylib.xnat.XnatAuth) – XNAT authentication
  • path (str) – API URL path
  • fmt (pylib.xnat.Format) – API result format
  • params (dict) – Additional query parameters
  • autobox (bool) – Autobox response content into an appropriate reader or other data structure
Returns:

Tuple of (URL, dict | xml.etree.ElementTree | csv.reader | str)

Return type:

tuple

pylib.xnat.has(auth, xsitype, project=None)

Test if a Project contains any items of a particular xsi:type

Parameters:
  • auth (pylib.xnat.XnatAuth) – XNAT authentication
  • xsitype – XNAT xsi:type
  • xsitype – str
  • project (str) – XNAT Project
Returns:

True or False

Return type:

bool

pylib.xnat.mrsession(auth, fmt, autobox=True, accession=None, project=None)

Get MR Session information

Example usage::
>>> from pylib.xnat import mrsession
>>> mrsession(auth, Format.CSV)
Parameters:
  • auth (pylib.xnat.XnatAuth) – XNAT authentication
  • accession (str) – XNAT Accession ID
  • fmt (pylib.xnat.Format) – Saved search format
  • autobox (bool) – Autobox response data into a reader or data structure
  • accession – XNAT Accession ID
  • project (str) – XNAT Project ID
Returns:

Requested MR Session data

Return type:

dict | ElementTree | csv.reader | sqlite3.Connection

pylib.xnat.patch1(f, auth, project, fmt, xsitype)

XNAT REST API patch #1

pylib.xnat.permissions(auth, project)

Get users assigned to a project

Parameters:project (str) – XNAT Project
Returns:Users assigned to the XNAT Project
Return type:dict
pylib.xnat.projects(auth)

Get all Projects in XNAT

Returns:Dictionary of Projects
Return type:dict
pylib.xnat.savedsearch(auth, name, fmt, autobox=True)

Download a saved search from XNAT.

Example usage::
>>> from pylib.xnat import auth, savedsearch, Format
>>> info = auth("gspcentral")
>>> c = savedsearch(info, "nrg_overview", Format.SQLDB).cursor()
>>> for row in c.execute("SELECT Label FROM nrg_overview"):
...     print(row)
...
(u'010115_AB1234C',)
(u'010215_CD1234E',)
Parameters:
  • auth (pylib.xnat.XnatAuth) – XNAT authentication
  • name (str) – Saved search ID
  • fmt (pylib.xnat.Format) – Saved search format
  • autobox (bool) – Autobox response data into a reader or data structure
Returns:

Saved search

Return type:

dict | ElementTree | csv.reader | sqlite3.Connection

pylib.xnat.scans(auth, fmt, autobox=True, accession=None, project=None)

Get scan information.

Example usage::
>>> from pylib.xnat import scans
>>> scans(auth, Format.CSV)
Parameters:
  • auth (pylib.xnat.XnatAuth) – XNAT authentication
  • accession (str) – XNAT Accession ID
  • fmt (pylib.xnat.Format) – Saved search format
  • autobox (bool) – Autobox response data into a reader or data structure
  • accession – XNAT Accession ID
  • project (str) – XNAT Project ID
Returns:

Requested scan data

Return type:

dict | ElementTree | csv.reader | sqlite3.Connection

pylib.xnat.scansearch(auth, accession, filt)

Search for scans by supplying a set of SQL-based conditionals.

Example usage::
>>> from pylib.xnat import scansearch
>>> query = {"eor1": "note LIKE %EOR1%",
...          "eor2": "note LIKE %EOR2%",
...          "mpr":  "series_description='T1_MEMPRAGE RMS' OR note LIKE %ANAT%"}
>>> scansearch(auth, accession, query)
{"mpr": [4], "eor1": [13], "eor2": [14]}
Parameters:
  • auth (pylib.xnat.XnatAuth) – XNAT authentication
  • accession (str) – XNAT Accession ID
  • filt (dict) – Scan filter
Returns:

Dictionary of tokens and matching scan ids

Return type:

dict

pylib.xnat.storexar(auth, archive, verify=True)

StoreXAR

Parameters:
pylib.xnat.subject(auth, label, project=None)

Get the URI, Accession ID, Label, Project, and all Experiment IDs for any Subject label. If the Subject label is tied to mulitple Accession IDs, a Project argument must be specified.

Example usage::
>>> from pylib.xnat import auth, subject
>>> info = auth("gspcentral")
>>> subject(info, "AB1234C")
Subject(uri=u'/data/experiments/XNAT_S0001', label=u'AB1234C', id=u'XNAT_S0001', 
    project=u'MyProject', experiments=[u'XNAT_E0001', u'XNAT_E0002'])
Parameters:
  • auth (pylib.xnat.XnatAuth) – XNAT authentication
  • label (str) – XNAT Subject label
  • project (str) – XNAT Subject Project
Returns:

Accession ID

Return type:

str