Commit 51cf185e authored by William Clarke's avatar William Clarke
Browse files

Add more helpful error if the basis set has too little time coverage.

parent 30cbd3e9
......@@ -15,6 +15,10 @@ from fsl_mrs.utils.constants import GYRO_MAG_RATIO, PPM_SHIFT, PPM_RANGE
import numpy as np
class BasisHasInsufficentPoints(Exception):
pass
class MRS(object):
"""
MRS Class - container for FID, Basis, and sequence info
......@@ -242,11 +246,21 @@ class MRS(object):
Usually the basis is simulated using different
timings and/or number of points.
This interpolates the basis to match the FID
This only works if the basis has greater time-domain
coverage than the FID.
"""
self.basis = misc.ts_to_ts(self.basis,
self.basis_dwellTime,
self.dwellTime,
self.numPoints)
try:
self.basis = misc.ts_to_ts(self.basis,
self.basis_dwellTime,
self.dwellTime,
self.numPoints)
except misc.InsufficentTimeCoverageError:
raise BasisHasInsufficentPoints('The basis spectra covers too little time. '
'Please provide a basis with time-domain coverage '
'greater than the input data. Alternatively truncate '
'your input data.')
self.basis_dwellTime = self.dwellTime
self.basis_bandwidth = 1 / self.dwellTime
self.numBasisPoints = self.numPoints
......
......@@ -6,10 +6,13 @@ Copyright Will Clarke, University of Oxford, 2021'''
from pathlib import Path
from fsl_mrs.core import MRS, mrs_from_files
import pytest
from fsl_mrs.utils import synthetic as syn
import numpy as np
from fsl_mrs.core import MRS, mrs_from_files
from fsl_mrs.core.mrs import BasisHasInsufficentPoints
from fsl_mrs.utils import synthetic as syn
from fsl_mrs.utils.misc import FIDToSpec, hz2ppm
from fsl_mrs.utils.constants import GYRO_MAG_RATIO
......@@ -195,3 +198,16 @@ def test_nucleus_identification():
header=hdr)
assert mrs.nucleus == nuc
def test_basis_size(synth_data):
fid, hdr, basis, names, bheader, axes = synth_data
# Truncate basis to test error reporting
basis = basis[:, :512]
with pytest.raises(BasisHasInsufficentPoints):
MRS(FID=fid,
header=hdr,
basis=basis,
names=names,
basis_hdr=bheader[0])
......@@ -161,6 +161,10 @@ def filter(mrs, FID, ppmlim, filter_type='bandpass'):
return y
class InsufficentTimeCoverageError(Exception):
pass
def ts_to_ts(old_ts, old_dt, new_dt, new_n):
"""Temporal resampling where the new time series has a smaller number of points
......@@ -174,6 +178,10 @@ def ts_to_ts(old_ts, old_dt, new_dt, new_n):
old_t = np.linspace(old_dt, old_dt * old_n, old_n) - old_dt
new_t = np.linspace(new_dt, new_dt * new_n, new_n) - new_dt
if new_t[-1] > old_t[-1]:
raise InsufficentTimeCoverageError('Input data covers less time than is requested by interpolation.'
' Change interpolation points or dwell time.')
f = interp1d(old_t, old_ts, axis=0)
new_ts = f(new_t)
......
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