Alternative return value for io.loadmat

Hello! Loading .mat files with scipy.io.loadmat returns a rather complex dict object with nested struct arrays. If the .mat file contains cell arrays, the resulting structure is particularly hard to parse for humans (see e.g. https://github.com/scipy/scipy/issues/7895). For this reason, people have come up with conversion routines that simplify the result of io.loadmat (e.g. http://blog.nephics.com/2019/08/28/better-loadmat-for-scipy). Simple example using the following MATLAB struct (download the .mat file here: https://github.com/scipy/scipy/files/1314287/matlab.zip):
s = struct('mycell', {{'a', 'b', 'c'}});
Accessing the value 'a' with scipy.io.loadmat:
mat["s"]["mycell"][0, 0][0, 0][0] 'a'
The solution from the blog post provides easier access to the same value because the nested struct arrays get flattened:
mat1["s"]["mycell"][0] 'a'
In https://github.com/scipy/scipy/issues/7895 we propose to add such an alternative return value structure to scipy.io.loadmat. Do you think this is a useful addition that should be included? If so, what would be the best way to integrate it? Should scipy.io.loadmat get a new keyword argument that influences what is returned (i.e. the default behavior should return the current value, whereas setting the kwarg to a specific value would return the alternative value)? Or should there be a new function that gets the current value and converts it to the alternative structure? Thanks! Clemens

On Wed, Mar 18, 2020 at 8:59 AM Brunner, Clemens ( clemens.brunner@uni-graz.at) <clemens.brunner@uni-graz.at> wrote:
Hello!
Loading .mat files with scipy.io.loadmat returns a rather complex dict object with nested struct arrays. If the .mat file contains cell arrays, the resulting structure is particularly hard to parse for humans (see e.g. https://github.com/scipy/scipy/issues/7895). For this reason, people have come up with conversion routines that simplify the result of io.loadmat (e.g. http://blog.nephics.com/2019/08/28/better-loadmat-for-scipy).
Simple example using the following MATLAB struct (download the .mat file here: https://github.com/scipy/scipy/files/1314287/matlab.zip):
s = struct('mycell', {{'a', 'b', 'c'}});
Accessing the value 'a' with scipy.io.loadmat:
mat["s"]["mycell"][0, 0][0, 0][0] 'a'
The solution from the blog post provides easier access to the same value because the nested struct arrays get flattened:
mat1["s"]["mycell"][0] 'a'
In https://github.com/scipy/scipy/issues/7895 we propose to add such an alternative return value structure to scipy.io.loadmat.
Do you think this is a useful addition that should be included?
There's enough feedback from users and people writing workarounds that I think it's clear that there's value in supporting this within SciPy. If so, what would be the best way to integrate it? Should scipy.io.loadmat
get a new keyword argument that influences what is returned (i.e. the default behavior should return the current value, whereas setting the kwarg to a specific value would return the alternative value)? Or should there be a new function that gets the current value and converts it to the alternative structure?
I answered this on the issue. Thanks for working on this Clemens! Cheers, Ralf
Thanks! Clemens _______________________________________________ SciPy-Dev mailing list SciPy-Dev@python.org https://mail.python.org/mailman/listinfo/scipy-dev
participants (2)
-
Brunner, Clemens (clemens.brunner@uni-graz.at)
-
Ralf Gommers