aslprep.niworkflows.interfaces.utils module

Utilities.

class AddTPMs(from_file=None, resource_monitor=None, **inputs)[source]

Bases: SimpleInterface

Calculate the union of several TPMs

Mandatory Inputs:

in_files (a list of items which are a pathlike object or string representing an existing file) – Input list of ROIs.

Optional Inputs:

indices (a list of items which are an integer) – Select specific maps.

Outputs:

out_file (a pathlike object or string representing an existing file) – Union of binarized input files.

class AddTSVHeader(from_file=None, resource_monitor=None, **inputs)[source]

Bases: SimpleInterface

Add a header row to a TSV file

>>> cwd = os.getcwd()
>>> os.chdir(tmpdir)

An example TSV:

>>> np.savetxt('data.tsv', np.arange(30).reshape((6, 5)), delimiter='\t')

Add headers:

>>> addheader = AddTSVHeader()
>>> addheader.inputs.in_file = 'data.tsv'
>>> addheader.inputs.columns = ['a', 'b', 'c', 'd', 'e']
>>> res = addheader.run()
>>> df = pd.read_csv(res.outputs.out_file, delim_whitespace=True,
...                  index_col=None)
>>> df.columns.ravel().tolist()
['a', 'b', 'c', 'd', 'e']
>>> np.all(df.values == np.arange(30).reshape((6, 5)))
True
>>> os.chdir(cwd)
Mandatory Inputs:
  • columns (a list of items which are a string) – Header for columns.

  • in_file (a pathlike object or string representing an existing file) – Input file.

Outputs:

out_file (a pathlike object or string representing an existing file) – Output average file.

class CopyHeader(from_file=None, resource_monitor=None, **inputs)[source]

Bases: SimpleInterface

Copy a header from the hdr_file to out_file with data drawn from in_file.

Mandatory Inputs:
  • hdr_file (a pathlike object or string representing an existing file) – The file we get the header from.

  • in_file (a pathlike object or string representing an existing file) – The file we get the data from.

Outputs:

out_file (a pathlike object or string representing an existing file) – Written file path.

class CopyXForm(fields=None, **inputs)[source]

Bases: SimpleInterface

Copy the x-form matrices from hdr_file to out_file.

Mandatory Inputs:

hdr_file (a pathlike object or string representing an existing file) – The file we get the header from.

output_spec[source]

alias of DynamicTraitedSpec

class DictMerge(from_file=None, resource_monitor=None, **inputs)[source]

Bases: SimpleInterface

Merge (ordered) dictionaries.

Optional Inputs:

in_dicts (a list of items which are a dictionary with keys which are any value and with values which are any value or an OrderedDict or None) – Dictionaries to be merged. In the event of a collision, values from dictionaries later in the list receive precedence.

Outputs:

out_dict (a dictionary with keys which are any value and with values which are any value) – Merged dictionary.

class GenerateSamplingReference(from_file=None, resource_monitor=None, **inputs)[source]

Bases: SimpleInterface

Generates a reference grid for resampling one image keeping original resolution, but moving data to a different space (e.g. MNI).

If the fov_mask optional input is provided, then the abbr:FoV (field-of-view) is cropped to a bounding box containing the brain mask plus an offest of two voxels along all dimensions. The fov_mask should be to the brain mask calculated from the T1w, and should not contain the brain stem. The mask is resampled into target space, and then the bounding box is calculated. Finally, the FoV is adjusted to that bounding box.

Mandatory Inputs:
  • fixed_image (a pathlike object or string representing an existing file) – The reference file, defines the FoV.

  • moving_image (a pathlike object or string representing an existing file) – The pixel size reference.

Optional Inputs:
  • fov_mask (a pathlike object or string representing an existing file or None) – Mask to clip field of view (in fixed_image space). (Nipype default value: None)

  • keep_native (a boolean) – Calculate a grid with native resolution covering the volume extent given by fixed_image, fast forward fixed_image otherwise. (Nipype default value: True)

  • xform_code (None or 2 or 4) – Force xform code. (Nipype default value: None)

Outputs:

out_file (a pathlike object or string representing an existing file) – One file with all inputs flattened.

class JoinTSVColumns(from_file=None, resource_monitor=None, **inputs)[source]

Bases: SimpleInterface

Add a header row to a TSV file

>>> cwd = os.getcwd()
>>> os.chdir(tmpdir)

An example TSV:

>>> data = np.arange(30).reshape((6, 5))
>>> np.savetxt('data.tsv', data[:, :3], delimiter='\t')
>>> np.savetxt('add.tsv', data[:, 3:], delimiter='\t')

Join without naming headers:

>>> join = JoinTSVColumns()
>>> join.inputs.in_file = 'data.tsv'
>>> join.inputs.join_file = 'add.tsv'
>>> res = join.run()
>>> df = pd.read_csv(res.outputs.out_file, delim_whitespace=True,
...                  index_col=None, dtype=float, header=None)
>>> df.columns.ravel().tolist() == list(range(5))
True
>>> np.all(df.values.astype(int) == data)
True

Adding column names:

