Skip to content

Commit 4e1414b

Browse files
committed
Merge branch 'main' into sampling/initial
2 parents 68a9d33 + 6e6a9a0 commit 4e1414b

39 files changed

+1166
-848
lines changed

.github/workflows/analysis.yml

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,20 +70,21 @@ jobs:
7070
run: |
7171
python -m pytest -m slow --selectAlgorithm ${{ matrix.algorithm }} --saveFileName test_output_${{ matrix.algorithm }}_${{ matrix.SNR }}.csv --SNR ${{ matrix.SNR }} --fitCount 300 --saveDurationFileName test_duration_${{ matrix.algorithm }}_${{ matrix.SNR }}.csv
7272
- name: Upload raw data
73-
uses: actions/upload-artifact@v3
73+
uses: actions/upload-artifact@v4
7474
with:
7575
name: Working_Data
7676
retention-days: 1
7777
path: |
7878
test_output_${{ matrix.algorithm }}_${{ matrix.SNR }}.csv
7979
test_duration_${{ matrix.algorithm }}_${{ matrix.SNR }}.csv
80+
overwrite: true
8081

8182
merge:
8283
runs-on: ubuntu-latest
8384
needs: build
8485
steps:
8586
- name: Download artifacts
86-
uses: actions/download-artifact@v3
87+
uses: actions/download-artifact@v4
8788
with:
8889
path: artifacts
8990
- name: Merge fitting results
@@ -95,12 +96,13 @@ jobs:
9596
head -n 1 $(ls artifacts/Working_Data/test_duration_*.csv | head -n 1) > test_duration.csv
9697
tail -q -n +2 artifacts/Working_Data/test_duration_*.csv >> test_duration.csv
9798
- name: Upload merged artifacts
98-
uses: actions/upload-artifact@v3
99+
uses: actions/upload-artifact@v4
99100
with:
100101
name: Data
101102
path: |
102103
test_output.csv
103104
test_duration.csv
105+
overwrite: true
104106

105107
analyze:
106108
runs-on: ubuntu-latest
@@ -121,13 +123,13 @@ jobs:
121123
any::data.table
122124
any::ggplot2
123125
- name: Download artifacts
124-
uses: actions/download-artifact@v3
126+
uses: actions/download-artifact@v4
125127
with:
126128
name: Data
127129
- name: Generate figures
128130
run: Rscript --vanilla tests/IVIMmodels/unit_tests/analyze.r test_output.csv test_duration.csv
129131
- name: Upload figures
130-
uses: actions/upload-artifact@v3
132+
uses: actions/upload-artifact@v4
131133
if: always()
132134
with:
133135
name: Figures
@@ -141,6 +143,8 @@ jobs:
141143
durations.pdf
142144
curve_plot.pdf
143145
fitted_curves.pdf
146+
overwrite: true
147+
144148

145149
compare:
146150
runs-on: ubuntu-latest
@@ -158,16 +162,17 @@ jobs:
158162
any::tidyverse
159163
any::assertr
160164
- name: Download artifacts
161-
uses: actions/download-artifact@v3
165+
uses: actions/download-artifact@v4
162166
with:
163167
name: Data
164168
- name: Test against previous results
165169
run: Rscript --vanilla tests/IVIMmodels/unit_tests/compare.r test_output.csv test_reference.csv tests/IVIMmodels/unit_tests/reference_output.csv test_results.csv
166170
- name: Upload data
167-
uses: actions/upload-artifact@v3
171+
uses: actions/upload-artifact@v4
168172
if: always()
169173
with:
170174
name: Comparison
171175
path: |
172176
test_reference.csv
173177
test_results.csv
178+
overwrite: true

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["Dp"])
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+
}

