Git Product home page Git Product logo

matloader's Introduction

matloader

matloader is a fork of scipy's loader for MAT files (the format used by MATLAB), that adds support for classdef object loading, but is much slower (for files containing complex data structures), because it has been rewritten in pure Python.

  • MATLAB is a registered trademark of The Mathworks, Inc.

Installation

matloader can be installed using a standard

python setup.py install

Testing

Run nosetests.

matloader's People

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

lrq3000 u55

matloader's Issues

AttributeError: 'tuple' object has no attribute 'tostring'

Hi,

I get the following error on both python2 and python3 when trying to load a *.mat file that contains a MATLAB class:

In [5]: x = matloader.loadmat('159.mat')
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-5-6b112fe7d7a1> in <module>()
----> 1 x = matloader.loadmat('159.mat')

/usr/lib/python3.6/site-packages/matloader/mio.py in loadmat(file_name, mdict, appendmat, variable_names, **kwargs)
    133     """
    134     MR = mat_reader_factory(file_name, appendmat, **kwargs)
--> 135     matfile_dict = MR.get_variables(variable_names)
    136     if mdict is not None:
    137         mdict.update(matfile_dict)

/usr/lib/python3.6/site-packages/matloader/mio5.py in get_variables(self, variable_names)
    432         if isinstance(variable_names, string_types):
    433             variable_names = [variable_names]
--> 434         self._set_workspace()
    435         self._prepare_stream()
    436         variables = {"__header__": self._desc,

/usr/lib/python3.6/site-packages/matloader/mio5.py in _set_workspace(self)
    491             lambda stream, _: stream.seek((-stream.tell()) % 8, 1))
    492 
--> 493         entry, = reader._read_iter()
    494         data = entry.data
    495         name, fw = data["MCOS"].item().item()

/usr/lib/python3.6/site-packages/matloader/mio5.py in _read_iter(self, stream, info_only, load_only)
    328                     for p in pr:
    329                         for field in fields:
--> 330                             p[field] = next(reader).data
    331                     if matrix_cls == mxOBJECT_CLASS:
    332                         pr = MatlabObject(pr, classname)

/usr/lib/python3.6/site-packages/matloader/mio5.py in _read_iter(self, stream, info_only, load_only)
    347                     dtype = object
    348                 elif matrix_cls == mxOPAQUE_CLASS:
--> 349                     name, = self._as_identifiers(dims) or ("",)
    350                     opaque_components = []
    351                     while stream.tell() < entry_end:

/usr/lib/python3.6/site-packages/matloader/mio5.py in _as_identifiers(data)
    222         """
    223         names = [  # Extra call to str to avoid returning unicode on Python 2.
--> 224             name for name in str(data.tostring().decode("ascii")).split("\0")
    225             if name]
    226         if len(set(names)) < len(names):

AttributeError: 'tuple' object has no attribute 'tostring'

Class objects returned as nested numpy arrays

Hi,

I discovered that a MATLAB class object in a *.mat file is converted to a Numpy structured array nested inside of a regular ndarray of shape=(1,1) and dtype=object. In order to directly access the structured array, I have to pick the [0,0] index of the outer ndarray. Since nested Numpy arrays are almost never made intentionally, I suspect that this a bug. This can be seen with the 'testclass_simple_6.mat' file in the tests/data/ directory.

In [2]: mat = matloader.loadmat('testclass_simple_6.mat')

In [3]: mat['simple'].shape
Out[3]: (1, 1)

In [4]: mat['simple'][0,0].shape
Out[4]: ()

In [5]: type(mat['simple'])
Out[5]: numpy.ndarray

In [6]: type(mat['simple'][0,0])
Out[6]: numpy.void

In [7]: mat['simple']['array_field_2']
Out[7]: array([[array([[ 0.,  1.,  2.,  3.,  4.,  5.]])]], dtype=object)

In [8]: mat['simple'][0,0]['array_field_2']
Out[8]: array([[ 0.,  1.,  2.,  3.,  4.,  5.]])

Failure with squeeze_me=True

Hi again,

The following fails for a *.mat file that contains a MATLAB class:

In [2]: x = matloader.loadmat('159.mat', squeeze_me=True)
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-2-5abbb4686d75> in <module>()
----> 1 x = matloader.loadmat('159.mat', squeeze_me=True)

/usr/lib/python3.6/site-packages/matloader/mio.py in loadmat(file_name, mdict, appendmat, variable_names, **kwargs)
    133     """
    134     MR = mat_reader_factory(file_name, appendmat, **kwargs)
--> 135     matfile_dict = MR.get_variables(variable_names)
    136     if mdict is not None:
    137         mdict.update(matfile_dict)

/usr/lib/python3.6/site-packages/matloader/mio5.py in get_variables(self, variable_names)
    439                      "__globals__": [],  # FIXME Not covered by tests.
    440                      "__version__": self._version}
--> 441         for entry in self._read_iter(load_only=variable_names):
    442             if entry is None:
    443                 continue

/usr/lib/python3.6/site-packages/matloader/mio5.py in _read_iter(self, stream, info_only, load_only)
    256                     for entry in self._read_iter(
    257                         ZlibInputStream(stream, nbytes),
--> 258                         info_only=info_only, load_only=load_only):
    259                         yield entry
    260                 except ValueError:

/usr/lib/python3.6/site-packages/matloader/mio5.py in _read_iter(self, stream, info_only, load_only)
    362                         classname, opaque_ids = opaque_components
    363                         opaque_ids = opaque_ids.data
--> 364                         if not self._is_opaque_ids(opaque_ids):
    365                             raise ValueError("Unsupported opaque format: {}".
    366                                              format(opaque_ids[0]))

/usr/lib/python3.6/site-packages/matloader/mio5.py in _is_opaque_ids(self, obj)
    467 
    468     def _is_opaque_ids(self, obj):
--> 469         return obj.dtype == np.uint32 and obj[0, 0] == 0xdd000000
    470 
    471     def _resolve_opaque_ids(self, opaque_ids):

IndexError: too many indices for array

The problem seems to be that obj on line 469 has already been squeezed to 1d, and so obj[0,0] fails.

Thanks.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.