-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path20080222a.py
45 lines (40 loc) · 1.22 KB
/
20080222a.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
"""
Visualize a matrix as a heat map.
"""
import numpy as np
from SnippetUtil import HandlingError
import SnippetUtil
import EnglishModel
import HeatMap
import MatrixUtil
import Form
import FormOut
def get_form():
"""
@return: the body of a form
"""
# define the default matrix lines
dictionary_rate_matrix = EnglishModel.get_transition_matrix()
labels = list(sorted(set(a for a, b in dictionary_rate_matrix)))
T = MatrixUtil.dict_to_row_major(dictionary_rate_matrix, labels, labels)
T = np.array(T)
# define the form objects
form_objects = [
Form.Matrix('matrix', 'matrix', T),
Form.Integer('maxcategories', 'maximum number of categories',
5, low=2)]
return form_objects
def get_form_out():
return FormOut.Html()
def get_response_content(fs):
"""
@param fs: a FieldStorage object containing the cgi arguments
@return: a (response_headers, response_text) pair
"""
# create the html response representing the heat map
M = fs.matrix
heatmap = HeatMap.HeatMap(M.tolist(), fs.maxcategories)
renderer = HeatMap.PreHeatMap(heatmap)
html_string = renderer.get_example_html()
# return the response
return html_string + '\n'