开发者

Storing a matlab file using python

开发者 https://www.devze.com 2023-04-11 17:51 出处:网络
I have a c开发者_JAVA技巧omplex data structure data = {} temp = {} data[\'bigdata\'] = temp; After this I copy \'key\' and \'data\' values from some other data structure into temp, like this

I have a c开发者_JAVA技巧omplex data structure

data = {}
temp = {}

data['bigdata'] = temp;

After this I copy 'key' and 'data' values from some other data structure into temp, like this

for key in backup.keys():
    temp[key] = []

and

for key in backup.keys():
    for val in backup[key]:
        temp[key].append(val);

After that If I do

sio.savemat(outputMATfile,data,oned_as ='column')

it is giving me error saying

TTypeError: data type not understood

Is it not possible to store a complex dictionary like this in a matlab file, using python ?


edit: Answer (and question somewhat) have changed significantly to be more general. It would still be useful it the asker would tell us what kinds of objects the values in backup are.

The scipy.io.savemat can apparently take a dictionary of dictionaries of arrays, so this structure

from numpy import array
import scipy.io
data = {
    'bigdata' : {
        'a' : array([1, 2, 3]),
        'b' : array([1, 2, 3]),
        'c' : array([1, 2, 3]),
     }
}
scipy.io.savemat('test.mat', data)

loads into matlab as

>> load test.mat
>> bigdata

bigdata = 

    a: [3x1 int64]
    c: [3x1 int64]
    b: [3x1 int64]

I imagine these dictionaries can be nested up to python's recursion limit, since the implementation is recursive. I tested 6 levels of nesting dictionaries. Edit: Now you're asking about a structure like this:

data = {
    'key1' : ['a' : apple, 'b' : banana],
    'key2' : ['c' : crabapple, 'd' : dragonfruit],
    ...
    }

and you haven't specified what apple, banana etc. are. It depends on what data from these Python objects you want in the Matlab objects. I tested a few classes like str (converted to char array), set (failed to convert to array), and list (array if homogeneous, character array if some strings, some numbers). The code looks pretty duck-type-ish, so if these objects have any a common data-holding interface it should get through; I present an excerpt here of the most relevant bit for the matlab5 version:

def to_writeable(source)
    if isinstance(source, np.ndarray):
        return source
    if source is None:
        return None
    # Objects that have dicts
    if hasattr(source, '__dict__'):
        source = dict((key, value) for key, value in source.__dict__.items()
                      if not key.startswith('_'))
    # Mappings or object dicts
    if hasattr(source, 'keys'):
        dtype = []
        values = []
        for field, value in source.items():
            if (isinstance(field, basestring) and
                not field[0] in '_0123456789'):
                dtype.append((field,object))
                values.append(value)
        if dtype:
            return np.array( [tuple(values)] ,dtype)
        else:
            return None
    # Next try and convert to an array
    narr = np.asanyarray(source)
    if narr.dtype.type in (np.object, np.object_) and \
       narr.shape == () and narr == source:
        # No interesting conversion possible
        return None
    return narr
  • Code for recursive write function that calls above function
0

精彩评论

暂无评论...
验证码 换一张
取 消