class EditGroup(Group): def __init__(self, *args, **kwargs): self._app = Application.instance self.__topwin = kwargs.pop('topwin') Group.__init__(self, *args, **kwargs) frm = Frame(self) self.__center = ParamItem(frm) setMultiAttr(self.__center, labelText='center(deg)', entryText=0, checkFunc=self._app.checkInt, entryWidth=5, labelWidth=10) self.__center.pack(side=TOP) self._app.balloon.bind_widget( self.__center, balloonmsg='Specify the beam center here.') self.__width = ParamItem(frm) setMultiAttr(self.__width, labelText='width(deg)', entryText=20, checkFunc=self._app.checkInt, entryWidth=5, labelWidth=10) self.__width.pack(side=TOP) self._app.balloon.bind_widget( self.__width, balloonmsg='Specify the beam width here.') self.__uiImages = [] imageAddBtn = ImageTk.PhotoImage( file=uiImagePath('Pattern_Add_Button.png')) self.__uiImages.append(imageAddBtn) btn = Button(frm, image=imageAddBtn, command=self.onAdd) btn.pack(side=LEFT) self._app.balloon.bind_widget( btn, balloonmsg='Add new beam to the ideal pattern.') imageDelBtn = ImageTk.PhotoImage( file=uiImagePath('Pattern_Del_Button.png')) self.__uiImages.append(imageDelBtn) btn = Button(frm, image=imageDelBtn, command=self.onDel) btn.pack(side=LEFT) self._app.balloon.bind_widget( btn, balloonmsg='Remove the selected beam in the listbox.') imageClrBtn = ImageTk.PhotoImage( file=uiImagePath('Pattern_Clear_Button.png')) self.__uiImages.append(imageClrBtn) btn = Button(frm, image=imageClrBtn, command=self.onClear) btn.pack(side=LEFT) self._app.balloon.bind_widget( btn, balloonmsg='Clear the listbox of the beam parameters.') imagePlotBtn = ImageTk.PhotoImage( file=uiImagePath('Pattern_Plot_Button.png')) self.__uiImages.append(imagePlotBtn) btn = Button(frm, image=imagePlotBtn, command=self.onPlotIdealPattern) btn.pack(side=LEFT) self._app.balloon.bind_widget(btn, balloonmsg='Plot the ideal pattern.') frm.pack(side=LEFT, fill=Y) self.__paramlist = ScrolledList(self) self.__paramlist.list.config(height=4, width=10) self.__paramlist.pack(side=LEFT) self.name = 'Edit Ideal Pattern' self.optgrp = None def onAdd(self): self.__paramlist.list.insert( END, '{0}, {1}'.format(self.__center.getInt(), self.__width.getInt())) def onDel(self): self.__paramlist.list.delete(ANCHOR) def onClear(self): self.__paramlist.clear() def onPlotIdealPattern(self): printCode = True center, width = self.beamData self.__topwin.setIdealPattern(center, width) self.__topwin.plotIdealPattern() @property def beamData(self): beamParams = self.__paramlist.list.get(0, END) if not beamParams: self._app.printError('An error occurred!') self._app.printTip([{ 'type': 'text', 'content': '''This exception happens when the listbox of the beam parameters are empty. To make a valid ideal pattern, at least one beam should be specified. ''' }]) return center, width = zip( *[map(float, param.split(',')) for param in beamParams]) return center, width
class OptimizeGroup(Group): def __init__(self, *args, **kwargs): self._app = Application.instance self.__uiImages = [] self.__topwin = kwargs.pop('topwin') Group.__init__(self, *args, **kwargs) frm = Frame(self) frm.pack(side=TOP) imageMLbl = ImageTk.PhotoImage(file=uiImagePath('Pattern_M_Label.png')) self.__uiImages.append(imageMLbl) Label(frm, image=imageMLbl).pack(side=LEFT) self.__M = ParamItem(frm) self.__M.label.config(text='M') self.__M.entryWidth = 6 self.__M.entryText = 10 self.__M.entry.bind('<Return>', lambda dumb: self.onSolve()) self.__M.checkFunc = self._app.checkInt self.__M.pack(side=RIGHT) self._app.balloon.bind_widget( frm, balloonmsg='The number of the array elements.') imageSolveBtn = ImageTk.PhotoImage( file=uiImagePath('Pattern_Solve_Button.png')) self.__uiImages.append(imageSolveBtn) self.__btnSolve = Button(self, image=imageSolveBtn, command=self.onSolve) self.__btnSolve.pack(side=TOP) self._app.balloon.bind_widget( self.__btnSolve, balloonmsg='Launch the solver to synthesize the correlation matrix.' ) frm = Frame(self) frm.pack(side=TOP) imageDisplayBtn = ImageTk.PhotoImage( file=uiImagePath('Pattern_Display_Button.png')) self.__uiImages.append(imageDisplayBtn) Label(frm, image=imageDisplayBtn).pack(side=LEFT) self.__bDisplay = IntVar(0) chkDisplay = Checkbutton(frm, text="Display", variable=self.__bDisplay) chkDisplay.pack(side=TOP) self._app.balloon.bind_widget(frm, balloonmsg='Display solver output.') self.name = 'Optimize' def onSolve(self): printCode = True topwin = self.__topwin center, width = topwin.grpEdit.beamData topwin.figureBook.clear() topwin.setIdealPattern(center, width) topwin.plotIdealPattern() # Create a new thread for solving the correlation matrix. def solveFunc(): printCode = True topwin.solve(M=self.__M.getInt(), display=self.__bDisplay.get()) WaveSynThread.start(func=solveFunc) # Though method "start" will not return until the solve returns, the GUI will still # responding to user input because Tk.update is called by start repeatedly. # While the thread is not alive, the optimization procedure is finished. topwin.plotCurrentData()
class EditGroup(Group): def __init__(self, *args, **kwargs): self._app = Application.instance self.__topwin = kwargs.pop('topwin') Group.__init__(self, *args, **kwargs) frm = Frame(self) self.__center = ParamItem(frm) setMultiAttr(self.__center, labelText = 'center(deg)', entryText = 0, checkFunc = self._app.checkInt, entryWidth = 5, labelWidth = 10 ) self.__center.pack(side=TOP) self._app.balloon.bind_widget(self.__center, balloonmsg='Specify the beam center here.') self.__width = ParamItem(frm) setMultiAttr(self.__width, labelText = 'width(deg)', entryText = 20, checkFunc = self._app.checkInt, entryWidth = 5, labelWidth = 10 ) self.__width.pack(side=TOP) self._app.balloon.bind_widget(self.__width, balloonmsg='Specify the beam width here.') self.__uiImages = [] imageAddBtn = ImageTk.PhotoImage( file=uiImagePath('Pattern_Add_Button.png') ) self.__uiImages.append(imageAddBtn) btn = Button(frm, image=imageAddBtn, command=self.onAdd) btn.pack(side=LEFT) self._app.balloon.bind_widget(btn, balloonmsg='Add new beam to the ideal pattern.') imageDelBtn = ImageTk.PhotoImage( file=uiImagePath('Pattern_Del_Button.png') ) self.__uiImages.append(imageDelBtn) btn = Button(frm, image=imageDelBtn, command=self.onDel) btn.pack(side=LEFT) self._app.balloon.bind_widget(btn, balloonmsg='Remove the selected beam in the listbox.') imageClrBtn = ImageTk.PhotoImage( file=uiImagePath('Pattern_Clear_Button.png') ) self.__uiImages.append(imageClrBtn) btn = Button(frm, image=imageClrBtn, command=self.onClear) btn.pack(side=LEFT) self._app.balloon.bind_widget(btn, balloonmsg='Clear the listbox of the beam parameters.') imagePlotBtn = ImageTk.PhotoImage( file=uiImagePath('Pattern_Plot_Button.png') ) self.__uiImages.append(imagePlotBtn) btn = Button(frm, image=imagePlotBtn, command=self.onPlotIdealPattern) btn.pack(side=LEFT) self._app.balloon.bind_widget(btn, balloonmsg='Plot the ideal pattern.') frm.pack(side=LEFT, fill=Y) self.__paramlist = ScrolledList(self) self.__paramlist.list.config(height=4, width=10) self.__paramlist.pack(side=LEFT) self.name = 'Edit Ideal Pattern' self.optgrp = None def onAdd(self): self.__paramlist.list.insert(END, '{0}, {1}'.format(self.__center.getInt(), self.__width.getInt())) def onDel(self): self.__paramlist.list.delete(ANCHOR) def onClear(self): self.__paramlist.clear() def onPlotIdealPattern(self): printCode = True center, width = self.beamData self.__topwin.setIdealPattern(center, width) self.__topwin.plotIdealPattern() @property def beamData(self): beamParams = self.__paramlist.list.get(0, END) if not beamParams: self._app.printError('An error occurred!') self._app.printTip( [ { 'type':'text', 'content':'''This exception happens when the listbox of the beam parameters are empty. To make a valid ideal pattern, at least one beam should be specified. ''' } ] ) return center, width = zip(*[map(float, param.split(',')) for param in beamParams]) return center, width
class OptimizeGroup(Group): def __init__(self, *args, **kwargs): self._app = Application.instance self.__topwin = kwargs.pop('topwin') super(OptimizeGroup, self).__init__(*args, **kwargs) paramFrm = Frame(self) paramFrm.pack(side=LEFT, expand=YES, fill=Y) self.__num = ParamItem(paramFrm) setMultiAttr(self.__num, labelText = 'num', entryText = '1', labelWidth = 5, entryWidth = 8, checkFunc = self._app.checkInt ) self.__num.entry.bind('<Return>', lambda event: self.onSolveClick()) self.__num.pack(side=TOP) self.__pci = ParamItem(paramFrm) setMultiAttr(self.__pci, labelText = 'PCI', entryText = '100', labelWidth = 5, entryWidth = 8, checkFunc = self._app.checkInt ) self.__pci.pack(side=TOP) self.__bParallel = IntVar() self.__chkParallel = Checkbutton(paramFrm, text="Parallel", variable=self.__bParallel) self.__chkParallel.pack() progfrm = Frame(self) progfrm.pack(side=LEFT, expand=YES, fill=Y) self.__genbtn = Button(progfrm, text='Generate', command=self.onSolveClick) self.__genbtn.pack(side=TOP) Button(progfrm, text='Stop', command=self.onStopBtnClick).pack(side=TOP) self.__progress = IntVar() self.__finishedwav = IntVar() self.__progbar = Progressbar(progfrm, orient='horizontal', variable=self.__progress, maximum=100) self.__progbar.pack(side=LEFT) self.__progbar.config(length=55) self.__finishedwavbar = Progressbar(progfrm, orient='horizontal', variable=self.__finishedwav) self.__finishedwavbar.pack(side=LEFT) self.__finishedwavbar.config(length=30) self.name = 'Generate' self.getparams = None self.__stopflag = False def onSolveClick(self): t1 = time.clock() if self.__bParallel.get(): self.parallelRun() else: self.serialRun() deltaT = time.clock() - t1 print(autoSubs('Total time consumption: $deltaT (s)')) def serialRun(self): tbicon = self._app.tbicon self.__stopflag = False wavnum = self.__num.getInt() progress = [0] waveform = [0] algorithm = self.__topwin.currentAlgorithm params = self.__topwin.grpParams.getParams() class AlgoThread(threading.Thread): def __init__(self, algo, params, waveform, progress): self.algo = algo self.progress = progress threading.Thread.__init__(self) def run(self): printCode = True self.algo.run(**params) self.__finishedwavbar['maximum'] = wavnum def progressChecker(k, K, y, *args, **kwargs): progress[0] = int(k / K * 100) algorithm.progressChecker.append(progressChecker) algorithm.progressChecker.interval = int(self.__pci.entryText) try: for cnt in range(wavnum): algothread = AlgoThread(algorithm, params, waveform, progress) algothread.start() while algothread.isAlive(): self.__progress.set(progress[0]) tbicon.progress = int((cnt*100+progress[0])/(wavnum*100)*100) self.__topwin.update() if self.__stopflag: break time.sleep(0.05) self.__progress.set(0) if self.__stopflag: break printCode = True self.__topwin.plotCurrentData() self.__finishedwav.set(cnt+1) finally: algorithm.progressChecker.remove(progressChecker) self.__finishedwav.set(0) tbicon.state = guicomponents.tk.TBPF_NOPROGRESS def parallelRun(self): class AlgoThread(threading.Thread): def __init__(self, algorithm, parameters, num): self.algorithm = algorithm self.parameters = parameters self.num = num super(AlgoThread, self).__init__() def run(self): printCode = True #parameters = Scripting.paramsToStr(**self.parameters) paramStr = evalFmt('[([], dict({Scripting.paramsToStr(**self.parameters)}))]*{self.num}') self.algorithm.parallelRunAndPlot(ScriptCode(paramStr)) algorithm = self.__topwin.currentAlgorithm parameters = self.__topwin.grpParams.getParams() theThread = AlgoThread(algorithm, parameters, self.__num.getInt()) theThread.start() while theThread.isAlive(): self.__topwin.update() time.sleep(0.05) def onStopBtnClick(self): self.__stopflag = True
class OptimizeGroup(Group): def __init__(self, *args, **kwargs): self._app = Application.instance self.__uiImages = [] self.__topwin = kwargs.pop('topwin') Group.__init__(self, *args, **kwargs) frm = Frame(self) frm.pack(side=TOP) imageMLbl = ImageTk.PhotoImage( file=uiImagePath('Pattern_M_Label.png') ) self.__uiImages.append(imageMLbl) Label(frm, image=imageMLbl).pack(side=LEFT) self.__M = ParamItem(frm) self.__M.label.config(text='M') self.__M.entryWidth = 6 self.__M.entryText = 10 self.__M.entry.bind('<Return>', lambda dumb: self.onSolve()) self.__M.checkFunc = self._app.checkInt self.__M.pack(side=RIGHT) self._app.balloon.bind_widget(frm, balloonmsg='The number of the array elements.') imageSolveBtn = ImageTk.PhotoImage( file=uiImagePath('Pattern_Solve_Button.png') ) self.__uiImages.append(imageSolveBtn) self.__btnSolve = Button(self, image=imageSolveBtn, command=self.onSolve) self.__btnSolve.pack(side=TOP) self._app.balloon.bind_widget(self.__btnSolve, balloonmsg='Launch the solver to synthesize the correlation matrix.') frm = Frame(self) frm.pack(side=TOP) imageDisplayBtn = ImageTk.PhotoImage( file=uiImagePath('Pattern_Display_Button.png') ) self.__uiImages.append(imageDisplayBtn) Label(frm, image=imageDisplayBtn).pack(side=LEFT) self.__bDisplay = IntVar(0) chkDisplay = Checkbutton(frm, text="Display", variable=self.__bDisplay) chkDisplay.pack(side=TOP) self._app.balloon.bind_widget(frm, balloonmsg='Display solver output.') self.name = 'Optimize' def onSolve(self): printCode = True topwin = self.__topwin center, width = topwin.grpEdit.beamData topwin.figureBook.clear() topwin.setIdealPattern(center, width) topwin.plotIdealPattern() # Create a new thread for solving the correlation matrix. def solveFunc(): printCode = True topwin.solve(M=self.__M.getInt(), display=self.__bDisplay.get()) WaveSynThread.start(func=solveFunc) # Though method "start" will not return until the solve returns, the GUI will still # responding to user input because Tk.update is called by start repeatedly. # While the thread is not alive, the optimization procedure is finished. topwin.plotCurrentData()