-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
362 lines (311 loc) · 12.9 KB
/
main.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
import os
import sys
import webbrowser
from math import isnan
from os import mkdir
from os.path import expanduser, join
import kivy.resources
import matplotlib as mpl
import numpy as np
from imageio import imsave
from kivy.app import App
from kivy.config import Config
from kivy.core.window import Window
from kivy.graphics import ClearBuffers, Fbo, Scale, Translate
from kivy.properties import NumericProperty
from kivy.uix.behaviors import FocusBehavior
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.popup import Popup
from kivy.uix.recycleboxlayout import RecycleBoxLayout
from kivy.uix.recycleview import RecycleView
from kivy.uix.recycleview.views import RecycleDataViewBehavior
from kivy.uix.screenmanager import Screen, ScreenManager
from maintable import MainTable
Config.set('kivy', 'exit_on_escape', '0')
Config.set('input', 'mouse', 'mouse,multitouch_on_demand')
Config.set('graphics', 'minimum_width', '720')
Config.set('graphics', 'minimum_height', '576')
Config.set('graphics', 'width', '720')
Config.set('graphics', 'height', '576')
mpl.use('QT5Agg')
def resourcePath():
'''Returns path containing content - either locally or in pyinstaller tmp file'''
if hasattr(sys, '_MEIPASS'):
return os.path.join(sys._MEIPASS)
return os.path.join(os.path.abspath("."))
class SelectableRecycleBoxLayout(FocusBehavior, RecycleBoxLayout):
''' Adds selection and focus behaviour to the view. '''
class Col(RecycleDataViewBehavior, BoxLayout):
''' Add selection support to the Label '''
index = 0
def refresh_view_attrs(self, rv, index, data):
''' Catch and handle the view changes '''
self.index = data['col_number']
return super(Col, self).refresh_view_attrs(
rv, index, data)
class IterPopup(Popup):
cols = NumericProperty(0)
class MagPopup(Popup):
cols = NumericProperty(0)
class RV(RecycleView):
def __init__(self, **kwargs):
super(RV, self).__init__(**kwargs)
self.data = []
class NosWidget(Screen):
def __init__(self, **kwargs):
super(NosWidget, self).__init__(**kwargs)
self.noe_input = self.ids.noe_input
def validate_input(self, instance):
try:
elem = int(instance.text)
app = App.get_running_app()
app.main.gen_table(elem + 1)
app.sm.current = 'main'
instance.text = ''
app.main.focus_row = ''
app.main.focus_column = ''
except ValueError:
self.ids.info_label.text = 'Please, enter the correct number of columns'
class MainWidget(Screen):
def validate_input(self, instance):
if instance.focus:
self.focus_row = instance.param
if self.focus_row == 'vignette':
self.focus_row = ''
return
self.focus_column = instance.parent.col_number
if not instance.focus:
if not isnan(instance.data):
return
instance.text = instance.text.replace(',', '.')
try:
instance.data = float(instance.text)
except ValueError:
instance.text = '' if isnan(instance.data) else '{:g}'.format(instance.data)
if isnan(instance.data):
return
if instance.param == 'vignette':
if float(instance.text) > 1 or float(instance.text) < 0:
instance.data = float('nan')
return
self.table2.set_vignetting(float(instance.text))
self.update_table()
return
col = int(instance.parent.col_number)
row = self.forward_map[instance.param]
if self.table2.table.loc[row, col] == '':
instance.text = ''
return
val = float(instance.data)
instance.user = True
self.table2.change_value(row, col, val)
self.update_table()
def help_me(self, instance):
if instance.param == 'vignette':
return
col = int(instance.parent.col_number)
row = self.forward_map[instance.param]
if self.table2.table.loc[row, col] == '':
return
if instance.text != '' and instance.user:
return
if isnan(self.table2.table.loc[row, col]):
instance.user = False
def validate_popup_input(self, instance):
if not instance.focus:
instance.text = instance.text.replace(',', '.')
try:
instance.data = float(instance.text)
except ValueError:
instance.text = '{:g}'.format(instance.data)
def update_table(self):
temp = self.table2.table.to_dict()
for item in temp.items():
it = dict([(self.reverse_map[key], float('nan') if value == '' else value)
for key, value in item[1].items()])
it['col_number'] = item[0]
self.ids.table.data[item[0]] = it
if not isnan(self.table2.LHI):
self.ids.lhi_label.data = str(self.table2.LHI)
else:
self.ids.lhi_label.data = float('nan')
if isnan(self.table2.vignette):
self.ids.vignette_input.data = float('nan')
else:
self.ids.vignette_input.data = self.table2.vignette
def gen_table(self, cols):
nan = float('nan')
self.ids.table.data = [{'col_number': i, 'f': nan, 'd': nan, 'h': nan, 'alpha': nan,
'v': nan, 'l': nan, 'y1': nan, 'beta': nan, 'q': nan, 't': nan, 'phi': nan} for i in range(cols)]
self.table2 = MainTable(cols)
self.cols = cols
self.iter_pop = IterPopup(cols=cols)
self.mag_pop = MagPopup(cols=cols)
def key_action(self, key, keycode, text, modif):
if len(modif) >= 1:
print(modif, keycode)
if keycode[1] == 'z' and 'ctrl' in modif:
self.update_label('Undo')
try:
self.table2.undo_changes()
self.update_table()
except AttributeError:
pass
app = App.get_running_app()
if app.sm.current == 'main' and not self.focus_column == '' and not self.focus_row == '':
if keycode[1] == 'left':
all = len(self.ids.table.children[0].children)
col = all - self.focus_column
if col > 0 and col < all:
for children in self.ids.table.children[0].children[col].children:
if children.param == self.focus_row:
children.focus = True
if keycode[1] == 'right':
all = len(self.ids.table.children[0].children)
col = all - (self.focus_column + 2)
if col >= 0 and col < all:
for children in self.ids.table.children[0].children[col].children:
try:
if children.param == self.focus_row:
children.focus = True
except AttributeError:
pass
if keycode[1] == 'up':
all = len(self.ids.table.children[0].children)
col = all - (self.focus_column + 1)
index = self.ids.table.children[0].children[col].par_q.index(self.focus_row) - 1
param = self.ids.table.children[0].children[col].par_q[index]
for children in self.ids.table.children[0].children[col].children:
try:
if children.param == param:
children.focus = True
except AttributeError:
pass
if keycode[1] == 'down':
all = len(self.ids.table.children[0].children)
col = all - (self.focus_column + 1)
index = self.ids.table.children[0].children[col].par_q.index(self.focus_row) + 1
param = self.ids.table.children[0].children[col].par_q[index]
for children in self.ids.table.children[0].children[col].children:
try:
if children.param == param:
children.focus = True
except AttributeError:
pass
def _activ_key(self, *args):
""" The active keyboard is being closed. """
self.keyboard = None
def plot(self):
try:
self.table2.plot()
except ValueError:
self.update_label('Not enough data')
def export(self):
name = join(self.home, 'out.csv')
self.table2.table.to_csv(name)
def history(self):
name = join(self.home, 'history.log')
with open(name, 'w') as file:
for line in self.table2.history_input:
file.write('{}\n'.format(line))
def update_label(self, msg):
self.ids.info_label.text = msg
def magnify(self, v_check, from_col, to_col, val):
var = 'V' if v_check else 'Q'
self.table2.magnification(float(from_col), float(to_col), float(val), var)
self.update_table()
tmp = ['Magnification {:.2f}, from surface {:.0f} to {:.0f}, type {}'.format(
dic['magn'], dic['from'], dic['to'], dic['type']) for dic in self.table2.magnify]
self.ids.magnify_info.text = '\n'.join(tmp)
def iterate(self, d_check, L_check, from_c, to_c, targ, start, row, col):
if not (d_check or L_check):
dim = 'T'
elif d_check:
dim = 'd'
else:
dim = 'L'
if row == '\u03B1':
row = 'alpha'
elif row == '\u03B2':
row = 'Beta'
if self.table2.table.loc[row, int(col)] == '':
self.update_label("Can't iterate this value")
return
from_c, to_c, targ, start, col = int(from_c), int(to_c), float(targ), float(start), int(col)
try:
self.table2.iterate(row, col, from_c, to_c, dim, targ, start)
self.update_table()
except ValueError as e:
self.update_label(str(e))
def capture(self):
name = join(self.home, 'screen.png')
self.export_png(name)
def export_png(self, name):
if self.parent is not None:
canvas_parent_index = self.parent.canvas.indexof(self.canvas)
if canvas_parent_index > -1:
self.parent.canvas.remove(self.canvas)
fbo = Fbo(size=self.size, with_stencilbuffer=True)
with fbo:
ClearBuffers()
Scale(1, -1, 1)
Translate(-self.x, -self.y - self.height, 0)
fbo.add(self.canvas)
fbo.draw()
test = np.frombuffer(fbo.texture.pixels, dtype=np.uint8)
test = np.copy(test.reshape((self.height, self.width, 4)))
alpha = -test[:, :, -1]
test[:, :, -1] = alpha
imsave(name, -test)
fbo.remove(self.canvas)
if self.parent is not None and canvas_parent_index > -1:
self.parent.canvas.insert(canvas_parent_index, self.canvas)
return True
def help(self):
webbrowser.open("https://github.com/Tokariew/PIEG/wiki")
def __init__(self, **kwargs):
super(MainWidget, self).__init__(**kwargs)
self.table = self.ids.table.data
self.table2 = []
self.forward_map = {'f': 'f', 'd': 'd', 'h': 'H', 'alpha': 'alpha', 'v': 'V',
'l': 'L', 'y1': 'Y', 'beta': 'Beta', 'q': 'Q', 't': 'T', 'phi': 'phi', 'vignette': 'vignette', 'lhi': 'lhi'}
self. reverse_map = dict(reversed(item) for item in self.forward_map.items())
self.keyboard = Window.request_keyboard(self._activ_key, self)
self.keyboard.bind(on_key_down=self.key_action)
self.cols = 0
self.iter_pop = None
self.mag_pop = None
self.focus_column = ''
self.focus_row = ''
self.home = join(expanduser('~'), 'Documents', 'PIEG')
mpl.rcParams["savefig.directory"] = self.home
try:
mkdir(self.home)
except FileExistsError:
pass
class GabApp(App):
use_kivy_settings = False
def build(self):
self.version = '19.11'
self.sm = ScreenManager()
self.main = MainWidget(name='main')
self.nos = NosWidget(name='nos')
self.sm.add_widget(self.nos)
self.sm.add_widget(self.main)
self.title = f'PIEG {self.version}'
self.icon = 'data/pieg.png'
return self.sm
if __name__ == '__main__':
import traceback
from datetime import datetime
kivy.resources.resource_add_path(resourcePath())
try:
GabApp().run()
except:
app = App.get_running_app()
err_time = datetime.now().isoformat(timespec='minutes').replace(':', '-')
log_name = join(app.main.home, f'log_{err_time}.txt')
with open(log_name, 'w') as log_file:
traceback.print_exc(file=log_file)
raise Exception('Exit found abruptly')
app.main.history()