Jul-25-2020, 08:27 AM
I am trying to call a variable mode (can take either 2 or 4 ) declared in RandomProcedure class. I want to use it in the MainWindow class so that when I change the mode parameter in the GUI, I can save the mode into my file name. I called the first class into the second but if i put self.mode as a filename paramter it remains stuck to 2 even if I type in 4 in the GUI. here I use fake data to test. i can see the mode updated in the .csv datafile generated. now i want to get the self.mode that is updated in the execute() function (you can see in the print output when run the code below that the value changes). although i return it in the execute() i cannot pass it out to the Mainwindow class to use it as a filename paramter, i still get error.
import logging log = logging.getLogger(__name__) log.addHandler(logging.NullHandler()) import sys import tempfile from tempfile import NamedTemporaryFile import random import datetime from datetime import datetime as dt, timedelta import time from time import sleep import pandas as pd from pymeasure.log import console_log from pymeasure.display.Qt import QtGui from pymeasure.display.windows import ManagedWindow from pymeasure.experiment import Procedure, Results from pymeasure.experiment import IntegerParameter, FloatParameter, Parameter from copy import deepcopy class RandomProcedure(Procedure): iterations = IntegerParameter('Loop Iterations', default=10) delay = FloatParameter('Delay Time', units='s', default=0.2) seed = Parameter('Random Seed', default='20') mode = IntegerParameter('Measurement mode : 2 or 4 probes', default=2) DATA_COLUMNS = ['Mode', 'Iteration', 'Timecode', 'Elapsed', 'Random Number'] def startup(self): log.info("Setting the time of the random number generator") random.seed(self.seed) def execute(self): log.info("Starting the loop of %d iterations" % self.iterations) start = datetime.datetime.now() for i in range(self.iterations): timecode = dt.now() elapsed = (datetime.datetime.now() - start).total_seconds() data = { 'Mode': self.mode, 'Iteration': i, 'Timecode': timecode, 'Elapsed': elapsed, 'Random Number': random.randint(1, 100) } self.emit('results', data,) log.debug("Emitting results: %s" % data) sleep(self.delay) if self.should_stop(): log.warning("Caught the stop flag in the procedure") break print(self.mode) val = self.mode # print(val) return val class MainWindow(ManagedWindow, RandomProcedure): def __init__(self): self.val = self.mode super(MainWindow, self).__init__( procedure_class=RandomProcedure, inputs=['iterations', 'delay', 'seed', 'mode'], displays=['iterations', 'delay', 'seed', 'mode'], x_axis='Iteration', y_axis='Random Number', sequencer=True, # Added line sequencer_inputs=['iterations', 'delay', 'seed', 'mode'], # Added line # sequence_file="gui_sequencer_example_sequence.txt", # Added line, optional ) self.setWindowTitle('GUI Example') self.count = 0 def queue(self, *, procedure=None): self.count = self.count + 1 val = RandomProcedure.execute(self) # i call execute() to get the update value of the mode filename = 'D:\\test\\' + str(dt.now().strftime("%m%d%Y")) + '_' + 'mode_' + str(val) + '_' + str(self.count) + '.csv' log.info("Constructing the Results with a data file: %s" % filename) if procedure is None: procedure = self.make_procedure() results = Results(procedure, filename) experiment = self.new_experiment(results) self.manager.queue(experiment) if __name__ == "__main__": app = QtGui.QApplication(sys.argv) window = MainWindow() window.show() sys.exit(app.exec_())