5
5
from src .wrappers .OsipiBase import OsipiBase
6
6
from utilities .data_simulation .GenerateData import GenerateData
7
7
import numpy as np
8
- import random
9
8
10
9
def read_nifti_file (input_file ):
11
10
"""
12
11
For reading the 4d nifti image
13
12
"""
14
13
nifti_img = nib .load (input_file )
15
- return nifti_img .get_fdata ()
14
+ return nifti_img .get_fdata (), nifti_img . header
16
15
17
16
def read_json_file (json_file ):
18
17
"""
@@ -30,25 +29,28 @@ def read_json_file(json_file):
30
29
31
30
return json_data
32
31
33
- def save_nifti_3d (data , output_file , ** kwargs ):
32
+ def save_nifti_file (data , output_file , affine = None , ** kwargs ):
34
33
"""
35
34
For saving the 3d nifti images of the output of the algorithm
36
35
"""
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 )
38
39
nib .save (output_img , output_file )
39
40
40
41
if __name__ == "__main__" :
41
42
parser = argparse .ArgumentParser (description = "Read a 4D NIfTI phantom file along with BIDS JSON, b-vector, and b-value files." )
42
43
parser .add_argument ("input_file" , type = str , help = "Path to the input 4D NIfTI file." )
43
44
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." )
44
46
parser .add_argument ("--algorithm" , type = str , choices = ["algorithm1" , "algorithm2" ], default = "algorithm1" , help = "Select the algorithm to use." )
45
47
parser .add_argument ("algorithm_args" , nargs = argparse .REMAINDER , help = "Additional arguments for the algorithm." )
46
48
47
49
args = parser .parse_args ()
48
50
49
51
try :
50
52
# Read the 4D NIfTI file
51
- data = read_nifti_file (args .input_file )
53
+ data , _ = read_nifti_file (args .input_file )
52
54
53
55
# Construct the full paths for the JSON, b-vector, and b-value files
54
56
json_file = os .path .join (args .bids_dir , "dataset_description.json" )
@@ -61,20 +63,14 @@ def save_nifti_3d(data, output_file, **kwargs):
61
63
bvals = read_json_file (bval_file )
62
64
63
65
# Pass additional arguments to the algorithm
64
- rng = np .random .RandomState (42 )
65
66
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" ]
71
67
# signal = gd.ivim_signal(D, Dp, f, S0, bvals, SNR, rician_noise)
72
68
73
69
# Passing the values to the selectect algorithm and saving it
74
70
[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 )
78
74
79
75
except Exception as e :
80
76
print (f"Error: { e } " )
0 commit comments