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

extended fitting tests

parent 1cb69f81
from fsl_mrs.utils.synthetic import syntheticFID
from fsl_mrs.utils.synthetic.synthetic_from_basis import syntheticFromBasisFile
from fsl_mrs.core import MRS
from fsl_mrs.utils.fitting import fit_FSLModel
from fsl_mrs.utils import mrs_io
from pytest import fixture
import numpy as np
from pathlib import Path
testsPath = Path(__file__).parent
basis_path = testsPath / 'testdata/fsl_mrs/steam_basis'
@fixture
def data():
......@@ -55,6 +60,25 @@ def test_fit_FSLModel_Newton(data):
assert np.allclose(fittedconcs,amplitudes,atol=1E-1)
assert np.allclose(fittedRelconcs,amplitudes/(amplitudes[0]+amplitudes[1]),atol=1E-1)
def test_fit_FSLModel_lorentzian_Newton(data):
mrs = data[0]
amplitudes = data[1]
metab_groups = [0]*mrs.numBasis
Fitargs = {'ppmlim': [0.2,4.2],
'method': 'Newton',
'baseline_order': -1,
'metab_groups': metab_groups,
'model': 'lorentzian'}
res = fit_FSLModel(mrs,**Fitargs)
fittedconcs = res.getConc(metab = mrs.names)
fittedRelconcs = res.getConc(scaling='internal',metab = mrs.names)
assert np.allclose(fittedconcs,amplitudes,atol=1E-1)
assert np.allclose(fittedRelconcs,amplitudes/(amplitudes[0]+amplitudes[1]),atol=1E-1)
def test_fit_FSLModel_MH(data):
mrs = data[0]
......@@ -72,4 +96,53 @@ def test_fit_FSLModel_MH(data):
fittedRelconcs = res.getConc(scaling='internal',metab = mrs.names)
assert np.allclose(fittedconcs,amplitudes,atol=1E-1)
assert np.allclose(fittedRelconcs,amplitudes/(amplitudes[0]+amplitudes[1]),atol=1E-1)
\ No newline at end of file
assert np.allclose(fittedRelconcs,amplitudes/(amplitudes[0]+amplitudes[1]),atol=1E-1)
def test_fit_FSLModel_lorentzian_MH(data):
mrs = data[0]
amplitudes = data[1]
metab_groups = [0]*mrs.numBasis
Fitargs = {'ppmlim':[0.2,4.2],
'method':'MH',
'baseline_order':-1,
'metab_groups':metab_groups,
'MHSamples':100,
'model': 'lorentzian'}
res = fit_FSLModel(mrs,**Fitargs)
fittedconcs = res.getConc(metab = mrs.names)
fittedRelconcs = res.getConc(scaling='internal',metab = mrs.names)
assert np.allclose(fittedconcs,amplitudes,atol=1E-1)
assert np.allclose(fittedRelconcs,amplitudes/(amplitudes[0]+amplitudes[1]),atol=1E-1)
def test_fit_FSLModel_on_invivo_sim():
FIDs,hdr,trueconcs = syntheticFromBasisFile(basis_path,
noisecovariance=[[1E-3]],
broadening=(9.0, 9.0),
concentrations={'Mac':2.0})
basis,names,header = mrs_io.read_basis(basis_path)
mrs = MRS(FID=FIDs,header=hdr,basis=basis,basis_hdr=header[0],names=names)
mrs.processForFitting()
metab_groups = [0]*mrs.numBasis
Fitargs = {'ppmlim':[0.2,4.2],
'method':'MH',
'baseline_order':-1,
'metab_groups':metab_groups,
'MHSamples':100}
res = fit_FSLModel(mrs,**Fitargs)
fittedRelconcs = res.getConc(scaling='internal',metab = mrs.names)
answers = np.asarray(trueconcs)
answers /= (answers[names.index('Cr')]+trueconcs[names.index('PCr')])
assert np.allclose(fittedRelconcs,answers,atol=5E-2)
\ No newline at end of file
......@@ -74,7 +74,7 @@ def syntheticFromBasisFile(basisFile,
Returns:
FIDs: Numpy array of synthetic FIDs
outHeader: Header suitable for loading FIDs into MRS object.
concentrations: Final concentration scalinfs
concentrations: Final concentration scalings
"""
basis,names,header = mrs_io.read_basis(basisFile)
......
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