Commit 7c74c410 authored by William Clarke's avatar William Clarke
Browse files

Merge and split with headers.

parent 0715bece
......@@ -8,6 +8,7 @@
import json
from pathlib import Path
import re
import numpy as np
from nibabel.nifti1 import Nifti1Extension
......@@ -187,6 +188,78 @@ class NIFTI_MRS(Image):
dim += 4
return dim
def add_hdr_field(self, key, value):
"""Add a field to the header extension
To do: validate type (standard or user)
:param key: Field key
:type key: str
:param value: Value of field to add
"""
dim_n = re.compile(r'dim_[567].*')
if dim_n.match(key):
raise ValueError('Modify dimension headers through dedicated methods.')
current_hdr_ext = self.hdr_ext
current_hdr_ext.update({key: value})
self.hdr_ext = current_hdr_ext
def remove_hdr_field(self, key):
"""Remove a field from the header extension
:param key: Key to remove
:type key: str
"""
if key == 'SpectrometerFrequency' or key == 'ResonantNucleus':
raise ValueError('You cannot remove the required metadata.')
dim_n = re.compile(r'dim_[567].*')
if dim_n.match(key):
raise ValueError('Modify dimension headers through dedicated methods.')
current_hdr_ext = self.hdr_ext
current_hdr_ext.pop(key, None)
self.hdr_ext = current_hdr_ext
def set_dim_info(self, dim, info_str):
"""Set or update the 'dim_N_info' field
:param dim: The dim tag or python dimension index (i.e. N-1)
:type dim: str or int
:param info_str: New info string
:type info_str: str
"""
dim = self._dim_tag_to_index(dim)
current_hdr_ext = self.hdr_ext
current_hdr_ext[f'dim_{dim + 1}_info'] = info_str
self.hdr_ext = current_hdr_ext
def set_dim_header(self, dim, hdr_obj):
"""Set or update the 'dim_N_header' field
hdr_obj replaces the current value.
:param dim: The dim tag or python dimension index (i.e. N-1)
:type dim: str or int
:param hdr_obj: dict containing the dimension headers
:type hdr_obj: dict
"""
dim = self._dim_tag_to_index(dim)
# Check size
def size_chk(obj):
if len(obj) != self.shape[dim]:
raise ValueError(f'New dim header length must be {self.shape[dim]}')
for key in hdr_obj:
if isinstance(hdr_obj[key], list):
size_chk(hdr_obj[key])
elif isinstance(hdr_obj[key], dict)\
and 'value' in hdr_obj[key]:
size_chk(hdr_obj[key]['value'])
current_hdr_ext = self.hdr_ext
current_hdr_ext[f'dim_{dim + 1}_header'] = hdr_obj
self.hdr_ext = current_hdr_ext
def copy(self, remove_dim=None):
'''Return a copy of this image, optionally with a dimension removed.
Args:
......@@ -204,10 +277,13 @@ class NIFTI_MRS(Image):
if dd > new_obj.ndim:
hdr_ext.pop(f'dim_{dd}', None)
hdr_ext.pop(f'dim_{dd}_header', None)
hdr_ext.pop(f'dim_{dd}_info', None)
elif dd >= dim:
hdr_ext[f'dim_{dd}'] = hdr_ext[f'dim_{dd + 1}']
if f'dim_{dd + 1}_header' in hdr_ext:
hdr_ext[f'dim_{dd}_header'] = hdr_ext[f'dim_{dd + 1}_header']
if f'dim_{dd + 1}_info' in hdr_ext:
hdr_ext[f'dim_{dd}_info'] = hdr_ext[f'dim_{dd + 1}_info']
new_obj.hdr_ext = hdr_ext
new_obj._set_dim_tags()
......
......@@ -7,6 +7,7 @@ Copyright Will Clarke, University of Oxford, 2021'''
# Imports
from pathlib import Path
import pytest
import numpy as np
from fsl_mrs.core import NIFTI_MRS
......@@ -130,3 +131,56 @@ def test_gen_new_nifti_mrs(tmp_path):
nmrs.save(tmp_path / 'out')
assert (tmp_path / 'out.nii.gz').exists()
def test_add_remove_field():
nmrs = NIFTI_MRS(data['unprocessed'])
with pytest.raises(ValueError) as exc_info:
nmrs.remove_hdr_field('SpectrometerFrequency')
assert exc_info.type is ValueError
assert exc_info.value.args[0] == 'You cannot remove the required metadata.'
with pytest.raises(ValueError) as exc_info:
nmrs.remove_hdr_field('ResonantNucleus')
assert exc_info.type is ValueError
assert exc_info.value.args[0] == 'You cannot remove the required metadata.'
with pytest.raises(ValueError) as exc_info:
nmrs.remove_hdr_field('dim_5')
assert exc_info.type is ValueError
assert exc_info.value.args[0] == 'Modify dimension headers through dedicated methods.'
with pytest.raises(ValueError) as exc_info:
nmrs.add_hdr_field('dim_5_header', {'p1': [1, 2, 3]})
assert exc_info.type is ValueError
assert exc_info.value.args[0] == 'Modify dimension headers through dedicated methods.'
nmrs.add_hdr_field('RepetitionTime', 5.0)
assert 'RepetitionTime' in nmrs.hdr_ext
assert nmrs.hdr_ext['RepetitionTime'] == 5.0
nmrs.remove_hdr_field('RepetitionTime')
assert 'RepetitionTime' not in nmrs.hdr_ext
def test_set_dim_info():
nmrs = NIFTI_MRS(data['unprocessed'])
nmrs.set_dim_info('DIM_DYN', 'my info')
assert nmrs.hdr_ext['dim_6_info'] == 'my info'
def test_set_dim_header():
nmrs = NIFTI_MRS(data['unprocessed'])
with pytest.raises(ValueError) as exc_info:
nmrs.set_dim_header('DIM_DYN', {'my_hdr': np.arange(10).tolist()})
assert exc_info.type is ValueError
assert exc_info.value.args[0] == 'New dim header length must be 64'
nmrs.set_dim_header('DIM_DYN', {'my_hdr': np.arange(64).tolist()})
assert nmrs.hdr_ext['dim_6_header'] == {'my_hdr': np.arange(64).tolist()}
"""Test the miscellaneous functions for NIFTI-MRS tools
Author: Will Clarke <william.clarke@ndcn.ox.ac.uk>
Copyright (C) 2021 University of Oxford
"""
import fsl_mrs.utils.nifti_mrs_tools.misc as misc
def test_short_to_long():
dict_repr = misc.dim_n_header_short_to_long({'start': 0.0, 'increment': 0.1}, 3)
assert dict_repr == [0.0, 0.1, 0.2]
dict_repr = misc.dim_n_header_short_to_long([0.0, 0.1, 0.2], 3)
assert dict_repr == [0.0, 0.1, 0.2]
dict_repr = misc.dim_n_header_short_to_long({'value': [0.0, 0.1, 0.2], 'description': 'test'}, 3)
assert dict_repr == {'value': [0.0, 0.1, 0.2], 'description': 'test'}
dict_repr = misc.dim_n_header_short_to_long({'value': {'start': 0.0, 'increment': 0.1}, 'description': 'test'}, 3)
assert dict_repr == {'value': [0.0, 0.1, 0.2], 'description': 'test'}
def test_long_to_short():
dict_repr = misc.dim_n_header_long_to_short([0.0, 0.1, 0.2])
assert dict_repr == {'start': 0.0, 'increment': 0.1}
dict_repr = misc.dim_n_header_long_to_short({'start': 0.0, 'increment': 0.1})
assert dict_repr == {'start': 0.0, 'increment': 0.1}
dict_repr = misc.dim_n_header_long_to_short({'value': [0.0, 0.1, 0.2], 'description': 'test'})
assert dict_repr == {'value': {'start': 0.0, 'increment': 0.1}, 'description': 'test'}
dict_repr = misc.dim_n_header_long_to_short({'value': {'start': 0.0, 'increment': 0.1}, 'description': 'test'})
assert dict_repr == {'value': {'start': 0.0, 'increment': 0.1}, 'description': 'test'}
def test_dict_to_list():
list_repr = misc._dict_to_list({'start': 0.0, 'increment': 0.1}, 3)
assert list_repr == [0.0, 0.1, 0.2]
list_repr = misc._dict_to_list({'start': 0.0, 'increment': 0.1}, 1)
assert list_repr == [0.0, ]
list_repr = misc._dict_to_list({'start': 1, 'increment': 1}, 3)
assert list_repr == [1, 2, 3]
list_repr = misc._dict_to_list({'start': 1, 'increment': -1}, 3)
assert list_repr == [1, 0, -1]
list_repr = misc._dict_to_list([1, 0, -1], 3)
assert list_repr == [1, 0, -1]
def test_list_to_dict():
dict_repr = misc._list_to_dict([0.0, 0.1, 0.2])
assert dict_repr == {'start': 0.0, 'increment': 0.1}
dict_repr = misc._list_to_dict([1, 2, 3])
assert dict_repr == {'start': 1, 'increment': 1}
dict_repr = misc._list_to_dict([1, 0, -1])
assert dict_repr == {'start': 1, 'increment': -1}
dict_repr = misc._list_to_dict([1, 2, 4])
assert dict_repr == [1, 2, 4]
dict_repr = misc._list_to_dict(['ON', 'OFF'])
assert dict_repr == ['ON', 'OFF']
......@@ -10,7 +10,9 @@ import pytest
import numpy as np
from fsl_mrs.utils import mrs_io
from fsl_mrs.utils.preproc import nifti_mrs_tools as nmrs_tools
from fsl_mrs.utils import nifti_mrs_tools as nmrs_tools
from fsl_mrs.utils.nifti_mrs_tools.split_merge import NIfTI_MRSIncompatible
from fsl_mrs.core.nifti_mrs import gen_new_nifti_mrs
testsPath = Path(__file__).parent
test_data_split = testsPath / 'testdata' / 'fsl_mrs_preproc' / 'metab_raw.nii.gz'
......@@ -19,6 +21,269 @@ test_data_merge_2 = testsPath / 'testdata' / 'fsl_mrs_preproc' / 'quant_raw.nii.
test_data_other = testsPath / 'testdata' / 'fsl_mrs_preproc' / 'ecc.nii.gz'
def test_split_dim_header():
"""Test the ability to split the dim_N_header fields"""
hdr_in = {'dim_5': 'DIM_DYN',
'dim_5_info': 'averages',
'dim_5_header': {'p1': [1, 2, 3, 4],
'p2': [0.1, 0.2, 0.3, 0.4]},
'dim_6': 'DIM_EDIT',
'dim_6_info': 'edit',
'dim_6_header': {'p1': {'start': 1, 'increment': 1},
'p2': [0.1, 0.2, 0.3, 0.4]},
'dim_7': 'DIM_USER_0',
'dim_7_info': 'other',
'dim_7_header': {'p1': {'value': {'start': 1, 'increment': 1}, 'description': 'user'},
'p2': [0.1, 0.2, 0.3, 0.4]}}
# Headers occuring as a list.
hdr1, hdr2 = nmrs_tools.split_merge._split_dim_header(hdr_in, 5, 4, 1)
assert hdr1 == {'dim_5': 'DIM_DYN',
'dim_5_info': 'averages',
'dim_5_header': {'p1': [1, 2],
'p2': [0.1, 0.2]},
'dim_6': 'DIM_EDIT',
'dim_6_info': 'edit',
'dim_6_header': {'p1': {'start': 1, 'increment': 1},
'p2': [0.1, 0.2, 0.3, 0.4]},
'dim_7': 'DIM_USER_0',
'dim_7_info': 'other',
'dim_7_header': {'p1': {'value': {'start': 1, 'increment': 1}, 'description': 'user'},
'p2': [0.1, 0.2, 0.3, 0.4]}}
assert hdr2 == {'dim_5': 'DIM_DYN',
'dim_5_info': 'averages',
'dim_5_header': {'p1': [3, 4],
'p2': [0.3, 0.4]},
'dim_6': 'DIM_EDIT',
'dim_6_info': 'edit',
'dim_6_header': {'p1': {'start': 1, 'increment': 1},
'p2': [0.1, 0.2, 0.3, 0.4]},
'dim_7': 'DIM_USER_0',
'dim_7_info': 'other',
'dim_7_header': {'p1': {'value': {'start': 1, 'increment': 1}, 'description': 'user'},
'p2': [0.1, 0.2, 0.3, 0.4]}}
hdr1, hdr2 = nmrs_tools.split_merge._split_dim_header(hdr_in, 5, 4, [1, 3])
assert hdr1 == {'dim_5': 'DIM_DYN',
'dim_5_info': 'averages',
'dim_5_header': {'p1': [1, 3],
'p2': [0.1, 0.3]},
'dim_6': 'DIM_EDIT',
'dim_6_info': 'edit',
'dim_6_header': {'p1': {'start': 1, 'increment': 1},
'p2': [0.1, 0.2, 0.3, 0.4]},
'dim_7': 'DIM_USER_0',
'dim_7_info': 'other',
'dim_7_header': {'p1': {'value': {'start': 1, 'increment': 1}, 'description': 'user'},
'p2': [0.1, 0.2, 0.3, 0.4]}}
assert hdr2 == {'dim_5': 'DIM_DYN',
'dim_5_info': 'averages',
'dim_5_header': {'p1': [2, 4],
'p2': [0.2, 0.4]},
'dim_6': 'DIM_EDIT',
'dim_6_info': 'edit',
'dim_6_header': {'p1': {'start': 1, 'increment': 1},
'p2': [0.1, 0.2, 0.3, 0.4]},
'dim_7': 'DIM_USER_0',
'dim_7_info': 'other',
'dim_7_header': {'p1': {'value': {'start': 1, 'increment': 1}, 'description': 'user'},
'p2': [0.1, 0.2, 0.3, 0.4]}}
# Headers as a dict
hdr1, hdr2 = nmrs_tools.split_merge._split_dim_header(hdr_in, 6, 4, 1)
assert hdr1 == {'dim_5': 'DIM_DYN',
'dim_5_info': 'averages',
'dim_5_header': {'p1': [1, 2, 3, 4],
'p2': [0.1, 0.2, 0.3, 0.4]},
'dim_6': 'DIM_EDIT',
'dim_6_info': 'edit',
'dim_6_header': {'p1': {'start': 1, 'increment': 1},
'p2': [0.1, 0.2]},
'dim_7': 'DIM_USER_0',
'dim_7_info': 'other',
'dim_7_header': {'p1': {'value': {'start': 1, 'increment': 1}, 'description': 'user'},
'p2': [0.1, 0.2, 0.3, 0.4]}}
assert hdr2 == {'dim_5': 'DIM_DYN',
'dim_5_info': 'averages',
'dim_5_header': {'p1': [1, 2, 3, 4],
'p2': [0.1, 0.2, 0.3, 0.4]},
'dim_6': 'DIM_EDIT',
'dim_6_info': 'edit',
'dim_6_header': {'p1': {'start': 3, 'increment': 1},
'p2': [0.3, 0.4]},
'dim_7': 'DIM_USER_0',
'dim_7_info': 'other',
'dim_7_header': {'p1': {'value': {'start': 1, 'increment': 1}, 'description': 'user'},
'p2': [0.1, 0.2, 0.3, 0.4]}}
hdr1, hdr2 = nmrs_tools.split_merge._split_dim_header(hdr_in, 6, 4, [1, ])
assert hdr1 == {'dim_5': 'DIM_DYN',
'dim_5_info': 'averages',
'dim_5_header': {'p1': [1, 2, 3, 4],
'p2': [0.1, 0.2, 0.3, 0.4]},
'dim_6': 'DIM_EDIT',
'dim_6_info': 'edit',
'dim_6_header': {'p1': [1, 3, 4],
'p2': [0.1, 0.3, 0.4]},
'dim_7': 'DIM_USER_0',
'dim_7_info': 'other',
'dim_7_header': {'p1': {'value': {'start': 1, 'increment': 1}, 'description': 'user'},
'p2': [0.1, 0.2, 0.3, 0.4]}}
assert hdr2 == {'dim_5': 'DIM_DYN',
'dim_5_info': 'averages',
'dim_5_header': {'p1': [1, 2, 3, 4],
'p2': [0.1, 0.2, 0.3, 0.4]},
'dim_6': 'DIM_EDIT',
'dim_6_info': 'edit',
'dim_6_header': {'p1': [2, ],
'p2': [0.2, ]},
'dim_7': 'DIM_USER_0',
'dim_7_info': 'other',
'dim_7_header': {'p1': {'value': {'start': 1, 'increment': 1}, 'description': 'user'},
'p2': [0.1, 0.2, 0.3, 0.4]}}
# User defined structures
hdr1, hdr2 = nmrs_tools.split_merge._split_dim_header(hdr_in, 7, 4, 1)
assert hdr1 == {'dim_5': 'DIM_DYN',
'dim_5_info': 'averages',
'dim_5_header': {'p1': [1, 2, 3, 4],
'p2': [0.1, 0.2, 0.3, 0.4]},
'dim_6': 'DIM_EDIT',
'dim_6_info': 'edit',
'dim_6_header': {'p1': {'start': 1, 'increment': 1},
'p2': [0.1, 0.2, 0.3, 0.4]},
'dim_7': 'DIM_USER_0',
'dim_7_info': 'other',
'dim_7_header': {'p1': {'value': {'start': 1, 'increment': 1}, 'description': 'user'},
'p2': [0.1, 0.2]}}
assert hdr2 == {'dim_5': 'DIM_DYN',
'dim_5_info': 'averages',
'dim_5_header': {'p1': [1, 2, 3, 4],
'p2': [0.1, 0.2, 0.3, 0.4]},
'dim_6': 'DIM_EDIT',
'dim_6_info': 'edit',
'dim_6_header': {'p1': {'start': 1, 'increment': 1},
'p2': [0.1, 0.2, 0.3, 0.4]},
'dim_7': 'DIM_USER_0',
'dim_7_info': 'other',
'dim_7_header': {'p1': {'value': {'start': 3, 'increment': 1}, 'description': 'user'},
'p2': [0.3, 0.4]}}
def test_merge_dim_header():
"""Test the ability to merge the dim_N_header fields"""
hdr_in_1 = {'dim_5': 'DIM_DYN',
'dim_5_info': 'averages',
'dim_5_header': {'p1': [1, 2, 3, 4],
'p2': [0.1, 0.2, 0.3, 0.4]},
'dim_6': 'DIM_EDIT',
'dim_6_info': 'edit',
'dim_6_header': {'p1': {'start': 1, 'increment': 1},
'p2': [0.1, 0.2, 0.3, 0.4]},
'dim_7': 'DIM_USER_0',
'dim_7_info': 'other',
'dim_7_header': {'p1': {'value': {'start': 1, 'increment': 1}, 'description': 'user'},
'p2': [0.1, 0.2, 0.3, 0.4]}}
hdr_in_2 = {'dim_5': 'DIM_DYN',
'dim_5_info': 'averages',
'dim_5_header': {'p1': [1, 2, 3, 4],
'p2': [0.1, 0.2, 0.3, 0.4]},
'dim_6': 'DIM_EDIT',
'dim_6_info': 'edit',
'dim_6_header': {'p1': {'start': 1, 'increment': 1},
'p2': [0.1, 0.2, 0.3, 0.4]},
'dim_7': 'DIM_USER_0',
'dim_7_info': 'other',
'dim_7_header': {'p1': {'value': {'start': 1, 'increment': 1}, 'description': 'user'},
'p2': [0.1, 0.2, 0.3, 0.4]}}
hdr_out = nmrs_tools.split_merge._merge_dim_header(hdr_in_1, hdr_in_2, 5, 4, 4)
assert hdr_out == {'dim_5': 'DIM_DYN',
'dim_5_info': 'averages',
'dim_5_header': {'p1': [1, 2, 3, 4, 1, 2, 3, 4],
'p2': [0.1, 0.2, 0.3, 0.4, 0.1, 0.2, 0.3, 0.4]},
'dim_6': 'DIM_EDIT',
'dim_6_info': 'edit',
'dim_6_header': {'p1': {'start': 1, 'increment': 1},
'p2': [0.1, 0.2, 0.3, 0.4]},
'dim_7': 'DIM_USER_0',
'dim_7_info': 'other',
'dim_7_header': {'p1': {'value': {'start': 1, 'increment': 1}, 'description': 'user'},
'p2': [0.1, 0.2, 0.3, 0.4]}}
hdr_in_2 = {'dim_5': 'DIM_DYN',
'dim_5_info': 'averages',
'dim_5_header': {'p1': [1, 2, 3, 4],
'p2': [0.1, 0.2, 0.3, 0.4]},
'dim_6': 'DIM_EDIT',
'dim_6_info': 'edit',
'dim_6_header': {'p1': {'start': 5, 'increment': 1},
'p2': [0.1, 0.2, 0.3, 0.4]},
'dim_7': 'DIM_USER_0',
'dim_7_info': 'other',
'dim_7_header': {'p1': {'value': {'start': 1, 'increment': 1}, 'description': 'user'},
'p2': [0.1, 0.2, 0.3, 0.4]}}
hdr_out = nmrs_tools.split_merge._merge_dim_header(hdr_in_1, hdr_in_2, 6, 4, 4)
assert hdr_out == {'dim_5': 'DIM_DYN',
'dim_5_info': 'averages',
'dim_5_header': {'p1': [1, 2, 3, 4],
'p2': [0.1, 0.2, 0.3, 0.4]},
'dim_6': 'DIM_EDIT',
'dim_6_info': 'edit',
'dim_6_header': {'p1': {'start': 1, 'increment': 1},
'p2': [0.1, 0.2, 0.3, 0.4, 0.1, 0.2, 0.3, 0.4]},
'dim_7': 'DIM_USER_0',
'dim_7_info': 'other',
'dim_7_header': {'p1': {'value': {'start': 1, 'increment': 1}, 'description': 'user'},
'p2': [0.1, 0.2, 0.3, 0.4]}}
hdr_out = nmrs_tools.split_merge._merge_dim_header(hdr_in_2, hdr_in_1, 6, 4, 4)
assert hdr_out == {'dim_5': 'DIM_DYN',
'dim_5_info': 'averages',
'dim_5_header': {'p1': [1, 2, 3, 4],
'p2': [0.1, 0.2, 0.3, 0.4]},
'dim_6': 'DIM_EDIT',
'dim_6_info': 'edit',
'dim_6_header': {'p1': [5, 6, 7, 8, 1, 2, 3, 4],
'p2': [0.1, 0.2, 0.3, 0.4, 0.1, 0.2, 0.3, 0.4]},
'dim_7': 'DIM_USER_0',
'dim_7_info': 'other',
'dim_7_header': {'p1': {'value': {'start': 1, 'increment': 1}, 'description': 'user'},
'p2': [0.1, 0.2, 0.3, 0.4]}}
hdr_in_2 = {'dim_5': 'DIM_DYN',
'dim_5_info': 'averages',
'dim_5_header': {'p1': [1, 2, 3, 4],
'p2': [0.1, 0.2, 0.3, 0.4]},
'dim_6': 'DIM_EDIT',
'dim_6_info': 'edit',
'dim_6_header': {'p1': {'start': 1, 'increment': 1},
'p2': [0.1, 0.2, 0.3, 0.4]},
'dim_7': 'DIM_USER_0',
'dim_7_info': 'other',
'dim_7_header': {'p1': {'value': {'start': 5, 'increment': 1}, 'description': 'user'},
'p2': [0.1, 0.2, 0.3, 0.4]}}
hdr_out = nmrs_tools.split_merge._merge_dim_header(hdr_in_1, hdr_in_2, 7, 4, 4)
assert hdr_out == {'dim_5': 'DIM_DYN',
'dim_5_info': 'averages',
'dim_5_header': {'p1': [1, 2, 3, 4],
'p2': [0.1, 0.2, 0.3, 0.4]},
'dim_6': 'DIM_EDIT',
'dim_6_info': 'edit',
'dim_6_header': {'p1': {'start': 1, 'increment': 1},
'p2': [0.1, 0.2, 0.3, 0.4]},
'dim_7': 'DIM_USER_0',
'dim_7_info': 'other',
'dim_7_header': {'p1': {'value': {'start': 1, 'increment': 1}, 'description': 'user'},
'p2': [0.1, 0.2, 0.3, 0.4, 0.1, 0.2, 0.3, 0.4]}}
with pytest.raises(NIfTI_MRSIncompatible) as exc_info:
hdr_out = nmrs_tools.split_merge._merge_dim_header(hdr_in_1, hdr_in_2, 5, 4, 4)
assert exc_info.type is NIfTI_MRSIncompatible
assert exc_info.value.args[0] == "Both files must have matching dimension headers apart from the one being merged."\
" dim_7_header does not match."
def test_split():
"""Test the split functionality
"""
......@@ -162,19 +427,19 @@ def test_merge():
assert exc_info.value.args[0] == "Dimension must be an int (4, 5, or 6) or string (DIM_TAG string)."
# Incompatible shapes
with pytest.raises(nmrs_tools.NIfTI_MRSIncompatible) as exc_info:
with pytest.raises(NIfTI_MRSIncompatible) as exc_info:
nmrs_tools.merge((nmrs_1, nmrs_bad_shape), 'DIM_DYN')
assert exc_info.type is nmrs_tools.NIfTI_MRSIncompatible
assert exc_info.type is NIfTI_MRSIncompatible
assert exc_info.value.args[0] == "The shape of all concatentated objects must match. "\
"The shape ((1, 1, 1, 4096, 4, 2)) of the 1 object does "\
"not match that of the first ((1, 1, 1, 4096, 32, 2))."
# Incompatible tags
with pytest.raises(nmrs_tools.NIfTI_MRSIncompatible) as exc_info:
with pytest.raises(NIfTI_MRSIncompatible) as exc_info:
nmrs_tools.merge((nmrs_1, nmrs_no_tag), 'DIM_DYN')
assert exc_info.type is nmrs_tools.NIfTI_MRSIncompatible
assert exc_info.type is NIfTI_MRSIncompatible
assert exc_info.value.args[0] == "The tags of all concatentated objects must match. "\
"The tags (['DIM_COIL', None, None]) of the 1 object does "\
"not match that of the first (['DIM_COIL', 'DIM_DYN', None])."
......@@ -194,6 +459,30 @@ def test_merge():
assert out.data.shape == (1, 1, 1, 4096, 32, 2, 2)
assert out.hdr_ext['dim_7'] == 'DIM_EDIT'
# Merge some synthetic data with header information
nhdr_1 = gen_new_nifti_mrs(np.ones((1, 1, 1, 10, 4), dtype=complex),
1 / 1000,
100,
'1H',
dim_tags=['DIM_DYN', None, None])
nhdr_2 = nhdr_1.copy()
nhdr_1.set_dim_header('DIM_DYN', {'RepetitionTime': [1, 2, 3, 4]})
nhdr_2.set_dim_header('DIM_DYN', {'RepetitionTime': [1, 2, 3, 4]})
out = nmrs_tools.merge((nhdr_1, nhdr_2, nhdr_2), 'DIM_DYN')
assert out.data.shape == (1, 1, 1, 10, 12)
assert out.hdr_ext['dim_5'] == 'DIM_DYN'
assert out.hdr_ext['dim_5_header'] == {'RepetitionTime': [1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4]}
nhdr_1.set_dim_header('DIM_DYN', {'RepetitionTime': {'start': 1, 'increment': 1}})
nhdr_2.set_dim_header('DIM_DYN', {'RepetitionTime': [5, 6, 7, 8]})
out = nmrs_tools.merge((nhdr_1, nhdr_2), 'DIM_DYN')
assert out.data.shape == (1, 1, 1, 10, 8)
assert out.hdr_ext['dim_5'] == 'DIM_DYN'
assert out.hdr_ext['dim_5_header'] == {'RepetitionTime': {'start': 1, 'increment': 1}}
def test_reorder():