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

SciPy Trac scipy-tickets@scipy....
Sun Jun 10 07:20:48 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:
 {{{
 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)
 }}}

--

Comment(by rgommers):

 <reformat description>

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


More information about the Scipy-tickets mailing list