forked from dionysio/SlepemapyAnalysis
/
map.py
408 lines (305 loc) · 16.1 KB
/
map.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
# -*- coding: utf-8 -*-
from drawable import *
import analysis
import pandas as pd
import numpy as np
from sys import maxint
import colorbrewer
from codecs import open as copen
from kartograph import Kartograph
#Helper functions for bin classification
def _nested_means_classification(data,num):
"""recursive helper function of nested-means
"""
if num<=0 or data.empty:
return []
breaks = [data.mean()]+_nested_means_classification(data[data<mean],num-1)+_nested_means_classification(data[data>=mean],num-1)
breaks = list(set(breaks)) #drop duplicate bins
breaks.sort()
return breaks
def nested_means_classification(data,classes=8):
"""Data is divided by nested-means.
:param data: values to bin
:param classes: divide values into this many bins, should be a power of 2
"""
if len(data)<classes:
return [data.min()-1,data.max()+1]
breaks = [data.min()-1,data.max()+1]+_nested_means_classification(data,np.log2(classes))
breaks = list(set(breaks)) #drop duplicate bins
breaks.sort()
return breaks
def equidistant_classification(data,classes=8):
"""Data is divided by equally distant ranges.
:param data: values to bin
:param classes: divide values into this many bins
"""
x = (data.max() - data.min())/classes
breaks = [data.min()-1,data.max()+1]+[i*x for i in range(1,classes)]
breaks = list(set(breaks)) #drop duplicate bins
breaks.sort()
return breaks
def jenks_classification(data, classes=8):
"""Port of original javascript implementation by Tom MacWright from https://gist.github.com/tmcw/4977508
Data is divided by jenks algorithm.
:param data: values to bin
:param classes: divide values into this many bins
"""
input = data.copy()
input.sort()
input = input.tolist()
length = len(data)
#define initial values of the LC and OP
lower_class_limits = [[1 for x in range(0,classes+1)] if y==0 else [0 for x in range(0,classes+1)] for y in range(0,length+1)] #LC
variance_combinations = [[0 for x in range(0,classes+1)] if y==0 else [maxint for x in range(0,classes+1)] for y in range(0,length+1)] #OP
variance = 0
#calculate optimal LC
for i in range(1,length):
sum = 0 #SZ
sum_squares = 0 #ZSQ
counter = 0 #WT
for j in range(0,i+1):
i3 = i - j + 1 #III
value = input[i3-1]
counter+=1 #WT
sum += value
sum_squares += value * value
variance = sum_squares - (sum * sum) / counter
i4 = i3 - 1 #IV
if (i4 != 0) :
for k in range(0,classes+1):
#deciding whether an addition of this element will increase the class variance beyond the limit
#if it does, break the class
if (variance_combinations[i][k] >= (variance + variance_combinations[i4][k - 1])) :
lower_class_limits[i][k] = i3
variance_combinations[i][k] = variance + variance_combinations[i4][k - 1]
lower_class_limits[i][1] = 1
variance_combinations[i][1] = variance #we can use variance_combinations in calculations of goodness-of-fit, but we do not need it right now
#create breaks
length -= 1
breaks = []
breaks.append(input[0]-1) #append lower bound that was not found during calculations
breaks.append(input[length]+1) #append upper bound that was not found during calculations
while (classes > 1):
breaks.append(input[lower_class_limits[length][classes] - 2])
length = lower_class_limits[length][classes] -1
classes-=1
breaks = list(set(breaks)) #drop duplicate bins
breaks.sort()
return breaks
class Map(Drawable):
def __init__( self, path='',codes=None, difficulties = None, df=None, user=None, place_asked=None,
lower_bound = 50, upper_bound = 236, session_numbers=True):
"""Draws world map by default. All other defaults are same as in Drawable.
"""
Drawable.__init__(self,path,codes,difficulties,df,user,place_asked,lower_bound,upper_bound,session_numbers)
config ={
"layers": {
"states": {
"src": self.current_dir+"/ne_110m_admin_1_countries/ne_110m_admin_0_countries.shp",
"filter": ["continent", "in", ["Europe","Asia","Africa","South America","Oceania","North America"]],
"class": "states"
}
}
}
self.set_config(config)
self._k = Kartograph()
def set_config(self,config):
self.config = config
@staticmethod
def bin_data(data,binning_function=None,number_of_bins=6,
additional_countries=None,additional_labels=[],colour_range="YlOrRd"):
"""Combines classification methods with colouring, returns binned data with assigned colours
:param data: values to bin
:param binning_function: which function to use for binning -- default is None (-> jenks_classification)
:param number_of_bins: how many bins to divide data-- default is 6
:param reverse_colours: whether to reverse generated color scheme
:param additional_countries: whether to add additional countries AFTER binning -- default is None
:param additional_labels: whether to add additional labels AFTER calculations -- default is []
:param colours: use these colours instead of predefined ones
"""
if binning_function is None:
binning_function = jenks_classification
binned = pd.DataFrame(data)
binned = binned.reset_index()
binned.columns=['country','counts']
bins = binning_function(binned['counts'],number_of_bins)
binned['bin'] = pd.cut(binned['counts'], bins=bins,labels=False)
if colour_range == 'RdYlGn':
colours = colorbrewer.RdYlGn[len(bins)-1] #Red, Yellow, Green
else:
colours = colorbrewer.YlOrRd[len(bins)-1] #Yellow, Orange, Red
binned['rgb'] = binned.bin.apply(lambda x: colours[x])
binned = binned.append(additional_countries)
colours = list(reversed(colours))
colours = pd.DataFrame(zip(colours))
if additional_countries is not None:
colours = colours.append([[additional_countries.rgb.values[0]]],ignore_index=True)
colours = colours.append([[(255,255,255)]],ignore_index=True) #white for No data bin
colours.columns = ['rgb']
colours['label'] = Map.bins_to_string(bins)+additional_labels+['No data']
return (binned,colours)
@staticmethod
def bins_to_string(bins):
""" Returns list of strings from the bins in the interval form: (lower,upper]
:param bins: bins to get strings from
"""
bins[0]+=1 #corrections for bins
bins[-1]-=1
bins = [round(x,2) for x in bins]
labels = ['('+str(bins[curr])+', '+str(bins[curr+1])+']' for curr in range(len(bins)-1)]
labels.reverse()
return labels
def generate_css(self,data,path,optional_css=''):
"""Generates css for coloring in countries.
:param data: df with columns [country,rgb], where country is an ID and rgb are colour values
:param path: output directory
:param optional_css: append additional css at the end of the calculated css-- default is ''
"""
with open(path,'w+') as css:
if not data.empty:
data.apply(lambda x:
css.write('.states[iso_a2='+self.codes[self.codes.id==int(x.country)]['code'].values[0].upper()+']'+
'{\n\tfill: '+self.colour_value_rgb_string(x.rgb[0],x.rgb[1],x.rgb[2])+';\n}\n'),axis=1)
if optional_css:
optional = open(optional_css,'r')
css.write(optional.read())
@staticmethod
def colour_value_rgb_string(r,g,b):
"""Returns string in format 'rgb(r,g,b)'.
"""
return '\'rgb('+str(r)+', '+str(g)+', '+str(b)+')\''
@staticmethod
def draw_bins(data,path,x=5,y=175,bin_width=15,font_size=12):
"""Draws bins into svg.
:param data: data with columns [label,r,g,b] where label is text next to the bin and rgb are colour values
:param path: path to svg
:param x: starting x position of the legend
:param y: starting y position of the legend
:param bin_width: width of each individual bin -- default is 15
:param font_size: font size of labels -- default is 12
"""
with copen(path,'r+','utf-8') as svg:
svg.seek(-6,2) #skip to the position right before </svg> tag
svg.write('\n<g transform = \"translate('+str(x)+' '+str(y)+')\">\n') #group
for i in range(len(data)):
svg.write( '<rect x=\"0\" y=\"'+str((i+1)*bin_width)+
'\" width=\"'+str(bin_width)+'\" height=\"'+str(bin_width)+
'" fill='+Map.colour_value_rgb_string(data.rgb.values[i][0],data.rgb.values[i][1],data.rgb.values[i][2])+ '/>\n')
svg.write( '<text x=\"20\" y=\"'+str((i+1)*bin_width+11)+
'\" stroke=\"none\" fill=\"black\" font-size=\"'+str(font_size)+
'" font-family=\"sans-serif\">'+data.label.values[i]+'</text>\n')
svg.write('</g>\n</svg>') #group
@staticmethod
def draw_title(path,title='',x=400,y=410,font_size=20,colour='black'):
"""Draws title into svg.
:param path: path to svg
:param title: text do input into picture
:param x: starting x position of the title
:param y: starting y position of the title
:param font_size: font size of labels -- default is 20
:param colour: title colour
"""
with copen(path,'r+','utf-8') as svg:
svg.seek(-6,2)
svg.write( '\n<text x =\"'+str(x)+'\" y=\"'+str(y)+'\" stroke=\"none\" font-size=\"'+
str(font_size)+'\" fill=\"'+colour+'\" font-family=\"sans-serif\">'+
title+'</text>\n</svg>')
def draw_map(self,path,title='',colours=None):
"""General drawing method through kartograph. Looks for css in current_dir+'/style.css' for styling css.
:param path: output directory
:param title: name of map
:param colours: dataframe with colours for bins -- default is None
"""
with open(self.current_dir+'/style.css') as css:
self._k.generate(self.config,outfile=path,stylesheet=css.read())
if colours is not None:
self.draw_bins(colours,path)
if title:
self.draw_title(path,title)
############################################################################
def mistaken_countries(self,binning_function=None,path='',number_of_bins=6):
""" Draws map of most mistaken countries for this specific one
:param binning_function: which function to use for binning -- default is None (-> jenks_classification)
:param path: output directory -- default is '' (current dir)
:param number_of_bins: how many bins to divide data into-- default is 6
"""
if not path:
path = self.current_dir+'/maps/'
data = analysis.mistaken_countries(self.frame)
colours = None
if not (data.empty or self.place_asked is None):
place = pd.DataFrame([[self.place_asked,(0,255,255)]],columns=['country','rgb'])
(data,colours) = self.bin_data(data,binning_function,number_of_bins,additional_countries=place,additional_labels=[self.get_country_name(self.place_asked)])
self.generate_css(data[['country','rgb']],path=self.current_dir+'/style.css')
self.draw_map(path+'mistaken_countries.svg','Mistaken countries',colours)
def number_of_answers(self,binning_function=None,path='',number_of_bins=6):
"""Draws map of total number of answers per country.
:param binning_function: which function to use for binning -- default is None (-> jenks_classification)
:param path: output directory -- default is '' (current dir)
:param number_of_bins: how many bins to divide data into-- default is 6
"""
if not path:
path = self.current_dir+'/maps/'
data = analysis.number_of_answers(self.frame)
colours = None
if not data.empty:
(data,colours) = self.bin_data(data,binning_function,number_of_bins)
self.generate_css(data[['country','rgb']],path=self.current_dir+'/style.css')
self.draw_map(path+'number_of_answers.svg','Number of answers',colours)
def response_time(self,binning_function=None,path='',number_of_bins=6):
"""Draws map of mean response time per country.
:param binning_function: which function to use for binning -- default is None (-> jenks_classification)
:param path: output directory -- default is '' (current dir)
:param number_of_bins: how many bins to divide data into-- default is 6
"""
if not path:
path = self.current_dir+'/maps/'
data = analysis.response_time(self.frame)
colours = None
if not data.empty:
(data,colours) = self.bin_data(data,binning_function,number_of_bins)
self.generate_css(data[['country','rgb']],path=self.current_dir+'/style.css')
self.draw_map(path+'response_time.svg','Response time',colours)
def difficulty(self,binning_function=None,path='',number_of_bins=6):
"""Draws map of total number of answers per country.
:param binning_function: which function to use for binning -- default is None (-> jenks_classification)
:param path: output directory -- default is '' (current dir)
:param number_of_bins: how many bins to divide data into-- default is 6
"""
if not path:
path = self.current_dir+'/maps/'
data = analysis.difficulty_probabilities(self.difficulties)
colours = None
if not data.empty:
(data,colours) = self.bin_data(data,binning_function,number_of_bins,colour_range="RdYlGn")
self.generate_css(data[['country','rgb']],path=self.current_dir+'/style.css')
self.draw_map(path+'difficulty.svg','Difficulty for an average user',colours)
def success(self,binning_function=None,path='',number_of_bins=6):
"""Draws map of mean success rate per country.
:param binning_function: which function to use for binning -- default is None (-> jenks_classification)
:param path: output directory -- default is '' (current dir)
:param number_of_bins: how many bins to divide data into-- default is 6
"""
if not path:
path = self.current_dir+'/maps/'
data = analysis.mean_success(self.frame)
colours = None
if not data.empty:
(data,colours) = self.bin_data(data,binning_function,number_of_bins,colour_range="RdYlGn")
self.generate_css(data[['country','rgb']],path=self.current_dir+'/style.css')
self.draw_map(path+'success.svg','Mean success rate',colours)
def skill(self,binning_function=None,path='',number_of_bins=6):
"""Draws map of skill per country.
:param binning_function: which function to use for binning -- default is None (-> jenks_classification)
:param path: output directory -- default is '' (current dir)
:param number_of_bins: how many bins to divide data into-- default is 6
"""
if not path:
path = self.current_dir+'/maps/'
data = analysis.mean_skill_session(self.frame,self.difficulties,threshold=None)[1]
data = analysis.success_probabilities(data,self.difficulties)
colours = None
if not data.empty:
(data,colours) = self.bin_data(data,binning_function,number_of_bins,colour_range="RdYlGn")
self.generate_css(data[['country','rgb']],path=self.current_dir+'/style.css')
self.draw_map(path+'skill.svg','Skill ',colours)