-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrasmission.py
245 lines (206 loc) · 7.8 KB
/
trasmission.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
__author__ = 'Toni'
import numpy as np
import numpy.random as rnd
from PyQt4.QtCore import QString, QTimer, Qt
from PyQt4.QtGui import *
from matplotlib.backends.backend_qt4agg import NavigationToolbar2QTAgg as NavigationToolbar
from matplotlibwidget import MatplotlibWidget
from tras_w import Ui_TrasmissionWindow
import math
try:
_fromUtf8 = QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QApplication.translate(context, text, disambig)
class Trasmission(QMainWindow):
def __init__(self, parent, tmodel):
QMainWindow.__init__(self, parent)
self.ui = Ui_TrasmissionWindow()
self.ui.setupUi(self)
self.tmodel = tmodel
# Initialize the matplotlib window and the toolbar
self.mpl_window = MatplotlibWidget()
self.ui.verticalLayout_2.addWidget(self.mpl_window)
self.toolbar = NavigationToolbar(self.mpl_window, self)
self.ui.verticalLayout_2.addWidget(self.toolbar)
# Initialize the color column
self.color_list = generate_colors(self.tmodel.num_analites)
self.color_col = ColorColumn(self.ui.wid_column, self.tmodel.num_teo_plates,
self.tmodel.num_analites, self.color_list,
self.tmodel.current_state[0], 380, 300)
# Simulation
self.timer = QTimer()
self.timer.timeout.connect(self.timeout)
self.ui.pb_exec.clicked.connect(self.start_stop)
self.running = False
def start_stop(self):
if self.running:
self.running = False
self.timer.stop()
self.ui.pb_exec.setText("Ejecutar")
else: # not running now
self.running = True
self.timer.start()
self.ui.pb_exec.setText("Detener")
def timeout(self):
self.tmodel.update(1)
temp = self.tmodel.current_state[0].copy()
self.color_col.update_conc(temp, (self.tmodel.count_iter < 10))
self.color_col.repaint()
self.plot()
if self.tmodel.finished:
self.timer.stop()
def plot(self):
self.mpl_window.axes.hold(False)
self.mpl_window.axes.axis = ([1, self.tmodel.num_teo_plates, 0, 100])
x = np.linspace(0, self.tmodel.num_teo_plates, self.tmodel.num_teo_plates)
y = self.tmodel.current_state[0]
for i in np.arange(self.tmodel.num_analites):
self.mpl_window.axes.set_xlabel(_translate("Numero de platos teoricos", "Numero de platos teoricos", None))
self.mpl_window.axes.set_ylabel(_translate("Intensidad de la senal", "Intensidad de la senal", None))
self.mpl_window.axes.plot(x, y[i], color2str(self.color_list[i]), label=str(chr(65 + i)))
self.mpl_window.axes.hold(True)
self.mpl_window.axes.grid()
self.mpl_window.axes.legend()
self.mpl_window.draw()
class ColorColumn(QWidget):
def __init__(self, parent, num_teo_plates, num_analites, color_list, init_conc, height=250, width=150):
QWidget.__init__(self, parent)
self.num_plates = num_teo_plates
self.num_analites = num_analites
self.color_list = color_list
self.intensity = init_conc
self.max_intensity = 0.0
self.normalize_conc()
self.margin = 30
self.setGeometry(22, 0, width, height)
print 'Total Height: ' + str(self.height())
print 'Number of plates: ' + str(self.num_plates)
self.show()
def normalize_conc(self, set_max_conc=False):
if set_max_conc:
self.max_intensity = np.max(self.intensity)
if self.max_intensity > 1e-3:
self.intensity /= self.max_intensity
def update_conc(self, norm_conc, set_max_conc=False):
self.intensity = norm_conc
self.normalize_conc(set_max_conc)
def paintEvent(self, event):
qp = QPainter()
qp.begin(self)
# Setting the background color
qp.fillRect(0, 0, 120, self.height() + 15, QBrush(QColor(45, 45, 25)))
target_size = (self.height() - self.margin)
plate_height = 1
if self.num_plates >= target_size:
intensity_field = pool(self.intensity, target_size)
else:
intensity_field = expand(self.intensity, target_size)
for i in np.arange(intensity_field.shape[1]):
color = combine(self.color_list, intensity_field[:, i])
qp.fillRect(10, i * plate_height + 10, 100, plate_height, QBrush(color))
qp.end()
def combine(colors, intense):
"""
:return: The addition of both colors
"""
c = QColor()
l = len(colors)
r, g, b = 0, 0, 0
s = np.sum(intense)
if s == 0:
return QColor(255, 255, 255)
for i in range(l):
r += min(255, colors[i][0] * (intense[i] / s))
g += min(255, colors[i][1] * (intense[i] / s))
b += min(255, colors[i][2] * (intense[i] / s))
rx, gx, bx = int(r / l), int(g / l), int(b / l)
mx = max(intense)
# r = int(255 - (255 - int(r / l)) * max(intense))
# g = int(255 - (255 - int(g / l)) * max(intense))
# b = int(255 - (255 - int(b / l)) * max(intense))
r = eval_color(rx, mx)
g = eval_color(gx, mx)
b = eval_color(bx, mx)
c.setRed(r)
c.setGreen(g)
c.setBlue(b)
return c
def eval_color(cx, ci):
b = math.log(1 / (255.0 - cx + 1))
y = (255 - cx + 1) * math.exp(b * ci) + cx - 1
return int(y)
def generate_colors(n):
c = []
for i in np.arange(1, n + 1):
b = np.array([(i & (1 << j)) > 0 for j in np.arange(3)])
c.append(((i / 8 + 1) * 90) % 200 * b)
return c
def color2str(col):
s = "#"
cs = [int(col[i]) for i in range(3)]
for i in cs:
if i > 15:
s += hex(i)[2:]
else:
s += '0' + hex(i)[2:]
return s
def expand(smaller, bigger_size):
"""
Expands the smaller array in the second dimension
"""
smaller_size = smaller.shape[1]
k = bigger_size / smaller_size
bigger = np.ones((smaller.shape[0], bigger_size))
r = bigger_size % smaller_size
count_extra = 0
step_for_extra = smaller_size
if r != 0:
step_for_extra /= r
for i in np.arange(smaller_size):
for j in np.arange(smaller.shape[0]):
if ((i + 1) % step_for_extra == 0) and count_extra < r:
st = k * i + count_extra
fn = st + k + 1
# print smaller[j][i]
bigger[j][st:fn] = smaller[j][i]
if j == smaller.shape[0] - 1:
count_extra += 1
else:
st = k * i + count_extra
fn = st + k
# print smaller[j][i]
bigger[j][st:fn] = smaller[j][i]
return bigger
def pool(bigger, smaller_size):
"""
Pools down the bigger array in the second dimension
"""
bigger_size = bigger.shape[1]
k = bigger_size / smaller_size
smaller = np.ones((bigger.shape[0], smaller_size))
r = bigger_size % smaller_size
count_extra = 0
step_for_extra = smaller_size
if r != 0:
step_for_extra /= r
for i in np.arange(smaller_size):
for j in np.arange(bigger.shape[0]):
if ((i + 1) % step_for_extra == 0) and count_extra < r:
st = k * i + count_extra
fn = st + k + 1
smaller[j][i] = np.average(bigger[j][st:fn])
if j == smaller.shape[0] - 1:
count_extra += 1
else:
st = k * i + count_extra
fn = st + k
smaller[j][i] = np.average(bigger[j][st:fn])
return smaller