test_utils_results.py 3.15 KB
Newer Older
1
2
3
4
5
6
7
8
9
# Test features of the results class

# Imports
from fsl_mrs.utils.synthetic import syntheticFID
from fsl_mrs.core import MRS
from fsl_mrs.utils.fitting import fit_FSLModel
from pytest import fixture
import numpy as np

William Clarke's avatar
William Clarke committed
10
11

# Set up some synthetic data to use
12
13
14
@fixture(scope='module')
def data():
    noiseCov = 0.01
William Clarke's avatar
William Clarke committed
15
16
17
18
19
20
    amplitude = np.asarray([0.5, 0.5, 1.0])*10
    chemshift = np.asarray([3.0, 3.05, 2.0])-4.65
    lw = [10, 10, 10]
    phases = [0, 0, 0]
    g = [0, 0, 0]
    basisNames = ['Cr', 'PCr', 'NAA']
21
    begintime = 0.00005
22
23

    basisFIDs = []
William Clarke's avatar
William Clarke committed
24
25
26
27
28
29
30
31
    for idx, _ in enumerate(amplitude):
        tmp, basisHdr = syntheticFID(noisecovariance=[[0.0]],
                                     chemicalshift=[chemshift[idx]+0.1],
                                     amplitude=[1.0],
                                     linewidth=[lw[idx]/5],
                                     phase=[phases[idx]],
                                     g=[g[idx]],
                                     begintime=0)
32
33
34
        basisFIDs.append(tmp[0])
    basisFIDs = np.asarray(basisFIDs)

William Clarke's avatar
William Clarke committed
35
36
37
38
39
40
41
    synFID, synHdr = syntheticFID(noisecovariance=[[noiseCov]],
                                  chemicalshift=chemshift,
                                  amplitude=amplitude,
                                  linewidth=lw,
                                  phase=phases,
                                  g=g,
                                  begintime=begintime)
42

William Clarke's avatar
William Clarke committed
43
44
45
46
47
    synMRS = MRS(FID=synFID[0],
                 header=synHdr,
                 basis=basisFIDs,
                 basis_hdr=basisHdr,
                 names=basisNames)
48
49

    metab_groups = [0]*synMRS.numBasis
William Clarke's avatar
William Clarke committed
50
51
52
53
54
55
56
57
58
59
    Fitargs = {'ppmlim': [0.2, 4.2],
               'method': 'MH',
               'baseline_order': -1,
               'metab_groups': metab_groups,
               'MHSamples': 100,
               'disable_mh_priors': True}

    res = fit_FSLModel(synMRS, **Fitargs)

    return res, amplitude
60
61
62
63
64
65
66


def test_peakcombination(data):

    res = data[0]
    amplitudes = data[1]

William Clarke's avatar
William Clarke committed
67
    res.combine([['Cr', 'PCr']])
68
69
70
71

    fittedconcs = res.getConc()
    fittedRelconcs = res.getConc(scaling='internal')

William Clarke's avatar
William Clarke committed
72
    amplitudes = np.append(amplitudes, amplitudes[0]+amplitudes[1])
73
74

    assert 'Cr+PCr' in res.metabs
William Clarke's avatar
William Clarke committed
75
76
77
78
79
    assert np.allclose(fittedconcs, amplitudes, atol=2E-1)
    assert np.allclose(fittedRelconcs,
                       amplitudes/(amplitudes[0]+amplitudes[1]),
                       atol=2E-1)

80
81
82
83
84

def test_units(data):
    res = data[0]

    # Phase
William Clarke's avatar
William Clarke committed
85
86
87
    p0, p1 = res.getPhaseParams(phi0='degrees', phi1='seconds')
    assert np.isclose(p0, 0.0, atol=1E-1)
    assert np.isclose(p1, 0.00005, atol=3E-5)
88
89
90
91

    # Shift
    shift = res.getShiftParams(units='ppm')
    shift_hz = res.getShiftParams(units='Hz')
William Clarke's avatar
William Clarke committed
92
93
    assert np.isclose(shift, 0.1, atol=1E-2)
    assert np.isclose(shift_hz, 0.1*123.0, atol=1E-1)
94
95

    # Linewidth
96
97
    lw = res.getLineShapeParams(units='Hz')[0]
    lw_ppm = res.getLineShapeParams(units='ppm')[0]
William Clarke's avatar
William Clarke committed
98
99
100
    assert np.isclose(lw, 8.0, atol=1E-1)  # 10-2
    assert np.isclose(lw_ppm, 8.0/123.0, atol=1E-1)

101
102
103

def test_qcOutput(data):
    res = data[0]
William Clarke's avatar
William Clarke committed
104
    SNR, FWHM = res.getQCParams()
105

William Clarke's avatar
William Clarke committed
106
    assert np.allclose(FWHM, 10.0, atol=1E0)
107
    assert SNR.size == 3