Expand source code
class TestT2StimFit:
image_ge, affine_ge, te_ge = fetch.t2_ge(1)
image_ge = image_ge[35:45, 50:65, 2:4, :] # Crop to speed up tests
image_philips, affine_philips, te_philips = fetch.t2_philips(2)
image_philips = image_philips[35:45, 50:65, 2:4, :]
image_siemens, affine_siemens, te_siemens = fetch.t2_siemens(1)
image_siemens = image_siemens[35:45, 40:55, 2:4, :]
# selective
def test_selectiveness(self):
# Selective
model = StimFitModel(mode='selective', ukrin_vendor='ge')
mapper = T2StimFit(self.image_ge, self.affine_ge, model)
stats = arraystats.ArrayStats(mapper.t2_map).calculate()
npt.assert_allclose([stats["mean"]["3D"], stats["std"]["3D"],
stats["min"]["3D"], stats["max"]["3D"]],
[164.331581, 199.057747, 51.268116, 1455.551225],
rtol=1e-2, atol=0.25)
# Non-selective
model = StimFitModel(mode='non_selective', ukrin_vendor='ge')
mapper = T2StimFit(self.image_ge, self.affine_ge, model)
stats = arraystats.ArrayStats(mapper.t2_map).calculate()
npt.assert_allclose([stats["mean"]["3D"], stats["std"]["3D"],
stats["min"]["3D"], stats["max"]["3D"]],
[165.994692, 203.583211, 51.827107, 1497.168001],
rtol=1e-2, atol=0.25)
# n_comp
def test_n_comp(self):
# Two Components
model = StimFitModel(mode='selective', ukrin_vendor='ge', n_comp=2)
mapper = T2StimFit(self.image_ge[0, 14, :, :], self.affine_ge, model)
npt.assert_allclose([mapper.t2_map[0, 0]],
[117.991529],
rtol=5e-2, atol=0.1)
# Three Components
# Cant get this to be stable across operating systems so commented out.
# model = StimFitModel(mode='selective', ukrin_vendor='ge', n_comp=3)
# mapper = T2StimFit(self.image_ge[0, 14, :, :], self.affine_ge, model)
# npt.assert_allclose([mapper.t2_map[0, 2]],
# [1245.291925],
# rtol=5e-2, atol=0.1)
# vendor
def test_vendor(self):
# Philips
model = StimFitModel(mode='selective', ukrin_vendor='philips')
mapper = T2StimFit(self.image_philips, self.affine_philips, model)
stats = arraystats.ArrayStats(mapper.t2_map).calculate()
npt.assert_allclose([stats["mean"]["3D"], stats["std"]["3D"],
stats["min"]["3D"], stats["max"]["3D"]],
[281.52594, 596.832203, 36.470879, 3000.0],
rtol=1e-6, atol=1e-4)
# Siemens
model = StimFitModel(mode='selective', ukrin_vendor='siemens')
mapper = T2StimFit(self.image_siemens, self.affine_siemens, model)
stats = arraystats.ArrayStats(mapper.t2_map).calculate()
npt.assert_allclose([stats["mean"]["3D"], stats["std"]["3D"],
stats["min"]["3D"], stats["max"]["3D"]],
[120.47096, 190.454984, 26.621704, 2999.999651],
rtol=1e-5, atol=1e-2)
# mask
def test_mask(self):
mask = self.image_ge[..., 0] > 3000
model = StimFitModel(mode='non_selective', ukrin_vendor='ge')
mapper = T2StimFit(self.image_ge, self.affine_ge, model, mask=mask)
stats = arraystats.ArrayStats(mapper.t2_map).calculate()
npt.assert_allclose([stats["mean"]["3D"], stats["std"]["3D"],
stats["min"]["3D"], stats["max"]["3D"]],
[156.693513, 207.797, 0.0, 1497.168001],
rtol=1e-2, atol=0.25)
# threading
def test_st(self):
model = StimFitModel(mode='non_selective', ukrin_vendor='ge')
mapper = T2StimFit(self.image_ge, self.affine_ge, model,
multithread=False)
stats = arraystats.ArrayStats(mapper.t2_map).calculate()
npt.assert_allclose([stats["mean"]["3D"], stats["std"]["3D"],
stats["min"]["3D"], stats["max"]["3D"]],
[165.994692, 203.583211, 51.827107, 1497.168001],
rtol=1e-2, atol=0.25)
# normalisation
def test_normalisation_warning(self):
with pytest.warns(UserWarning):
model = StimFitModel(mode='non_selective', ukrin_vendor='ge')
mapper = T2StimFit(self.image_ge * 2, self.affine_ge, model,
norm=False)
def test_etl_signal_exception(self):
with pytest.raises(Exception):
model = StimFitModel(mode='non_selective', ukrin_vendor='ge')
mapper = T2StimFit(self.image_ge[..., :-2], self.affine_ge, model)
# to_nifti
def test_to_nifti(self):
mask = self.image_ge[..., 0] > 3000
model = StimFitModel(mode='non_selective', ukrin_vendor='ge')
mapper = T2StimFit(self.image_ge, self.affine_ge, model, mask=mask)
if os.path.exists('test_output'):
shutil.rmtree('test_output')
os.makedirs('test_output', exist_ok=True)
# Check all is saved.
mapper.to_nifti(output_directory='test_output',
base_file_name='t2stimfittest', maps='all')
output_files = os.listdir('test_output')
assert len(output_files) == 6
assert 't2stimfittest_b1_map.nii.gz' in output_files
assert 't2stimfittest_b1_map_scaled.nii.gz' in output_files
assert 't2stimfittest_m0_map.nii.gz' in output_files
assert 't2stimfittest_mask.nii.gz' in output_files
assert 't2stimfittest_r2_map.nii.gz' in output_files
assert 't2stimfittest_t2_map.nii.gz' in output_files
for f in os.listdir('test_output'):
os.remove(os.path.join('test_output', f))
# Check that no files are saved.
mapper.to_nifti(output_directory='test_output',
base_file_name='t2stimfittest', maps=[])
output_files = os.listdir('test_output')
assert len(output_files) == 0
# Check that only t2, mask and r2 are saved.
mapper.to_nifti(output_directory='test_output',
base_file_name='t2stimfittest', maps=['mask', 't2',
'r2'])
output_files = os.listdir('test_output')
assert len(output_files) == 3
assert 't2stimfittest_mask.nii.gz' in output_files
assert 't2stimfittest_t2_map.nii.gz' in output_files
assert 't2stimfittest_r2_map.nii.gz' in output_files
for f in os.listdir('test_output'):
os.remove(os.path.join('test_output', f))
# Check that it fails when no maps are given
with pytest.raises(ValueError):
mapper.to_nifti(output_directory='test_output',
base_file_name='t2stimfittest', maps='')
# Delete 'test_output' folder
shutil.rmtree('test_output')
def test_get_fit_signal(self):
model = StimFitModel(mode='non_selective', ukrin_vendor='ge')
mapper = T2StimFit(self.image_ge, self.affine_ge, model,
multithread=False)
fit_signal = mapper.get_fit_signal()
stats = arraystats.ArrayStats(fit_signal).calculate()
npt.assert_allclose([stats["mean"]["4D"], stats["std"]["4D"],
stats["min"]["4D"], stats["max"]["4D"]],
[0.4638738536540625,
0.191494386548333,
0.013169224663907297,
1.0085580888230137],
rtol=1e-6, atol=1e-4)