Skip to content

Commit

Permalink
Merge pull request #223 from alexdaniel654/feature/t1_neg_value_warning
Browse files Browse the repository at this point in the history
T1 Magnitude Correct Data Detection
  • Loading branch information
alexdaniel654 authored Jul 5, 2024
2 parents f062b53 + 3ddcfed commit 385ac90
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
28 changes: 27 additions & 1 deletion ukat/mapping/t1.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,36 @@ def __init__(self, pixel_array, ti, parameters=2, mask=None, tss=0,
self.tss = tss
self.tss_axis = tss_axis

if np.nanmin(pixel_array) < 0:
# Assume the data has been magnitude corrected if the first
# percentile of the first inversion time is negative.
if np.percentile(pixel_array[..., 0], 1) < 0:
self.mag_corr = True
neg_percent = (np.sum(pixel_array[..., 0] < 0)
/ pixel_array[..., 0].size)
if neg_percent < 0.05:
warnings.warn('Fitting data to a magnitude corrected '
'inversion recovery curve however, less than 5% '
'of the data from the first inversion is '
'negative. If you have performed magnitude '
'correction ignore this warning, otherwise the '
'negative values could be due to noise or '
'preprocessing steps such as EPI distortion '
'correction and registration.\n'
f'Percentage of first inversion data that is '
f'negative = {neg_percent:.2%}')
else:
self.mag_corr = False
if np.nanmin(pixel_array) < 0:
warnings.warn('Negative values found in data from the first '
'inversion but as the first percentile is not '
'negative, it is assumed these are negative '
'due to noise or preprocessing steps such as '
'EPI distortion correction and registration. '
'As such the data will be fit to the modulus of '
'the recovery curve.\n'
f'Min value = {np.nanmin(pixel_array[..., 0])}\n'
'1st percentile = '
f'{np.percentile(pixel_array[..., 0], 1)}')

if self.parameters == 2:
if self.mag_corr:
Expand Down
27 changes: 27 additions & 0 deletions ukat/mapping/tests/test_t1.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,33 @@ def test_tss_valid_axis(self):
inversion_list=np.linspace(0, 2000, 10),
affine=self.affine, tss=1, tss_axis=2)

def test_mag_corr_warning(self):
# Test warning for small number of negative values thus assuming no
# magnitude correction has been performed

# Make the absolute of the signal into a 4D array
signal_array = np.tile(np.abs(self.correct_signal_two_param),
(10, 10, 3, 1))
# Add a single negative value to the signal
signal_array[0, 0, 0, 0] = -1

with pytest.warns(UserWarning):
mapper = T1(signal_array, self.t, self.affine, multithread=False)

# Test warning for enough negative values to assume magnitude
# correction has been performed but still not that many negative values

# Make the of the signal into a 4D array
signal_array = np.tile(np.abs(self.correct_signal_two_param),
(10, 10, 3, 1))
# Add a row of signals with negative values to the image
# 3.3% of first inversion is negative but 1st percentile is negative.
signal_array[:, 0, 0, :] = self.correct_signal_two_param

with pytest.warns(UserWarning):
mapper = T1(signal_array, self.t, self.affine, multithread=False)


def test_molli_2p_warning(self):
signal_array = np.tile(self.correct_signal_three_param, (10, 10, 3, 1))
with pytest.warns(UserWarning):
Expand Down

0 comments on commit 385ac90

Please sign in to comment.