@@ -36,12 +36,7 @@ def read_bval_file(bval_file):
36
36
if not os .path .exists (bval_file ):
37
37
raise FileNotFoundError (f"File '{ bval_file } ' not found." )
38
38
39
- with open (bval_file , "r" ) as f :
40
- try :
41
- bval_data = [int (val ) for val in f .read ().split ()]
42
- except ValueError as e :
43
- raise ValueError (f"Error reading bval file '{ bval_file } ': { e } " )
44
-
39
+ bval_data = np .genfromtxt (bval_file , dtype = float )
45
40
return bval_data
46
41
47
42
def read_bvec_file (bvec_file ):
@@ -51,15 +46,8 @@ def read_bvec_file(bvec_file):
51
46
if not os .path .exists (bvec_file ):
52
47
raise FileNotFoundError (f"File '{ bvec_file } ' not found." )
53
48
54
- with open (bvec_file , "r" ) as f :
55
- try :
56
- lines = f .readlines ()
57
- bvec_data = []
58
- for line in lines :
59
- bvec_data .append ([float (val ) for val in line .split ()])
60
- except ValueError as e :
61
- raise ValueError (f"Error reading bvec file '{ bvec_file } ': { e } " )
62
-
49
+ bvec_data = np .genfromtxt (bvec_file )
50
+ bvec_data = np .transpose (bvec_data ) # Transpose the array
63
51
return bvec_data
64
52
65
53
def save_nifti_file (data , output_file , affine = None , ** kwargs ):
@@ -108,18 +96,32 @@ def loop_over_first_n_minus_1_dimensions(arr):
108
96
bvals = read_bval_file (args .bval_file )
109
97
110
98
# Pass additional arguments to the algorithm
99
+
111
100
fit = OsipiBase (algorithm = args .algorithm )
112
- f_image = np .zeros_like (data .shape [:data .ndim - 1 ])
113
- D_image = np .zeros_like (data .shape [:data .ndim - 1 ])
114
- Dp_image = np .zeros_like (data .shape [:data .ndim - 1 ])
101
+ f_image = []
102
+ Dp_image = []
103
+ D_image = []
104
+
115
105
for idx , view in loop_over_first_n_minus_1_dimensions (data ):
116
106
[f_fit , Dp_fit , D_fit ] = fit .osipi_fit (view , bvals )
117
- f_image [idx ]= f_fit
118
- Dp_image [idx ]= Dp_fit
119
- D_image [idx ]= D_fit
107
+ f_image .append (f_fit )
108
+ Dp_image .append (Dp_fit )
109
+ D_image .append (D_fit )
110
+
111
+ # Convert lists to NumPy arrays
112
+ f_image = np .array (f_image )
113
+ Dp_image = np .array (Dp_image )
114
+ D_image = np .array (D_image )
115
+
116
+ # Reshape arrays if needed
117
+ f_image = f_image .reshape (data .shape [:data .ndim - 1 ])
118
+ Dp_image = Dp_image .reshape (data .shape [:data .ndim - 1 ])
119
+ D_image = D_image .reshape (data .shape [:data .ndim - 1 ])
120
+
120
121
save_nifti_file (f_image , "f.nii.gz" , args .affine )
121
- save_nifti_file (Dp_image , "dp.nii.gz" , args .affline )
122
- save_nifti_file (D_image , "d.nii.gz" , args .affline )
122
+ save_nifti_file (Dp_image , "dp.nii.gz" , args .affine )
123
+ save_nifti_file (D_image , "d.nii.gz" , args .affine )
123
124
124
125
except Exception as e :
125
126
print (f"Error: { e } " )
127
+
0 commit comments