You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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)
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.
Output:
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:
Workaround
Don't use in-place modification with the dict returned by loadmat, i.e. use
rather than
The text was updated successfully, but these errors were encountered: