app.py 16.9 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Template Python module generated based on 'app_template', 'phantasy-ui'
is required to make it executable as a PyQt5 app.

Created by: makeBasePyQtApp.

An example to create an app template:

>>> makeBasePyQtApp --app my_great_app --template AppWindow

Show the available templates:

>>> makeBasePyQtApp -l
"""
16
17
import sys

18
from PyQt5.QtCore import pyqtSignal
19
from PyQt5.QtCore import pyqtSlot
20
21
from PyQt5.QtCore import QEventLoop
from PyQt5.QtCore import QVariant
22
from PyQt5.QtGui import QColor
23
from PyQt5.QtGui import QDoubleValidator
24
from PyQt5.QtWidgets import QMainWindow
25
from PyQt5.QtWidgets import QMessageBox
26

27
28
from mpl4qt.widgets import MatplotlibBaseWidget

29
from phantasy import MachinePortal
30
from phantasy_ui import BaseAppForm
31
from phantasy_ui import milli_sleep
32
from phantasy_ui.widgets import ElementWidget
33
from phantasy_ui.widgets import LatticeWidget
34
from phantasy_apps.allison_scanner.data import draw_beam_ellipse_with_params
35

36
37
38
from .utils import ResultsModel
from .utils import TWISS_KEYS_X
from .utils import TWISS_KEYS_Y
39
40
from .ui.ui_app import Ui_MainWindow

41
42
DEFAULT_MACHINE, DEFAULT_SEGMENT = "ARIS_VA", "F1"
QUAD_FIELD_NAME = "I" # B2
43

44

45
class MyAppWindow(BaseAppForm, Ui_MainWindow):
46

47
    # update ellipse drawing size
48
49
    ellipse_size_factor_changed = pyqtSignal()

50
51
52
53
54
55
    # loaded machine/segment changed
    lattice_changed = pyqtSignal(QVariant)

    # update layout drawings
    update_layout = pyqtSignal()

56
57
58
59
60
61
62
63
64
65
66
67
68
69
    def __init__(self, version, **kws):
        super(self.__class__, self).__init__()

        # app version, title
        self.setAppVersion(version)
        self.setAppTitle("My App")

        # app info in about dialog
        # self.app_about_info = "About info of My App."

        # UI
        self.setupUi(self)
        self.postInitUi()

70
71
72
73
74
75
        # post init
        self._post_init()

    def _post_init(self):
        """Initialize UI, user customized code put here.
        """
76
77
78
79
        #
        self.quad1_name_cbb.currentTextChanged.connect(self.on_quad1_name_changed)
        self.elemlist_cbb.currentTextChanged.connect(self.on_target_element_changed)

80
81
82
83
        #
        self._size_factor = self.size_factor_sbox.value()
        self.ellipse_size_factor_changed.connect(self.draw_ellipse)

84
85
86
87
88
89
90
91
        # initial vars for FLAME model
        self.results = None
        self.last_bs = None
        self.fm = None

        # ElementWidget for selected quad and target element
        self._quad_widget = None
        self._elem_widget = None
92
93
94
95
96
97
98
99
100
101
102
103
104
        # element query
        self.quad_info_btn.clicked.connect(self.on_query_quad_info)
        self.elem_info_btn.clicked.connect(self.on_query_elem_info)

        # lattice load window
        self.lattice_load_window = None
        self.__mp = None
        self.__lat = None

        # update layout drawings
        self.update_layout.connect(self.draw_layout)
        # lattice changed
        self.lattice_changed.connect(self.on_lattice_changed)
105

106
107
        # connect valueChanged signal of quad1_grad_dsbox to on_quad1_grad_changed()
        self.quad1_grad_dsbox.valueChanged.connect(self.on_quad1_grad_changed)
108

Tong Zhang's avatar
Tong Zhang committed
109
110
111
        # envelope and trajectory
        self.__init_envelope_plot()
        self.__init_trajectory_plot()
112

113
114
115
116
117
118
119
120
121
122
123
124
125
        # ellipse drawing figure configuration
        for o in (self.xlim_x1_lineEdit, self.xlim_x2_lineEdit, ):
            o.setValidator(QDoubleValidator())
            o.textChanged.connect(self.on_xlimit_changed)

        for o in (self.ylim_y1_lineEdit, self.ylim_y2_lineEdit, ):
            o.setValidator(QDoubleValidator())
            o.textChanged.connect(self.on_ylimit_changed)

        self.grid_on_chkbox.toggled.connect(self.on_grid_enabled)
        self.mticks_on_chkbox.toggled.connect(self.on_mticks_enabled)
        self.tight_layout_on_chkbox.toggled.connect(self.on_tightlayout_enabled)

126
127
128
129
130
131
132
133
134
135
136
        # preload default machine/segment
        self.__preload_lattice(DEFAULT_MACHINE, DEFAULT_SEGMENT)

    def __preload_lattice(self, mach, segm):
        self.actionLoad_Lattice.triggered.emit()
        self.lattice_load_window.mach_cbb.setCurrentText(mach)
        self.lattice_load_window.seg_cbb.setCurrentText(segm)
        loop = QEventLoop()
        self.lattice_load_window.latticeChanged.connect(loop.exit)
        self.lattice_load_window.load_btn.clicked.emit()
        loop.exec_()
137
        # auto xyscale (ellipse drawing)
138
        self.auto_limits()
139

Tong Zhang's avatar
Tong Zhang committed
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
    def __init_envelope_plot(self):
        """Initialize plot area for beam envelope.
        """
        o = self.envelope_plot
        o.add_curve()
        o.setLineID(0)  # X
        o.setLineColor(QColor('#0000FF'))
        o.setLineLabel("$\sigma_x$")
        o.setLineID(1)  # Y
        o.setLineColor(QColor('#FF0000'))
        o.setLineLabel("$\sigma_y$")

    def __init_trajectory_plot(self):
        """Initialize plot area for beam trajectory.
        """
        o = self.trajectory_plot
        o.add_curve()
        o.setLineID(0)  # X
        o.setLineColor(QColor('#0000FF'))
        o.setLineLabel("$x_0$")
        o.setLineID(1)  # Y
        o.setLineColor(QColor('#FF0000'))
        o.setLineLabel("$y_0$")

164
165
166
167
168
169
170
    @pyqtSlot('QString')
    def on_quad1_name_changed(self, name: str) -> None:
        """When the current selected quad name is changed, do:
        show the current setting of the selected quad on quad1_grad_dsbox.
        when set value to quad1_grad_dsbox, disconnect valueChanged and
        reconnect, to avoid unnecessary trigging.
        """
171
        self.quad_selected = self.__mp.get_elements(name=name)[0]
172
        milli_sleep(500)
173
        self._quad_widget = ElementWidget(self.quad_selected)
174
        self.quad1_grad_dsbox.valueChanged.disconnect()
175
176
        try:
            self.quad1_grad_dsbox.setValue(
177
                self.quad_selected.current_setting(QUAD_FIELD_NAME))
178
179
        except TypeError:
            # current_settings('B2') is None --> most likely VA is not running
180
181
182
183
            QMessageBox.critical(
                self, "ARIS Beam Ellipse",
                "Cannot reach process variables, please either start virtual accelerator or ensure Channel Access is permittable.",
                QMessageBox.Ok, QMessageBox.Ok)
184
185
            sys.exit(1)

186
187
188
189
190
191
192
193
        self.quad1_grad_dsbox.valueChanged.connect(self.on_quad1_grad_changed)

    @pyqtSlot(float)
    def on_quad1_grad_changed(self, grad: float) -> None:
        """When the setting of the selected quad is changed, do:
        1. print the setting of selected quad
        2. update drawing with online simulated results
        """
194
        setattr(self.quad_selected, QUAD_FIELD_NAME, grad)
195

196
        # update simulation
197
198
        self.__lat.sync_settings()
        _, fm = self.__lat.run()
199
        self.fm = fm
200
201
202
203
        self.results, _ = fm.run(monitor='all')
        r, _ = fm.run(monitor=[self.elemlist_cbb.currentText()])
        if r != []:
            self.last_bs = r[0][-1]
204
205

        # update drawing
206
207
208
209
210
211
212
        self.update_drawing()

    def update_drawing(self):
        """This is the routine to update the figure with the updated drawing.
        Here I'm drawing the beam envelop along the entire beamline, try to
        replace with your routine for beam ellipse drawing.
        """
213
        self.draw_ellipse()
Tong Zhang's avatar
Tong Zhang committed
214
215
        self.draw_envelope()
        self.draw_trajectory()
216
217
218
219

    def draw_envelope(self):
        """Draw beam envelop onto the figure area.
        """
Tong Zhang's avatar
Tong Zhang committed
220
        results_dict = self.fm.collect_data(self.results, 'pos', 'xrms', 'yrms')
221
        pos = results_dict['pos'] + self.__z0
222
223
        xrms = results_dict['xrms']
        yrms = results_dict['yrms']
Tong Zhang's avatar
Tong Zhang committed
224
225
226
227
228
229
230
231
        for line_id, urms in zip((0, 1), (xrms, yrms)):
            self.envelope_plot.setLineID(line_id)
            self.envelope_plot.update_curve(pos, urms)

    def draw_trajectory(self):
        """Draw beam centroid trajectory onto the figure area.
        """
        results_dict = self.fm.collect_data(self.results, 'pos', 'xcen', 'ycen')
232
        pos = results_dict['pos'] + self.__z0
Tong Zhang's avatar
Tong Zhang committed
233
234
235
236
237
        xcen = results_dict['xcen']
        ycen = results_dict['ycen']
        for line_id, ucen in zip((0, 1), (xcen, ycen)):
            self.trajectory_plot.setLineID(line_id)
            self.trajectory_plot.update_curve(pos, ucen)
238

239
    @pyqtSlot()
240
241
    def draw_ellipse(self):
        """Draw x and y beam ellipse onto the figure area.
