Loading pymatreader/utils.py +10 −5 Original line number Diff line number Diff line Loading @@ -31,7 +31,7 @@ from __future__ import annotations import types from typing import TYPE_CHECKING from typing import TYPE_CHECKING, Any from warnings import warn import numpy as np Loading Loading @@ -72,7 +72,7 @@ standard_matlab_classes = ( def _import_h5py() -> h5py: try: import h5py import h5py # noqa PLC0415 except Exception as exc: raise ImportError(f'h5py is required to read MATLAB files >= v7.3 ({exc})') return h5py Loading Loading @@ -363,7 +363,7 @@ def _check_for_scipy_mat_struct( return data def _handle_scipy_ndarray(data: np.ndarray | MatlabFunction) -> np.ndarray | list: def _handle_scipy_ndarray(data: np.ndarray | MatlabFunction) -> np.ndarray | list | dict: if data.dtype == np.dtype('object') and not isinstance(data, MatlabFunction): as_list = [] for element in data: Loading @@ -376,11 +376,16 @@ def _handle_scipy_ndarray(data: np.ndarray | MatlabFunction) -> np.ndarray | lis if isinstance(data, np.ndarray): data = np.array(data) assert isinstance(data, (np.ndarray, list, dict)) return data def _todict_from_np_struct(data: np.ndarray) -> dict[str, np.ndarray | int | float | str | list]: data_dict: dict[str, np.ndarray | int | float | str | list] = {} def _todict_from_np_struct(data: np.ndarray) -> dict[str, Any]: data_dict: dict[str, Any] = {} if data.dtype.names is None: return data_dict for cur_field_name in data.dtype.names: try: Loading tests/test_pymatreader.py +6 −14 Original line number Diff line number Diff line Loading @@ -174,7 +174,7 @@ def test_raw_old_eeglab(): def test_raw_h5_eeglab_event_type(): """Test that the event type of EEGLab data is read correctly.""" data = read_mat(Path(test_data_folder, testdata_eeglab_h5)) from .helper_functions.mne_eeglab_stuff import prepare_events_like_mne from .helper_functions.mne_eeglab_stuff import prepare_events_like_mne # noqa PLC0415 events = prepare_events_like_mne(data) Loading @@ -186,7 +186,7 @@ def test_raw_h5_eeglab_event_type(): def test_raw_old_eeglab_event_type(): """Test that the event type of old EEGLab data is read correctly.""" data = read_mat(Path(test_data_folder, testdata_eeglab_old)) from .helper_functions.mne_eeglab_stuff import prepare_events_like_mne from .helper_functions.mne_eeglab_stuff import prepare_events_like_mne # noqa PLC0415 events = prepare_events_like_mne(data) first_event = events[0] Loading Loading @@ -320,22 +320,15 @@ def test_sparse_matrices(version): assert matrix.dtype == np.float64 # Check the shape of the matrix mat_shapes = dict( col=(N, 1), row=(1, N), wide=(N, 2 * N), square=(N, N), tall=(2 * N, N) ) mat_shapes = dict(col=(N, 1), row=(1, N), wide=(N, 2 * N), square=(N, N), tall=(2 * N, N)) assert matrix.shape == mat_shapes[name] # Check every single value of the matrix # Load the "true" data from the CSV file csv_matrix = np.loadtxt( Path(test_data_folder, f'sparse_{empty}{name}.csv'), delimiter=',' ).reshape(mat_shapes[name]) csv_matrix = np.loadtxt(Path(test_data_folder, f'sparse_{empty}{name}.csv'), delimiter=',').reshape( mat_shapes[name] ) np.testing.assert_allclose(matrix.toarray(), csv_matrix, atol=1e-15) Loading Loading @@ -397,4 +390,3 @@ def test_whosmat_file_not_found(): """Test that whosmat raises OSError for a missing file.""" with pytest.raises(OSError): whosmat(Path(test_data_folder, invalid_fname)) Loading
pymatreader/utils.py +10 −5 Original line number Diff line number Diff line Loading @@ -31,7 +31,7 @@ from __future__ import annotations import types from typing import TYPE_CHECKING from typing import TYPE_CHECKING, Any from warnings import warn import numpy as np Loading Loading @@ -72,7 +72,7 @@ standard_matlab_classes = ( def _import_h5py() -> h5py: try: import h5py import h5py # noqa PLC0415 except Exception as exc: raise ImportError(f'h5py is required to read MATLAB files >= v7.3 ({exc})') return h5py Loading Loading @@ -363,7 +363,7 @@ def _check_for_scipy_mat_struct( return data def _handle_scipy_ndarray(data: np.ndarray | MatlabFunction) -> np.ndarray | list: def _handle_scipy_ndarray(data: np.ndarray | MatlabFunction) -> np.ndarray | list | dict: if data.dtype == np.dtype('object') and not isinstance(data, MatlabFunction): as_list = [] for element in data: Loading @@ -376,11 +376,16 @@ def _handle_scipy_ndarray(data: np.ndarray | MatlabFunction) -> np.ndarray | lis if isinstance(data, np.ndarray): data = np.array(data) assert isinstance(data, (np.ndarray, list, dict)) return data def _todict_from_np_struct(data: np.ndarray) -> dict[str, np.ndarray | int | float | str | list]: data_dict: dict[str, np.ndarray | int | float | str | list] = {} def _todict_from_np_struct(data: np.ndarray) -> dict[str, Any]: data_dict: dict[str, Any] = {} if data.dtype.names is None: return data_dict for cur_field_name in data.dtype.names: try: Loading
tests/test_pymatreader.py +6 −14 Original line number Diff line number Diff line Loading @@ -174,7 +174,7 @@ def test_raw_old_eeglab(): def test_raw_h5_eeglab_event_type(): """Test that the event type of EEGLab data is read correctly.""" data = read_mat(Path(test_data_folder, testdata_eeglab_h5)) from .helper_functions.mne_eeglab_stuff import prepare_events_like_mne from .helper_functions.mne_eeglab_stuff import prepare_events_like_mne # noqa PLC0415 events = prepare_events_like_mne(data) Loading @@ -186,7 +186,7 @@ def test_raw_h5_eeglab_event_type(): def test_raw_old_eeglab_event_type(): """Test that the event type of old EEGLab data is read correctly.""" data = read_mat(Path(test_data_folder, testdata_eeglab_old)) from .helper_functions.mne_eeglab_stuff import prepare_events_like_mne from .helper_functions.mne_eeglab_stuff import prepare_events_like_mne # noqa PLC0415 events = prepare_events_like_mne(data) first_event = events[0] Loading Loading @@ -320,22 +320,15 @@ def test_sparse_matrices(version): assert matrix.dtype == np.float64 # Check the shape of the matrix mat_shapes = dict( col=(N, 1), row=(1, N), wide=(N, 2 * N), square=(N, N), tall=(2 * N, N) ) mat_shapes = dict(col=(N, 1), row=(1, N), wide=(N, 2 * N), square=(N, N), tall=(2 * N, N)) assert matrix.shape == mat_shapes[name] # Check every single value of the matrix # Load the "true" data from the CSV file csv_matrix = np.loadtxt( Path(test_data_folder, f'sparse_{empty}{name}.csv'), delimiter=',' ).reshape(mat_shapes[name]) csv_matrix = np.loadtxt(Path(test_data_folder, f'sparse_{empty}{name}.csv'), delimiter=',').reshape( mat_shapes[name] ) np.testing.assert_allclose(matrix.toarray(), csv_matrix, atol=1e-15) Loading Loading @@ -397,4 +390,3 @@ def test_whosmat_file_not_found(): """Test that whosmat raises OSError for a missing file.""" with pytest.raises(OSError): whosmat(Path(test_data_folder, invalid_fname))