-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvisualisation.py
executable file
·196 lines (124 loc) · 4.78 KB
/
visualisation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Functions for visualising frames and clusters.
"""
#...for the MATH.
import numpy as np
#...for the plotting.
import pylab as plt
#...for the colours.
from matplotlib import colorbar, colors
#...for setting the axes ticks.
from matplotlib.ticker import MultipleLocator, FormatStrFormatter
def addRadiusCircle(figax, x, y, r):
""" Draws a circle representing the cluster radius. """
# Adjust the centre coordinates to account foe the pixel size.
x_c = x + 0.5; y_c = y + 0.5
# Set the size of the "cross-hairs".
rl = 1.5
# Add the "cross-hairs" to the plot.
figax.plot([x_c-rl,x_c+rl], [y_c-rl,y_c+rl], 'k-', lw=1)
figax.plot([x_c-rl,x_c+rl], [y_c+rl,y_c-rl], 'k-', lw=1)
# Add the circle representing the cluster radius to the plot.
figax.add_patch(plt.Circle((x_c,y_c),r,fill=False,lw=3.0))
figax.add_patch(plt.Circle((x_c,y_c),r,fc='k',alpha=0.1,lw=3.0))
figax.add_patch(plt.Circle((x_c,y_c),r,fill=False,lw=1.0,ec='g'))
def addLineOfBestFit(figax, m, c):
""" Adds a line of best fit to the cluster image. """
## The x values.
xs = np.arange(0.0,256.0,0.1)
## The y values.
ys = m*(xs - 0.5) + c + 0.5
figax.plot(xs, ys, 'k-', lw=3)
figax.plot(xs, ys, 'g-', lw=1)
def makeKlusterImage(klusterid, kl, outputpath):
""" Create the kluster image. """
pixels = kl.getPixelMap()
x_min = kl.getXMin()
x_max = kl.getXMax()
y_min = kl.getYMin()
y_max = kl.getYMax()
w = kl.getWidth()
h = kl.getHeight()
## The maximum count value.
C_max = kl.getMaxCountValue()
x_bar = kl.getXUW()
y_bar = kl.getYUW()
radius = kl.getRadiusUW()
m, c, sumR = kl.getLineOfBestFitValues()
# Create the figure.
plt.close('all')
figsize = 5.0
## The figure for the cluster image.
blobfig = plt.figure(1, figsize=(figsize*1.27, figsize), dpi=150, facecolor='w', edgecolor='w')
# Set the beyond-frame background colour.
blobfigax = blobfig.add_subplot(111, axisbg='#222222')
# Add the frame background (blue).
blobfigax.add_patch(plt.Rectangle((0,0),256,256,facecolor='#82bcff'))
# Add a grid.
plt.grid(1)
# Select the "hot" colour map for the pixel counts.
cmap = plt.cm.hot
colax, _ = colorbar.make_axes(plt.gca())
col_max = 10*(np.floor(C_max/10.)+1)
colorbar.ColorbarBase(colax,cmap=cmap,norm=colors.Normalize(vmin=0,vmax=col_max))
# Add the line of best fit.
addLineOfBestFit(blobfigax, m, c)
# Add the radius circle.
addRadiusCircle(blobfigax, x_bar, y_bar, radius)
# Loop over the pixels and plot them.
for X, C in pixels.iteritems():
x = X % 256; y = X / 256
scaled_C = float(C)/float(col_max)
blobfigax.add_patch(plt.Rectangle((x,y),1,1,facecolor=cmap(scaled_C)))
# Set the axis limits based on the cluster radius.
b = 3 # border
xlim_min = x_bar - (np.floor(radius)+b)
xlim_max = x_bar + (np.floor(radius)+b)
ylim_min = y_bar - (np.floor(radius)+b)
ylim_max = y_bar + (np.floor(radius)+b)
blobfigax.set_xlim([xlim_min, xlim_max])
blobfigax.set_ylim([ylim_min, ylim_max])
# Set the axis tick mark spacing.
blobfigax.xaxis.set_major_locator(MultipleLocator(10))
blobfigax.yaxis.set_major_locator(MultipleLocator(10))
# Save the figure.
blobfig.savefig(outputpath + "/%s.png" % (klusterid))
def makeFrameImage(basename, pixels, outputpath):
""" Create the frame image. """
x_min = 0
x_max = 256
y_min = 0
y_max = 256
w = 256
h = 256
## The maximum count value.
C_max = max(pixels.values())
# Create the figure.
plt.close('all')
figsize = 5.0 #max(radius*0.8, 3.0)
## The figure for the frame.
frfig = plt.figure(1, figsize=(figsize*1.27, figsize), dpi=150, facecolor='w', edgecolor='w')
## The frame axes.
frfigax = frfig.add_subplot(111, axisbg='#222222')
# Add the frame background (blue).
frfigax.add_patch(plt.Rectangle((0,0),256,256,facecolor='#82bcff'))
# Add a grid.
plt.grid(1)
# Select the "hot" colour map for the pixel counts.
cmap = plt.cm.hot
colax, _ = colorbar.make_axes(plt.gca())
col_max = 10*(np.floor(C_max/10.)+1)
colorbar.ColorbarBase(colax,cmap=cmap,norm=colors.Normalize(vmin=0,vmax=col_max))
# Loop over the pixels and plot them.
for X, C in pixels.iteritems():
x = X % 256; y = X / 256
scaled_C = float(C)/float(col_max)
frfigax.add_patch(plt.Rectangle((x,y),1,1,edgecolor=cmap(scaled_C),facecolor=cmap(scaled_C)))
# Set the axis limits based on the cluster radius.
b = 3 # border
frfigax.set_xlim([0 - b, 256 + 3])
frfigax.set_ylim([0 - b, 256 + 3])
# Save the figure.
frfig.savefig(outputpath + "/%s.png" % (basename))