242
        """
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
        #
        s = self.last_bs
        #
        vals_x = (s.xcen, s.xpcen, s.xrms, s.xprms, s.xemittance,
                  s.xnemittance, s.xtwiss_alpha, s.xtwiss_beta,
                  (s.xtwiss_alpha**2 + 1) / s.xtwiss_beta, 1)
        vals_y = (s.ycen, s.ypcen, s.yrms, s.yprms, s.yemittance,
                  s.ynemittance, s.ytwiss_alpha, s.ytwiss_beta,
                  (s.ytwiss_alpha**2 + 1) / s.ytwiss_beta, 1)
        params_x = dict(zip(TWISS_KEYS_X, vals_x))
        params_y = dict(zip(TWISS_KEYS_Y, vals_y))

        self._plot_ellipse(self.x_ellipse_plot,
                           params_x,
                           color='b',
258
                           factor=self._size_factor,
259
260
261
262
263
264
                           xoy='x',
                           fill='g',
                           anote=False)
        self._plot_ellipse(self.y_ellipse_plot,
                           params_y,
                           color='r',
265
                           factor=self._size_factor,
266
267
268
                           xoy='y',
                           fill='m',
                           anote=False)
269
270
271
272
273
274
        #
        params = {k: v for k, v in params_x.items()}
        params.update(params_y)
        data = [(k, v, '-') for k, v in params.items()]
        self._show_results(data)

275
276

    def _plot_ellipse(self, figure_obj, params, **kws):
277
278
279
        xoy = kws.get('xoy', 'x')
        xlbl = f"{xoy} [mm]"
        ylbl = f"{xoy}' [mrad]"
280
281
282
283
284
        figure_obj.clear_figure()
        draw_beam_ellipse_with_params(params,
                                      ax=figure_obj.axes,
                                      color=kws.get('color', 'b'),
                                      factor=kws.get('factor', 4),
285
                                      xoy=xoy,
286
287
                                      fill=kws.get('fill', 'g'),
                                      anote=kws.get('anote', False))
288
289
        figure_obj.setFigureXlabel(xlbl)
        figure_obj.setFigureYlabel(ylbl)
290
        figure_obj.update_figure()
291
292
293
294
295
        self.on_xlimit_changed('')
        self.on_ylimit_changed('')
        self.grid_on_chkbox.toggled.emit(self.grid_on_chkbox.isChecked())
        self.mticks_on_chkbox.toggled.emit(self.mticks_on_chkbox.isChecked())
        self.tight_layout_on_chkbox.toggled.emit(self.tight_layout_on_chkbox.isChecked())
296

297
298
299
300
301
302
303
304
305
306
    def draw_layout(self):
        for o in (self.layout_plot, self.envelope_layout_plot, self.trajectory_layout_plot):
            o.clear_figure()
            _, ax = self.__lat.layout.draw(ax=o.axes, fig=o.figure,
                                           span=(1.05, 1.1),
                                           fig_opt={'figsize': (20, 8), 'dpi': 130})
        self.envelope_plot_splitter.setStretchFactor(0, 4)
        self.envelope_plot_splitter.setStretchFactor(1, 1)
        self.trajectory_plot_splitter.setStretchFactor(0, 4)
        self.trajectory_plot_splitter.setStretchFactor(1, 1)
307
308
309
310
311

    @pyqtSlot('QString')
    def on_target_element_changed(self, ename: str):
        """Get beam state result after the selected element from FLAME model.
        """
312
        elem = self.__lat[ename]
313
        self._elem_widget = ElementWidget(elem)
314
        self.family_lineEdit.setText(elem.family)
315
        self.pos_lineEdit.setText(f"{elem.sb + self.__z0:.3f} m")
316
317
318
319
320
321
322
323
324
        r, _ = self.fm.run(monitor=[ename])
        if r == []:
            QMessageBox.warning(self, "Select Element",
                    "Selected element cannot be located in model, probably for splitable element, select the closest one.",
                    QMessageBox.Ok, QMessageBox.Ok)
            return
        self.last_bs = r[0][-1]
        self.draw_ellipse()

325
326
327
328
    def _show_results(self, data):
        m = ResultsModel(self.twiss_results_treeView, data)
        m.set_model()

329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
    @pyqtSlot()
    def on_query_quad_info(self):
        """Pop up dialog for selected quad for info query.
        """
        if self._quad_widget is None:
            return
        self._quad_widget.show()
        self._quad_widget.raise_()

    @pyqtSlot()
    def on_query_elem_info(self):
        """Pop up dialog for selected element for info query.
        """
        if self._elem_widget is None:
            return
        self._elem_widget.show()
        self._elem_widget.raise_()

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
    @pyqtSlot('QString')
    def on_xlimit_changed(self, s):
        """xlimit to be updated.
        """
        try:
            x1 = float(self.xlim_x1_lineEdit.text())
            x2 = float(self.xlim_x2_lineEdit.text())
        except ValueError:
            pass
        else:
            for o in self.ellipse_area.findChildren(MatplotlibBaseWidget):
                o.set_xlimit(x1, x2)

    @pyqtSlot('QString')
    def on_ylimit_changed(self, s):
        """ylimit to be updated.
        """
        try:
            y1 = float(self.ylim_y1_lineEdit.text())
            y2 = float(self.ylim_y2_lineEdit.text())
        except ValueError:
            pass
        else:
            for o in self.ellipse_area.findChildren(MatplotlibBaseWidget):
                o.set_ylimit(y1, y2)

    @pyqtSlot(bool)
    def on_grid_enabled(self, enabled):
        for o in self.ellipse_area.findChildren(MatplotlibBaseWidget):
            o.setFigureGridToggle(enabled)

    @pyqtSlot(bool)
    def on_mticks_enabled(self, enabled):
        for o in self.ellipse_area.findChildren(MatplotlibBaseWidget):
            o.setFigureMTicksToggle(enabled)

    @pyqtSlot(bool)
    def on_tightlayout_enabled(self, enabled):
        for o in self.ellipse_area.findChildren(MatplotlibBaseWidget):
            o.setTightLayoutToggle(enabled)

388
389
390
391
    def auto_limits(self):
        self.on_auto_xlim()
        self.on_auto_ylim()

392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
    @pyqtSlot()
    def on_auto_xlim(self):
        """Auto set xlimit.
        """
        x0, x1 = 0, 1
        for o in self.ellipse_area.findChildren(MatplotlibBaseWidget):
            o.set_autoscale()
            x0_, x1_ = o.axes.get_xlim()
            if x0_ < x0:
                x0 = x0_
            if x1_ > x1:
                x1 = x1_
        self.xlim_x1_lineEdit.setText(f'{x0:.1f}')
        self.xlim_x2_lineEdit.setText(f'{x1:.1f}')

    @pyqtSlot()
    def on_auto_ylim(self):
        """Auto set ylimit.
        """
        y0, y1 = 0, 1
        for o in self.ellipse_area.findChildren(MatplotlibBaseWidget):
            o.set_autoscale()
            y0_, y1_ = o.axes.get_ylim()
            if y0_ < y0:
                y0 = y0_
            if y1_ > y1:
                y1 = y1_
        self.ylim_y1_lineEdit.setText(f'{y0:.1f}')
        self.ylim_y2_lineEdit.setText(f'{y1:.1f}')

422
423
424
425
426
427
    @pyqtSlot(int)
    def on_ellipse_size_changed(self, i: int) -> None:
        """Ellipse size factor changed.
        """
        self._size_factor = i
        self.ellipse_size_factor_changed.emit()
428

429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
    @pyqtSlot()
    def onLoadLattice(self):
        """Load machine/segment.
        """
        if self.lattice_load_window is None:
            self.lattice_load_window = LatticeWidget()
            self.lattice_load_window.latticeChanged.connect(self.lattice_changed)
            self.lattice_load_window.latticeChanged.connect(
                    self.lattice_load_window.close)
        self.lattice_load_window.show()

    @pyqtSlot(QVariant)
    def on_lattice_changed(self, mp):
        """A new machine/segment is loaded.
        """
        self.__mp = mp
        self.__lat = mp.work_lattice_conf
        self.__z0 = self.__lat.layout.z

        # update layout drawings
        self.update_layout.emit()

        # update element list (QUAD)
        quad_name_list = [i.name for i in mp.get_elements(type='QUAD')]
        self.quad1_name_cbb.currentTextChanged.disconnect()
        self.quad1_name_cbb.clear()
        self.quad1_name_cbb.addItems(quad_name_list)
        self.quad1_name_cbb.currentTextChanged.connect(self.on_quad1_name_changed)

        # update element list (at which view results)
        ename_list = [i.name for i in self.__lat]
        self.elemlist_cbb.currentTextChanged.disconnect()
        self.elemlist_cbb.addItems(ename_list)
        self.elemlist_cbb.currentTextChanged.connect(self.on_target_element_changed)

        # update selected element (QUAD) and its settings
        self.quad1_name_cbb.currentTextChanged.emit(self.quad1_name_cbb.currentText())

        # update plots
        self.quad1_grad_dsbox.valueChanged.emit(self.quad1_grad_dsbox.value())
        # reset current selected element with the last element
        self.elemlist_cbb.setCurrentIndex(self.elemlist_cbb.count() - 1)
        self.elemlist_cbb.currentTextChanged.emit(self.elemlist_cbb.currentText())

473

474
475
476
477
478
479
480
481
482
483
if __name__ == "__main__":
    from PyQt5.QtWidgets import QApplication
    import sys

    version = 0.1
    app = QApplication(sys.argv)
    w = MyAppWindow(version)
    w.show()
    w.setWindowTitle("This is an app from template")
    sys.exit(app.exec_())