Skip to content

Commit c679a0f

Browse files
Merge pull request #77 from OSIPI/wrapper_dev
Wrapper dev
2 parents 5c217fc + 8d57500 commit c679a0f

20 files changed

+481
-190
lines changed

WrapImage/nifti_wrapper.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,10 @@ def loop_over_first_n_minus_1_dimensions(arr):
109109
n = data.ndim
110110
total_iteration = np.prod(data.shape[:n-1])
111111
for idx, view in tqdm(loop_over_first_n_minus_1_dimensions(data), desc=f"{args.algorithm} is fitting", dynamic_ncols=True, total=total_iteration):
112-
[f_fit, Dp_fit, D_fit] = fit.osipi_fit(view, bvals)
113-
f_image.append(f_fit)
114-
Dp_image.append(Dp_fit)
115-
D_image.append(D_fit)
112+
fit_result = fit.osipi_fit(view, bvals)
113+
f_image.append(fit_result["f"])
114+
Dp_image.append(fit_result["D*"])
115+
D_image.append(fit_result["D"])
116116

117117
# Convert lists to NumPy arrays
118118
f_image = np.array(f_image)

doc/Introduction_to_TF24_IVIM-MRI_CodeCollection_github_and_IVIM_Analysis_using_Python.ipynb

Lines changed: 88 additions & 113 deletions
Large diffs are not rendered by default.

