Skip to content

Commit

Permalink
Adds catch for fitting data without b0 measurements. (#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
rutgerfick committed Apr 27, 2019
1 parent db023da commit cf885b2
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
19 changes: 18 additions & 1 deletion dmipy/core/modeling_framework.py
Original file line number Diff line number Diff line change
Expand Up @@ -813,7 +813,7 @@ def _check_tissue_model_acquisition_scheme(self, acquisition_scheme):
Parameters
----------
acquisition_scheme : DmipyAcquisitionScheme instance,
An acquisition scheme that has been instantiated using dMipy.
An acquisition scheme that has been instantiated using Dmipy.
"""
for model in self.models:
if model._model_type == 'TissueResponseModel':
Expand All @@ -835,6 +835,20 @@ def _check_tissue_model_acquisition_scheme(self, acquisition_scheme):
msg += "model are not the same."
raise ValueError(msg)

def _check_acquisition_scheme_has_b0s(self, acquisition_scheme):
"""
Checks if acquisition scheme has any b0-measurements. This for the
moment is a prerequisite for signal-attenuation-based model fitting.
Parameters
----------
acquisition_scheme : DmipyAcquisitionScheme instance,
An acquisition scheme that has been instantiated using Dmipy.
"""
if not np.any(acquisition_scheme.b0_mask):
raise ValueError('acquisition scheme must have b0-measurements '
'for signal-attenuation-based model fitting.')

def _construct_convolution_kernel(self, **kwargs):
"""
Helper function that constructs the convolution kernel for the given
Expand Down Expand Up @@ -1039,6 +1053,7 @@ def fit(self, acquisition_scheme, data,
"""
self._check_tissue_model_acquisition_scheme(acquisition_scheme)
self._check_model_params_with_acquisition_params(acquisition_scheme)
self._check_acquisition_scheme_has_b0s(acquisition_scheme)

# estimate S0
self.scheme = acquisition_scheme
Expand Down Expand Up @@ -1399,6 +1414,7 @@ def fit(self, acquisition_scheme, data,
"""
self._check_tissue_model_acquisition_scheme(acquisition_scheme)
self._check_model_params_with_acquisition_params(acquisition_scheme)
self._check_acquisition_scheme_has_b0s(acquisition_scheme)

# estimate S0
self.scheme = acquisition_scheme
Expand Down Expand Up @@ -1812,6 +1828,7 @@ def fit(self, acquisition_scheme, data, mask=None, solver='csd',
"""
self._check_if_kernel_parameters_are_fixed()
self._check_tissue_model_acquisition_scheme(acquisition_scheme)
self._check_acquisition_scheme_has_b0s(acquisition_scheme)
self._check_model_params_with_acquisition_params(acquisition_scheme)

self.voxel_varying_kernel = False
Expand Down
13 changes: 13 additions & 0 deletions dmipy/core/tests/test_raises_multicompartment_input.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
from dmipy.core import modeling_framework
from dmipy.core.acquisition_scheme import (
acquisition_scheme_from_bvalues)
from dmipy.signal_models import (
cylinder_models, plane_models, gaussian_models)
from numpy.testing import assert_raises
from dmipy.data.saved_acquisition_schemes import (
wu_minn_hcp_acquisition_scheme)
import numpy as np


def test_raise_combination_NRM_and_others():
Expand Down Expand Up @@ -64,3 +67,13 @@ def test_set_fixed_parameter_raises():
'C1Stick_1_mu', [1])
assert_raises(ValueError, mod.set_fixed_parameter,
'blabla', [1])


def test_fitting_without_b0_raises():
bvals = np.atleast_1d(1e9)
bvecs = np.atleast_2d([1., 0., 0.])
scheme = acquisition_scheme_from_bvalues(bvals, bvecs)
mc = modeling_framework.MultiCompartmentModel(
[gaussian_models.G1Ball()])
data = np.atleast_1d(1.)
assert_raises(ValueError, mc.fit, scheme, data)

0 comments on commit cf885b2

Please sign in to comment.