sovabids.parsers#

Module with parser utilities.

Module Contents#

Functions#

placeholder_to_regex(placeholder[, encloser, matcher])

Translate a placeholder pattern to a regex pattern.

parse_from_placeholder(string, pattern[, encloser, ...])

Parse string from a placeholder pattern.

parse_from_regex(string, pattern, fields)

Parse string from regex pattern.

parse_entity_from_bidspath(path, entity[, mode])

Get the value of a bids-entity from a path.

_modify_entities_of_placeholder_pattern(pattern[, mode])

Convert between sovabids entities pattern notation and the shorter notation.

parse_entities_from_bidspath(targetpath[, entities, mode])

Get the bids entities from a bidspath.

parse_path_pattern_from_entities(sourcepath, bids_entities)

Get the path pattern from a path and a dictionary of bids entities and their values.

find_bidsroot(path)

Get the bidsroot from an absolute path describing a bids file inside a subject subfolder.

sovabids.parsers.placeholder_to_regex(placeholder, encloser='%', matcher='(.+)')[source]#

Translate a placeholder pattern to a regex pattern.

Parameters
  • placeholder (str) – The placeholder pattern to translate.

  • matcher (str, optional) – The regex pattern to use for the placeholder, ie : (.*?),(.*),(.+).

  • encloser (str, optional) – The symbol which encloses the fields of the placeholder pattern.

Returns

  • pattern (str) – The regex pattern.

  • fields (list of str) – The fields as they appear in the regex pattern.

sovabids.parsers.parse_from_placeholder(string, pattern, encloser='%', matcher='(.+)')[source]#

Parse string from a placeholder pattern.

Danger: It will replace underscores and hyphens with an empty character in all fields except for the ignore field. This to accomodate to the bids standard restrictions automatically.

Parameters
  • string (str) – The string to parse.

  • pattern (str) – The placeholder pattern to use for parsing.

  • matcher (str, optional) – The regex pattern to use for the placeholder, ie : (.*?),(.*),(.+).

  • encloser (str, optional) – The symbol which encloses the fields of the placeholder pattern.

Returns

The dictionary with the fields and values requested.

Return type

dict

sovabids.parsers.parse_from_regex(string, pattern, fields)[source]#

Parse string from regex pattern.

Danger: It will replace underscores and hyphens with an empty character in all fields except for the ignore field. This to accomodate to the bids standard restrictions automatically.

Parameters
  • string (str) – The string to parse.

  • pattern (str) – The regex pattern to use for parsing.

  • fields (list of str) – List of fields in the same order as they appear in the regex pattern.

Returns

The dictionary with the fields and values requested.

Return type

dict

sovabids.parsers.parse_entity_from_bidspath(path, entity, mode='r2l')[source]#

Get the value of a bids-entity from a path.

Parameters
  • path (str) – The bidspath we are going to derive the information on. Should be the complete path of file of a modality (ie an _eeg file).

  • entity (str) – The entity we are going to extract. SHOULD be one of sub|ses|task|acq|run

  • mode (str) – Direction of lookup. One of r2l|l2r . r2l (right to left) l2r (left to right)

Returns

value – The extracted value of the entity as a string. If None, it means the entity was not found on the string.

Return type

str

sovabids.parsers._modify_entities_of_placeholder_pattern(pattern, mode='append')[source]#

Convert between sovabids entities pattern notation and the shorter notation.

The shorter notation is: %dataset%, %task%, %session%, %subject%, %run%, %acquisition%

Parameters
  • string (str) – The pattern string to convert.

  • mode (str) – Whether to append ‘entities’ or cut it. One of {‘append’,’cut’}

Returns

The converted pattern string.

Return type

str

sovabids.parsers.parse_entities_from_bidspath(targetpath, entities=['sub', 'ses', 'task', 'acq', 'run'], mode='r2l')[source]#

Get the bids entities from a bidspath.

Parameters
  • targetpath (str) – The bidspath we are going to derive the information on.

  • entities (list of str) – The entities we are going to extract. Defaults to sub,ses,task,acq,run

  • mode (str) – Direction of lookup. One of r2l|l2r . r2l (right to left) l2r (left to right)

Returns

A dictionary with the extracted entities. {‘sub’:’11’,’task’:’resting’,’ses’:’V1’,’acq’:’A’,’run’:1}

Return type

dict

sovabids.parsers.parse_path_pattern_from_entities(sourcepath, bids_entities)[source]#

Get the path pattern from a path and a dictionary of bids entities and their values.

Parameters
  • sourcepath (str) – The sourcepath that will be modified to get the path pattern

  • bids_entities (dict) – Dictionary with the entities and their values on the path. Ie {‘sub’:’11’,’task’:’resting’,’ses’:’V1’,’acq’:’A’,’run’:1} There should be no ambiguity between the sourcepath and each of the values. Otherwise an error will be raised.

Returns

The path pattern in placeholder format

Return type

str

sovabids.parsers.find_bidsroot(path)[source]#

Get the bidsroot from an absolute path describing a bids file inside a subject subfolder.

Parameters

path (str) – The absolute path to any bids file inside a sub- folder.

Returns

The bidsroot absolute path.

Return type

str