matplotlib: latex strings are not interpreted

If the LaTeX strings are not intepreted in matplotlib (e.g., in the xlabel), check your configuration file
( see previous post for matplotlib configuration file).

You can either do it for one session :

   rc('text', usetex=True)
   plot([1,2],[3,4]); title('a^2')

or permanently within the matplolibrc by adding this line:

text.usetex        : true
Posted in Python | Tagged , | Leave a comment

Matplotlib configuration

Note 4/Sept/2013: the new location is /home/user/.config/matplotlib

If you have ipython, try this command

ipython -pylab

and if you have Qt, you can change the backend (library used for plotting) and use qt as follows:

ipython -pylab qt

Matploltib set up a special file called matplotlibrc, which can be found in the directory .matplotlib/ ( with a dot in front of it).

To locate your .matplotlib/ directory, use matplotlib.get_configdir():

   >>> import matplotlib as mpl
   >>> mpl.get_configdir()
   '/home/user/.matplotlib'

If you would like to use a different configuration directory, you can do so by specifying the location in your MPLCONFIGDIR environment variable

In order to fill the configuration file you will need to know the keywords. The full list is available here: http://matplotlib.org/users/customizing.html

Posted in Python | Tagged , | Leave a comment

Fitting distribution by combing R and Python

The following example illustrates how to use R package with Python by using the Rpy2 package.
This problem solve in this example is to fit a normal distribution on some data samples.

 
from rpy2.robjects import r
from rpy2.robjects.packages import importr
 
rnorm = r('rnorm')  # create an alias to the R function rnorm
x = rnorm(n=1000, mean=0, sd=1) # generates the data sample (normal distribution)
 
# load the MASS library for distribution fitting
MASS = importr('MASS')
params = MASS.fitdistr(x, 'normal')  # perform the fitting here
print params

The result is stored in the variable params. The above code print the following results:

      mean           sd     
  -0.07962739    1.00467570 
 ( 0.03177064) ( 0.02246523)

Telling you that the distribution has mean of -0.08 and a standard deviation of 0. The bracketted numbers indicates how trustful are the results. Numbers in parentheses are confidence intervals around the parameter.

Posted in Python | Tagged , , | Leave a comment

Profiling in Python

Lot of information is available in the Python documentation website: http://docs.python.org/library/profile.html . However, here is a small snapshot of how to use it :

Let us say you have a function call test(). First you need to run it:

import cProfile
cProfile.run('from your_module import test; test()', 'prof')

It will save profiling results in ‘prof’, which can then be parsed with pstats module:

import pstats
p = pstats.Stats('fooprof')

You can then introspect the data. For instance the following code sort the results by time and print the 10 largest one.

p.sort_stats('time').print_stats(10)
Posted in Python | Tagged | Leave a comment

vim: replace the windows ^M character under Linux

Within VIM, there is a way to replace all the return carriage from Windows (that appear as ^M) by replacing them with the Linux version:

% s/^M/\r/g

When \r stands for the return carriage in Linux (not \n as C users would expect) and ^M is not a concatenation of the characters ^ and M but a special character create by typing “Ctrl+V Enter” in VIM.

Posted in Computer Science | Tagged | Leave a comment

SVN: How to revert (roll back) to a previous version ?

One reason to use SVN tool is to be able to revert to a previous version. How do we do that ?
Imagine that the latest version (the one on the HEAD) is not what you want anymore, but that the revision 13 is the good one. Just type:

svn merge -rHEAD:13

Commit the code, and you’ve go the code as it was at revision 13.

Posted in Computer Science | Tagged | Leave a comment

running PHP from Command Line Shell

If you want to test some PHP code without using the browser interface, you can create a script to use command line shell as follows. Let us call this script scripting.php. Under Linux, create this file with the php extension and specify the executable path:

#!/usr/bin/php -q
echo 'Hello';

Change the mode to executable:

chmod 755 scripting.php

and run it :

./scripting.php

It should work if your PHP is already well configured.

Posted in Computer Science | Tagged | Leave a comment

R: how to use command line arguments from R scripts

If you want to script R with arguments, you can type (under a Linux shell):

R --no-restore --no-save --args 10 < script.R

where –args is a list of arguments that you want to use in your script. This script will contain the following commands:

args <- commandArgs(trailingOnly=TRUE)

where trailingOnly means that commandArgs uses only the –args option so that you do not have to bother about the position of th –args argument.

Posted in Computer Science | Tagged | Leave a comment

Thunderbird hangs forever when “Copying message to Sent folder”

I set up a new account under Thunderbird with an IMAP in-going and SMTP out-going. All mails and folders are correclty imported from the server. I received new mails can delete them (it is updated on the server). So everything looks good. However, when I try to send a new message, the message is sent correctly but while trying to save the mail in the ‘Sent’ folder, it hangs forever.

What is happening is that Thunderbird wants to save a copy of the sent message in a folder called ‘Sent’ which is located in the root of the account folders. If the folder does not exist, Thunderbird will attempt to create it. If it cannot be created, Thunderbird will hang until you cancel the send. In my case, this folder already exists so Thunderbird hangs forever.

One solution is to prevent Thunderbird to save the file since your server is already taking care of that action. So, you need to disable the client to save the file on the server but only locally:

    Tools –> Account Settings
    Select 'Copies and Folders' under your account.
    Look for the box that says 'When sending messages, automatically…' serve that there is a checkbox below it labelled 'Place a copy in…'
    Switch from "Sent folder on " + <you account > to the one below that says "Other" and select the "Sent" folder)
Posted in Internet related, Uncategorized | Tagged , | 77 Comments

Fedora 15 : Firefox drag and drop does not work

The solution is to install gtk2-engines package:

sudo yum install gtk2-engines.i686
Posted in Linux | Tagged , | Leave a comment