scipy.io.loadmat nested structures (i.e. dictionaries)

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)

-------------Problems Reply------------

Here are the functions, which reconstructs the dictionaries just use this loadmat instead of scipy.io's loadmat:

import scipy.io as spio

def loadmat(filename):
'''
this function should be called instead of direct spio.loadmat
as it cures the problem of not properly recovering python dictionaries
from mat files. It calls the function check keys to cure all entries
which are still mat-objects
'''
data = spio.loadmat(filename, struct_as_record=False, squeeze_me=True)
return _check_keys(data)

def _check_keys(dict):
'''
checks if entries in dictionary are mat-objects. If yes
todict is called to change them to nested dictionaries
'''
for key in dict:
if isinstance(dict[key], spio.matlab.mio5_params.mat_struct):
dict[key] = _todict(dict[key])
return dict

def _todict(matobj):
'''
A recursive function which constructs from matobjects nested dictionaries
'''
dict = {}
for strg in matobj._fieldnames:
elem = matobj.__dict__[strg]
if isinstance(elem, spio.matlab.mio5_params.mat_struct):
dict[strg] = _todict(elem)
else:
dict[strg] = elem
return dict

Found a solution, one can access the content of the "scipy.io.matlab.mio5_params.mat_struct object" can be investigated via:

v['b'].__dict__['c'].__dict__['d']

Cheers

Just an enhancement to mergen's answer, which unfortunately will stop recursing if it reaches a cell array of objects. The following version will make lists of them instead, and continuing the recursion into the cell array elements if possible.

def loadmat(filename):
'''
this function should be called instead of direct spio.loadmat
as it cures the problem of not properly recovering python dictionaries
from mat files. It calls the function check keys to cure all entries
which are still mat-objects
'''
data = spio.loadmat(filename, struct_as_record=False, squeeze_me=True)
return _check_keys(data)

def _check_keys(dict):
'''
checks if entries in dictionary are mat-objects. If yes
todict is called to change them to nested dictionaries
'''
for key in dict:
if isinstance(dict[key], spio.matlab.mio5_params.mat_struct):
dict[key] = _todict(dict[key])
return dict

def _todict(matobj):
'''
A recursive function which constructs from matobjects nested dictionaries
'''
dict = {}
for strg in matobj._fieldnames:
elem = matobj.__dict__[strg]
if isinstance(elem, spio.matlab.mio5_params.mat_struct):
dict[strg] = _todict(elem)
elif isinstance(elem,np.ndarray):
dict[strg] = _tolist(elem)
else:
dict[strg] = elem
return dict

def _tolist(ndarray):
'''
A recursive function which constructs lists from cellarrays
(which are loaded as numpy ndarrays), recursing into the elements
if they contain matobjects.
'''
elem_list = []
for sub_elem in ndarray:
if isinstance(sub_elem, spio.matlab.mio5_params.mat_struct):
elem_list.append(_todict(sub_elem))
elif isinstance(sub_elem,np.ndarray):
elem_list.append(_tolist(sub_elem))
else:
elem_list.append(sub_elem)
return elem_list

Category:python Views:2 Time:2011-08-10

