fsl.utils.bids

This module provides a few functions for working with BIDS data sets.

BIDSFile

The BIDSFile class parses and stores the entities and suffix contained in a BIDS file.

isBIDSDir

Returns True if dirname is the root directory of a BIDS dataset.

inBIDSDir

Returns True if filename looks like it is within a BIDS dataset directory, False otherwise.

isBIDSFile

Returns True if filename looks like a BIDS image or JSON file.

loadMetadata

Load all of the metadata associated with filename.

All of the other functions in this module should not be considered part of the public API.

See https://bids-specification.readthedocs.io/en/stable/ for more information about BIDS.

Note

The pybids library is a more suitable choice if you are after a more robust and featured interface for working with BIDS datasets.

class fsl.utils.bids.BIDSFile(filename)[source]

Bases: object

The BIDSFile class parses and stores the entities and suffix contained in a BIDS file. See the parseFilename() function.

The match() method can be used to compare two BIDSFile instances.

The following attributes are available on a BIDSFile instance:

  • filename: Absolute path to the file

  • entities: Dict of key : value pairs, the entities that are present in the file name (e.g. {'sub' : '01})

  • suffix: File suffix (e.g. T1w, bold, etc.)

__init__(filename)[source]

Create a BIDSFile.

__str__()[source]

Return a strimg representation of this BIDSFile.

__repr__()[source]

Return a strimg representation of this BIDSFile.

match(other, suffix=True)[source]

Compare this BIDSFile to other.

Parameters:
  • otherBIDSFile to compare

  • suffix – Defaults to True. If False, the comparison is made solely on the entity values.

Returns:

True if self.suffix == other.suffix (unless suffix is False) and if all of the entities in other are present in self, False otherwise.

__dict__ = mappingproxy({'__module__': 'fsl.utils.bids', '__doc__': "The ``BIDSFile`` class parses and stores the entities and suffix\n    contained in a BIDS file. See the :func:`parseFilename` function.\n\n    The :meth:`match` method can be used to compare two ``BIDSFile`` instances.\n\n    The following attributes are available on a ``BIDSFile`` instance:\n\n     - ``filename``: Absolute path to the file\n     - ``entities``: Dict of ``key : value`` pairs, the entities that are\n       present in the file name (e.g. ``{'sub' : '01}``)\n     - ``suffix``: File suffix (e.g. ``T1w``, ``bold``, etc.)\n    ", '__init__': <function BIDSFile.__init__>, '__str__': <function BIDSFile.__str__>, '__repr__': <function BIDSFile.__repr__>, 'match': <function BIDSFile.match>, '__dict__': <attribute '__dict__' of 'BIDSFile' objects>, '__weakref__': <attribute '__weakref__' of 'BIDSFile' objects>, '__annotations__': {}})
__module__ = 'fsl.utils.bids'
__weakref__

list of weak references to the object (if defined)

fsl.utils.bids.parseFilename(filename)[source]

Parses a BIDS-like file name, returning the entities and suffix encoded in the name. See the isBIDSFile() function for an explanation of what is considered to be a valid BIDS file name.

Note

This function assumes that no period (.) characters occur in the body of a BIDS filename.

Returns:

A tuple containing: - A dict containing the entities - The suffix

fsl.utils.bids.isBIDSDir(dirname)[source]

Returns True if dirname is the root directory of a BIDS dataset.

fsl.utils.bids.inBIDSDir(filename)[source]

Returns True if filename looks like it is within a BIDS dataset directory, False otherwise.

fsl.utils.bids.isBIDSFile(filename, strict=True)[source]

Returns True if filename looks like a BIDS image or JSON file.

A BIDS file name must consist of zero or more “entities” (alpha-numeric name-value pairs), a “suffix”, all separated by underscores, and a regular file extension. For example, the following file:

sub-01_ses-01_task-stim_bold.nii.gz

has suffix bold, entities sub=01, ses=01 and task=stim, and extension .nii.gz.

Parameters:
  • filename – Name of file to check

  • strict – If True (the default), the file must be within a BIDS dataset directory, as defined by inBIDSDir().

fsl.utils.bids.loadMetadata(filename)[source]

Load all of the metadata associated with filename.

Parameters:

filename – Path to a data file in a BIDS dataset.

Returns:

A dict containing all of the metadata associated with filename