niworkflows.interfaces.utility module

Interfaces under evaluation before upstreaming to nipype.interfaces.utility.

class niworkflows.interfaces.utility.AddTSVHeader(from_file=None, resource_monitor=None, **inputs)[source]

Bases: SimpleInterface

Add a header row to a TSV file

Examples

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
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 niworkflows.interfaces.utility.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 niworkflows.interfaces.utility.JoinTSVColumns(from_file=None, resource_monitor=None, **inputs)[source]

Bases: SimpleInterface

Add a header row to a TSV file

Examples

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
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 niworkflows.interfaces.utility.KeySelect(keys=None, fields=None, **inputs)[source]

Bases: BaseInterface

An interface that operates similarly to an OrderedDict.

>>> ks = KeySelect(keys=['MNI152NLin6Asym', 'MNI152Lin', 'fsaverage'],
...                fields=['field1', 'field2', 'field3'])
>>> ks.inputs.field1 = ['fsl', 'mni', 'freesurfer']
>>> ks.inputs.field2 = ['volume', 'volume', 'surface']
>>> ks.inputs.field3 = [True, False, False]
>>> ks.inputs.key = 'MNI152Lin'
>>> ks.run().outputs

field1 = mni
field2 = volume
field3 = False
key = MNI152Lin
>>> ks = KeySelect(fields=['field1', 'field2', 'field3'])
>>> ks.inputs.keys=['MNI152NLin6Asym', 'MNI152Lin', 'fsaverage']
>>> ks.inputs.field1 = ['fsl', 'mni', 'freesurfer']
>>> ks.inputs.field2 = ['volume', 'volume', 'surface']
>>> ks.inputs.field3 = [True, False, False]
>>> ks.inputs.key = 'MNI152Lin'
>>> ks.run().outputs

field1 = mni
field2 = volume
field3 = False
key = MNI152Lin
>>> ks.inputs.field1 = ['fsl', 'mni', 'freesurfer']
>>> ks.inputs.field2 = ['volume', 'volume', 'surface']
>>> ks.inputs.field3 = [True, False, False]
>>> ks.inputs.key = 'fsaverage'
>>> ks.run().outputs

field1 = freesurfer
field2 = surface
field3 = False
key = fsaverage
>>> ks.inputs.field1 = ['fsl', 'mni', 'freesurfer']
>>> ks.inputs.field2 = ['volume', 'volume', 'surface']
>>> ks.inputs.field3 = [True, False]  
Traceback (most recent call last):
ValueError: Trying to set an invalid value
>>> ks.inputs.key = 'MNINLin2009cAsym'
Traceback (most recent call last):
ValueError: Selected key "MNINLin2009cAsym" not found in the index
>>> ks = KeySelect(fields=['field1', 'field2', 'field3'])
>>> ks.inputs.keys=['MNI152NLin6Asym']
>>> ks.inputs.field1 = ['fsl']
>>> ks.inputs.field2 = ['volume']
>>> ks.inputs.field3 = [True]
>>> ks.inputs.key = 'MNI152NLin6Asym'
>>> ks.run().outputs

field1 = fsl
field2 = volume
field3 = True
key = MNI152NLin6Asym
Mandatory Inputs:
  • key (a string) – Selective key.

  • keys (a list of items which are a string) – Index of keys.

Outputs:

key (a string) – Propagates selected key.

input_spec

alias of _KeySelectInputSpec

output_spec

alias of _KeySelectOutputSpec

class niworkflows.interfaces.utility.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.