phantoms/MR_XCAT_qMRI/sim_ivim_sig.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -459,9 +459,9 @@ def parse_bvalues_file(file_path):
459459
signals = np.squeeze(voxels[int(voxels.shape[0] * voxel_selector_fraction)]).tolist()
460460
generic_data[name] = {
461461
'noise': noise,
462-
'D': np.mean(Dim[selector], axis=0),
463-
'f': np.mean(fim[selector], axis=0),
464-
'Dp': np.mean(Dpim[selector], axis=0),
462+
'D': np.median(Dim[selector], axis=0),
463+
'f': np.median(fim[selector], axis=0),
464+
'Dp': np.median(Dpim[selector], axis=0),
465465
'data': signals
466466
}
467467
generic_data['config'] = {

src/original/IAR_LundUniversity/ivim_fit_method_biexp.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,13 @@ def ivim_model(self, b, S0, f, D_star, D):
6161

6262
def set_bounds(self, bounds):
6363
# Use this function for fits that uses curve_fit
64-
if bounds == None:
64+
if bounds is None:
6565
self.bounds = np.array([(0, 0, 0.005, 0), (np.inf, 1, 0.1, 0.004)])
6666
else:
67-
self.bounds = np.array([(0, *bounds[0]), (np.inf, *bounds[1])])
67+
self.bounds = np.array([bounds[0], bounds[1]])
6868

6969
def set_initial_guess(self, initial_guess):
70-
if initial_guess == None:
70+
if initial_guess is None:
7171
self.initial_guess = (1, 0.2, 0.03, 0.001)
7272
else:
7373
self.initial_guess = initial_guess

src/original/IAR_LundUniversity/ivim_fit_method_linear.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,13 @@ def sivim_model(self, b, S0, f, D):
8383

8484
def set_bounds(self, bounds):
8585
# Use this function for fits that uses curve_fit
86-
if bounds == None:
86+
if bounds is None:
8787
self.bounds = np.array([(0, 0, 0), (np.inf, 1, 0.004)])
8888
else:
89-
self.bounds = np.array([(0, *bounds[0]), (np.inf, *bounds[1])])
89+
self.bounds = np.array([bounds[0], bounds[1]])
9090

9191
def set_initial_guess(self, initial_guess):
92-
if initial_guess == None:
92+
if initial_guess is None:
9393
self.initial_guess = (1, 0.2, 0.001)
9494
else:
9595
self.initial_guess = initial_guess

src/original/IAR_LundUniversity/ivim_fit_method_modified_mix.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def __init__(self, gtab, bounds=None, maxiter=10, xtol=1e-8, rescale_units=False
7878
if gtab.bvals[-1] >= 10:
7979
self.bvals = gtab.bvals/1000
8080

81-
if bounds == None:
81+
if bounds is None:
8282
# Bounds expressed as (lower bound, upper bound) for [f, D*, D].
8383
self.bounds = np.array([(0, 1), (5, 100), (0, 4)])
8484
elif (bounds[0][1] <= 1) or rescale_units: # Realistically, if mm2/s units are used, D* bound is <= 1

src/original/IAR_LundUniversity/ivim_fit_method_modified_topopro.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ def __init__(self, gtab, bounds=[[0, 0.005, 1e-5], [1, 0.1, 0.004]], \
7676
# and are thus not changed.
7777
if gtab.bvals[-1] >= 10:
7878
self.bvals = gtab.bvals/1000
79-
if bounds == None:
79+
if bounds is None:
8080
# Bounds expressed as (lower bound, upper bound) for [f, D*, D].
8181
self.bounds = np.array([(0, 1), (5, 100), (0, 4)])
8282
elif (bounds[0][1] <= 1) or rescale_units: # Realistically, if mm2/s units are used, D* bound is <= 1

src/original/IAR_LundUniversity/ivim_fit_method_segmented_2step.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,13 +86,13 @@ def ivim_signal(self, b, S0, f, D_star, D):
8686

8787
def set_bounds(self, bounds):
8888
# Use this function for fits that uses curve_fit
89-
if bounds == None:
89+
if bounds is None:
9090
self.bounds = np.array([(0, 0, 0.005, 0), (np.inf, 1, 0.1, 0.004)])
9191
else:
92-
self.bounds = np.array([(0, *bounds[0]), (np.inf, *bounds[1])])
92+
self.bounds = np.array([bounds[0], bounds[1]])
9393

9494
def set_initial_guess(self, initial_guess):
95-
if initial_guess == None:
95+
if initial_guess is None:
9696
self.initial_guess = (1, 0.2, 0.03, 0.001)
9797
else:
9898
self.initial_guess = initial_guess

0 commit comments

Comments
 (0)