source: subversion/applications/rendering/genericHeatmapRender/heatmapPNGgenerator.py @ 29094

Last change on this file since 29094 was 16116, checked in by stevefaeembra, 10 years ago

initial version.. very basic heatmapping code using Python/Matplotlib?

File size: 4.1 KB
Line 
1'''
2    This program is free software; you can redistribute it and/or modify
3    it under the terms of the GNU General Public License as published by
4    the Free Software Foundation; either version 2 of the License, or
5    (at your option) any later version.
6
7    This program is distributed in the hope that it will be useful,
8    but WITHOUT ANY WARRANTY; without even the implied warranty of
9    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10    GNU General Public License for more details.
11
12    You should have received a copy of the GNU General Public License along
13    with this program; if not, write to the Free Software Foundation, Inc.,
14    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
15'''
16
17
18import numpy as np
19from matplotlib.mlab import griddata
20import matplotlib.pyplot as plt
21import matplotlib.image as im
22import matplotlib.pylab as pylab
23import numpy.ma as ma
24from numpy.random import uniform
25from numpy.random import random_integers
26import string
27from location import location
28import math
29
30class heatmapPNGgenerator(object):
31    '''
32    Uses matplotlib to generate a PNG of a heatmap
33    (currently, on the local file system only)
34    Using PNG as output to allow alpha transparency
35    replace the following attributes with paramaters
36    when calling Render() to tweak the settings
37    '''
38
39    showContours=False      #show contours?
40    showLocations=False     #show locations?
41    showFill=True           #fill bands?
42    logarithmic=True        #log10 scale
43    bands=50                #number of contour bands
44    opacity=0.7             #opacity
45    colorscheme=plt.cm.jet  #colour scheme
46    target="c:\\w00t.png"   #save to filename
47    title="Heatmap"         #not used
48    save=True               #save to file
49    display=False           #show on Matplotlib window
50    resolution=(400,300)    #grid resolution (width, height). May want to increase for larger areas to reduce pixelisation (but takes longer)
51
52    def __init__(self,_locations,**kwargs):
53        '''
54        Constructor, allow override of defaults shown above
55        e.g. hmg=heatmapPNGgenerator(mylocations,opacity=0.4,logarithmic=False,bands=10,resolution=(200,100))
56        '''
57        self.locations=_locations
58        if len(kwargs)>0:
59            # overwrite defaults shown above.
60            for key in kwargs.keys():
61                self.__setattr__(key,kwargs[key])
62           
63    def render(self):
64        '''
65        Render to target png
66        '''
67        x=self.locations.getX()
68        y=self.locations.getY()
69        z=self.locations.getZ()
70       
71        assert not(self.save and self.display), "cannot call render() with BOTH save and display options set to True."
72        assert len(x)==len(y)==len(z), "x, y and z arrays must be same size"
73        assert len(x)>2, "Need at least 3 points to generate a polygon  (got %d)" % len(x)
74       
75        if self.logarithmic: z=[math.log10(u) for u in z]
76        minx,miny,maxx,maxy=self.locations.getBounds()
77        print "Bounds BL(%.2f,%.2f) TR(%.2f,%.2f)" % (minx,miny,maxx,maxy)
78   
79        # max fullscreen...
80        fig=plt.figure()
81        left, bottom, width, height = (0.00,0.0,1.0,1.0) 
82        llbounds = (left,bottom,width,height) 
83        ax = fig.add_axes(llbounds) 
84        fig.figurePatch.set_alpha(0.8)
85       
86        # define grid.
87        xi = np.linspace(minx,maxx,self.resolution[0])
88        yi = np.linspace(miny,maxy,self.resolution[1])
89       
90        # grid the data.
91        zi = griddata(x,y,z,xi,yi)
92       
93        # contour the gridded data, plotting dots at the randomly spaced data points.
94        if self.showContours : CS = plt.contour(xi,yi,zi,bands,linewidth=0.0,colors='k')
95        if self.showFill: CS = plt.contourf(xi,yi,zi,self.bands,alpha=self.opacity,linewidth=0.0,cmap=self.colorscheme)
96       
97        # plot data points.
98        if self.showLocations: plt.scatter(x,y,marker='+',c='b',s=5)
99   
100        plt.xlim(minx,maxx)
101        plt.ylim(miny,maxy)
102        plt.title(str(self.title))
103        if self.display: plt.show()
104        if self.save: plt.savefig(self.target,transparent=True) 
Note: See TracBrowser for help on using the repository browser.