Commit b3a2e146 authored by William Clarke's avatar William Clarke
Browse files

Changes needed for mpress basis construction. Handle multi FID jMRUI basis files.

parent 8af9f2d5
......@@ -70,6 +70,8 @@ def main():
help='Optionally provide a different name for the basis.')
addparser.add_argument('--scale', action="store_true",
help='Rescale amplitude to mean of target basis.')
addparser.add_argument('--conj', action="store_true",
help='Conjugate added basis.')
addparser.set_defaults(func=add)
# Shift tool
......@@ -79,7 +81,8 @@ def main():
shiftparser.add_argument('file', type=Path,
help='Basis file to modify')
shiftparser.add_argument('metabolite', type=str,
help='Metabolite to shift')
help='Metabolite to shift,'
' use "all" to shift all metabolites')
shiftparser.add_argument('ppm_shift', type=float,
help='Shift to apply (in ppm).')
shiftparser.add_argument('output', type=Path,
......@@ -182,7 +185,12 @@ def add(args):
bw = 1 / json_dict['basis_dwell']
width = json_dict['basis_width']
basis_tools.add_basis(fid, name, cf, bw, args.target, scale=args.scale, width=width, sim_info=args.info)
basis_tools.add_basis(
fid, name, cf, bw, args.target,
scale=args.scale,
width=width,
conj=args.conj,
sim_info=args.info)
def shift(args):
......@@ -190,7 +198,11 @@ def shift(args):
from fsl_mrs.utils.mrs_io import read_basis
basis = read_basis(args.file)
basis = basis_tools.shift_basis(basis, args.metabolite, args.ppm_shift)
if args.metabolite == 'all':
for name in basis.names:
basis = basis_tools.shift_basis(basis, name, args.ppm_shift)
else:
basis = basis_tools.shift_basis(basis, args.metabolite, args.ppm_shift)
basis.save(args.output, overwrite=True)
......
......@@ -59,6 +59,7 @@ def test_read_FID_SVS():
BasisTestData = {'fsl': op.join(testsPath, 'testdata/mrs_io/basisset_FSL'),
'raw': op.join(testsPath, 'testdata/mrs_io/basisset_LCModel_raw'),
'txt': op.join(testsPath, 'testdata/mrs_io/basisset_JMRUI'),
'txt_single': op.join(testsPath, 'testdata/mrs_io/basis_set_jMRUI.txt'),
'lcm': op.join(testsPath, 'testdata/mrs_io/basisset_LCModel.BASIS')}
......@@ -79,23 +80,27 @@ def test_read_Basis():
basis_fsl = mrsio.read_basis(BasisTestData['fsl'])
basis_txt = mrsio.read_basis(BasisTestData['txt'])
basis_txt_single = mrsio.read_basis(BasisTestData['txt_single'])
basis_lcm = mrsio.read_basis(BasisTestData['lcm'])
# Check each returns a basis object
assert isinstance(basis_fsl, Basis)
assert isinstance(basis_txt, Basis)
assert isinstance(basis_txt_single, Basis)
assert isinstance(basis_lcm, Basis)
# lcm basis file is zeropadded by a factor of 2
# Test that all contain the same amount of data.
assert basis_fsl.original_points == 2048
assert basis_txt.original_points == 2048
assert basis_txt_single.original_points == 2048
assert basis_lcm.original_points == (2 * 2048)
# Test that the number of names match the amount of data
numNames = 21
assert len(basis_fsl.names) == numNames
assert len(basis_txt.names) == numNames
assert len(basis_txt_single.names) == 17
assert len(basis_lcm.names) == numNames
......
......@@ -50,6 +50,7 @@ def test_add(tmp_path):
subprocess.check_call(['basis_tools', 'add',
'--info', 'some info',
'--scale',
'--conj',
'--name', 'new_basis',
str(mac),
str(out_loc)])
......@@ -69,6 +70,17 @@ def test_shift(tmp_path):
assert out_loc.is_dir()
assert (out_loc / 'NAA.json').is_file()
out_loc = tmp_path / 'test_basis2'
subprocess.check_call(['basis_tools', 'shift',
str(fsl),
'all',
'1.0',
str(out_loc)])
assert out_loc.is_dir()
assert (out_loc / 'NAA.json').is_file()
def test_rescale(tmp_path):
out_loc = tmp_path / 'test_basis1'
......
......@@ -49,6 +49,19 @@ def test_fsl_mrs(tmp_path):
assert op.exists(op.join(tmp_path, 'options.txt'))
def test_no_ref(tmp_path):
subprocess.check_call(['fsl_mrs',
'--data', data['metab'],
'--basis', data['basis'],
'--output', tmp_path,
'--metab_groups', 'Mac',
'--overwrite',
'--combine', 'Cr', 'PCr',
'--report'])
assert op.exists(op.join(tmp_path, 'report.html'))
def test_alt_ref(tmp_path):
subprocess.check_call(['fsl_mrs',
......
......@@ -48,9 +48,9 @@ def test_add_basis(tmp_path):
assert exc_info.type is basis_tools.IncompatibleBasisError
assert exc_info.value.args[0] == "The new basis FID covers too little time, try padding."
fid_pad = np.pad(fid, (0, fid.size))
basis_tools.add_basis(fid_pad, 'mac2', cf, bw, out_loc)
new_basis = mrs_io.read_basis(out_loc)
index = new_basis.names.index('mac2')
assert 'mac2' in new_basis.names
......@@ -62,6 +62,12 @@ def test_add_basis(tmp_path):
assert 'mac3' in new_basis.names
assert new_basis.basis_fwhm[index] == 10
basis_tools.add_basis(fid_pad, 'mac4', cf, bw, out_loc, width=10, conj=True)
new_basis = mrs_io.read_basis(out_loc)
index = new_basis.names.index('mac4')
assert 'mac4' in new_basis.names
assert np.allclose(new_basis._raw_fids[:, index], fid_pad[0::2].conj())
def test_shift():
basis = mrs_io.read_basis(fsl_basis_path)
......
This source diff could not be displayed because it is stored in LFS. You can view the blob instead.
This source diff could not be displayed because it is stored in LFS. You can view the blob instead.
This source diff could not be displayed because it is stored in LFS. You can view the blob instead.
This source diff could not be displayed because it is stored in LFS. You can view the blob instead.
This source diff could not be displayed because it is stored in LFS. You can view the blob instead.
This source diff could not be displayed because it is stored in LFS. You can view the blob instead.
This source diff could not be displayed because it is stored in LFS. You can view the blob instead.
This source diff could not be displayed because it is stored in LFS. You can view the blob instead.
This source diff could not be displayed because it is stored in LFS. You can view the blob instead.
This source diff could not be displayed because it is stored in LFS. You can view the blob instead.
This source diff could not be displayed because it is stored in LFS. You can view the blob instead.
This source diff could not be displayed because it is stored in LFS. You can view the blob instead.
This source diff could not be displayed because it is stored in LFS. You can view the blob instead.
This source diff could not be displayed because it is stored in LFS. You can view the blob instead.
This source diff could not be displayed because it is stored in LFS. You can view the blob instead.
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment