Commit 731c7a45 authored by William Clarke's avatar William Clarke
Browse files

Fix bug in mrs_tools split.

parent f2d796e6
......@@ -256,7 +256,7 @@ def split(args):
split_name = args.file.with_suffix('').with_suffix('').name
# 2. Merge the files
if args.index:
if args.index is not None:
split_1, split_2 = nmrs_tools.split(to_split, args.dim, args.index)
elif args.indices:
split_1, split_2 = nmrs_tools.split(to_split, args.dim, args.indices)
......
......@@ -7,6 +7,7 @@ Copyright Will Clarke, University of Oxford, 2021'''
# Imports
import subprocess
from pathlib import Path
import nibabel as nib
# Files
testsPath = Path(__file__).parent
......@@ -99,6 +100,10 @@ def test_split(tmp_path):
assert (tmp_path / 'split_file_1.nii.gz').exists()
assert (tmp_path / 'split_file_2.nii.gz').exists()
f1 = nib.load(tmp_path / 'split_file_1.nii.gz')
f2 = nib.load(tmp_path / 'split_file_2.nii.gz')
assert f1.shape[5] == 32
assert f2.shape[5] == 32
subprocess.check_call(['mrs_tools', 'split',
'--dim', 'DIM_DYN',
......@@ -108,15 +113,24 @@ def test_split(tmp_path):
assert (tmp_path / 'metab_raw_1.nii.gz').exists()
assert (tmp_path / 'metab_raw_2.nii.gz').exists()
f1 = nib.load(tmp_path / 'metab_raw_1.nii.gz')
f2 = nib.load(tmp_path / 'metab_raw_2.nii.gz')
assert f1.shape[5] == 32
assert f2.shape[5] == 32
subprocess.check_call(['mrs_tools', 'split',
'--dim', 'DIM_DYN',
'--indices', '31', '34', '40',
'--filename', 'indicies_select',
'--output', str(tmp_path),
'--file', str(test_data_split)])
assert (tmp_path / 'metab_raw_1.nii.gz').exists()
assert (tmp_path / 'metab_raw_2.nii.gz').exists()
assert (tmp_path / 'indicies_select_1.nii.gz').exists()
assert (tmp_path / 'indicies_select_2.nii.gz').exists()
f1 = nib.load(tmp_path / 'indicies_select_1.nii.gz')
f2 = nib.load(tmp_path / 'indicies_select_2.nii.gz')
assert f1.shape[5] == 61
assert f2.shape[5] == 3
# Test reorder option
......
......@@ -53,7 +53,7 @@ def split(nmrs, dimension, index_or_indicies):
# Construct indexing
if isinstance(index_or_indicies, int):
if index_or_indicies < 0\
or index_or_indicies >= nmrs.shape[dim_index]:
or (index_or_indicies + 1) >= nmrs.shape[dim_index]:
raise ValueError('index_or_indicies must be between 0 and N-1,'
f' where N is the size of the specified dimension ({nmrs.shape[dim_index]}).')
index = np.arange(index_or_indicies + 1, nmrs.shape[dim_index])
......
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