-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
87 lines (74 loc) · 2.68 KB
/
utils.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
from math import sqrt
import wx
from biopepa_csv_parser import BioPepaCsvParser
from worldstate import WorldState
#world = WorldState.Instance()
def open_results_file(self):
file_chooser = wx.FileDialog(
self,
message="Choose a file",
wildcard="*.csv",
style=wx.OPEN | wx.MULTIPLE | wx.CHANGE_DIR)
if file_chooser.ShowModal() == wx.ID_OK:
paths = file_chooser.GetPaths()
results = {}
parser = BioPepaCsvParser()
for path in paths:
parser.parse_csv(path)
results[path.split('/')[-1]] = parser.results_dict
WorldState.Instance().session_dict['results'] = results
WorldState.Instance().parser = parser
file_chooser.Destroy()
else:
file_chooser.Destroy()
def rgb_to_hex(rgb):
"""
Having trouble getting matplotlib to take an rgb tuple, so convert to hex which is working.
Taken from this thread: http://stackoverflow.com/questions/214359/converting-hex-color-to-rgb-and-vice-versa
"""
return '#%02x%02x%02x' % rgb
def euclid_distance(p1, p2):
return sqrt((p1[0] - p2[0]) ** 2 + (p1[1] - p2[1]) ** 2)
def point_to_line_distance((l1_x, l1_y), (l2_x, l2_y), (p_x, p_y)):
"""
Used in annotations to see if we have selected it. It is adapted from a
wikipedia page
"""
m = float(l2_y - l1_y)/float(l2_x - l1_x)
a = -m
b = 1
c = l1_y - (m*l1_x)
top = abs(a*p_x + b*p_y - c)
bottom = sqrt(a**2 + b**2)
return top/float(bottom)
def rgba_to_rgb((r, g, b), a):
"""
Blend the colour of the line segment with the background - ration specified by
alpha value. This prevents the blending with the other line segment
Taken from stack overflow:
"""
bg = tuple([255 * (1 - a)] * 3)
fg = (r * a, g * a, b * a)
add_tuples = lambda (r1, g1, b1), (r2, g2, b2): (r1 + r2, g1 + g2, b1 + b2)
return add_tuples(bg, fg)
def calc_graph_size(dpi, cols, num_sidebars, phi):
"""
Want it to stick to the golden ration as Tufte recommends it hence phi
"""
graph_width = int(((WorldState.Instance().dispW / cols) * (cols - num_sidebars)) / dpi)
graph_height = int(graph_width/phi)
return (graph_width, graph_height)
def reset_sash_position(sash):
"""
Gets rid of this ugly code everywhere
"""
sash.SetSashPosition(sash.GetSashPosition() + 1)
sash.SetSashPosition(sash.GetSashPosition() - 1)
def refresh_plot():
"""
Null pointers on the mac if I don't tell it to not redraw the legend
unless necessary.
"""
WorldState.Instance().session_dict['redraw_legend'] = False
WorldState.Instance().draw_plot.plot()
WorldState.Instance().session_dict['redraw_legend'] = True