doc/wrapper_usage_example.ipynb

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"\n",
8+
"# Organisation of code submissions and standardisation to a common interface\n",
9+
"\n",
10+
"## General structure\n",
11+
"Code submissions are located in the src/original folder, where submissions are named as `<initials>_<institution>`. Due to code submissions having different authors, it is expected that they all vary in their usage, inputs, and outputs. In order to facilitate testing in a larger scale, a common interface has been created in the form of the `OsipiBase` class (src/wrappers). This class acts as a parent class for standardised versions of the different code submissions. Together, they create the common interface of function calls and function outputs that allows us to perform mass testing, but also creates easy usage.\n",
12+
"\n",
13+
"The src/standardized folder contains the standardised version of each code submission. Here, a class is created following a naming convention (`<initials>_<institution>_<algorithm name>`), with `__init__()` and `ivim_fit()` methods that integrate well with the OsipiBase class. The idea is that every submitted fitting algorithm should be initialised in the same way, and executed in the same way.\n",
14+
"\n"
15+
]
16+
},
17+
{
18+
"cell_type": "markdown",
19+
"metadata": {},
20+
"source": [
21+
"\n",
22+
"## The standardized versions\n",
23+
"The standardised versions of each submission is a class that contains two methods. These classes inherit the functionalities of `OsipiBase`.\n",
24+
"\n",
25+
"### `__init__()`\n",
26+
"The `__init__()` method ensures that the algorithm is initiated correctly in accordance with OsipiBase. Custom code is to be inserted below the `super()` call. This method should contain any of the neccessary steps for the following `ivim_fit()` method to only require signals and b-values as input.\n",
27+
"\n",
28+
"Below is an example from src/standardized/IAR_LU_biexp.py"
29+
]
30+
},
31+
{
32+
"cell_type": "code",
33+
"execution_count": null,
34+
"metadata": {
35+
"vscode": {
36+
"languageId": "plaintext"
37+
}
38+
},
39+
"outputs": [],
40+
"source": [
41+
"def __init__(self, bvalues=None, thresholds=None, bounds=None, initial_guess=None, weighting=None, stats=False):\n",
42+
" \"\"\"\n",
43+
" Everything this algorithm requires should be implemented here.\n",
44+
" Number of segmentation thresholds, bounds, etc.\n",
45+
" \n",
46+
" Our OsipiBase object could contain functions that compare the inputs with\n",
47+
" the requirements.\n",
48+
" \"\"\"\n",
49+
" super(IAR_LU_biexp, self).__init__(bvalues, thresholds, bounds, initial_guess) ######## On this line, change \"IAR_LU_biexp\" to the name of the class\n",
50+
"\n",
51+
" ######## Your code below #########\n",
52+
" \n",
53+
" # Check the inputs\n",
54+
" \n",
55+
" # Initialize the algorithm\n",
56+
" if self.bvalues is not None:\n",
57+
" bvec = np.zeros((self.bvalues.size, 3))\n",
58+
" bvec[:,2] = 1\n",
59+
" gtab = gradient_table(self.bvalues, bvec, b0_threshold=0)\n",
60+
" \n",
61+
" self.IAR_algorithm = IvimModelBiExp(gtab)\n",
62+
" else:\n",
63+
" self.IAR_algorithm = None"
64+
]
65+
},
66+
{
67+
"cell_type": "markdown",
68+
"metadata": {},
69+
"source": [
70+
"\n",
71+
"### `ivim_fit()`\n",
72+
"The purpose of this method is to take a singe voxel signal and b-values as input, and return IVIM parameters as output. This is where most of the custom code will go that is related to each individual code submission. The idea here is to have calls to submitted functions in the src/originals folder. This ensures that the original code is not tampered with. However if required, the original code could be just pasted in here as well.\n",
73+
"\n",
74+
"Below is an example from src/standardized/IAR_LU_biexp.py"
75+
]
76+
},
77+
{
78+
"cell_type": "code",
79+
"execution_count": null,
80+
"metadata": {
81+
"vscode": {
82+
"languageId": "plaintext"
83+
}
84+
},
85+
"outputs": [],
86+
"source": [
87+
"\n",
88+
"def ivim_fit(self, signals, bvalues, **kwargs):\n",
89+
" \"\"\"Perform the IVIM fit\n",
90+
"\n",
91+
" Args:\n",
92+
" signals (array-like)\n",
93+
" bvalues (array-like, optional): b-values for the signals. If None, self.bvalues will be used. Default is None.\n",
94+
"\n",
95+
" Returns:\n",
96+
" _type_: _description_\n",
97+
" \"\"\"\n",
98+
" \n",
99+
" if self.IAR_algorithm is None:\n",
100+
" if bvalues is None:\n",
101+
" bvalues = self.bvalues\n",
102+
" else:\n",
103+
" bvalues = np.asarray(bvalues)\n",
104+
" \n",
105+
" bvec = np.zeros((bvalues.size, 3))\n",
106+
" bvec[:,2] = 1\n",
107+
" gtab = gradient_table(bvalues, bvec, b0_threshold=0)\n",
108+
" \n",
109+
" self.IAR_algorithm = IvimModelBiExp(gtab)\n",
110+
" \n",
111+
" fit_results = self.IAR_algorithm.fit(signals)\n",
112+
" \n",
113+
" results = {}\n",
114+
" results[\"f\"] = fit_results.model_params[1]\n",
115+
" results[\"D*\"] = fit_results.model_params[2]\n",
116+
" results[\"D\"] = fit_results.model_params[3]\n",
117+
" \n",
118+
" return results"
119+
]
120+
},
121+
{
122+
"cell_type": "markdown",
123+
"metadata": {},
124+
"source": [
125+
"\n",
126+
"## The `OsipiBase` class\n",
127+
"The usage of the OsipiBase class mainly consists of running the osipi_fit() method. In this method, the inputs from `__init__()` of the standardised version of a code submission, and the signals and b-values input to `osipi_fit()` is processed and fed into the `ivim_fit()` function.\n",
128+
"\n",
129+
"It is the `osipi_fit()` method that provides the common interface for model fitting. As one may note, `ivim_fit()` takes a single voxel as input. `OsipiBase.osipi_fit()` supports multidimensional inputs, which is then iteratively fed into `ivim_fit()`, and returns a corresponding output. Support for future types of input will be implemented here. This ensures that the `ivim_fit()` method can be written as simply as possible, which simplifies the inclusion of new code submissions into the standard interface.\n"
130+
]
131+
},
132+
{
133+
"cell_type": "markdown",
134+
"metadata": {},
135+
"source": [
136+
"\n",
137+
"## Example usage of standardized version of an algorithm\n",
138+
"### Using the standardized version directly\n",
139+
"The standardised versions can be used directly by\n",
140+
"1. Importing the class\n",
141+
"2. Initialising the object with the required parameters, e.g. `IAR_LU_biexp(bounds=[(0, 1), (0.005, 0.1), (0, 0.004)])`\n",
142+
"3. Call `osipi_fit(signals, bvalues)` for model fitting\n",
143+
"\n",
144+
"### Using the `OsipiBase` class with algorithm names\n",
145+
"Standardised versions can also be initiated using the OsipiBase.osipi_initiate_algorithm() method.\n",
146+
"\n",
147+
"1. Import `OsipiBase`\n",
148+
"2. Initiate `OsipiBase` with the algorithm keyword set to the standardised name of the desired algorithm e.g., `OsipiBase(algorithm=IAR_LU_biexp)`\n",
149+
"3. Call `osipi_fit()` for model fitting"
150+
]
151+
},
152+
{
153+
"cell_type": "markdown",
154+
"metadata": {},
155+
"source": []
156+
}
157+
],
158+
"metadata": {
159+
"language_info": {
160+
"name": "python"
161+
}
162+
},
163+
"nbformat": 4,
164+
"nbformat_minor": 2
165+
}

