Commit eb52cb94 authored by William Clarke's avatar William Clarke
Browse files

Tweaks to mrsi and sythetic from basis code. Numpy>1.20 for potential sliding window code.

parent c6dc31cc
......@@ -22,17 +22,6 @@ class MRSI(object):
mask=None, basis=None, names=None,
basis_hdr=None, H2O=None):
# process mask
if mask is None:
mask = np.full(FID.shape, True)
elif mask.shape[0:3] == FID.shape[0:3]:
mask = mask != 0.0
else:
raise ValueError(f'Mask must be None or numpy'
f' array of the same shape'
f' as FID. Mask {mask.shape[0:3]},'
f' FID {FID.shape[0:3]}.')
# process H2O
if H2O is None:
H2O = np.full(FID.shape, None)
......@@ -43,7 +32,9 @@ class MRSI(object):
# Load into properties
self.data = FID
self.H2O = H2O
self.mask = mask
# process mask
self.set_mask(mask)
if header is not None:
self.header = header
......@@ -244,7 +235,7 @@ class MRSI(object):
def set_mask(self, mask):
""" Load mask as numpy array."""
if mask is None:
mask = np.full(self.data.shape, True)
mask = np.full(self.data.shape[0:3], True)
elif mask.shape[0:3] == self.data.shape[0:3]:
mask = mask != 0.0
else:
......
......@@ -50,7 +50,7 @@ def standardConcentrations(basisNames):
return concs
def prep_mrs_for_synthetic(basisFile, points, bandwidth, ignore, ind_scaling, concentrations):
def prep_mrs_for_synthetic(basisFile, points, bandwidth, ignore, ind_scaling, concentrations, metab_groups, add_mm):
"""Prepare an mrs object for use in creating a synthetic spectrum,
and return selected concentrations.
......@@ -70,6 +70,12 @@ def prep_mrs_for_synthetic(basisFile, points, bandwidth, ignore, ind_scaling, co
empty_mrs.ignore(ignore)
empty_mrs.processForFitting(ind_scaling=ind_scaling)
mg = parse_metab_groups(empty_mrs, metab_groups)
if add_mm:
n = empty_mrs.add_MM_peaks(gamma=40, sigma=30)
new_metab_groups = [i + max(mg) + 1 for i in range(n)]
mg = mg + new_metab_groups
if concentrations is None:
concentrations = standardConcentrations(empty_mrs.names)
elif isinstance(concentrations, (list, np.ndarray)):
......@@ -87,7 +93,7 @@ def prep_mrs_for_synthetic(basisFile, points, bandwidth, ignore, ind_scaling, co
else:
raise ValueError('Concentrations must be None, a list,'
'or a dict containing overides for particular metabolites.')
return empty_mrs, concentrations
return empty_mrs, concentrations, mg
def syntheticFromBasisFile(basisFile,
......@@ -99,11 +105,14 @@ def syntheticFromBasisFile(basisFile,
baseline_ppm=None,
broadening=(9.0, 0.0),
shifting=0.0,
phi0=0.0,
phi1=0.0,
coilamps=[1.0],
coilphase=[0.0],
noisecovariance=[[0.1]],
bandwidth=4000.0,
points=2048):
points=2048,
add_default_mm=False):
""" Create synthetic data from a set of FSL-MRS basis files.
Args:
......@@ -127,6 +136,7 @@ def syntheticFromBasisFile(basisFile,
noisecovariance (list of floats, optional): N coils x N coils array of noise variance/covariance.
bandwidth (float,optional): Bandwidth of output spectrum in Hz
points (int,optional): Number of points in output spectrum.
add_default_mm (bool,optional): Add default MM after scaling
Returns:
FIDs: Numpy array of synthetic FIDs
......@@ -134,16 +144,17 @@ def syntheticFromBasisFile(basisFile,
concentrations: Final concentration scalings
"""
empty_mrs, concentrations = prep_mrs_for_synthetic(basisFile,
points,
bandwidth,
ignore,
ind_scaling,
concentrations)
empty_mrs, concentrations, mg = prep_mrs_for_synthetic(basisFile,
points,
bandwidth,
ignore,
ind_scaling,
concentrations,
metab_groups,
add_default_mm)
# Currently hardcoded to voigt model. Sigma can always be set to 0.
_, _, fwd_model, _, p2x = models.getModelFunctions('voigt')
mg = parse_metab_groups(empty_mrs, metab_groups)
g = max(mg) + 1
if not isinstance(broadening, list):
......@@ -187,8 +198,8 @@ def syntheticFromBasisFile(basisFile,
gamma,
sigma,
eps,
0,
0,
phi0,
phi1,
b)
FIDs = synthetic_from_fwd_model(fwd_model,
......
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