Skip to content

Commit 5b6f6ae

Browse files
committed
Fixed the error in the script wrapper
1 parent ade52fd commit 5b6f6ae

File tree

1 file changed

+25
-23
lines changed

1 file changed

+25
-23
lines changed

WrapImage/nifti_wrapper.py

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,7 @@ def read_bval_file(bval_file):
3636
if not os.path.exists(bval_file):
3737
raise FileNotFoundError(f"File '{bval_file}' not found.")
3838

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)
4540
return bval_data
4641

4742
def read_bvec_file(bvec_file):
@@ -51,15 +46,8 @@ def read_bvec_file(bvec_file):
5146
if not os.path.exists(bvec_file):
5247
raise FileNotFoundError(f"File '{bvec_file}' not found.")
5348

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
6351
return bvec_data
6452

6553
def save_nifti_file(data, output_file, affine=None, **kwargs):
@@ -108,18 +96,32 @@ def loop_over_first_n_minus_1_dimensions(arr):
10896
bvals = read_bval_file(args.bval_file)
10997

11098
# Pass additional arguments to the algorithm
99+
111100
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+
115105
for idx, view in loop_over_first_n_minus_1_dimensions(data):
116106
[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+
120121
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)
123124

124125
except Exception as e:
125126
print(f"Error: {e}")
127+

0 commit comments

Comments
 (0)