def test_spectral_registration(): time_axis = numpy.arange(0, 0.512, 5e-4) target_fid = suspect.MRSData(suspect.basis.gaussian(time_axis, 0, 0, 50.0), 5e-4, 123) for i in range(1, 15): input_fid = suspect.MRSData( suspect.basis.gaussian(time_axis, i, 0, 50.0), 5e-4, 123) frequency_shift, phase_shift = suspect.processing.frequency_correction.spectral_registration( input_fid, target_fid) numpy.testing.assert_allclose(frequency_shift, i)
def test_slice_ppm(): data = suspect.MRSData(numpy.ones(1000), 1e-3, 123) spectrum = data.spectrum() a_slice = spectrum.slice_ppm(5.7, 3.7) assert a_slice == slice(377, 623) reversed_slice = spectrum.slice_ppm(3.7, 5.7) assert a_slice == slice(377, 623)
def test_adjust_first_phase(): data = suspect.MRSData(numpy.fft.ifft(numpy.ones(10)), 1e-1, 123) phased_data = suspect.adjust_phase(data, 0, numpy.pi / 10).spectrum() numpy.testing.assert_almost_equal(phased_data[0], -1j) numpy.testing.assert_almost_equal(phased_data[-1], numpy.exp(1j * 0.4 * numpy.pi)) numpy.testing.assert_almost_equal(phased_data[5], 1)
def test_create_mrs(): data = suspect.MRSData(numpy.zeros(1024), 5e-4, 123) assert data.shape == (1024, ) assert data.dt == 5e-4 assert data.np == 1024 assert data.df == 1.953125 numpy.testing.assert_equal(data, data.fid())
def test_spectrum_2_fid(): data = suspect.MRSData(numpy.ones(1024, 'complex'), 5e-4, 123) spectrum = data.spectrum() assert type(spectrum) == suspect.MRSSpectrum numpy.testing.assert_equal(spectrum, numpy.fft.fftshift(numpy.fft.fft(data))) fid = spectrum.fid() numpy.testing.assert_equal(data, fid)
def test_slice_hz(): data = suspect.MRSData(numpy.ones(1024), 1e-3, 123) spectrum = data.spectrum() whole_slice = spectrum.slice_hz(-500, 500) assert whole_slice == slice(0, 1024) restricted_slice = spectrum.slice_hz(-100, 200) assert restricted_slice == slice(409, 717) with pytest.raises(ValueError): too_large_slice = spectrum.slice_hz(-1000, 1000)
def test_centre(): position = (12.4, -9.8, 11.0) transform = _transforms.transformation_matrix( (1, 0, 0), (0, 1, 0), (12.4, -9.8, 11.0), (10, 10, 10)) data = suspect.MRSData(numpy.ones(1024, 'complex'), 5e-4, 123, transform=transform) numpy.testing.assert_equal(data.centre, position) numpy.testing.assert_equal(data.position, position)
def test_water_peak_alignment(): spectrum = numpy.zeros(128, 'complex') spectrum[0] = 1 fids = suspect.MRSData(numpy.zeros((16, 128), 'complex'), 1.0 / 128, 123) for i in range(fids.shape[0]): rolled_spectrum = numpy.roll(spectrum, i) fids[i] = numpy.fft.ifft(rolled_spectrum) frequency_shift = suspect.processing.frequency_correction.residual_water_alignment( fids[i]) numpy.testing.assert_almost_equal(frequency_shift, i)
def test_frequency_transform(): spectrum = numpy.zeros(128, 'complex') spectrum[0] = 1 for i in range(16): rolled_spectrum = numpy.roll(spectrum, i) fid = suspect.MRSData(numpy.fft.ifft(rolled_spectrum), 1.0 / 128, 123) transformed_fid = fid.adjust_frequency(-i) transformed_spectrum = numpy.fft.fft(transformed_fid) numpy.testing.assert_almost_equal(transformed_spectrum, spectrum)
def test_write_raw(): # lcmodel needs to know the transform properties transform = suspect.transformation_matrix([1, 0, 0], [0, 1, 0], [0, 0, 0], [10, 10, 10]) data = suspect.MRSData(numpy.zeros(1, 'complex'), 1e-3, 123.456, transform=transform) mock = unittest.mock.mock_open() with patch.object(builtins, 'open', mock): suspect.io.lcmodel.save_raw("/home/ben/test_raw.raw", data)
def test_felix_save_mat(): data = suspect.MRSData(numpy.zeros((16, 32), dtype='complex'), 1e-3, 123.456) mock = unittest.mock.mock_open() with patch.object(builtins, 'open', mock): suspect.io.felix.save_mat("test.mat", data) print(mock.mock_calls) # handle = mock() # print(handle.write.call_args()) assert 3 == 4
def test_frequency_transform(): spectrum = numpy.zeros(128, 'complex') spectrum[0] = 1 for i in range(16): rolled_spectrum = numpy.roll(spectrum, i) fid = suspect.MRSData(numpy.fft.ifft(rolled_spectrum), 1.0 / 128, 123) transformed_fid = suspect.processing.frequency_correction.transform_fid( fid, -i, 0) transformed_spectrum = numpy.fft.fft(transformed_fid) numpy.testing.assert_almost_equal(transformed_spectrum, spectrum)
def test_apodize(): data = suspect.MRSData(numpy.ones(1024), 5e-4, 123.456) raw_spectrum = numpy.fft.fft(data) apodized_data = suspect.processing.apodize( data, suspect.processing.gaussian_window, {"line_broadening": data.df * 8}) spectrum = numpy.fft.fft(apodized_data) numpy.testing.assert_allclose(spectrum[4].real, 0.5 * numpy.amax(spectrum), rtol=0.01) numpy.testing.assert_allclose(numpy.sum(spectrum), numpy.sum(raw_spectrum))
def test_null_transform(): fid = numpy.ones(128, 'complex') data = suspect.MRSData(fid, 1.0 / 128, 123) transformed_data = data.adjust_frequency(0) transformed_data = transformed_data.adjust_phase(0, 0) assert type(transformed_data) == suspect.MRSData numpy.testing.assert_equal(transformed_data, data) # test again using suspect namespace transformed_data = suspect.adjust_frequency(data, 0) transformed_data = suspect.adjust_phase(transformed_data, 0, 0) numpy.testing.assert_equal(transformed_data, data)
def test_resample_single_slice(): source_volume = suspect.base.ImageBase(np.random.random((20, 20, 20)), transform=np.eye(4)) slc = source_volume.resample(source_volume.row_vector, source_volume.col_vector, [1, 20, 10], centre=(5, 10, 0)) assert slc.shape == (20, 10) np.testing.assert_equal(source_volume[0, :, :10], slc) # once we have a single slice, test creating a mask from a 2D reference spec_volume = suspect.MRSData(np.zeros(20), 0.1, 123, transform=np.eye(4)) mask = suspect.image.create_mask(spec_volume, slc) assert mask.shape == (20, 10)
def test_lcmodel_all_files(): # lcmodel needs to know the transform properties transform = suspect.transformation_matrix([1, 0, 0], [0, 1, 0], [0, 0, 0], [10, 10, 10]) data = suspect.MRSData(numpy.zeros(1, 'complex'), 1e-3, 123.456, transform=transform) mock = unittest.mock.mock_open() with patch.object(builtins, 'open', mock): suspect.io.lcmodel.write_all_files( os.path.join(os.getcwd(), "lcmodel"), data)
def test_windowed_spectral_registration(): time_axis = np.arange(0, 0.512, 5e-4) # construct a pair of fids with a large aligned peak at 100Hz and a smaller # peak offset by 5 * df. If the big peak is not excluded that will override # the offset peak target_fid = suspect.MRSData(suspect.basis.gaussian(time_axis, 0, 0, 10.0) + suspect.basis.gaussian(time_axis, 100, 0, 10.0) * 10, 5e-4, 123) moving_fid = suspect.MRSData(suspect.basis.gaussian(time_axis, 5 * target_fid.df, 0, 10.0) + suspect.basis.gaussian(time_axis, 100, 0, 10.0) * 10, 5e-4, 123) # ensure that the un-windowed version gives the correct answer (of 0) fs, ps = suspect.processing.frequency_correction.spectral_registration(moving_fid, target_fid) np.testing.assert_allclose(fs, 0, atol=0.3) # first we test supplying a Hz frequency range fs, ps = suspect.processing.frequency_correction.spectral_registration(moving_fid, target_fid, frequency_range=(-50, 50)) np.testing.assert_allclose(fs, 5 * target_fid.df, atol=0.3) # next we test using a ppm slice fs, ps = suspect.processing.frequency_correction.spectral_registration(moving_fid, target_fid, frequency_range=target_fid.slice_ppm(5, 4.4)) np.testing.assert_allclose(fs, 5 * target_fid.df, atol=0.3) # finally we test using a mask in the frequency domain spectral_mask = np.zeros_like(target_fid) spectral_mask[490:534] = 1 fs, ps = suspect.processing.frequency_correction.spectral_registration(moving_fid, target_fid, frequency_range=spectral_mask) np.testing.assert_allclose(fs, 5 * target_fid.df, atol=0.3)
def test_whiten(): noise = suspect.MRSData(np.random.randn(32, 2048), 1e-3, 123) # make sure the noise has some correlations in it noise[0] += noise[1] noise[2] += 0.25 * noise[3] noise[4] += 0.9 * noise[5] # make sure the covariance matrix is not I cov_pre = np.cov(noise) np.testing.assert_raises(AssertionError, np.testing.assert_equal, cov_pre, np.eye(32)) white_noise = suspect.processing.channel_combination.whiten(noise, noise) cov_post = np.cov(white_noise) np.testing.assert_almost_equal(cov_post, np.eye(32)) # do the whitening specifying a length of noise white_noise = suspect.processing.channel_combination.whiten(noise, 2048) cov_post = np.cov(white_noise) np.testing.assert_almost_equal(cov_post, np.eye(32))
def test_lcmodel_all_files(): data = suspect.MRSData(numpy.zeros(1, 'complex'), 1e-3, 123.456) mock = unittest.mock.mock_open() with patch.object(builtins, 'open', mock): suspect.io.lcmodel.write_all_files( os.path.join(os.getcwd(), "lcmodel"), data)
def test_write_raw(): data = suspect.MRSData(numpy.zeros(1, 'complex'), 1e-3, 123.456) mock = unittest.mock.mock_open() with patch.object(builtins, 'open', mock): suspect.io.lcmodel.save_raw("/home/ben/test_raw.raw", data)
def test_write_dpt(): data = suspect.MRSData(numpy.zeros(1), 1e-3, 123.456) mock = unittest.mock.mock_open() with patch.object(builtins, 'open', mock): suspect.io.tarquin.save_dpt("/home/ben/test_dpt.dpt", data)
def test_average_mrs(): data = suspect.MRSData(numpy.ones((2, 1024), 'complex'), 5e-4, 123) averaged_data = data.inherit(numpy.average(data, axis=0)) assert type(averaged_data) == suspect.MRSData assert averaged_data.dt == 5e-4 assert averaged_data.f0 == 123
def test_slice_mrs(): data = suspect.MRSData(numpy.ones((2, 1024), 'complex'), 5e-4, 123) repetition = data[0] assert repetition.shape == (1024, ) assert repetition.dt == 5e-4
def test_adjust_zero_phase(): data = suspect.MRSData(numpy.ones(10), 1e-3, 123) phased_data = suspect.adjust_phase(data, numpy.pi / 2) numpy.testing.assert_allclose(phased_data, 1j * numpy.ones(10))
def test_felix_save_mat(): data = suspect.MRSData(numpy.zeros((16, 32), dtype='complex'), 1e-3, 123.456) mock = unittest.mock.mock_open() with patch.object(builtins, 'open', mock): suspect.io.felix.save_mat("test.mat", data)
def test_zero_rank_to_scalar(): data = suspect.MRSData(numpy.ones(1024, 'complex'), 5e-4, 123) sum = numpy.sum(data) assert numpy.isscalar(sum)
def test_null_transform(): fid = numpy.ones(128, 'complex') data = suspect.MRSData(fid, 1.0 / 128, 123) transformed_data = suspect.processing.frequency_correction.transform_fid( data, 0, 0) assert type(transformed_data) == suspect.MRSData