src/standardized/ETP_SRI_LinearFitting.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,21 @@ def ivim_fit(self, signals, bvalues=None, linear_fit_option=False, **kwargs):
6464
ETP_object = LinearFit()
6565
else:
6666
ETP_object = LinearFit(self.thresholds[0])
67-
67+
68+
results = {}
6869
if linear_fit_option:
6970
f, Dstar = ETP_object.linear_fit(bvalues, signals)
70-
return f, Dstar
71+
72+
results["f"] = f
73+
results["D*"] = Dstar
74+
75+
return results
7176
else:
7277
f, D, Dstar = ETP_object.ivim_fit(bvalues, signals)
73-
return f, Dstar, D
78+
79+
results["f"] = f
80+
results["D*"] = Dstar
81+
results["D"] = D
82+
83+
return results
7484

src/standardized/IAR_LU_biexp.py

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,41 @@ def ivim_fit(self, signals, bvalues, **kwargs):
7676

7777
fit_results = self.IAR_algorithm.fit(signals)
7878

79-
f = fit_results.model_params[1]
80-
Dstar = fit_results.model_params[2]
81-
D = fit_results.model_params[3]
79+
results = {}
80+
results["f"] = fit_results.model_params[1]
81+
results["D*"] = fit_results.model_params[2]
82+
results["D"] = fit_results.model_params[3]
8283

83-
return f, Dstar, D
84+
return results
85+
86+
def ivim_fit_full_volume(self, signals, bvalues, **kwargs):
87+
"""Perform the IVIM fit
88+
89+
Args:
90+
signals (array-like)
91+
bvalues (array-like, optional): b-values for the signals. If None, self.bvalues will be used. Default is None.
92+
93+
Returns:
94+
_type_: _description_
95+
"""
96+
97+
if self.IAR_algorithm is None:
98+
if bvalues is None:
99+
bvalues = self.bvalues
100+
else:
101+
bvalues = np.asarray(bvalues)
102+
103+
bvec = np.zeros((bvalues.size, 3))
104+
bvec[:,2] = 1
105+
gtab = gradient_table(bvalues, bvec, b0_threshold=0)
106+
107+
self.IAR_algorithm = IvimModelBiExp(gtab)
108+
109+
fit_results = self.IAR_algorithm.fit(signals)
110+
111+
results = {}
112+
results["f"] = fit_results.model_params[..., 1]
113+
results["D*"] = fit_results.model_params[..., 2]
114+
results["D"] = fit_results.model_params[..., 3]
115+
116+
return results

src/standardized/IAR_LU_modified_mix.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,12 @@ def ivim_fit(self, signals, bvalues, **kwargs):
7676

7777
fit_results = self.IAR_algorithm.fit(signals)
7878

79-
f = fit_results.model_params[1]
80-
Dstar = fit_results.model_params[2]
81-
D = fit_results.model_params[3]
79+
#f = fit_results.model_params[1]
80+
#Dstar = fit_results.model_params[2]
81+
#D = fit_results.model_params[3]
82+
results = {}
83+
results["f"] = fit_results.model_params[1]
84+
results["D*"] = fit_results.model_params[2]
85+
results["D"] = fit_results.model_params[3]
8286

