[Scipy-tickets] [SciPy] #1517: scipy.io.loadmat nested structures (i.e. dictionaries)

SciPy Trac scipy-tickets@scipy....
Sun Jun 10 07:23:35 CDT 2012


#1517: scipy.io.loadmat nested structures (i.e. dictionaries)
-----------------------------------+----------------------------------------
 Reporter:  mergen                 |       Owner:  mbrett     
     Type:  defect                 |      Status:  new        
 Priority:  normal                 |   Milestone:  Unscheduled
Component:  scipy.io               |     Version:  devel      
 Keywords:  loadmat, dictionaries  |  
-----------------------------------+----------------------------------------

Old description:

> Using the given routines (how to load Matlab .mat files with scipy), I
> could not access deeper nested structures to recover them into
> dictionaries
>
> To present the problem I run into in more detail, I give the following
> toy example:
> {{{
> load scipy.io as spio
> a = {'b':{'c':{'d': 3}}}
> # my dictionary: a['b']['c']['d'] = 3
> spio.savemat('xy.mat',a)
> }}}
>
> Now I want to read the mat-File back into python. I tried the following:
> {{{
> vig=spio.loadmat('xy.mat',squeeze_me=True)
> }}}
>
> If I now want to access the fields I get:
> {{{
> >> vig['b']
> array(((array(3),),), dtype=[('c', '|O8')])
> >> vig['b']['c']
> array(array((3,), dtype=[('d', '|O8')]), dtype=object)
> >> vig['b']['c']['d']
> ---------------------------------------------------------------------------
> ValueError                                Traceback (most recent call
> last)
>
> /<ipython console> in <module>()
>
> ValueError: field named d not found.
> }}}
>
> However, by using the option struct_as_record=False the field could be
> accessed:
> {{{
> v=spio.loadmat('xy.mat',squeeze_me=True,struct_as_record=False)
> }}}
>
> Now it was possible to access it by
> {{{
> >> v['b'].c.d
> array(3)
> }}}

New description:

 Using the given routines (how to load Matlab .mat files with scipy), I
 could not access deeper nested structures to recover them into
 dictionaries

 To present the problem I run into in more detail, I give the following toy
 example:
 {{{
 import scipy.io as spio
 a = {'b':{'c':{'d': 3}}}
 # my dictionary: a['b']['c']['d'] = 3
 spio.savemat('xy.mat',a)
 }}}

 Now I want to read the mat-File back into python. I tried the following:
 {{{
 vig=spio.loadmat('xy.mat',squeeze_me=True)
 }}}

 If I now want to access the fields I get:
 {{{
 >> vig['b']
 array(((array(3),),), dtype=[('c', '|O8')])
 >> vig['b']['c']
 array(array((3,), dtype=[('d', '|O8')]), dtype=object)
 >> vig['b']['c']['d']
 ---------------------------------------------------------------------------
 ValueError                                Traceback (most recent call
 last)

 /<ipython console> in <module>()

 ValueError: field named d not found.
 }}}

 However, by using the option struct_as_record=False the field could be
 accessed:
 {{{
 v=spio.loadmat('xy.mat',squeeze_me=True,struct_as_record=False)
 }}}

 Now it was possible to access it by
 {{{
 >> v['b'].c.d
 array(3)
 }}}

--

Comment(by rgommers):

 <fix import in description>

 This is now actually broken with numpy 1.7.0-dev:
 {{{
 Out[12]:
 array(((array(3),),),
       dtype=[('c', 'O')])

 In [13]: vig['b']['c']
 ERROR: An unexpected error occurred while tokenizing input
 The following traceback may be corrupted or invalid
 The error message is: ('EOF in multi-line statement', (223, 0))
 ERROR: An unexpected error occurred while tokenizing input
 The following traceback may be corrupted or invalid
 The error message is: ('EOF in multi-line statement', (46, 0))
 Out[13]:
 ---------------------------------------------------------------------------
 RuntimeError                              Traceback (most recent call
 last)
 /Users/rgommers/<ipython-input-13-c2c1a4e01a98> in <module>()
 ----> 1 vig['b']['c']

 /Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-
 packages/ipython-0.12-py2.6.egg/IPython/core/displayhook.pyc in
 __call__(self, result)
     236             self.start_displayhook()
     237             self.write_output_prompt()
 --> 238             format_dict = self.compute_format_data(result)
     239             self.write_format_data(format_dict)
     240             self.update_user_ns(result)

 /Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-
 packages/ipython-0.12-py2.6.egg/IPython/core/displayhook.pyc in
 compute_format_data(self, result)
     148             MIME type representation of the object.
     149         """
 --> 150         return self.shell.display_formatter.format(result)
     151
     152     def write_format_data(self, format_dict):

 /Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-
 packages/ipython-0.12-py2.6.egg/IPython/core/formatters.pyc in
 format(self, obj, include, exclude)
     124                     continue
     125             try:
 --> 126                 data = formatter(obj)
     127             except:
     128                 # FIXME: log the exception


 /Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-
 packages/ipython-0.12-py2.6.egg/IPython/core/formatters.pyc in
 __call__(self, obj)
     445                 type_pprinters=self.type_printers,
     446                 deferred_pprinters=self.deferred_printers)
 --> 447             printer.pretty(obj)
     448             printer.flush()
     449             return stream.getvalue()

 /Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-
 packages/ipython-0.12-py2.6.egg/IPython/lib/pretty.pyc in pretty(self,
 obj)
     349             if hasattr(obj_class, '_repr_pretty_'):
     350                 return obj_class._repr_pretty_(obj, self, cycle)
 --> 351             return _default_pprint(obj, self, cycle)
     352         finally:
     353             self.end_group()

 /Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-
 packages/ipython-0.12-py2.6.egg/IPython/lib/pretty.pyc in
 _default_pprint(obj, p, cycle)
     469     if getattr(klass, '__repr__', None) not in _baseclass_reprs:
     470         # A user-provided repr.

 --> 471         p.text(repr(obj))
     472         return
     473     p.begin_group(1, '<')

 /Users/rgommers/Code/numpy/numpy/core/numeric.pyc in array_repr(arr,
 max_line_width, precision, suppress_small)
    1444     if arr.size > 0 or arr.shape==(0,):
    1445         lst = array2string(arr, max_line_width, precision,
 suppress_small,
 -> 1446                            ', ', "array(")
    1447     else: # show zero-length shape unless it is (0,)
    1448         lst = "[], shape=%s" % (repr(arr.shape),)

 /Users/rgommers/Code/numpy/numpy/core/arrayprint.pyc in array2string(a,
 max_line_width, precision, suppress_small, separator, prefix, style,
 formatter)
     438     if a.shape == ():
     439         x = a.item()
 --> 440         if isna(x):
     441             lst = str(x).replace('NA', _na_str, 1)
     442         else:

 RuntimeError: field-NA is not supported yet

 In [14]: np.__version__
 Out[14]: '1.7.0.dev-fd78546'
 }}}

-- 
Ticket URL: <http://projects.scipy.org/scipy/ticket/1517#comment:3>
SciPy <http://www.scipy.org>
SciPy is open-source software for mathematics, science, and engineering.


More information about the Scipy-tickets mailing list