Skip to content

Commit 6db42c1

Browse files
committed
Make the required change for follow up
1 parent 6c77917 commit 6db42c1

File tree

1 file changed

+10
-14
lines changed

1 file changed

+10
-14
lines changed

WrapImage/nifti_wrapper.py

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,13 @@
55
from src.wrappers.OsipiBase import OsipiBase
66
from utilities.data_simulation.GenerateData import GenerateData
77
import numpy as np
8-
import random
98

109
def read_nifti_file(input_file):
1110
"""
1211
For reading the 4d nifti image
1312
"""
1413
nifti_img = nib.load(input_file)
15-
return nifti_img.get_fdata()
14+
return nifti_img.get_fdata(), nifti_img.header
1615

1716
def read_json_file(json_file):
1817
"""
@@ -30,25 +29,28 @@ def read_json_file(json_file):
3029

3130
return json_data
3231

33-
def save_nifti_3d(data, output_file, **kwargs):
32+
def save_nifti_file(data, output_file, affine=None, **kwargs):
3433
"""
3534
For saving the 3d nifti images of the output of the algorithm
3635
"""
37-
output_img = nib.nifti1.Nifti1Image(data, np.eye(4), **kwargs)
36+
if affine is None:
37+
affine = np.eye(data.ndim + 1)
38+
output_img = nib.nifti1.Nifti1Image(data, affine , **kwargs)
3839
nib.save(output_img, output_file)
3940

4041
if __name__ == "__main__":
4142
parser = argparse.ArgumentParser(description="Read a 4D NIfTI phantom file along with BIDS JSON, b-vector, and b-value files.")
4243
parser.add_argument("input_file", type=str, help="Path to the input 4D NIfTI file.")
4344
parser.add_argument("bids_dir", type=str, help="Path to the BIDS directory.")
45+
parser.add_argument("--affine", type=float, nargs="+", help="Affine matrix for NIfTI image.")
4446
parser.add_argument("--algorithm", type=str, choices=["algorithm1", "algorithm2"], default="algorithm1", help="Select the algorithm to use.")
4547
parser.add_argument("algorithm_args", nargs=argparse.REMAINDER, help="Additional arguments for the algorithm.")
4648

4749
args = parser.parse_args()
4850

4951
try:
5052
# Read the 4D NIfTI file
51-
data = read_nifti_file(args.input_file)
53+
data, _ = read_nifti_file(args.input_file)
5254

5355
# Construct the full paths for the JSON, b-vector, and b-value files
5456
json_file = os.path.join(args.bids_dir, "dataset_description.json")
@@ -61,20 +63,14 @@ def save_nifti_3d(data, output_file, **kwargs):
6163
bvals = read_json_file(bval_file)
6264

6365
# Pass additional arguments to the algorithm
64-
rng = np.random.RandomState(42)
6566
fit = OsipiBase(algorithm=args.algorithm)
66-
S0 = 1
67-
gd = GenerateData(rng=rng)
68-
D = data["D"]
69-
f = data["f"]
70-
Dp = data["Dp"]
7167
# signal = gd.ivim_signal(D, Dp, f, S0, bvals, SNR, rician_noise)
7268

7369
# Passing the values to the selectect algorithm and saving it
7470
[f_fit, Dp_fit, D_fit] = fit.osipi_fit(signal, bvals)
75-
save_nifti_3d(f_fit, "f.nii.gz")
76-
save_nifti_3d(Dp_fit, "dp.nii.gz")
77-
save_nifti_3d(D_fit, "d.nii.gz")
71+
save_nifti_file(f_fit, "f.nii.gz", args.affine)
72+
save_nifti_file(Dp_fit, "dp.nii.gz", args.affline)
73+
save_nifti_file(D_fit, "d.nii.gz", args.affline)
7874

7975
except Exception as e:
8076
print(f"Error: {e}")

0 commit comments

Comments
 (0)