heatmap of the visual information retrieval experiment – part 3

Finally I corrected a couple of bugs in the script that I was using for the visualization. Then I spent a considerable amount of time to try to overlay the obtained heatmap to the original results map I obtained before. PIL is a great resource in this sense. I found a couple of hacks that were pointing in that direction [1], [2]. Unfortunately, they did not work for my case.

Finally I decided to give a try to the embedded function called blend. It worked well for my situation.

So what I am doing in the script is to:

1. divide the map into a certain number of cells;

2. count the frequency with which the items pile in each cell;

3. export the obtained matrix in csv (comma separated values);

4. open this file with R via RPy in the script;

5. process this file so to obtain the wanted heatmap;

6. save the heatmap in an external file;

7. process the heatmap with PIL so to make it right for the other images;

8. blend the map over the other existing images.

The part of the code that do the “interesting” part of this is reported below. The results is showed in the pictures below.

Lsivscng Map Freq

Tags: , ,


Ehm … Sorry if the code is a bit messy …

def heatmapper(case, dens_matrix):

    “”” This method will build the heatmap corresponding to each of the four cases.

    Then this image will be treated with PIL to make it compatible with the others for

    further analysis.

        “””

    # this is to build the header for the file

    alphabet = ‘abcdefghijklmnopqrstuvwxyz’

    alphabet2 = ‘ abcdefghijklmnopqrstuvwxyz’

    header = “;”.join([strip(alphabet2[x/26])+alphabet[x%26] for x in range(density)])

    # let’s start writing the csv file

    f = open(‘./exp1_files/exp1_hm_%s.csv’%case, ‘w’)

    f.write(header+’\n’)

    # for each row of the matrix

    for row in dens_matrix:

        raw = []

        for element in row:

            if element[1] == 0: # you need to change with the map below

                raw.append(str(0))

            else:   

                # 1st map

                # I select the general frequency of the results

                # raw.append(str(element[0]))

                # 2nd map

                # I select the number of items read over the total

                # raw.append(str(float(element[1]) / float(element[0])))

                # 3rd map

                # items selected over the total

                # raw.append(str(float(element[2]) / float(element[0])))

                # 4th map

                # items selected over items read

                raw.append(str(float(element[2]) / float(element[1])))

               

        f.write(“;”.join(raw)+’\n’)

    f.close()

    # now we have the exchange file we can start the R processing

    r.library(“graphics”)

    r(‘vir_hm_%s <- read.table(“./exp1_files/exp1_hm_%s.csv”, header = TRUE, sep = “;”)’%(case,case))

    r(‘x  <- as.matrix(vir_hm_%s)’%case)

    # print “Heatmap as PNG,”

    r.X11(display = “craftmac2.epfl.ch:0.0”)#, width = 7, height = 7)

    r.png(“./exp1_files/heatmap_%s.png”%case, width=1200, height=1200)

    r(‘heatmap(x, Rowv = NA, Colv = NA, scale=”column”, col = topo.colors(32), main = NA, xlab = NA, ylab = NA)’)

    r.dev_off()

    # now we work on the picture with PIL

    im = Image.open(“./exp1_files/heatmap_%s.png”%case)

    box = (43,71,1110,1138)

    hm_im = im.crop(box)

    hm_im = hm_im.rotate(180)

    hm_im = hm_im.resize((1000,1000))

    hm_im = hm_im.convert(“RGBA”)

    hm_im.save(“./exp1_files/heatmap_%s.png”%case, ‘png’)

    print “Done heatmap case “+case

    return

Leave a Reply