Related post

  • Access array contents from a .mat file loaded using Scipy.io.loadmat - python 2011-06-08

    UPDATE: This is a long question that boils down to, can someone explain the numpy array class to me? I answered my own question below. I am working on a project to import data from matlab into a mysql database whose contents will be made available th

  • How to load cell array of strings in Matlab mat files into Python list or tuple using Scipy.io.loadmat 2011-01-27

    I am a Matlab user new to Python. I would like to write a cell array of strings in Matlab to a Mat file, and load this Mat file using Python (maybe scipy.io.loadmat) into some similar type (e.g list of strings or tuple of strings). But loadmat read t

  • initializing structures and nesting structures 2009-10-29

    gcc I am just getting back into c programming and I am just practicing with structures. However, I have a nested structure that I want to fill from another initialized structure. However, I don't think my technique is correct way to do this. Any advi

  • Flattening a deeply nested structure in XSLT 2009-12-16

    I'm trying to learn XSLT (for some holiday coding fun). I think I now have a pretty good understanding of the basics (grabbing subtrees, filtering-out elements, and renaming elements). Where I'm having trouble is when it comes to drastically reorgani

  • How to use C# nested structures to access tree of data 2010-03-12

    I'm importing some XML to C#, and want to be able to access data from the XML in the form of what I think is a nested structure. (I may be wrong!) What I have in my XML is in the following form: <hardwareSettings initial="true> <cameraSettin

  • Creating a TreeView Nested Structure Using Self Referencing Table 2010-06-04

    I am trying to create a TreeView nested structure with the use of self referencing table fields. Here is a simple example: Category 1 Product 1 Toy 1 Toy 2 Product 2 Toy 3 Toy 4 more categories.. The database table has a single table called "Category

  • How to Assign the value to Main /Parent Structure in a Nested Structure? 2010-08-06

    Hi I have writen an Nested Structure in C# . Find the code snippet below: public struct SMB_MESSAGE { #region SMB Parameter public struct SMB_PARAMETERS { public byte WordCount; public ushort[] Words; } #endregion #region SMB Data public struct SMB_D

  • Are there any performance penalties when using nested structures? 2010-09-16

    Are there any performance penalties when I use multiple nested structures/classes (kinda like using muti dimension heap arrays) or is it just an organizational feature of the language to make it easier to keep track of data and the compiler doesn't a

  • Cleaner way to update nested structures 2010-10-10

    Say I have got following two case classes: case class Address(street: String, city: String, state: String, zipCode: Int) case class Person(firstName: String, lastName: String, address: Address) and the following instance of Person class: val raj = Pe

  • MarshalAs nested structure 2010-10-12

    I have two C++ structures that I have to send as parameters when calling a DLL method from C#. For example, lets define them as: struct A { int data; } struct B { int MoreData; A * SomeData; } A method that I need to call from C# has the following si

  • What is the CSS "@media" nested structure/namespace? 2010-10-21

    What is this name space like wrapping/nested structure in CSS? I have never seen this before. Please help me understand what is this. Thanks @media screen, projection { * { margin: 0px; padding: 0px; } body { background: #fff; overflow-y: scroll; _ov

  • more problem with nested structure 2011-01-27

    object *head = NULL, *tail = NULL; // EDIT struct object { vector <int> data; object * read ( void ) ; struct obj { obj *next; object * brach ( object * ) ; }; }; object * object :: read ( void ) { ... // some code to read and return (dynamical

  • Turn the dictionary keys into variable names with same values in Python from .mat Matlab files using scipy.io.loadmat 2011-06-16

    I am trying to take a basic dictionary temp = {'key':array([1,2])} loaded from a .mat file with scipy.io.loadmat. Turn the keys in the Python dictionary file returned by loadmat() into variable names with values the same as the representing keys. So

  • Freemarker, Maps and Mongo. Getting a value out a nested structure 2011-07-21

    I have a Mongo Database and I am making a small web application to display the values from the database. I'm having a little problem with getting a value from a nested structure. The database structure I have is: { "_id": ObjectId("4e244ddcb1633b25c3

  • How to marshal nested structure? 2011-08-08

    I have three C++ structures that I need to send as parameters when calling a DLL method from C#. I simplified them as the following: typedef struct { int data1; int data2; } A; typedef struct { int numStructA; A *pStructA; int moreData; } B; typedef

  • nested structure array access in python using SWIG 2011-10-10

    I haven't been able to figure out how to access the array elements SubStatus in the following nested structure. I do seem to be able to see the first element, but don't understand how to force indexing, e.g., as a list. Any help is much appreciated.

  • Using mongoimport to read CSV into nested structure? 2011-10-18

    I have a mongo document with a structure like: { "foo": { "bar1": "val1", "bar2": "val2"} } I'd like to import my data from a csv using mongoimport --type csv --headerline [...] I am not sure how to format the field name in the csv to address the nes

  • clojure rename-keys in nested structure 2011-10-19

    Suppose I have a nested structure, something like this: {:data1 {:categories [ {:name "abc" :id 234 :desc "whatever"} {:name "def" :id 456 :desc "nothing"}] } :data2 {...} :data3 {...} } And I need to transform the key names in the maps. I can transf

  • Compact access to variables in nested structures 2011-11-01

    Given this simple C code: struct { struct a { int foo; }; struct b { char *bar; }; } s; I am wondering whether there is a way to access a variable in one of the nested structures in a more compact way than s.a.foo = 5, for instance. --------------Sol

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.162 (s). 11 q(s)