Commit 4f543d7e authored by William Clarke's avatar William Clarke
Browse files

Change tissue fraction error limit and normalise.

parent 03c604c4
......@@ -10,6 +10,7 @@ This document contains the FSL-MRS release history in reverse chronological orde
- Dynamic fitting results can now be saved to and loaded from a directory.
- Added MH sample option to fsl_mrs, matches interactive python interface.
- Changes to the dynamic fitting results API.
- Allow tissue fractions with larger errors, but normalise. Error thrown if sum < 0.9.
1.1.8 (Tuesday 5th October 2021)
--------------------------------
......
......@@ -68,13 +68,20 @@ def test_volumefraction_calc():
assert qci.f_WM == 0.40
assert qci.f_CSF == 0.15
with pytest.warns(UserWarning):
qci.set_fractions({'GM': 0.49, 'WM': 0.49, 'CSF': 0.0})
assert qci.f_GM == 0.5
assert qci.f_WM == 0.5
assert qci.f_CSF == 0.0
with pytest.raises(ValueError) as exc_info:
qci.set_fractions({'GM': 0.44, 'WM': 0.40, 'CSF': 0.15})
qci.set_fractions({'GM': 0.44, 'WM': 0.40, 'CSF': 0.05})
assert exc_info.type is ValueError
assert exc_info.value.args[0] == "fractions must be a dict containing 'WM', 'GM', 'CSF' keys"\
", and must sum to 1. Currently they are:"\
" {'GM': 0.44, 'WM': 0.4, 'CSF': 0.15} (sum=0.9900)."
" {'GM': 0.44, 'WM': 0.4, 'CSF': 0.05} (sum=0.8900)."
def test_molefraction_calc():
......
......@@ -5,6 +5,7 @@
#
# Copyright (C) 2020 University of Oxford
# SHBASECOPYRIGHT
import warnings
import numpy as np
import matplotlib.pyplot as plt
......@@ -267,13 +268,23 @@ class QuantificationInfo(object):
:param fractions: Tissue volume fractions, must contain 'WM', 'GM', 'CSF' fields.
:type fractions: dict
:raises ValueError: [description]
:raises TypeError: [description]
:raises TypeError: Raised if fractions are not a dictionary
"""
required = ['WM', 'GM', 'CSF']
if isinstance(fractions, dict):
if all(item in fractions.keys() for item in required)\
and np.isclose(np.sum([fractions['WM'], fractions['GM'], fractions['CSF']]), 1.0, atol=1E-3):
and np.isclose(np.sum([fractions['WM'], fractions['GM'], fractions['CSF']]), 1.0, atol=1E-2):
self._fractions = fractions
elif all(item in fractions.keys() for item in required)\
and np.isclose(np.sum([fractions['WM'], fractions['GM'], fractions['CSF']]), 1.0, atol=1E-1):
original_frac = np.asarray([fractions['WM'], fractions['GM'], fractions['CSF']])
sum_value = np.sum(original_frac)
norm_vec = original_frac / sum_value
norm_fractions = {'WM': norm_vec[0], 'GM': norm_vec[1], 'CSF': norm_vec[2]}
self._fractions = norm_fractions
warnings.warn("fractions should be a dict containing 'WM', 'GM', 'CSF' keys"
f", and must sum to 1. Currently they are: {fractions} (sum={sum_value:0.4f})."
f"This fraction is off by <10% and has been normalised to: {norm_fractions}.")
else:
sum_value = np.sum([fractions['WM'], fractions['GM'], fractions['CSF']])
raise ValueError("fractions must be a dict containing 'WM', 'GM', 'CSF' keys"
......
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