Skip to content

Commit 138573f

Browse files
committed
ENH: Add decimal places for bval/bvec serialization
Add decimal places parameter for bval/bvec serialization. Provide some sensible default values. Change the default precision for b-values from 6 to 2.
1 parent 0118b19 commit 138573f

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

src/nifreeze/data/dmri.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,13 @@ def to_filename(
208208
with h5py.File(filename, "r+") as out_file:
209209
out_file.attrs["Type"] = "dmri"
210210

211-
def to_nifti(self, filename: Path | str, insert_b0: bool = False) -> None:
211+
def to_nifti(
212+
self,
213+
filename: Path | str,
214+
insert_b0: bool = False,
215+
bvals_dec_places: int = 2,
216+
bvecs_dec_places: int = 6,
217+
) -> None:
212218
"""
213219
Write a NIfTI file to disk.
214220
@@ -218,6 +224,10 @@ def to_nifti(self, filename: Path | str, insert_b0: bool = False) -> None:
218224
The output NIfTI file path.
219225
insert_b0 : :obj:`bool`, optional
220226
Insert a :math:`b=0` at the front of the output NIfTI.
227+
bvals_dec_places : :obj:`int`, optional
228+
Decimal places to use when serializing b-values.
229+
bvecs_dec_places : :obj:`int`, optional
230+
Decimal places to use when serializing b-vectors.
221231
222232
"""
223233
if not insert_b0:
@@ -242,8 +252,8 @@ def to_nifti(self, filename: Path | str, insert_b0: bool = False) -> None:
242252

243253
# Save bvecs and bvals to text files
244254
# Each row of bvecs is one direction (3 rows, N columns).
245-
np.savetxt(bvecs_file, self.bvecs, fmt="%.6f")
246-
np.savetxt(bvals_file, self.bvals[np.newaxis, :], fmt="%.6f")
255+
np.savetxt(bvecs_file, self.bvecs, fmt=f"%.{bvecs_dec_places}f")
256+
np.savetxt(bvals_file, self.bvals[np.newaxis, :], fmt=f"%.{bvals_dec_places}f")
247257

248258

249259
def load(

0 commit comments

Comments
 (0)