Skip to content

Commit 5c217fc

Browse files
authored
Merge pull request #74 from Unique-Usman/main
Added a yml file to build the docker image
2 parents 6c8a829 + 12ae83f commit 5c217fc

File tree

5 files changed

+127
-4
lines changed

5 files changed

+127
-4
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
name: Build and Run Docker
2+
on:
3+
push:
4+
branches:
5+
- main
6+
pull_request:
7+
types: [opened, synchronize, edited]
8+
branches:
9+
- main
10+
- "docker/**"
11+
jobs:
12+
build-and-run-docker:
13+
runs-on: ubuntu-latest
14+
steps:
15+
- name: Checkout code
16+
uses: actions/checkout@v4
17+
18+
- name: Set up Python
19+
uses: actions/setup-python@v4
20+
with:
21+
python-version: '3.x'
22+
23+
- name: Install Python dependencies
24+
run: |
25+
python -m pip install --upgrade pip
26+
pip install -r requirements.txt
27+
28+
- name: Generate input files
29+
run: |
30+
python -m Docker.generate_signal_docker_test
31+
32+
- name: Verify input files
33+
run: |
34+
for file in ivim_simulation.nii.gz ivim_simulation.bval ivim_simulation.bvec; do
35+
if [ ! -f "$file" ]; then
36+
echo "Error: $file not found"
37+
exit 1
38+
fi
39+
done
40+
echo "All input files generated successfully"
41+
42+
- name: Set up Docker Buildx
43+
uses: docker/setup-buildx-action@v3
44+
45+
- name: Build Docker image
46+
run: |
47+
docker build -t tf2.4_ivim-mri_codecollection -f Docker/Dockerfile .
48+
49+
- name: Run Docker container
50+
run: |
51+
docker run --rm --name TF2.4_IVIM-MRI_CodeCollection \
52+
-v ${{ github.workspace }}:/usr/src/app \
53+
-v ${{ github.workspace }}:/usr/app/output \
54+
tf2.4_ivim-mri_codecollection ivim_simulation.nii.gz ivim_simulation.bvec ivim_simulation.bval
55+
56+
- name: Verify output files
57+
run: |
58+
for file in f.nii.gz dp.nii.gz d.nii.gz; do
59+
if [ ! -f "$file" ]; then
60+
echo "Error: $file not found"
61+
exit 1
62+
fi
63+
done
64+
echo "All output files generated successfully"
65+
66+
- name: Clean up artifacts and Docker image
67+
run: |
68+
docker rmi tf2.4_ivim-mri_codecollection || true
69+
rm -f tf2.4_ivim-mri_codecollection.tar.gz
70+
rm -f ${{ github.workspace }}/f.nii.gz
71+
rm -f ${{ github.workspace }}/dp.nii.gz
72+
rm -f ${{ github.workspace }}/d.nii.gz
73+
rm -f ${{ github.workspace }}/ivim_simulation.nii.gz
74+
rm -f ${{ github.workspace }}/ivim_simulation.bval
75+
rm -f ${{ github.workspace }}/ivim_simulation.bvec
76+
- name: Cleanup Docker
77+
run: |
78+
docker system prune -a --force

Docker/generate_signal_docker_test.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import numpy as np
2+
import nibabel as nib
3+
from utilities.data_simulation.GenerateData import GenerateData
4+
from WrapImage.nifti_wrapper import save_nifti_file
5+
6+
7+
def save_bval_bvec(filename, values):
8+
if filename.endswith('.bval'):
9+
# Convert list to a space-separated string for bval
10+
values_string = ' '.join(map(str, values))
11+
elif filename.endswith('.bvec'):
12+
# Convert 2D list to a line-separated, space-separated string for bvec
13+
values_string = '\n'.join(' '.join(map(str, row)) for row in values)
14+
else:
15+
raise ValueError("Unsupported file extension. Use '.bval' or '.bvec'.")
16+
17+
with open(filename, 'w') as file:
18+
file.write(values_string)
19+
20+
# Set random seed for reproducibility
21+
np.random.seed(42)
22+
# Create GenerateData instance
23+
gd = GenerateData()
24+
25+
# Generate random input data
26+
shape = (10, 10, 5)
27+
f_in = np.random.uniform(low=0, high=1, size=shape)
28+
D_in = np.random.uniform(low=0, high=1e-3, size=shape)
29+
Dp_in = np.random.uniform(low=0, high=1e-1, size=shape)
30+
S0 = 1000 # Setting a constant S0 for simplicity
31+
bvals = np.array([0, 50, 100, 500, 1000])
32+
bvals_reshaped = np.broadcast_to(bvals, shape)
33+
34+
# Generate IVIM signal
35+
signals = gd.ivim_signal(D_in, Dp_in, f_in, S0, bvals_reshaped)
36+
37+
# Save the generated image as a NIfTI file
38+
save_nifti_file(signals, "ivim_simulation.nii.gz")
39+
# Save the bval in a file
40+
save_bval_bvec("ivim_simulation.bval", [0, 50, 100, 500, 1000])
41+
# Save the bvec value
42+
save_bval_bvec("ivim_simulation.bvec", [[1, 0, 0], [0, 1, 0], [0, 0, 1]])

WrapImage/nifti_wrapper.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@ def save_nifti_file(data, output_file, affine=None, **kwargs):
5656
For saving the 3d nifti images of the output of the algorithm
5757
"""
5858
if affine is None:
59-
affine = np.eye(data.ndim + 1)
59+
affine = np.eye(4)
60+
else:
61+
affine = np.array(affine.reshape(4, 4))
6062
output_img = nib.nifti1.Nifti1Image(data, affine , **kwargs)
6163
nib.save(output_img, output_file)
6264

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
numpy<2
2+
nibabel
23
scipy
34
torchio
45
torch

utilities/data_simulation/GenerateData.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def exponential_signal(self, D, bvalues):
5353
bvalues : list or array of float
5454
The diffusion (b-values)
5555
"""
56-
assert D >= 0, 'D must be >= 0'
56+
assert np.all(D >= 0), 'all values in D must be >= 0'
5757
return self._op.exp(-self._op.asarray(bvalues, dtype='float64') * D)
5858

5959
def multiexponential_signal(self, D, F, S0, bvalues):
@@ -116,7 +116,7 @@ def linear_signal(self, D, bvalues, offset=0):
116116
offset : float
117117
The signal offset
118118
"""
119-
assert D >= 0, 'D must be >= 0'
119+
assert np.all(D >= 0), 'every value in D must be >= 0'
120120
data = -D * np.asarray(bvalues)
121121
return data + offset
122122

@@ -144,4 +144,4 @@ def multilinear_signal(self, D, F, S0, bvalues, offset=0):
144144
signal += f * self.linear_signal(d, bvalues)
145145
signal *= S0
146146
signal += offset
147-
return signal
147+
return signal

0 commit comments

Comments
 (0)