Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions nibabel/cmdline/parrec2nii.py
Original file line number Diff line number Diff line change
Expand Up @@ -358,8 +358,7 @@ def proc_file(infile, opts):
)
with open(basefilename + '.bvals', 'w') as fid:
# np.savetxt could do this, but it's just a loop anyway
for val in bvals:
fid.write(f'{val} ')
fid.writelines(f'{val} ' for val in bvals)
fid.write('\n')
else:
verbose('Writing .bvals and .bvecs files')
Expand All @@ -369,13 +368,11 @@ def proc_file(infile, opts):
bvecs = apply_affine(bv_reorient, bvecs)
with open(basefilename + '.bvals', 'w') as fid:
# np.savetxt could do this, but it's just a loop anyway
for val in bvals:
fid.write(f'{val} ')
fid.writelines(f'{val} ' for val in bvals)
fid.write('\n')
with open(basefilename + '.bvecs', 'w') as fid:
for row in bvecs.T:
for val in row:
fid.write(f'{val} ')
fid.writelines(f'{val} ' for val in row)
fid.write('\n')

# export data labels varying along the 4th dimensions if requested
Expand Down
5 changes: 4 additions & 1 deletion nibabel/openers.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
if ty.TYPE_CHECKING:
from types import TracebackType

from _typeshed import WriteableBuffer
from _typeshed import ReadableBuffer, WriteableBuffer

from ._typing import Self

Expand Down Expand Up @@ -231,6 +231,9 @@ def readinto(self, buffer: WriteableBuffer, /) -> int | None:
def write(self, b: bytes, /) -> int | None:
return self.fobj.write(b)

def writelines(self, lines: ty.Iterable[ReadableBuffer], /) -> None:
self.fobj.writelines(lines)

def seek(self, pos: int, whence: int = 0, /) -> int:
return self.fobj.seek(pos, whence)

Expand Down
7 changes: 3 additions & 4 deletions nibabel/volumeutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -830,8 +830,7 @@ def write_zeros(fileobj: io.IOBase, count: int, block_size: int = 8194) -> None:
nblocks = int(count // block_size)
rem = count % block_size
blk = b'\x00' * block_size
for bno in range(nblocks):
fileobj.write(blk)
fileobj.writelines(blk for bno in range(nblocks))
fileobj.write(b'\x00' * rem)


Expand Down Expand Up @@ -1365,7 +1364,7 @@ def shape_zoom_affine(
return aff


def rec2dict(rec: np.ndarray) -> dict[str, np.generic | np.ndarray]:
def rec2dict(rec: np.record) -> dict[str, np.generic | np.ndarray]:
"""Convert recarray to dictionary

Also converts scalar values to scalars
Expand All @@ -1388,7 +1387,7 @@ def rec2dict(rec: np.ndarray) -> dict[str, np.generic | np.ndarray]:
True
"""
dct = {}
for key in rec.dtype.fields:
for key in rec.dtype.fields or ():
val = rec[key]
try:
val = val.item()
Expand Down
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,6 @@ ignore = [
"RUF012", # TODO: enable
"RUF015",
"RUF017", # TODO: enable
"UP038", # https://github.com/astral-sh/ruff/issues/7871
# https://docs.astral.sh/ruff/formatter/#conflicting-lint-rules
"W191",
"E111",
Expand Down
Loading