diff --git a/src/machinevisiontoolbox/base/data.py b/src/machinevisiontoolbox/base/data.py index 4dd92f16..0f15ac6b 100644 --- a/src/machinevisiontoolbox/base/data.py +++ b/src/machinevisiontoolbox/base/data.py @@ -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]) diff --git a/tests/base/test_data.py b/tests/base/test_data.py index 885f92c3..ed8613d2 100644 --- a/tests/base/test_data.py +++ b/tests/base/test_data.py @@ -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")