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
6 changes: 3 additions & 3 deletions src/machinevisiontoolbox/base/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ def mvtb_load_matfile(filename: str) -> dict[str, Any]:
from collections import namedtuple

from scipy.io import loadmat
from scipy.io.matlab.mio5_params import mat_struct
from scipy.io.matlab import mat_struct

# get results as a dict
data = mvtb_load_data(filename, loadmat, squeeze_me=True, struct_as_record=False)

# if elements are a scipy.io.matlab.mio5_params.mat_struct, that is, they
# if elements are a scipy.io.matlab.mat_struct, that is, they
# were a MATLAB struct, convert them to a namedtuple
for key, value in data.items():
if isinstance(value, mat_struct): # scipy private API # type: ignore[arg-type]
if isinstance(value, mat_struct):
# print("fixing")
nt = namedtuple("matstruct", value._fieldnames) # type: ignore[misc] # field names are only known at run time
data[key] = nt(*[getattr(value, n) for n in value._fieldnames])
Expand Down
15 changes: 15 additions & 0 deletions tests/base/test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,21 @@ def test_matfile_struct_becomes_namedtuple(self):
nt.assert_array_equal(data["s"].b, [0, 1, 2])
nt.assert_array_equal(data["x"], np.eye(2))

def test_matfile_struct_gives_no_deprecation_warning(self):
# the mat_struct class used to be imported from scipy.io.matlab.mio5_params,
# which is deprecated and will be removed in SciPy 2.0
import warnings

from scipy.io import savemat

with tempfile.TemporaryDirectory() as tmp:
path = os.path.join(tmp, "s.mat")
savemat(path, {"s": {"a": 1.5}})
with warnings.catch_warnings():
warnings.simplefilter("error", DeprecationWarning)
data = mvtb_load_matfile(path)
self.assertEqual(data["s"].a, 1.5)

def test_jsonfile(self):
with tempfile.TemporaryDirectory() as tmp:
path = os.path.join(tmp, "d.json")
Expand Down
Loading