Skip to content

Instantly share code, notes, and snippets.

@drasch
Last active December 28, 2015 06:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drasch/7456328 to your computer and use it in GitHub Desktop.
Save drasch/7456328 to your computer and use it in GitHub Desktop.

Octave to Python

Octave

octave:1> a = rand(10,10)
a =

  0.859429   0.895490   0.366489   0.209172   0.115529   0.212583   0.346897   0.737492   0.520998   0.765509
  0.458690   0.832058   0.600122   0.703332   0.339640   0.579577   0.330600   0.228996   0.669966   0.292111
  0.690453   0.306055   0.040796   0.379313   0.295865   0.064997   0.925082   0.709484   0.145930   0.787097
  0.995525   0.717836   0.641777   0.189449   0.610176   0.976574   0.316809   0.709783   0.588515   0.694197
  0.578332   0.285901   0.989679   0.834628   0.914659   0.033759   0.798227   0.715250   0.171039   0.617217
  0.201751   0.233143   0.055314   0.182800   0.021884   0.305863   0.161870   0.467997   0.625401   0.504653
  0.979039   0.182347   0.284751   0.144414   0.871048   0.327347   0.135534   0.687959   0.118036   0.771967
  0.656888   0.463329   0.373998   0.176467   0.169954   0.812317   0.940715   0.471986   0.513240   0.363201
  0.923390   0.520225   0.859957   0.084463   0.650109   0.563110   0.085505   0.661934   0.590937   0.353477
  0.011857   0.850067   0.612539   0.618590   0.286480   0.844025   0.427313   0.112950   0.688636   0.364019

octave:2> save("-hdf5", 'test.h5')

Python

from tables import *
import numpy as np
In [10]: h5file = openFile('test.h5', mode='r')

In[27]: np.array(h5file.getNode('/a/value'))
Out[27]: 
array([[ 0.85942853,  0.45869015,  0.69045291,  0.9955255 ,  0.57833199,
        0.20175055,  0.97903879,  0.65688757,  0.92339013,  0.01185726],
      [ 0.89548958,  0.83205841,  0.30605546,  0.71783626,  0.28590104,
        0.2331425 ,  0.18234741,  0.46332915,  0.52022491,  0.85006653],
      [ 0.3664888 ,  0.60012242,  0.04079594,  0.64177667,  0.98967891,
        0.05531351,  0.28475066,  0.37399819,  0.8599565 ,  0.61253857],
      [ 0.2091725 ,  0.70333161,  0.37931316,  0.18944913,  0.83462842,
        0.18280043,  0.14441352,  0.1764671 ,  0.08446347,  0.61859039],
      [ 0.11552948,  0.33963994,  0.29586523,  0.6101756 ,  0.91465903,
        0.02188408,  0.87104812,  0.16995431,  0.65010927,  0.28648005],
      [ 0.21258272,  0.57957659,  0.06499659,  0.97657428,  0.03375861,
        0.30586279,  0.32734666,  0.81231662,  0.56311025,  0.8440249 ],
      [ 0.34689707,  0.33059982,  0.92508155,  0.31680851,  0.79822683,
        0.16187031,  0.13553413,  0.94071459,  0.08550529,  0.42731278],
      [ 0.73749221,  0.22899569,  0.70948351,  0.7097833 ,  0.71525036,
        0.4679971 ,  0.6879589 ,  0.47198606,  0.66193417,  0.11295035],
      [ 0.52099847,  0.66996573,  0.14593022,  0.58851511,  0.17103871,
        0.62540062,  0.11803642,  0.51324018,  0.59093715,  0.68863624],
      [ 0.76550949,  0.29211147,  0.78709733,  0.69419733,  0.61721683,
        0.50465322,  0.77196659,  0.3632014 ,  0.35347675,  0.36401924]])

Python to Octave

Python

In [28]: s2 = openFile('test2.h5', mode='w')

In [29]: s2.createArray(s2.root,'a', np.random.randn(10,10))
Out[29]: 
/a (Array(10, 10)) ''
  atom := Float64Atom(shape=(), dflt=0.0)
  maindim := 0
  flavor := 'numpy'
  byteorder := 'little'
  chunkshape := None

In [30]: s2.close()

Octave

octave:1> load 'test2.h5'
octave:2> a
a =

  0.7131209  -2.1341231   1.2262886  -1.2291569  -0.2285198  -0.4324507   0.2445414   0.8702512   0.4899783  -1.2861712
  1.3053592  -1.5935487  -0.2166692  -0.9083949  -0.1680680  -0.2283791   0.2474000   1.0191515   0.0813963  -1.3614287
  -0.7396352  -1.3789601  -0.2585963  -0.2461638  -0.2263464  -0.0730651  -0.7571070   0.8451095  -2.9420820  -0.6064189
  0.1768817  -0.2709983   0.8999636   0.2608454   1.2002653  -1.3739324  -0.1487325   0.1581497   0.5541764  -0.2338047
  -0.3878351   1.0805891  -0.2784655  -0.2252829   0.1414650  -1.0420837   2.0748913  -2.8347940   1.7320956  -0.9222796
  1.2738671  -0.7286286  -0.6598480  -2.2519772  -0.7001558   0.1603010  -0.5332355  -0.0500654   0.3594471  -0.6885272
  -0.4748571  -1.1039091  -0.2261936   0.7115729   0.3370656   1.3467805  -0.8753468  -0.3203379   0.8127135   1.1378636
  0.4688077  -0.1723309   0.0071681   0.3727903   0.5151999  -0.4269389   0.3310272   0.5319466  -0.9313046   0.6295486
  0.4547873  -1.4798898  -1.5841713   0.2594823  -1.2110460   0.8441917  -1.8153448  -1.4499722  -0.1499643   1.5062362
  0.0452690   0.0450779   0.1782524   0.3836340  -1.0348064  -2.9670641   0.7806250  -0.8790209  -0.9783835   0.0262051
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment