• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Python mio.savemat函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Python中scipy.io.matlab.mio.savemat函数的典型用法代码示例。如果您正苦于以下问题:Python savemat函数的具体用法?Python savemat怎么用?Python savemat使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了savemat函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。

示例1: test_unicode_mat4

def test_unicode_mat4():
    # Mat4 should save unicode as latin1
    bio = BytesIO()
    var = {"second_cat": u("Schrödinger")}
    savemat(bio, var, format="4")
    var_back = loadmat(bio)
    assert_equal(var_back["second_cat"], var["second_cat"])
开发者ID:Juanlu001,项目名称:scipy,代码行数:7,代码来源:test_mio.py


示例2: test_save_dict

def test_save_dict():
    # Test that dict can be saved (as recarray), loaded as matstruct
    dict_types = ((dict, False),)
    try:
        from collections import OrderedDict
    except ImportError:
        pass
    else:
        dict_types += ((OrderedDict, True),)
    ab_exp = np.array([[(1, 2)]], dtype=[('a', object), ('b', object)])
    ba_exp = np.array([[(2, 1)]], dtype=[('b', object), ('a', object)])
    for dict_type, is_ordered in dict_types:
        # Initialize with tuples to keep order for OrderedDict
        d = dict_type([('a', 1), ('b', 2)])
        stream = BytesIO()
        savemat(stream, {'dict': d})
        stream.seek(0)
        vals = loadmat(stream)['dict']
        assert_equal(set(vals.dtype.names), set(['a', 'b']))
        if is_ordered:  # Input was ordered, output in ab order
            assert_array_equal(vals, ab_exp)
        else:  # Not ordered input, either order output
            if vals.dtype.names[0] == 'a':
                assert_array_equal(vals, ab_exp)
            else:
                assert_array_equal(vals, ba_exp)
开发者ID:ChadFulton,项目名称:scipy,代码行数:26,代码来源:test_mio.py


示例3: test_read_opts

def test_read_opts():
    # tests if read is seeing option sets, at initialization and after
    # initialization
    arr = np.arange(6).reshape(1,6)
    stream = BytesIO()
    savemat(stream, {'a': arr})
    rdr = MatFile5Reader(stream)
    back_dict = rdr.get_variables()
    rarr = back_dict['a']
    assert_array_equal(rarr, arr)
    rdr = MatFile5Reader(stream, squeeze_me=True)
    assert_array_equal(rdr.get_variables()['a'], arr.reshape((6,)))
    rdr.squeeze_me = False
    assert_array_equal(rarr, arr)
    rdr = MatFile5Reader(stream, byte_order=boc.native_code)
    assert_array_equal(rdr.get_variables()['a'], arr)
    # inverted byte code leads to error on read because of swapped
    # header etc
    rdr = MatFile5Reader(stream, byte_order=boc.swapped_code)
    assert_raises(Exception, rdr.get_variables)
    rdr.byte_order = boc.native_code
    assert_array_equal(rdr.get_variables()['a'], arr)
    arr = np.array(['a string'])
    stream.truncate(0)
    stream.seek(0)
    savemat(stream, {'a': arr})
    rdr = MatFile5Reader(stream)
    assert_array_equal(rdr.get_variables()['a'], arr)
    rdr = MatFile5Reader(stream, chars_as_strings=False)
    carr = np.atleast_2d(np.array(list(arr.item()), dtype='U1'))
    assert_array_equal(rdr.get_variables()['a'], carr)
    rdr.chars_as_strings = True
    assert_array_equal(rdr.get_variables()['a'], arr)
开发者ID:ChadFulton,项目名称:scipy,代码行数:33,代码来源:test_mio.py


示例4: test_fieldnames

def test_fieldnames():
    # Check that field names are as expected
    stream = BytesIO()
    savemat(stream, {'a': {'a':1, 'b':2}})
    res = loadmat(stream)
    field_names = res['a'].dtype.names
    assert_equal(set(field_names), set(('a', 'b')))
开发者ID:ChadFulton,项目名称:scipy,代码行数:7,代码来源:test_mio.py


示例5: test_gzip_simple

def test_gzip_simple():
    xdense = np.zeros((20,20))
    xdense[2,3] = 2.3
    xdense[4,5] = 4.5
    x = SP.csc_matrix(xdense)

    name = 'gzip_test'
    expected = {'x':x}
    format = '4'

    tmpdir = mkdtemp()
    try:
        fname = pjoin(tmpdir,name)
        mat_stream = gzip.open(fname,mode='wb')
        savemat(mat_stream, expected, format=format)
        mat_stream.close()

        mat_stream = gzip.open(fname,mode='rb')
        actual = loadmat(mat_stream, struct_as_record=True)
        mat_stream.close()
    finally:
        shutil.rmtree(tmpdir)

    assert_array_almost_equal(actual['x'].todense(),
                              expected['x'].todense(),
                              err_msg=repr(actual))
开发者ID:ChadFulton,项目名称:scipy,代码行数:26,代码来源:test_mio.py


示例6: test_recarray

def test_recarray():
    # check roundtrip of structured array
    dt = [('f1', 'f8'),
          ('f2', 'S10')]
    arr = np.zeros((2,), dtype=dt)
    arr[0]['f1'] = 0.5
    arr[0]['f2'] = 'python'
    arr[1]['f1'] = 99
    arr[1]['f2'] = 'not perl'
    stream = BytesIO()
    savemat(stream, {'arr': arr})
    d = loadmat(stream, struct_as_record=False)
    a20 = d['arr'][0,0]
    yield assert_equal, a20.f1, 0.5
    yield assert_equal, a20.f2, 'python'
    d = loadmat(stream, struct_as_record=True)
    a20 = d['arr'][0,0]
    yield assert_equal, a20['f1'], 0.5
    yield assert_equal, a20['f2'], 'python'
    # structs always come back as object types
    yield assert_equal, a20.dtype, np.dtype([('f1', 'O'),
                                             ('f2', 'O')])
    a21 = d['arr'].flat[1]
    yield assert_equal, a21['f1'], 99
    yield assert_equal, a21['f2'], 'not perl'
开发者ID:ChadFulton,项目名称:scipy,代码行数:25,代码来源:test_mio.py


示例7: test_save_dict

def test_save_dict():
    # Test that dict can be saved (as recarray), loaded as matstruct
    d = {'a':1, 'b':2}
    stream = StringIO()
    savemat(stream, {'dict':d})
    stream.seek(0)
    vals = loadmat(stream)
开发者ID:stefanv,项目名称:scipy3,代码行数:7,代码来源:test_mio.py


示例8: save

 def save(self, path=None, full_save=False):
     
     import cPickle as pickle
     import os
     
     path = Analyzer.save(self, path=path)
     
     for roi, scores in self.scores.items():
         
         # Save full object!
         if full_save:
             with open(os.path.join(path, '%s_scores.pickle' %(roi)), 'wb') as output:
                 pickle.dump(scores, output)
                    
         for p, score in enumerate(scores):
                 
             mat_score = self._save_score(score)
                 
             # TODO: Better use of cv and attributes for leave-one-subject-out
             filename = "%s_perm_%04d_data.mat" %(roi, p)
             logger.info("Saving %s" %(filename))
             
             savemat(os.path.join(path, filename), mat_score)
             
     return
开发者ID:robbisg,项目名称:mvpa_itab_wu,代码行数:25,代码来源:roi_decoding.py


示例9: test_fieldnames

def test_fieldnames():
    # Check that field names are as expected
    stream = BytesIO()
    savemat(stream, {"a": {"a": 1, "b": 2}})
    res = loadmat(stream)
    field_names = res["a"].dtype.names
    assert_equal(set(field_names), set(("a", "b")))
开发者ID:Juanlu001,项目名称:scipy,代码行数:7,代码来源:test_mio.py


示例10: test_empty_string

def test_empty_string():
    # make sure reading empty string does not raise error
    estring_fname = pjoin(test_data_path, 'single_empty_string.mat')
    fp = open(estring_fname, 'rb')
    rdr = MatFile5Reader(fp)
    d = rdr.get_variables()
    fp.close()
    assert_array_equal(d['a'], np.array([], dtype='U1'))
    # empty string round trip.  Matlab cannot distiguish
    # between a string array that is empty, and a string array
    # containing a single empty string, because it stores strings as
    # arrays of char.  There is no way of having an array of char that
    # is not empty, but contains an empty string.
    stream = BytesIO()
    savemat(stream, {'a': np.array([''])})
    rdr = MatFile5Reader(stream)
    d = rdr.get_variables()
    assert_array_equal(d['a'], np.array([], dtype='U1'))
    stream.truncate(0)
    stream.seek(0)
    savemat(stream, {'a': np.array([], dtype='U1')})
    rdr = MatFile5Reader(stream)
    d = rdr.get_variables()
    assert_array_equal(d['a'], np.array([], dtype='U1'))
    stream.close()
开发者ID:ChadFulton,项目名称:scipy,代码行数:25,代码来源:test_mio.py


示例11: test_mat_struct_squeeze

def test_mat_struct_squeeze():
    stream = BytesIO()
    in_d = {"st": {"one": 1, "two": 2}}
    savemat(stream, in_d)
    # no error without squeeze
    out_d = loadmat(stream, struct_as_record=False)
    # previous error was with squeeze, with mat_struct
    out_d = loadmat(stream, struct_as_record=False, squeeze_me=True)
开发者ID:Juanlu001,项目名称:scipy,代码行数:8,代码来源:test_mio.py


示例12: test_sparse_in_struct

def test_sparse_in_struct():
    # reproduces bug found by DC where Cython code was insisting on
    # ndarray return type, but getting sparse matrix
    st = {'sparsefield': SP.coo_matrix(np.eye(4))}
    stream = BytesIO()
    savemat(stream, {'a':st})
    d = loadmat(stream, struct_as_record=True)
    yield assert_array_equal, d['a'][0,0]['sparsefield'].todense(), np.eye(4)
开发者ID:ChadFulton,项目名称:scipy,代码行数:8,代码来源:test_mio.py


示例13: test_scalar_squeeze

def test_scalar_squeeze():
    stream = BytesIO()
    in_d = {'scalar': [[0.1]], 'string': 'my name', 'st':{'one':1, 'two':2}}
    savemat(stream, in_d)
    out_d = loadmat(stream, squeeze_me=True)
    assert_(isinstance(out_d['scalar'], float))
    assert_(isinstance(out_d['string'], string_types))
    assert_(isinstance(out_d['st'], np.ndarray))
开发者ID:ChadFulton,项目名称:scipy,代码行数:8,代码来源:test_mio.py


示例14: test_scalar_squeeze

def test_scalar_squeeze():
    stream = BytesIO()
    in_d = {"scalar": [[0.1]], "string": "my name", "st": {"one": 1, "two": 2}}
    savemat(stream, in_d)
    out_d = loadmat(stream, squeeze_me=True)
    assert_(isinstance(out_d["scalar"], float))
    assert_(isinstance(out_d["string"], string_types))
    assert_(isinstance(out_d["st"], np.ndarray))
开发者ID:Juanlu001,项目名称:scipy,代码行数:8,代码来源:test_mio.py


示例15: test_save_empty_dict

def test_save_empty_dict():
    # saving empty dict also gives empty struct
    stream = BytesIO()
    savemat(stream, {'arr': {}})
    d = loadmat(stream)
    a = d['arr']
    assert_equal(a.shape, (1,1))
    assert_equal(a.dtype, np.dtype(object))
    assert_(a[0,0] is None)
开发者ID:dyao-vu,项目名称:meta-core,代码行数:9,代码来源:test_mio.py


示例16: test_long_field_names

def test_long_field_names():
    # Test limit for length of field names in structs
    lim = 63
    fldname = "a" * lim
    st1 = np.zeros((1, 1), dtype=[(fldname, object)])
    savemat(BytesIO(), {"longstruct": st1}, format="5", long_field_names=True)
    fldname = "a" * (lim + 1)
    st1 = np.zeros((1, 1), dtype=[(fldname, object)])
    assert_raises(ValueError, savemat, BytesIO(), {"longstruct": st1}, format="5", long_field_names=True)
开发者ID:Juanlu001,项目名称:scipy,代码行数:9,代码来源:test_mio.py


示例17: test_regression_653

def test_regression_653():
    # Saving a dictionary with only invalid keys used to raise an error. Now we
    # save this as an empty struct in matlab space.
    sio = BytesIO()
    savemat(sio, {'d':{1:2}}, format='5')
    back = loadmat(sio)['d']
    # Check we got an empty struct equivalent
    assert_equal(back.shape, (1,1))
    assert_equal(back.dtype, np.dtype(object))
    assert_(back[0,0] is None)
开发者ID:dyao-vu,项目名称:meta-core,代码行数:10,代码来源:test_mio.py


示例18: test_structname_len

def test_structname_len():
    # Test limit for length of field names in structs
    lim = 31
    fldname = 'a' * lim
    st1 = np.zeros((1,1), dtype=[(fldname, object)])
    savemat(BytesIO(), {'longstruct': st1}, format='5')
    fldname = 'a' * (lim+1)
    st1 = np.zeros((1,1), dtype=[(fldname, object)])
    assert_raises(ValueError, savemat, BytesIO(),
                  {'longstruct': st1}, format='5')
开发者ID:ChadFulton,项目名称:scipy,代码行数:10,代码来源:test_mio.py


示例19: test_empty_sparse

def test_empty_sparse():
    # Can we read empty sparse matrices?
    sio = BytesIO()
    import scipy.sparse
    empty_sparse = scipy.sparse.csr_matrix([[0,0],[0,0]])
    savemat(sio, dict(x=empty_sparse))
    sio.seek(0)
    res = loadmat(sio)
    assert_array_equal(res['x'].shape, empty_sparse.shape)
    assert_array_equal(res['x'].todense(), 0)
开发者ID:ymarfoq,项目名称:outilACVDesagregation,代码行数:10,代码来源:test_mio.py


示例20: test_round_types

def test_round_types():
    # Check that saving, loading preserves dtype in most cases
    arr = np.arange(10)
    stream = BytesIO()
    for dts in ("f8", "f4", "i8", "i4", "i2", "i1", "u8", "u4", "u2", "u1", "c16", "c8"):
        stream.truncate(0)
        stream.seek(0)  # needed for BytesIO in python 3
        savemat(stream, {"arr": arr.astype(dts)})
        vars = loadmat(stream)
        assert_equal(np.dtype(dts), vars["arr"].dtype)
开发者ID:Juanlu001,项目名称:scipy,代码行数:10,代码来源:test_mio.py



注:本文中的scipy.io.matlab.mio.savemat函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Python streams.make_stream函数代码示例发布时间:2022-05-27
下一篇:
Python mio.loadmat函数代码示例发布时间:2022-05-27
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap