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

Modifying values of dict returned by io.loadmat affects subsequent calls to io.loadmat #10230

Open
seth-a opened this issue May 28, 2019 · 1 comment
Labels

Comments

@seth-a
Copy link

seth-a commented May 28, 2019

In short, there seems to be some state inherent in io.loadmat that causes modifications to it's return values to affect future invocations. It can be worked around, but it's not immediately obvious.

Reproducing code example:

The matlab files are generated by (matlab)

one = 1
save('test.mat', 'one', '-v4')

edit I've tried running the above code in octave, or generating the file in python using savemat, but can't get the error to occur. The matlab file is attached if someone without access to matlab wants to investigate:
reproduce.zip

The error is demonstrated by the following code, which loads a mat file, copies a variable, then performs an in-place addition on the original variable. Re-loading the mat file returns the modified value rather than the correct value.

from scipy import io

fname = 'test.mat'
t = io.loadmat(fname)

# copy for future reference
v = t['one'].copy()

# in place modification of return value
t['one'] += 32

# Reload file
t2 = io.loadmat(fname)

print('original:', v)
print('modified:', t['one'])
print('reloaded:', t2['one'])

assert ((v == t2['one']).all()), "Same variable from same file doen't match itself"

Output:

original: [[1]]
modified: [[33]]
reloaded: [[33]]
Traceback (most recent call last):
  File "bug.py", line 14, in <module>
    assert ((v == t2['one']).all()), "Same variable from same file doen't match itself"
AssertionError: Same variable from same file doen't match itself

When loading a version 4 mat file this issue doesn't occur. The actual code where this bug manifested loads multiple variables from multiple mat files, and originally appeared as an unrelated variable being modified when a new mat file is loaded.

Scipy/Numpy/Python version information:

1.1.0 1.16.2 sys.version_info(major=3, minor=5, micro=2, releaselevel='final', serial=0)

Workaround

Don't use in-place modification with the dict returned by loadmat, i.e. use

t['one'] = t['one'] + 32

rather than

t['one'] += 32
@sethtroisi
Copy link
Contributor

Possible related to #7752

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants