Numpy arrays and Matlab

I need to move some of the numpy and scipy matrices generated in python to matlab so that I can use the cvx package for optimization. I used the matrix market format to export data from python to matlab. To do so we need to do the following:

import as sio
#A is the required matrix sparse or dense
sio.mmwrite(filename, A)
#note the extension .mtx is given to the filename by scipy

To work with matrix market format in matlab we need to have the files – mminfo.m, mmread.m, mmewrite.m all them can be found from the matrix market website. These files must be present either in the present directory in matlab or in the path directories. Suppose the file ‘Mat1.txt.mtx’ contained our matrix that we saved from python to read it in matlab we need to just write the following code.

A = mmread('Mat1.txt.mtx')

The required matrix will be stored as in variable A.

Filling missing data in python timeseries

I am using the ‘SCIKITS.TIMESERIES‘ python library for time series analysis. Here is how to fill the missing dates and the default data in the time series. The version 0.91.3 has bug in its timeseries.fill_missing_dates() method. One of the arguments it takes is fill_value, this is the default value we want to set for the missing data. But it does not work as intended. In fact the missing data is masked. To fill in the required data one must use the timeseries.filled(fill_value) method. Here is an example:

>>>import scikits.timeseries as ts

>>> datarr = ts.date_array(['2009-01-01', '2009-01-05'], freq='D')
>>> datarr
DateArray([01-Jan-2009, 05-Jan-2009],

>>> sr1 = ts.time_series([3,4], datarr)
>>> sr1
timeseries([3 4],
dates = [01-Jan-2009 05-Jan-2009],
freq  = D)

>>> m1 = sr1.fill_missing_dates(fill_value=0)
>>> m1
timeseries([3 -- -- -- 4],
dates = [01-Jan-2009 ... 05-Jan-2009],
freq  = D)

>>> m1.filled(0)
timeseries([3 0 0 0 4],
dates = [01-Jan-2009 ... 05-Jan-2009],
freq  = D)

EPD Python on Ubuntu

Enthought Python Distribution or EPD as it is popularly known as is a Python distribution with lot of libraries already available in it. Whats good about it? The libraries are in their latests version available (or may be one or two versions lower). Ubuntu repositories also have these libraries so one can install them from there. But, the problem with Ubuntu repositories is that they contain pretty old versions of the library. For example the current version of Networkx available in Natty repositories is 1.1 while that current stable version of Networkx is 1.5.

EPD is installed using the shell script installer enthought provides. I have installed it in the home directory. So by typing ‘python’ in terminal I do not have access to EPD python. What I did is the following – I made a link named it ‘pythoEPD’ and copies the link to ‘/usr/bin’. This way I will be able to access EPD python from anywhere on terminal by typing ‘pythonEPD’.

Although EPD has a good collection of libraries it might not have all that you need. I have not been very successful in installing downloaded libraries to the EPD library list. So it will be good to install them from ubuntu repositories using the awesome ‘apt-get’ command. But, these will not be available to EPD python. To make them available to the EPD python distribution one needs to add the standard python path to the EPD python path. To do that open terminal.

  1. Run python by typing – python.
  2. import sys; sys.path
  3. The above command will show you the standard python path. copy the entire list to clipboard.
  4. Open another terminal run – pythonEPD (to run EPD python).
  5. paste the list to a variable, say x.
  6. iterate over its entries and append it to the EPD python path.