-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathIAR_LU_biexp.py
More file actions
126 lines (98 loc) · 4.62 KB
/
IAR_LU_biexp.py
File metadata and controls
126 lines (98 loc) · 4.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import numpy as np
from dipy.core.gradients import gradient_table
from src.wrappers.OsipiBase import OsipiBase
from src.original.IAR_LundUniversity.ivim_fit_method_biexp import IvimModelBiExp
class IAR_LU_biexp(OsipiBase):
"""
Bi-exponential fitting algorithm by Ivan A. Rashid, Lund University
"""
# I'm thinking that we define default attributes for each submission like this
# And in __init__, we can call the OsipiBase control functions to check whether
# the user inputs fulfil the requirements
# Some basic stuff that identifies the algorithm
id_author = "Ivan A. Rashid, LU"
id_algorithm_type = "Bi-exponential fit"
id_return_parameters = "f, D*, D"
id_units = "seconds per milli metre squared or milliseconds per micro metre squared"
# Algorithm requirements
required_bvalues = 4
required_thresholds = [0,0] # Interval from "at least" to "at most", in case submissions allow a custom number of thresholds
required_bounds = False
required_bounds_optional = True # Bounds may not be required but are optional
required_initial_guess = False
required_initial_guess_optional = True
# Supported inputs in the standardized class
supported_bounds = True
supported_initial_guess = True
supported_thresholds = False
supported_dimensions = 1
supported_priors = False
def __init__(self, bvalues=None, thresholds=None, bounds=None, initial_guess=None, weighting=None, stats=False):
"""
Everything this algorithm requires should be implemented here.
Number of segmentation thresholds, bounds, etc.
Our OsipiBase object could contain functions that compare the inputs with
the requirements.
"""
super(IAR_LU_biexp, self).__init__(bvalues, thresholds, bounds, initial_guess)
if bounds is not None:
print('warning, bounds from wrapper are not (yet) used in this algorithm')
self.use_bounds = False
self.use_initial_guess = False
# Initialize the algorithm
if self.bvalues is not None:
bvec = np.zeros((self.bvalues.size, 3))
bvec[:,2] = 1
gtab = gradient_table(self.bvalues, bvec, b0_threshold=0)
self.IAR_algorithm = IvimModelBiExp(gtab, bounds=self.bounds, initial_guess=self.initial_guess)
else:
self.IAR_algorithm = None
def ivim_fit(self, signals, bvalues, **kwargs):
"""Perform the IVIM fit
Args:
signals (array-like)
bvalues (array-like, optional): b-values for the signals. If None, self.bvalues will be used. Default is None.
Returns:
_type_: _description_
"""
if self.IAR_algorithm is None:
if bvalues is None:
bvalues = self.bvalues
else:
bvalues = np.asarray(bvalues)
bvec = np.zeros((bvalues.size, 3))
bvec[:,2] = 1
gtab = gradient_table(bvalues, bvecs=bvec, b0_threshold=0)
self.IAR_algorithm = IvimModelBiExp(gtab, bounds=self.bounds, initial_guess=self.initial_guess)
fit_results = self.IAR_algorithm.fit(signals)
results = {}
results["f"] = fit_results.model_params[1]
results["Dp"] = fit_results.model_params[2]
results["D"] = fit_results.model_params[3]
results = self.D_and_Ds_swap(results)
return results
def ivim_fit_full_volume(self, signals, bvalues, **kwargs):
"""Perform the IVIM fit
Args:
signals (array-like)
bvalues (array-like, optional): b-values for the signals. If None, self.bvalues will be used. Default is None.
Returns:
_type_: _description_
"""
if self.IAR_algorithm is None:
if bvalues is None:
bvalues = self.bvalues
else:
bvalues = np.asarray(bvalues)
bvec = np.zeros((bvalues.size, 3))
bvec[:,2] = 1
gtab = gradient_table(bvalues, bvecs=bvec, b0_threshold=0)
self.IAR_algorithm = IvimModelBiExp(gtab, bounds=self.bounds, initial_guess=self.initial_guess)
b0_index = np.where(bvalues == 0)[0][0]
mask = signals[...,b0_index]>0
fit_results = self.IAR_algorithm.fit(signals, mask=mask)
results = {}
results["f"] = fit_results.model_params[..., 1]
results["Dp"] = fit_results.model_params[..., 2]
results["D"] = fit_results.model_params[..., 3]
return results