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

scipy.io.loadmat failure in 1.7.0 #14271

Closed
jcharlong opened this issue Jun 21, 2021 · 4 comments · Fixed by #14303
Closed

scipy.io.loadmat failure in 1.7.0 #14271

jcharlong opened this issue Jun 21, 2021 · 4 comments · Fixed by #14303
Labels
defect A clear bug or issue that prevents SciPy from being installed or used as expected scipy.io
Milestone

Comments

@jcharlong
Copy link
Contributor

jcharlong commented Jun 21, 2021

My issue is about a failure to load MATLAB files with scipy 1.7.0. I use scipy.io.loadmat (with squeeze_me=True) to load MATLAB files & it works just fine in 1.6.3.

I traced the issue down to a change in scipy.io.matlab.mio5.MatFile5Reader's get_variables method. I noticed when diffing the 1.7.0 branch with the 1.6.3 branch that line 313 was changed from asstr(hdr.name) (from numpy.compat import asstr) to hdr.name.decode('latin1'). Looks like this change was introduced in the following commit.

It appears as though asstr handles the case when hdr.name is None, which occurs with some of the MATLAB files I am working with.

Reverting to using asstr resolves the issue & so does changing the line to hdr.name.decode('latin1') if hdr.name is not None else 'None' (asstr(None) == 'None')). Not sure which would be more appropriate, or if hdr.name even should ever be None.

Error message:

Error
Traceback (most recent call last):
(redacted a few unrelated traceback lines)
  File "/usr/lib/python3.8/site-packages/scipy/io/matlab/mio.py", line 226, in loadmat
    matfile_dict = MR.get_variables(variable_names)
  File "/usr/lib/python3.8/site-packages/scipy/io/matlab/mio5.py", line 313, in get_variables
    name = hdr.name.decode('latin1')
AttributeError: 'NoneType' object has no attribute 'decode'

Scipy/Numpy/Python version information:

1.7.0 1.20.3 sys.version_info(major=3, minor=8, micro=5, releaselevel='final', serial=0)

@ev-br
Copy link
Member

ev-br commented Jun 21, 2021

Could you provide a self-contained example? Both python code and a (minimal) matlab file, which together reproduce the problem?

@ev-br ev-br added defect A clear bug or issue that prevents SciPy from being installed or used as expected scipy.io labels Jun 21, 2021
@jcharlong
Copy link
Contributor Author

I can reproduce locally by simply running

import scipy.io

data = scipy.io.loadmat('matlab.mat')

Where matlab.mat is the problem file.

I tried but can't seem to generate a new MATLAB file that can produce this effect. Even simply loading & re-saving a problem file (via MATLAB) appears to resolve the issue. Not sure what the original MATLAB version/environment/settings were when the file was created.

The issue is cropping up at work where we use some MATLAB files as inputs to some unit tests. Sadly because of this I cannot supply the original files as they contain proprietary data.

@ev-br
Copy link
Member

ev-br commented Jun 21, 2021

I think adding a if hdr.name is not None else 'None' guard would be OK if it fixes a backcompat corner case issue even if there is no unit test we could add.

Would you be willing to send a PR @jcharlong?

Am tentatively marking it for a 1.7.1 milestone -- something was bound to start it, I suppose.

@ev-br ev-br added this to the 1.7.1 milestone Jun 21, 2021
@jcharlong
Copy link
Contributor Author

Yes, I can take a stab at submitting a PR. Likely won't be able to get to it until this upcoming weekend at the earliest though.

jcharlong added a commit to jcharlong/scipy that referenced this issue Jun 26, 2021
Closes scipy#14271

Resolves a rare issue when None is returned instead of a string.
Returns 'None' since that's what the old "numpy.compat.asstr"
implementation returned.
tylerjereddy pushed a commit to tylerjereddy/scipy that referenced this issue Jul 23, 2021
Closes scipy#14271

Resolves a rare issue when None is returned instead of a string.
Returns 'None' since that's what the old "numpy.compat.asstr"
implementation returned.
ragibson pushed a commit to ragibson/scipy that referenced this issue Jul 25, 2021
Closes scipy#14271

Resolves a rare issue when None is returned instead of a string.
Returns 'None' since that's what the old "numpy.compat.asstr"
implementation returned.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
defect A clear bug or issue that prevents SciPy from being installed or used as expected scipy.io
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants