diff --git a/fsl/data/vest.py b/fsl/data/vest.py index e56c89ca6414212246db8908eca705eef2f89d13..9fe10c103ad8dc278d4ec4b163883803ecc6830f 100644 --- a/fsl/data/vest.py +++ b/fsl/data/vest.py @@ -87,10 +87,12 @@ def loadVestFile(path, ignoreHeader=True): """Loads numeric data from a VEST file, returning it as a ``numpy`` array. :arg ignoreHeader: if ``True`` (the default), the matrix shape specified - in the VEST header information is ignored. Otherwise, - if the number of rows/columns specified in the VEST - header information does not match the matrix shape, - a ``ValueError`` is raised. + in the VEST header information is ignored, and the shape + inferred from the data. Otherwise, if the number of + rows/columns specified in the VEST header information + does not match the matrix shape, a ``ValueError`` is + raised. + :returns: a ``numpy`` array containing the matrix data in the VEST file. """ @@ -108,8 +110,10 @@ def loadVestFile(path, ignoreHeader=True): if (ncols is not None) and (nrows is not None): break - - + if tuple(data.shape) != (nrows, ncols): + raise ValueError(f'Invalid VEST file ({path}) - data shape ' + f'({data.shape}) does not match header ' + f'({nrows}, {ncols})') return data diff --git a/fsl/scripts/Text2Vest.py b/fsl/scripts/Text2Vest.py new file mode 100644 index 0000000000000000000000000000000000000000..15989f945f0a39ef73163e9c6840eff893f2e5c7 --- /dev/null +++ b/fsl/scripts/Text2Vest.py @@ -0,0 +1,43 @@ +#!/usr/bin/env python +# +# Text2Vest.py - Convert an ASCII text matrix file into a VEST file. +# +# Author: Paul McCarthy <pauldmccarthy@gmail.com> +# +"""``Text2Vest`` simply takes a plain text ASCII text matrix file, and +adds a VEST header. +""" + + +import sys + +import numpy as np + +import fsl.data.vest as fslvest + + +usage = "Usage: Text2Vest <text_file> <vest_file>" + + +def main(argv=None): + """Convert a plain text file to a VEST file. """ + + if argv is None: + argv = sys.argv[1:] + + if len(argv) != 2: + print(usage) + return 0 + + infile, outfile = argv + data = np.loadtxt(infile) + vest = fslvest.generateVest(data) + + with open(outfile, 'wt') as f: + f.write(vest) + + return 0 + + +if __name__ == '__main__': + sys.exit(main()) diff --git a/fsl/scripts/Vest2Text.py b/fsl/scripts/Vest2Text.py new file mode 100644 index 0000000000000000000000000000000000000000..b12d8166a32a8ffd8eb2de33ada2e886ba9f8b39 --- /dev/null +++ b/fsl/scripts/Vest2Text.py @@ -0,0 +1,44 @@ +#!/usr/bin/env python +# +# Vest2Text.py - Convert a VEST matrix file into a plain text ASCII file. +# +# Author: Paul McCarthy <pauldmccarthy@gmail.com> +# +"""``Vest2Text`` takes a VEST file containing a 2D matrix, and converts it +into a plain-text ASCII file. +""" + + +import sys + +import numpy as np + +import fsl.data.vest as fslvest + + +usage = "Usage: Vest2Text <vest_file> <text_file>" + + +def main(argv=None): + """Convert a VEST file to a plain text file. """ + + if argv is None: + argv = sys.argv[1:] + + if len(argv) != 2: + print(usage) + return 0 + + infile, outfile = argv + data = fslvest.loadVestFile(infile) + + if np.issubdtype(data.dtype, np.integer): fmt = '%d' + else: fmt = '%0.12f' + + np.savetxt(outfile, data, fmt=fmt) + + return 0 + + +if __name__ == '__main__': + sys.exit(main())