Skip to content

Commit cfdc2d1

Browse files
committed
fdbpm now on pyqt5
1 parent ecc29ca commit cfdc2d1

5 files changed

Lines changed: 180 additions & 156 deletions

File tree

easy_gui.py

Lines changed: 26 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -1,112 +1,58 @@
1-
# -*- coding: utf-8 -*-
2-
# @Author: Marcel Reis-Soubkovsky
3-
# @Date: 2020-06-28 15:46:41
4-
# @Last Modified by: Marcel Reis-Soubkovsky
5-
# @Last Modified time: 2020-07-08 18:04:03
6-
7-
# from PySide2.QtWidgets import *
8-
from PySide2.QtUiTools import QUiLoader
9-
from PySide2.QtCore import Qt, QFile, QIODevice
10-
from PySide2.QtWidgets import QApplication, QWidget, QFileDialog, QVBoxLayout, QGridLayout, QMainWindow
11-
from PySide2.QtGui import QIcon
12-
import sys
13-
14-
from matplotlib.backends.backend_qt5agg import (
15-
FigureCanvas, NavigationToolbar2QT as NavigationToolbar)
1+
from PyQt5 import QtWidgets, QtGui
2+
3+
import matplotlib
4+
matplotlib.use('Qt5Agg')
165
import matplotlib.pyplot as plt
176

7+
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg
188
from matplotlib.figure import Figure
19-
from matplotlib.widgets import Slider, Button, RadioButtons
20-
from cycler import cycler
21-
22-
import numpy as np
239

24-
25-
class MplWidget(QWidget):
10+
class MplWidget(FigureCanvasQTAgg):
2611

27-
def __init__(self, parent = None):
28-
29-
# super().__init__()
30-
QWidget.__init__(self, parent)
31-
32-
self.mpl_canvas = FigureCanvas(Figure(constrained_layout=True, facecolor='#f0f0f0'))
33-
self.mpl_canvas.axes = self.mpl_canvas.figure.add_subplot(111)
34-
# self.mpl_canvas.setParent()
35-
36-
vertical_layout = QVBoxLayout()
37-
vertical_layout.addWidget(self.mpl_canvas)
38-
self.setLayout(vertical_layout)
39-
# vertical_layout.addWidget(NavigationToolbar(self.mpl_canvas, self)) #uncomment in order to have the matplotlib tools
12+
def __init__(self, parent=None, width=5, height=4, dpi=100):
4013

14+
fig = Figure(figsize=(width, height), dpi=dpi)
15+
self.axes = fig.add_subplot(111)
16+
super(MplWidget, self).__init__(fig)
4117

4218
# --------------------------------------
43-
class EasyGUI(QWidget):
19+
class EasyPlotGUI(QtWidgets.QMainWindow):
4420

4521
"""Easy GUI with Matplotlib
4622
"""
4723

48-
def __init__(self):
24+
def __init__(self, ui_python_file):
25+
self.app = QtWidgets.QApplication([])
4926
self.window_title="EasyPlotGUI"
50-
self.ui_filepath="test_example.ui"
51-
self.icon_path="logo.png"
27+
28+
self.icon_path=None
29+
self.ui=ui_python_file.Ui_MainWindow()
30+
super().__init__()
31+
self.ui.setupUi(self)
5232

53-
5433

