Skip to content

Wrapper dev #77

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 23 commits into from
Jan 3, 2025
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
8 changes: 4 additions & 4 deletions WrapImage/nifti_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,10 @@ def loop_over_first_n_minus_1_dimensions(arr):
n = data.ndim
total_iteration = np.prod(data.shape[:n-1])
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):
[f_fit, Dp_fit, D_fit] = fit.osipi_fit(view, bvals)
f_image.append(f_fit)
Dp_image.append(Dp_fit)
D_image.append(D_fit)
fit_result = fit.osipi_fit(view, bvals)
f_image.append(fit_result["f"])
Dp_image.append(fit_result["D*"])
D_image.append(fit_result["D"])

# Convert lists to NumPy arrays
f_image = np.array(f_image)
Expand Down

Large diffs are not rendered by default.

165 changes: 165 additions & 0 deletions doc/wrapper_usage_example.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"# Organisation of code submissions and standardisation to a common interface\n",
"\n",
"## General structure\n",
"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",
"\n",
"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",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"## The standardized versions\n",
"The standardised versions of each submission is a class that contains two methods. These classes inherit the functionalities of `OsipiBase`.\n",
"\n",
"### `__init__()`\n",
"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",
"\n",
"Below is an example from src/standardized/IAR_LU_biexp.py"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"vscode": {
"languageId": "plaintext"
}
},
"outputs": [],
"source": [
"def __init__(self, bvalues=None, thresholds=None, bounds=None, initial_guess=None, weighting=None, stats=False):\n",
" \"\"\"\n",
" Everything this algorithm requires should be implemented here.\n",
" Number of segmentation thresholds, bounds, etc.\n",
" \n",
" Our OsipiBase object could contain functions that compare the inputs with\n",
" the requirements.\n",
" \"\"\"\n",
" 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",
"\n",
" ######## Your code below #########\n",
" \n",
" # Check the inputs\n",
" \n",
" # Initialize the algorithm\n",
" if self.bvalues is not None:\n",
" bvec = np.zeros((self.bvalues.size, 3))\n",
" bvec[:,2] = 1\n",
" gtab = gradient_table(self.bvalues, bvec, b0_threshold=0)\n",
" \n",
" self.IAR_algorithm = IvimModelBiExp(gtab)\n",
" else:\n",
" self.IAR_algorithm = None"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"### `ivim_fit()`\n",
"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",
"\n",
"Below is an example from src/standardized/IAR_LU_biexp.py"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"vscode": {
"languageId": "plaintext"
}
},
"outputs": [],
"source": [
"\n",
"def ivim_fit(self, signals, bvalues, **kwargs):\n",
" \"\"\"Perform the IVIM fit\n",
"\n",
" Args:\n",
" signals (array-like)\n",
" bvalues (array-like, optional): b-values for the signals. If None, self.bvalues will be used. Default is None.\n",
"\n",
" Returns:\n",
" _type_: _description_\n",
" \"\"\"\n",
" \n",
" if self.IAR_algorithm is None:\n",
" if bvalues is None:\n",
" bvalues = self.bvalues\n",
" else:\n",
" bvalues = np.asarray(bvalues)\n",
" \n",
" bvec = np.zeros((bvalues.size, 3))\n",
" bvec[:,2] = 1\n",
" gtab = gradient_table(bvalues, bvec, b0_threshold=0)\n",
" \n",
" self.IAR_algorithm = IvimModelBiExp(gtab)\n",
" \n",
" fit_results = self.IAR_algorithm.fit(signals)\n",
" \n",
" results = {}\n",
" results[\"f\"] = fit_results.model_params[1]\n",
" results[\"D*\"] = fit_results.model_params[2]\n",
" results[\"D\"] = fit_results.model_params[3]\n",
" \n",
" return results"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"## The `OsipiBase` class\n",
"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",
"\n",
"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"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"## Example usage of standardized version of an algorithm\n",
"### Using the standardized version directly\n",
"The standardised versions can be used directly by\n",
"1. Importing the class\n",
"2. Initialising the object with the required parameters, e.g. `IAR_LU_biexp(bounds=[(0, 1), (0.005, 0.1), (0, 0.004)])`\n",
"3. Call `osipi_fit(signals, bvalues)` for model fitting\n",
"\n",
"### Using the `OsipiBase` class with algorithm names\n",
"Standardised versions can also be initiated using the OsipiBase.osipi_initiate_algorithm() method.\n",
"\n",
"1. Import `OsipiBase`\n",
"2. Initiate `OsipiBase` with the algorithm keyword set to the standardised name of the desired algorithm e.g., `OsipiBase(algorithm=IAR_LU_biexp)`\n",
"3. Call `osipi_fit()` for model fitting"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": []
}
],
"metadata": {
"language_info": {
"name": "python"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
16 changes: 13 additions & 3 deletions src/standardized/ETP_SRI_LinearFitting.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,21 @@ def ivim_fit(self, signals, bvalues=None, linear_fit_option=False, **kwargs):
ETP_object = LinearFit()
else:
ETP_object = LinearFit(self.thresholds[0])


results = {}
if linear_fit_option:
f, Dstar = ETP_object.linear_fit(bvalues, signals)
return f, Dstar

results["f"] = f
results["D*"] = Dstar

return results
else:
f, D, Dstar = ETP_object.ivim_fit(bvalues, signals)
return f, Dstar, D

results["f"] = f
results["D*"] = Dstar
results["D"] = D

return results

41 changes: 37 additions & 4 deletions src/standardized/IAR_LU_biexp.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,41 @@ def ivim_fit(self, signals, bvalues, **kwargs):

fit_results = self.IAR_algorithm.fit(signals)

f = fit_results.model_params[1]
Dstar = fit_results.model_params[2]
D = fit_results.model_params[3]
results = {}
results["f"] = fit_results.model_params[1]
results["D*"] = fit_results.model_params[2]
results["D"] = fit_results.model_params[3]

return f, Dstar, D
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, bvec, b0_threshold=0)

self.IAR_algorithm = IvimModelBiExp(gtab)

fit_results = self.IAR_algorithm.fit(signals)

results = {}
results["f"] = fit_results.model_params[..., 1]
results["D*"] = fit_results.model_params[..., 2]
results["D"] = fit_results.model_params[..., 3]

return results
12 changes: 8 additions & 4 deletions src/standardized/IAR_LU_modified_mix.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,12 @@ def ivim_fit(self, signals, bvalues, **kwargs):

fit_results = self.IAR_algorithm.fit(signals)

f = fit_results.model_params[1]
Dstar = fit_results.model_params[2]
D = fit_results.model_params[3]
#f = fit_results.model_params[1]
#Dstar = fit_results.model_params[2]
#D = fit_results.model_params[3]
results = {}
results["f"] = fit_results.model_params[1]
results["D*"] = fit_results.model_params[2]
results["D"] = fit_results.model_params[3]

return f, Dstar, D
return results
14 changes: 10 additions & 4 deletions src/standardized/IAR_LU_modified_topopro.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,14 @@ def ivim_fit(self, signals, bvalues, **kwargs):

fit_results = self.IAR_algorithm.fit(signals)

f = fit_results.model_params[1]
Dstar = fit_results.model_params[2]
D = fit_results.model_params[3]
#f = fit_results.model_params[1]
#Dstar = fit_results.model_params[2]
#D = fit_results.model_params[3]

return f, Dstar, D
#return f, Dstar, D
results = {}
results["f"] = fit_results.model_params[1]
results["D*"] = fit_results.model_params[2]
results["D"] = fit_results.model_params[3]

return results
14 changes: 10 additions & 4 deletions src/standardized/IAR_LU_segmented_2step.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,14 @@ def ivim_fit(self, signals, bvalues, thresholds=None, **kwargs):

fit_results = self.IAR_algorithm.fit(signals)

f = fit_results.model_params[1]
Dstar = fit_results.model_params[2]
D = fit_results.model_params[3]
#f = fit_results.model_params[1]
#Dstar = fit_results.model_params[2]
#D = fit_results.model_params[3]

return f, Dstar, D
#return f, Dstar, D
results = {}
results["f"] = fit_results.model_params[1]
results["D*"] = fit_results.model_params[2]
results["D"] = fit_results.model_params[3]

return results
14 changes: 10 additions & 4 deletions src/standardized/IAR_LU_segmented_3step.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,14 @@ def ivim_fit(self, signals, bvalues, **kwargs):

fit_results = self.IAR_algorithm.fit(signals)

f = fit_results.model_params[1]
Dstar = fit_results.model_params[2]
D = fit_results.model_params[3]
#f = fit_results.model_params[1]
#Dstar = fit_results.model_params[2]
#D = fit_results.model_params[3]

return f, Dstar, D
#return f, Dstar, D
results = {}
results["f"] = fit_results.model_params[1]
results["D*"] = fit_results.model_params[2]
results["D"] = fit_results.model_params[3]

return results
14 changes: 10 additions & 4 deletions src/standardized/IAR_LU_subtracted.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,14 @@ def ivim_fit(self, signals, bvalues, **kwargs):

fit_results = self.IAR_algorithm.fit(signals)

f = fit_results.model_params[1]
Dstar = fit_results.model_params[2]
D = fit_results.model_params[3]
#f = fit_results.model_params[1]
#Dstar = fit_results.model_params[2]
#D = fit_results.model_params[3]

return f, Dstar, D
#return f, Dstar, D
results = {}
results["f"] = fit_results.model_params[1]
results["D*"] = fit_results.model_params[2]
results["D"] = fit_results.model_params[3]

return results
9 changes: 5 additions & 4 deletions src/standardized/OGC_AmsterdamUMC_Bayesian_biexp.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,9 @@ def ivim_fit(self, signals, bvalues, initial_guess=None, **kwargs):
fit_results=fit_results+(1,)
fit_results = self.OGC_algorithm(bvalues, signals, self.neg_log_prior, x0=fit_results, fitS0=self.fitS0)

D = fit_results[0]
f = fit_results[1]
Dstar = fit_results[2]
results = {}
results["D"] = fit_results[0]
results["f"] = fit_results[1]
results["D*"] = fit_results[2]

return f, Dstar, D
return results
Loading
Loading