Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #149 replace _read_mat with scipy.io.loadmat #151

Merged
merged 1 commit into from
Feb 22, 2022
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
16 changes: 0 additions & 16 deletions nitransforms/io/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@
from pathlib import Path
import numpy as np
from nibabel import load as loadimg
from scipy.io.matlab.miobase import get_matfile_version
from scipy.io.matlab.mio4 import MatFile4Reader
from scipy.io.matlab.mio5 import MatFile5Reader

from ..patched import LabeledWrapStruct

Expand Down Expand Up @@ -146,19 +143,6 @@ def from_image(cls, imgobj):
raise NotImplementedError


def _read_mat(byte_stream):
mjv, _ = get_matfile_version(byte_stream)
if mjv == 0:
reader = MatFile4Reader(byte_stream)
elif mjv == 1:
reader = MatFile5Reader(byte_stream)
elif mjv == 2:
raise TransformFileError("Please use HDF reader for Matlab v7.3 files")
else:
raise TransformFileError("Not a Matlab file.")
return reader.get_variables()


def _ensure_image(img):
if isinstance(img, (str, Path)):
return loadimg(img)
Expand Down
3 changes: 1 addition & 2 deletions nitransforms/io/itk.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
"""Read/write ITK transforms."""
import warnings
import numpy as np
from scipy.io import savemat as _save_mat
from scipy.io import loadmat as _read_mat, savemat as _save_mat
from nibabel import Nifti1Header, Nifti1Image
from nibabel.affines import from_matvec
from .base import (
BaseLinearTransformList,
DisplacementsField,
LinearParameters,
TransformFileError,
_read_mat,
)

LPS = np.diag([-1, -1, 1, 1])
Expand Down
34 changes: 2 additions & 32 deletions nitransforms/tests/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import nibabel as nb
from nibabel.eulerangles import euler2mat
from nibabel.affines import from_matvec
from scipy.io import loadmat, savemat
from scipy.io import loadmat
from ..io import (
afni,
fsl,
Expand All @@ -21,7 +21,7 @@
FSLinearTransform as LT,
FSLinearTransformArray as LTA,
)
from ..io.base import _read_mat, LinearParameters, TransformFileError
from ..io.base import LinearParameters, TransformFileError

LPS = np.diag([-1, -1, 1, 1])
ITK_MAT = LPS.dot(np.ones((4, 4)).dot(LPS))
Expand Down Expand Up @@ -393,36 +393,6 @@ def test_LinearParameters(tmpdir):
LinearParameters.from_fileobj(tmpdir.join("file.txt").open())


@pytest.mark.parametrize("matlab_ver", ["4", "5"])
def test_read_mat1(tmpdir, matlab_ver):
"""Test read from matlab."""
tmpdir.chdir()

savemat("val.mat", {"val": np.ones((3,))}, format=matlab_ver)
with open("val.mat", "rb") as f:
mdict = _read_mat(f)

assert np.all(mdict["val"] == np.ones((3,)))


@pytest.mark.parametrize("matlab_ver", [-1] + list(range(2, 7)))
def test_read_mat2(tmpdir, monkeypatch, matlab_ver):
"""Check read matlab raises adequate errors."""
from ..io import base

tmpdir.chdir()
savemat("val.mat", {"val": np.ones((3,))})

def _mockreturn(arg):
return (matlab_ver, 0)

with monkeypatch.context() as m:
m.setattr(base, "get_matfile_version", _mockreturn)
with pytest.raises(TransformFileError):
with open("val.mat", "rb") as f:
_read_mat(f)


def test_afni_Displacements():
"""Test displacements fields."""
field = nb.Nifti1Image(np.zeros((10, 10, 10)), None, None)
Expand Down