5534
def show_gui(self):
56-
"""This is the core of the execution.
35+
"""Shows the GUI.
5736
"""
58-
app = QApplication([])
59-
60-
QWidget.__init__(self)
61-
62-
loader = QUiLoader()
63-
loader.registerCustomWidget(MplWidget)
64-
# self.ui = loader.load(designer_file, self)
65-
ui_file = QFile(self.ui_filepath)
66-
try:
67-
ui_file.open(QIODevice.ReadOnly)
68-
except:
69-
raise TypeError("Unable to open {}: {}".format(self.ui_filepath, ui_file.errorString()))
70-
# loader = QUiLoader()
71-
72-
self.ui = loader.load(ui_file, None)
73-
ui_file.close()
7437

7538
try:
76-
self.ax=self.ui.MplWidget.mpl_canvas.axes
77-
print("après")
78-
self.canvas=self.ui.MplWidget.mpl_canvas
79-
80-
custom_cycler = (cycler(color=['#ed2124','#808081','c', 'm', 'y', 'k']))
81-
plt.rc('axes', prop_cycle=custom_cycler)
39+
self.ax=self.ui.MplWidget.axes
40+
self.draw=self.ui.MplWidget.draw
8241
self.update_graph()
8342
except AttributeError:
84-
# raise AttributeError("ERROR")
8543
pass
8644

87-
if not self.ui:
88-
print(loader.errorString())
89-
sys.exit(-1)
90-
9145
self.update_interactivity()
9246

9347

9448
self.setWindowTitle(self.window_title)
9549

96-
97-
98-
grid_layout = QGridLayout()
99-
grid_layout.addWidget(self.ui)
100-
self.setLayout(grid_layout)
101-
102-
self.QFileDialog=QFileDialog
103-
self.QMainWindow=QMainWindow
104-
10550
if self.icon_path!=None:
106-
self.setWindowIcon(QIcon(self.icon_path))
51+
52+
self.setWindowIcon(QtGui.QIcon(self.icon_path))
10753

108-
self.show()
109-
app.exec_()
54+
self.show() # Show a window
55+
self.app.exec_()
11056

11157
def update_interactivity(self):
11258
"""This method adds the interactivity between the GUI elements and the code. An example of execution can be seen below. This method is supposed to be overwritten when EasyGUI is imported as a parent class for adding your own graphs.
@@ -122,55 +68,5 @@ def update_graph(self):
12268
12369
"""
12470
# example, to be overwritten
125-
y = np.random.rand(100)
126-
x = np.linspace(0,1,100)
127-
128-
self.ax.clear()
129-
self.ax.plot(x, y, label="Random")
130-
self.ax.legend()
131-
self.ax.set_title('Random Values')
132-
self.canvas.draw()
133-
134-
if __name__ == "__main__":
135-
# Test Example
136-
gui = EasyGUI()
137-
gui.window_title="Window Title"
138-
gui.ui_filepath="test_example.ui"
139-
gui.icon_path="./logo.png"
140-
gui.show_gui()
141-
142-
# EXAMPLE OF USAGE
143-
# Slider changing the frequency of a sine wave.
144-
145-
# class MyClass(EasyPlotGUI):
146-
# def __init__(self):
147-
# super().__init__()
148-
# self.window_title="My GUI Name"
149-
# self.ui_filepath="X:/xxxxx/xxxx/your_GUI.ui"
150-
# self.icon_path="X:/xxxxx/xxxx/your_GUI_icon.png"
151-
152-
# #initialize Graph variables for first plot
153-
# self.f=1
15471

155-
# def update_interactivity(self):
156-
# self.ui.mySlider.valueChanged(self.change_frequency())
157-
158-
# def change_frequency(self):
159-
# self.f=self.ui.mySlider.value()
160-
# self.update_graph()
161-
162-
# def update_graph(self):
163-
# x=np.linspace(0,1)
164-
# y=np.sin(2*np.pi*self.f*x)
165-
166-
# self.ax.clear()
167-
# self.ax.plot(x, y, label="Sine")
168-
# self.ax.legend()
169-
# self.ax.set_title('Sine Wave')
170-
# self.canvas.draw()
171-
172-
# #calling it
173-
# my_gui=MyClass()
174-
# my_gui.show_gui()
175-
176-
72+
self.draw()

fdbpm.py

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,24 @@
1-
import numpy as np
21
import time as t
32

4-
from PySide2.QtWidgets import *
5-
6-
from cycler import cycler
7-
83
import numpy as np
94

105
import matplotlib.pyplot as plt
11-
from matplotlib.widgets import Slider, Button, RadioButtons
6+
from matplotlib.widgets import Slider
7+
128
from functools import partial
139
import copy
1410

15-
from easy_gui import EasyGUI
11+
from easy_gui import EasyPlotGUI
12+
import interface_ui
1613

1714
from numba import jit
1815

1916

20-
class FdBpm(EasyGUI):
17+
class FdBpm(EasyPlotGUI):
2118
def __init__(self):
22-
super().__init__()
19+
super().__init__(interface_ui)
2320
self.slider_realtime = True
2421
self.window_title = "FD-BPM"
25-
self.ui_filepath = r"interface.ui"
2622
self.icon_path = r"fdbpm-logo.png"
2723

2824
def create_space(self):
@@ -155,7 +151,7 @@ def make_tri_matrix(self):
155151

156152
@staticmethod
157153
@jit(forceobj=True)
158-
def core_propag_jit(A,B, ro, light, tridiag_matrix, h,LENGTH,NUM_SAMPLES):
154+
def core_propag_jit(A, B, ro, light, tridiag_matrix, h, LENGTH, NUM_SAMPLES):
159155

160156
propag = np.zeros((int(LENGTH), int(NUM_SAMPLES)))
161157
d = np.zeros((NUM_SAMPLES), dtype='complex_')
@@ -172,7 +168,8 @@ def core_propag_jit(A,B, ro, light, tridiag_matrix, h,LENGTH,NUM_SAMPLES):
172168
# light = np.linalg.lstsq(tridiag_matrix,d)[0]
173169
light = np.linalg.solve(tridiag_matrix, d) # fastest option
174170
# propag[n,:] = np.abs(light)
175-
propag[n, :] = (light*light.conjugate()).real
171+
# propag[n, :] = (light*light.conjugate()).real
172+
propag[n, :] = light.real**2 + light.imag**2
176173
# propag[n,:] = np.real(light) # Oscillations
177174
# propag[n,:] = np.angle(light) # interesting
178175
return propag
@@ -185,7 +182,8 @@ def calculate_propagation(self, plotOn=True, timing=True):
185182
if(timing == True):
186183
start = t.time()
187184

188-
propag = self.core_propag_jit(self.A,self.B, self.ro, self.light, self.tridiag_matrix, self.h,self.LENGTH,self.NUM_SAMPLES)
185+
propag = self.core_propag_jit(
186+
self.A, self.B, self.ro, self.light, self.tridiag_matrix, self.h, self.LENGTH, self.NUM_SAMPLES)
189187

190188
if(timing == True):
191189
end = t.time()
@@ -376,8 +374,8 @@ def examples(self, name, chart_update=False):
376374

377375
def update_graph(self, slider=False):
378376

379-
canvas = self.ui.MplWidget.mpl_canvas
380-
ax = self.ui.MplWidget.mpl_canvas.axes # no canvas in the object
377+
378+
ax = self.ax # no canvas in the object
381379

382380
if int(np.floor(self.LENGTH/self.NUM_SAMPLES)) > 0:
383381
propag_img = self.calculate_propagation(
@@ -395,7 +393,7 @@ def update_graph(self, slider=False):
395393
# ax.clear()
396394
self.img.set_data(propag_img)
397395
plt.tight_layout(pad=0.)
398-
canvas.draw()
396+
self.draw()
399397

400398

401399
if __name__ == "__main__":

interface.ui

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
<string notr="true">FDBPM</string>
1818
</property>
1919
<property name="windowIcon">
20-
<iconset resource="resource.qrc">
20+
<iconset>
2121
<normaloff>:/icon/beampy-logo.png</normaloff>:/icon/beampy-logo.png</iconset>
2222
</property>
2323
<property name="autoFillBackground">
@@ -278,12 +278,10 @@
278278
<customwidget>
279279
<class>MplWidget</class>
280280
<extends>QWidget</extends>
281-
<header location="global">mplwidget.h</header>
281+
<header location="global">easy_gui</header>
282282
<container>1</container>
283283
</customwidget>
284284
</customwidgets>
285-
<resources>
286-
<include location="resource.qrc"/>
287-
</resources>
285+
<resources/>
288286
<connections/>
289287
</ui>

0 commit comments

Comments
 (0)