Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/standardized/IAR_LU_biexp.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,10 @@ def ivim_fit_full_volume(self, signals, bvalues, **kwargs):
Returns:
_type_: _description_
"""

# Make sure bounds and initial guess conform to the algorithm requirements
bounds = [[self.bounds["S0"][0], self.bounds["f"][0], self.bounds["Dp"][0], self.bounds["D"][0]],
[self.bounds["S0"][1], self.bounds["f"][1], self.bounds["Dp"][1], self.bounds["D"][1]]]
initial_guess = [self.initial_guess["S0"], self.initial_guess["f"], self.initial_guess["Dp"], self.initial_guess["D"]]
if self.IAR_algorithm is None:
if bvalues is None:
bvalues = self.bvalues
Expand All @@ -125,7 +128,7 @@ def ivim_fit_full_volume(self, signals, bvalues, **kwargs):
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)
self.IAR_algorithm = IvimModelBiExp(gtab, bounds=bounds, initial_guess=initial_guess)
b0_index = np.where(bvalues == 0)[0][0]
mask = signals[...,b0_index]>0
fit_results = self.IAR_algorithm.fit(signals, mask=mask)
Expand Down
9 changes: 8 additions & 1 deletion src/standardized/IVIM_NEToptim.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,17 @@ def initialize(self, bounds, initial_guess, fitS0, traindata, SNR, n):
SNR = (5, 100)
self.training_data(self.bvalues,n=n,SNR=SNR)
self.arg=Arg()
print('note that the bounds in the network are soft bounds and implemented by a sigmoid transform. In order for the network to be sensitive over the range, we extend the bounds ny 30%')
if bounds is not None:
self.arg.net_pars.cons_min = np.array([self.bounds["D"][0], self.bounds["f"][0], self.bounds["Dp"][0], self.bounds["S0"][0]])#bounds[0] # Dt, Fp, Ds, S0
self.arg.net_pars.cons_max = np.array([self.bounds["D"][1], self.bounds["f"][1], self.bounds["Dp"][1], self.bounds["S0"][1]])#bounds[1] # Dt, Fp, Ds, S0
self.use_bounds = True
self.bounds = bounds
else:
warnings.warn('No bounds indicated. default bounds are used of self.cons_min = [0, 0, 0.005, 0] and self.cons_max = [0.005, 0.8, 0.2, 2.0] # Dt, Fp, Ds, S0')
self.bounds = {"S0" : [0, 2], "f" : [0, 0.8], "Dp" : [0.005, 0.2], "D" : [0, 0.005]} # These are defined as [lower, upper]

self.use_bounds = {"f": True, "Dp": True, "D": True}
self.use_initial_guess = {"f": False, "Dp": False, "D": False}
if traindata is None:
self.net = deep.learn_IVIM(self.train_data['data'], self.bvalues, self.arg)
else:
Expand Down
22 changes: 8 additions & 14 deletions src/standardized/TCML_TechnionIIT_SLS.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from src.wrappers.OsipiBase import OsipiBase
from super_ivim_dc.source.Classsic_ivim_fit import IVIM_fit_sls
import numpy as np

import warnings
class TCML_TechnionIIT_SLS(OsipiBase):
"""
TCML_TechnionIIT_lsqlm fitting algorithm by Angeleene Ang, Moti Freiman and Noam Korngut, TechnionIIT
Expand Down Expand Up @@ -46,23 +46,17 @@ def __init__(self, bvalues=None, thresholds=None, bounds=None, initial_guess=Non
self.initialize(bounds, fitS0,thresholds)

def initialize(self, bounds, fitS0,thresholds):
if bounds is None:
print('warning, only D* is bounded between 0.001 and 0.5)')
self.bounds = ([0.0003, 0.001, 0.009, 0],[0.008, 0.5,0.04, 3])
else:
print('warning, although bounds are given, only D* is bounded)')
#bounds=bounds
#self.bounds = bounds
self.bounds = ([self.bounds["D"][0], self.bounds["f"][0], self.bounds["Dp"][0], self.bounds["S0"][0]],
[self.bounds["D"][1], self.bounds["f"][1], self.bounds["Dp"][1], self.bounds["S0"][1]])
self.use_bounds = {"f": False, "Dp": False, "D": False}
warnings.warn('bounds are only used for initialization fit')

self.use_initial_guess = {"f": False, "Dp": False, "D": False}

self.fitS0=fitS0
self.use_bounds = True
if thresholds is None:
self.thresholds = 150
print('warning, no thresholds were defined, so default bounds are used of 150')
else:
self.thresholds = thresholds
self.use_initial_guess = False

def ivim_fit(self, signals, bvalues, **kwargs):
"""Perform the IVIM fit
Expand All @@ -76,8 +70,8 @@ def ivim_fit(self, signals, bvalues, **kwargs):
"""

bvalues=np.array(bvalues)
bounds=np.array(self.bounds)
bounds=[bounds[0][[0, 2, 1, 3]], bounds[1][[0, 2, 1, 3]]]
bounds = ([self.bounds["D"][0], self.bounds["Dp"][0], self.bounds["f"][0], self.bounds["S0"][0]],
[self.bounds["D"][1], self.bounds["Dp"][1], self.bounds["f"][1], self.bounds["S0"][1]])
signals[signals<0]=0

fit_results = self.fit_least_squares(np.array(signals)[:,np.newaxis],bvalues, bounds, min_bval_high=self.thresholds)
Expand Down
25 changes: 8 additions & 17 deletions src/standardized/TCML_TechnionIIT_lsqBOBYQA.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,23 +48,15 @@ def __init__(self, bvalues=None, thresholds=None, bounds=None, initial_guess=Non

def initialize(self, bounds, initial_guess, fitS0):
if bounds is None:
print('warning, no bounds were defined, so default bounds are used of [0, 0, 0.005, 0.7],[0.005, 1.0, 0.2, 1.3]')
self.bounds = ([0, 0, 0.005, 0.7],[0.005, 1.0, 0.2, 1.3])
self.use_bounds = {"f": False, "Dp": False, "D": False}
else:
#bounds=bounds
#self.bounds = bounds
self.bounds = ([self.bounds["D"][0], self.bounds["f"][0], self.bounds["Dp"][0], self.bounds["S0"][0]],
[self.bounds["D"][1], self.bounds["f"][1], self.bounds["Dp"][1], self.bounds["S0"][1]])
self.use_bounds = {"f": True, "Dp": True, "D": True}

if initial_guess is None:
print('warning, no initial guesses were defined, so default bounds are used of [0.001, 0.001, 0.01, 1]')
self.initial_guess = [0.001, 0.1, 0.02, 1] # D, Dp, f, S0
self.use_initial_guess = {"f": False, "Dp": False, "D": False}
else:
#self.initial_guess = initial_guess
self.initial_guess = [self.initial_guess["D"], self.initial_guess["f"], self.initial_guess["Dp"], self.initial_guess["S0"]]
self.use_initial_guess = True
self.use_initial_guess = {"f": True, "Dp": True, "D": True}
self.fitS0=fitS0
self.use_initial_guess = True
self.use_bounds = True

def ivim_fit(self, signals, bvalues, **kwargs):
"""Perform the IVIM fit
Expand All @@ -78,10 +70,9 @@ def ivim_fit(self, signals, bvalues, **kwargs):
"""

bvalues=np.array(bvalues)
bounds=np.array(self.bounds)
bounds=[bounds[0][[0, 2, 1, 3]], bounds[1][[0, 2, 1, 3]]]
initial_guess = np.array(self.initial_guess)
initial_guess = initial_guess[[0, 2, 1, 3]]
bounds = ([self.bounds["D"][0], self.bounds["Dp"][0], self.bounds["f"][0], self.bounds["S0"][0]],
[self.bounds["D"][1], self.bounds["Dp"][1], self.bounds["f"][1], self.bounds["S0"][1]])
initial_guess = [self.initial_guess["D"], self.initial_guess["Dp"], self.initial_guess["f"], self.initial_guess["S0"]]

fit_results = self.fit_least_squares(bvalues, np.array(signals)[:,np.newaxis], bounds, initial_guess.copy())

Expand Down
19 changes: 11 additions & 8 deletions src/standardized/TCML_TechnionIIT_lsq_sls_BOBYQA.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,18 +66,21 @@ def ivim_fit(self, signals, bvalues, **kwargs):
Returns:
_type_: _description_
"""
bounds = ([self.bounds["D"][0], self.bounds["f"][0], self.bounds["Dp"][0], self.bounds["S0"][0]],
[self.bounds["D"][1], self.bounds["f"][1], self.bounds["Dp"][1], self.bounds["S0"][1]])

signals[signals<0]=0
bvalues=np.array(bvalues)
bounds=np.array(bounds)
bounds=[bounds[0][[0, 2, 1, 3]], bounds[1][[0, 2, 1, 3]]]
bounds = ([self.bounds["D"][0], self.bounds["Dp"][0], self.bounds["f"][0], self.bounds["S0"][0]],
[self.bounds["D"][1], self.bounds["Dp"][1], self.bounds["f"][1], self.bounds["S0"][1]])

fit_results = self.fit_least_squares(np.array(signals)[:,np.newaxis],bvalues, bounds,min_bval_high=self.thresholds)

results = {}
results["D"] = fit_results[0]
results["f"] = fit_results[2]
results["Dp"] = fit_results[1]
if fit_results[0].size > 0:
results["D"] = fit_results[0]
results["f"] = fit_results[2]
results["Dp"] = fit_results[1]
else:
results["D"] = 0
results["f"] = 0
results["Dp"] = 0

return results
20 changes: 8 additions & 12 deletions src/standardized/TCML_TechnionIIT_lsq_sls_lm.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from src.wrappers.OsipiBase import OsipiBase
from super_ivim_dc.source.Classsic_ivim_fit import IVIM_fit_sls_lm
import numpy as np
import warnings

class TCML_TechnionIIT_lsq_sls_lm(OsipiBase):
"""
Expand Down Expand Up @@ -47,23 +48,16 @@ def __init__(self, bvalues=None, thresholds=None, bounds=None, initial_guess=Non
self.initialize(bounds, fitS0,thresholds)

def initialize(self, bounds, fitS0,thresholds):
if bounds is None:
print(
'warning, no bounds were defined, so default bounds are used of ([0.0003, 0.001, 0.009, 0],[0.008, 0.5,0.04, 3])')
self.bounds = ([0.0003, 0.001, 0.009, 0], [0.008, 0.5, 0.04, 3])
else:
#bounds = bounds
#self.bounds = bounds
self.bounds = ([self.bounds["D"][0], self.bounds["f"][0], self.bounds["Dp"][0], self.bounds["S0"][0]],
[self.bounds["D"][1], self.bounds["f"][1], self.bounds["Dp"][1], self.bounds["S0"][1]])
self.use_bounds = {"f": False, "Dp": False, "D": False}
warnings.warn('bounds are only used for initialization fit')

if thresholds is None:
self.thresholds = 150
print('warning, no thresholds were defined, so default bounds are used of 150')
else:
self.thresholds = thresholds
self.fitS0=fitS0
self.use_bounds = True
self.use_initial_guess = False
self.use_initial_guess = {"f": False, "Dp": False, "D": False}

def ivim_fit(self, signals, bvalues, **kwargs):
"""Perform the IVIM fit
Expand All @@ -77,7 +71,9 @@ def ivim_fit(self, signals, bvalues, **kwargs):
"""
signals[signals<0]=0
bvalues=np.array(bvalues)
fit_results = self.fit_least_squares(np.array(signals)[:,np.newaxis],bvalues, self.bounds, min_bval_high=self.thresholds)
bounds = ([self.bounds["D"][0], self.bounds["Dp"][0], self.bounds["f"][0], self.bounds["S0"][0]],
[self.bounds["D"][1], self.bounds["Dp"][1], self.bounds["f"][1], self.bounds["S0"][1]])
fit_results = self.fit_least_squares(np.array(signals)[:,np.newaxis],bvalues, bounds, min_bval_high=self.thresholds)

results = {}
if fit_results[0].size > 0:
Expand Down
16 changes: 6 additions & 10 deletions src/standardized/TCML_TechnionIIT_lsq_sls_trf.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,21 +48,17 @@ def __init__(self, bvalues=None, thresholds=None, bounds=None, initial_guess=Non

def initialize(self, bounds, fitS0, thresholds):
if bounds is None:
print('warning, no bounds were defined, so default bounds are used of ([0.0003, 0.001, 0.009, 0],[0.008, 1.0,0.04, 3])')
self.bounds = ([0.0003, 0.001, 0.009, 0],[0.008, 1.0,0.04, 3])
self.use_bounds = {"f": False, "Dp": False, "D": False}
else:
#bounds=bounds
#self.bounds = bounds
self.bounds = ([self.bounds["D"][0], self.bounds["f"][0], self.bounds["Dp"][0], self.bounds["S0"][0]],
[self.bounds["D"][1], self.bounds["f"][1], self.bounds["Dp"][1], self.bounds["S0"][1]])
self.use_bounds = {"f": True, "Dp": True, "D": True}
self.use_initial_guess = {"f": False, "Dp": False, "D": False}

if thresholds is None:
self.thresholds = 200
print('warning, no thresholds were defined, so default bounds are used of 200')
else:
self.thresholds = thresholds
self.fitS0=fitS0
self.use_bounds = True
self.use_initial_guess = False

def ivim_fit(self, signals, bvalues, **kwargs):
"""Perform the IVIM fit
Expand All @@ -76,8 +72,8 @@ def ivim_fit(self, signals, bvalues, **kwargs):
"""
signals[signals<0]=0
bvalues=np.array(bvalues)
bounds=np.array(self.bounds)
bounds=[bounds[0][[0, 2, 1, 3]], bounds[1][[0, 2, 1, 3]]]
bounds = ([self.bounds["D"][0], self.bounds["Dp"][0], self.bounds["f"][0], self.bounds["S0"][0]],
[self.bounds["D"][1], self.bounds["Dp"][1], self.bounds["f"][1], self.bounds["S0"][1]])
fit_results = self.fit_least_squares(np.array(signals)[:,np.newaxis],bvalues, bounds,min_bval_high=self.thresholds)

results = {}
Expand Down
14 changes: 5 additions & 9 deletions src/standardized/TCML_TechnionIIT_lsqlm.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,13 @@ def __init__(self, bvalues=None, thresholds=None, bounds=None, initial_guess=Non
self.initialize(bounds, initial_guess, fitS0)

def initialize(self, bounds, initial_guess, fitS0):
self.use_bounds = {"f": False, "Dp": False, "D": False}

if initial_guess is None:
print('warning, no initial guesses were defined, so default bounds are used of [0.001, 0.1, 0.01, 1]')
self.initial_guess = [0.001, 0.1, 0.01, 1]
self.use_initial_guess = {"f": False, "Dp": False, "D": False}
else:
#self.initial_guess = initial_guess
self.initial_guess = [self.initial_guess["D"], self.initial_guess["f"], self.initial_guess["Dp"], self.initial_guess["S0"]]
self.use_initial_guess = True
self.use_initial_guess = {"f": True, "Dp": True, "D": True}
self.fitS0=fitS0
self.use_initial_guess = True
self.use_bounds = False

def ivim_fit(self, signals, bvalues, **kwargs):
"""Perform the IVIM fit
Expand All @@ -70,8 +67,7 @@ def ivim_fit(self, signals, bvalues, **kwargs):
"""

bvalues=np.array(bvalues)
initial_guess = np.array(self.initial_guess)
initial_guess = initial_guess[[0, 2, 1, 3]]
initial_guess = [self.initial_guess["D"], self.initial_guess["Dp"], self.initial_guess["f"], self.initial_guess["S0"]]
fit_results = self.fit_least_squares(bvalues, np.array(signals)[:,np.newaxis], initial_guess)

results = {}
Expand Down
26 changes: 9 additions & 17 deletions src/standardized/TCML_TechnionIIT_lsqtrf.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,23 +48,16 @@ def __init__(self, bvalues=None, thresholds=None, bounds=None, initial_guess=Non

def initialize(self, bounds, initial_guess, fitS0):
if bounds is None:
print('warning, no bounds were defined, so default bounds are used of ([0.0003, 0.001, 0.009, 0],[0.008, 0.5,0.04, 3])')
self.bounds = ([0.0003, 0.001, 0.009, 0],[0.008, 0.5,0.04, 3])
self.use_bounds = {"f": False, "Dp": False, "D": False}
else:
#bounds=bounds
#self.bounds = bounds
self.bounds = ([self.bounds["D"][0], self.bounds["f"][0], self.bounds["Dp"][0], self.bounds["S0"][0]],
[self.bounds["D"][1], self.bounds["f"][1], self.bounds["Dp"][1], self.bounds["S0"][1]])
self.use_bounds = {"f": True, "Dp": True, "D": True}

if initial_guess is None:
print('warning, no initial guesses were defined, so default bounds are used of [0.001, 0.1, 0.01, 1]')
self.initial_guess = [0.001, 0.1, 0.01, 1] # D, Dp, f, S0
self.use_initial_guess = {"f": False, "Dp": False, "D": False}
else:
#self.initial_guess = initial_guess
self.initial_guess = [self.initial_guess["D"], self.initial_guess["f"], self.initial_guess["Dp"], self.initial_guess["S0"]]
self.use_initial_guess = True
self.use_initial_guess = {"f": True, "Dp": True, "D": True}

self.fitS0=fitS0
self.use_initial_guess = True
self.use_bounds = True

def ivim_fit(self, signals, bvalues, **kwargs):
"""Perform the IVIM fit
Expand All @@ -76,12 +69,11 @@ def ivim_fit(self, signals, bvalues, **kwargs):
Returns:
_type_: _description_
"""
bounds = ([self.bounds["D"][0], self.bounds["Dp"][0], self.bounds["f"][0], self.bounds["S0"][0]],
[self.bounds["D"][1], self.bounds["Dp"][1], self.bounds["f"][1], self.bounds["S0"][1]])
initial_guess = [self.initial_guess["D"], self.initial_guess["Dp"], self.initial_guess["f"], self.initial_guess["S0"]]

bvalues=np.array(bvalues)
bounds=np.array(self.bounds)
bounds=[bounds[0][[0, 2, 1, 3]], bounds[1][[0, 2, 1, 3]]]
initial_guess = np.array(self.initial_guess)
initial_guess = initial_guess[[0, 2, 1, 3]]
fit_results = self.fit_least_squares(bvalues, np.array(signals)[:,np.newaxis], bounds,initial_guess)

results = {}
Expand Down
4 changes: 2 additions & 2 deletions src/wrappers/OsipiBase.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def __init__(self, bvalues=None, thresholds=None, bounds=None, initial_guess=Non
self.forced_default_bounds = True

if self.initial_guess is None:
print('warning, no initial guesses were defined, so default bounds are used of [0.001, 0.001, 0.01, 1]')
print('warning, no initial guesses were defined, so default initial guesses are used of [0.001, 0.001, 0.01, 1]')
self.initial_guess = {"S0" : 1, "f" : 0.1, "Dp" : 0.01, "D" : 0.001}
self.forced_default_initial_guess = True

Expand Down Expand Up @@ -528,7 +528,7 @@ def osipi_simple_bias_and_RMSE_test(self, SNR, bvalues, f, Dstar, D, noise_reali


def D_and_Ds_swap(self,results):
if results['D']>results['Dp']:
if results['D']>results['Dp'] and results['Dp'] < 0.05:
D=results['Dp']
results['Dp']=results['D']
results['D']=D
Expand Down
Loading
Loading