Allocation 2D arrays in C (and freeing memory)

By convention, when dealing with 2D array, the first dimension refer to the rows, and the second to the columns.

To create a 2D array (double pointer) in C, you first create a 1D array of pointers (rows), and then, for each row, create another one dimensional array (columns):

    double** array;  
    array = (double**) malloc(nX*sizeof(double*));  
    for (int i = 0; i < nX; i++)  
    {
       array[i] = (double*) malloc(nY*sizeof(double));  
       /* then array[i][j] = values */
    }

At the end, do not forget to free the memory, each row that has been created, and then the array pointer itself:

    for (i = 0; i < nX; i++){  
       free(array[i]);  
    }     
 free(array);
Posted in Uncategorized | Tagged | Leave a comment

RPy2 substitute function does not work

I’m using rpy2 to call R package from Python, which works pretty well most of the time. Yet, I’ve encountered an issue with the R function called substitute that do not behave as expected when called from rpy2. Consider the following R function that uses substitute to print the name of the argument called arg1 (not its content). Let us suppose that arg1 is a float, which value equals 1.

test_substitute <-
function(arg1){
        print(substitute(arg1))
        # do something with arg1
        print(arg1)
    }

Then, you can call the function from an R interface. It should behave as follows (The >>> signs indicates the prompt):

>>> a<-1
>>> test_substitute(a)
a
[1] 1

Now, in python, using RPY2, you will use the following code (assuming your R function is in the package Test::

>>> # importing the R function
>>> from rpy2 import robjects
>>> from rpy2.robjects.packages import importr
>>> Test = importr("Test") 
 
>>> a= 1.
>>> print Test.test_substitute(a)
[1] 1
[1] 1

The function prints the contents of a and not its name (we expected “a” as in the R interface)

Now, I don’t know any perfect solution, but from the Rpy2 documentation, I’ve seen on the

robjects formulae.html page some usage of robjects.r that could be useful and indeed, it makes the trick. Here is a solution.

You can create a function using robject.r :

>>> f = robjects.r('test_substitute')
>>> a = 1.
>>> f(a)
[1] 1
[1] 1

Yet, this does not work either, you need to create the R code an call it using robjects.r

>>> a = 1.
>>> robjects.r('a<-1; test_substitute(%s)' % 'a')
a
[1] 1
Posted in Computer Science | Tagged , | Leave a comment

Set the default printer under linux

If you want to set the default printer, you can use the command line interface and type:

export PRINTER=printer-name

If you want to set the default printer permanently, and if your shell is bash (most common by default), you should add this line in the ~/.bashrc file:

export PRINTER=printer-name

If you use another type of shell such as csh or tcsh, put this line in your ~/.cshrc or ~/.tcshrc files:

setenv PRINTER printer-name
Posted in Linux | Tagged , | Leave a comment

super() function in Python raises a TypeError

Playing with class inheritance, I came across an unexpected behavior of super() function, which is used in the inheritance process.

Consider this simple example of a class MyGraph thah inherits from a DiGraph class from networkx:

import networkx as nx
 
class MyGraph(nx.DiGraph):
 
    def __init__(self, data):
        super(MyGraph, self).__init__()
        self.data = data

Then, you can create an instance as follows:

a = MyGraph([1,2,3])

and everything works as expected. However, when I reloaded the module after some changes (or not), and tried to instantiate again, I got this error message:

TypeError: super(type, obj): obj must be an instance or subtype of type

Looking on the web, it appears that the problem resides in the mechanism of reloading modules. Reloading a module often changes the internal object in memory which makes the isinstance test of super return False. Indeed, if you use a print statement to check if the instance is an instance of MyGraph, a False is returned !!

I haven’t yet found a solution to this issue. More information related to this topic can be found in the
reload section of Python documentation

Solution and thorough explanation can be found in this good post on this subject.

Follow-up and solution: In the example above, I was reloading not only the module that causes problem but also another module. It appears, that the order matters ! I used :

If I inverse the reload order, then everything seems to work as expected...

Posted in Python | Tagged , | 7 Comments

a2ps usage

a2ps is a unix tool that ease the formatting of text files before printing.

a2ps inputfile -o output.ps

Print code that span over more than 80 columns. You will need to decrease the font size (-f 7), use only one columns, in portrait (-r)

a2ps -f 7 -r --columns=1  inputfile  -o output.ps
Posted in Linux | Tagged | Leave a comment

How to generate a PDF reference of an R package

If you have the Rd files in a man directory, then in a shell command, you can use this command:

R CMD Rd2dvi --pdf --title='Test of foo' -o /tmp/foo.pdf man/*.Rd
Posted in Computer Science | Tagged , | Leave a comment

HTML password protected

Password protecting a website (or a sub directory within a website) can be done easily thanks to two special files called .htaccess and .htpasswd

First, in the directory that you want to be protected, copy and paste the following code into a file called .htaccess :

AuthType Basic
AuthName "restricted area"
AuthUserFile /path/to/the/directory/you/are/protecting/.htpasswd
require valid-user

The exact path to the file is obviously important. Be aware that if wrong, it will still ask for a password but nothing will seem to work.

Second, you need to create the .htpasswd file, which looks like:

username:epgYW4tTGNZAB

The .htpasswd file should be made of one username and password per line, separated by a colon.
The password is encrypted so you will need to use a special tool to encrypt your password in this way (MD5).

Under Linux,you can type

echo -n "password" | md5sum | cut -d ' ' -f 1

or you can use the apache tool called htpasswd:

htpasswd -d -nb username password

Here -d means use the CRYPT protocol (note that there is a random seed so you may have several crypted passwords for a single password.

There are also quite a lot of online generator to be used if you do not have the tools mentionned above. Finally note that there are several protocols (CRYPT, MD5, SHA). It depends on your system. For instance MD5 is for apache servers only.

Posted in Internet related | Tagged | Leave a comment

pdf2ps font issues

Sometimes, when converting a PDF file to a PS file using ps2pdf, some fuzzy fonts may appear. A solution is to provide the fonts within the documents by using:

pdf2ps -dEmbedAllFonts=true <input> <output>
Posted in Linux | Tagged , | 2 Comments

scp does not work: wrong bashrc

When I tried to use scp to copy a file on a distant machine:

scp -v filename username@xxx.xxx.xxx:/home/username

it looked as if the SCP command works but the file is not copied. The interactive option (-v) does not provide any hints.

The problem seems to reside in the bash environment. If I remove the different parts of the bashrc, it works. So, you need to disable the bashrc. It seems to be known that when called by SSH, an SCP command calls the user’s login shell. To disable it, add this line in the .bashrc file in the home directory:

[ -z "$PS1" ] && return
Posted in Linux | Tagged , , | 3 Comments

How to resize images with awk and convert

If photos in a directory are too large, you may want to use a script to automatically convert all the files into another format. AWK and convert tools can help you. The following command should convert all the files with JPG extension into 1024×680 images.

mkdir converted
cd converted
ls ../*.JPG | awk '{print "convert " $1 " -resize 1024x680 " substr($1, 4, length($1)-7)".jpg "} ' > runme.sh
chmod 755 runme.sh
./runme.sh
rm -f runme.sh

The executable convert is part of ImageMagick package.

Posted in Linux, photos | Tagged , | Leave a comment