>>> join = JoinTSVColumns()
>>> join.inputs.in_file = 'data.tsv'
>>> join.inputs.join_file = 'add.tsv'
>>> join.inputs.columns = ['a', 'b', 'c', 'd', 'e']
>>> res = join.run()
>>> res.outputs.out_file  
'...data_joined.tsv'
>>> df = pd.read_csv(res.outputs.out_file, delim_whitespace=True,
...                  index_col=None)
>>> df.columns.ravel().tolist()
['a', 'b', 'c', 'd', 'e']
>>> np.all(df.values == np.arange(30).reshape((6, 5)))
True
>>> join = JoinTSVColumns()
>>> join.inputs.in_file = 'data.tsv'
>>> join.inputs.join_file = 'add.tsv'
>>> join.inputs.side = 'left'
>>> join.inputs.columns = ['a', 'b', 'c', 'd', 'e']
>>> res = join.run()
>>> df = pd.read_csv(res.outputs.out_file, delim_whitespace=True,
...                  index_col=None)
>>> df.columns.ravel().tolist()
['a', 'b', 'c', 'd', 'e']
>>> np.all(df.values == np.hstack((data[:, 3:], data[:, :3])))
True
>>> os.chdir(cwd)
Mandatory Inputs:
  • in_file (a pathlike object or string representing an existing file) – Input file.

  • join_file (a pathlike object or string representing an existing file) – File to be adjoined.

Optional Inputs:
  • columns (a list of items which are a string) – Header for columns.

  • side (‘right’ or ‘left’) – Where to join. (Nipype default value: right)

Outputs:

out_file (a pathlike object or string representing an existing file) – Output TSV file.

class NormalizeMotionParams(from_file=None, resource_monitor=None, **inputs)[source]

Bases: SimpleInterface

Convert input motion parameters into the designated convention.

Mandatory Inputs:

in_file (a pathlike object or string representing an existing file) – The input parameters file.

Optional Inputs:

format (‘FSL’ or ‘AFNI’ or ‘FSFAST’ or ‘NIPY’) – Output format. (Nipype default value: FSL)

Outputs:

out_file (a pathlike object or string representing an existing file) – Written file path.

class SanitizeImage(from_file=None, resource_monitor=None, **inputs)[source]

Bases: SimpleInterface

Check the correctness of x-form headers (matrix and code) and fixes problematic combinations of values. Removes any extension form the header if present. This interface implements the following logic: +——————-+——————+——————+——————+————————————————+ | valid quaternions | qform_code > 0 | sform_code > 0 | qform == sform | actions | +===================+==================+==================+==================+================================================+ | True | True | True | True | None | +——————-+——————+——————+——————+————————————————+ | True | True | False | * | sform, scode <- qform, qcode | +——————-+——————+——————+——————+————————————————+ | * | True | * | False | sform, scode <- qform, qcode | +——————-+——————+——————+——————+————————————————+ | * | False | True | * | qform, qcode <- sform, scode | +——————-+——————+——————+——————+————————————————+ | * | False | False | * | sform, qform <- best affine; scode, qcode <- 1 | +——————-+——————+——————+——————+————————————————+ | False | * | False | * | sform, qform <- best affine; scode, qcode <- 1 | +——————-+——————+——————+——————+————————————————+

Mandatory Inputs:

in_file (a pathlike object or string representing an existing file) – Input image.

Optional Inputs:
  • max_32bit (a boolean) – Cast data to float32 if higher precision is encountered. (Nipype default value: False)

  • n_volumes_to_discard (an integer) – Discard n first volumes. (Nipype default value: 0)

Outputs:
  • out_file (a pathlike object or string representing an existing file) – Validated image.

  • out_report (a pathlike object or string representing an existing file) – HTML segment containing warning.

class TPM2ROI(from_file=None, resource_monitor=None, **inputs)[source]

Bases: SimpleInterface

Convert tissue probability maps (TPMs) into ROIs

This interface follows the following logic:

  1. Erode in_mask by mask_erode_mm and apply to in_tpm

  2. Threshold masked TPM at prob_thresh

  3. Erode resulting mask by erode_mm

Mandatory Inputs:
  • in_mask (a pathlike object or string representing an existing file) – Binary mask of skull-stripped T1w image.

  • in_tpm (a pathlike object or string representing an existing file) – Tissue probability map file in T1 space.

Optional Inputs:
  • erode_mm (a float) – Erode output mask (kernel width in mm). Mutually exclusive with inputs: erode_prop.

  • erode_prop (a float) – Erode output mask (target volume ratio). Mutually exclusive with inputs: erode_mm.

  • mask_erode_mm (a float) – Erode input mask (kernel width in mm). Mutually exclusive with inputs: mask_erode_prop.

  • mask_erode_prop (a float) – Erode input mask (target volume ratio). Mutually exclusive with inputs: mask_erode_mm.

  • prob_thresh (a float) – Threshold for the tissue probability maps. (Nipype default value: 0.95)

Outputs:
  • eroded_mask (a pathlike object or string representing an existing file) – Resulting eroded mask.

  • roi_file (a pathlike object or string representing an existing file) – Output ROI file.

class TSV2JSON(from_file=None, resource_monitor=None, **inputs)[source]

Bases: SimpleInterface

Convert metadata from TSV format to JSON format.

Mandatory Inputs:
  • in_file (a pathlike object or string representing an existing file) – Input TSV file.

  • index_column (a string) – Name of the column in the TSV to be used as the top-level key in the JSON. All remaining columns will be assigned as nested keys.

Optional Inputs:
  • additional_metadata (a dictionary with keys which are any value and with values which are any value or an OrderedDict or None or None) – Any additional metadata that should be applied to all entries in the JSON. (Nipype default value: None)

  • drop_columns (a list of items which are any value or None) – List of columns in the TSV to be dropped from the JSON. (Nipype default value: None)

  • enforce_case (a boolean) – Enforce snake case for top-level keys and camel case for nested keys. (Nipype default value: True)

  • output (a pathlike object or string representing a file or None) – Path where the output file is to be saved. If this is None, then a JSON-compatible dictionary is returned instead.

Outputs:

output (a dictionary with keys which are any value and with values which are any value or a pathlike object or string representing an existing file or an OrderedDict or None) – Output dictionary or JSON file.