83-
return f, Dstar, D
87+
return results

src/standardized/IAR_LU_modified_topopro.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,14 @@ def ivim_fit(self, signals, bvalues, **kwargs):
7676

7777
fit_results = self.IAR_algorithm.fit(signals)
7878

79-
f = fit_results.model_params[1]
80-
Dstar = fit_results.model_params[2]
81-
D = fit_results.model_params[3]
79+
#f = fit_results.model_params[1]
80+
#Dstar = fit_results.model_params[2]
81+
#D = fit_results.model_params[3]
8282

83-
return f, Dstar, D
83+
#return f, Dstar, D
84+
results = {}
85+
results["f"] = fit_results.model_params[1]
86+
results["D*"] = fit_results.model_params[2]
87+
results["D"] = fit_results.model_params[3]
88+
89+
return results

src/standardized/IAR_LU_segmented_2step.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,14 @@ def ivim_fit(self, signals, bvalues, thresholds=None, **kwargs):
7777

7878
fit_results = self.IAR_algorithm.fit(signals)
7979

80-
f = fit_results.model_params[1]
81-
Dstar = fit_results.model_params[2]
82-
D = fit_results.model_params[3]
80+
#f = fit_results.model_params[1]
81+
#Dstar = fit_results.model_params[2]
82+
#D = fit_results.model_params[3]
8383

84-
return f, Dstar, D
84+
#return f, Dstar, D
85+
results = {}
86+
results["f"] = fit_results.model_params[1]
87+
results["D*"] = fit_results.model_params[2]
88+
results["D"] = fit_results.model_params[3]
89+
90+
return results

src/standardized/IAR_LU_segmented_3step.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,14 @@ def ivim_fit(self, signals, bvalues, **kwargs):
7676

7777
fit_results = self.IAR_algorithm.fit(signals)
7878

79-
f = fit_results.model_params[1]
80-
Dstar = fit_results.model_params[2]
81-
D = fit_results.model_params[3]
79+
#f = fit_results.model_params[1]
80+
#Dstar = fit_results.model_params[2]
81+
#D = fit_results.model_params[3]
8282

83-
return f, Dstar, D
83+
#return f, Dstar, D
84+
results = {}
85+
results["f"] = fit_results.model_params[1]
86+
results["D*"] = fit_results.model_params[2]
87+
results["D"] = fit_results.model_params[3]
88+
89+
return results

src/standardized/IAR_LU_subtracted.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,14 @@ def ivim_fit(self, signals, bvalues, **kwargs):
7676

7777
fit_results = self.IAR_algorithm.fit(signals)
7878

79-
f = fit_results.model_params[1]
80-
Dstar = fit_results.model_params[2]
81-
D = fit_results.model_params[3]
79+
#f = fit_results.model_params[1]
80+
#Dstar = fit_results.model_params[2]
81+
#D = fit_results.model_params[3]
8282

83-
return f, Dstar, D
83+
#return f, Dstar, D
84+
results = {}
85+
results["f"] = fit_results.model_params[1]
86+
results["D*"] = fit_results.model_params[2]
87+
results["D"] = fit_results.model_params[3]
88+
89+
return results

src/standardized/OGC_AmsterdamUMC_Bayesian_biexp.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,9 @@ def ivim_fit(self, signals, bvalues, initial_guess=None, **kwargs):
7878
fit_results=fit_results+(1,)
7979
fit_results = self.OGC_algorithm(bvalues, signals, self.neg_log_prior, x0=fit_results, fitS0=self.fitS0)
8080

81-
D = fit_results[0]
82-
f = fit_results[1]
83-
Dstar = fit_results[2]
81+
results = {}
82+
results["D"] = fit_results[0]
83+
results["f"] = fit_results[1]
84+
results["D*"] = fit_results[2]
8485

85-
return f, Dstar, D
86+
return results

0 commit comments

Comments
 (0)