class TiePieUi(QSplitter): ''' Handling user interface to manage TiePie HS4/Diff Oscilloscope ''' scpConnected = Signal() xAxeChanged = Signal(object, object) yAxeChanged = Signal(object, object) triggLevelChanged = Signal(object) def __init__(self, parent): #super(ObjectFT, self).__init__(Qt.Vertical, parent) super().__init__(parent) self.scp = None # variable to hold oscilloscope object self.mutex = QMutex() layoutWidget = QWidget() layout = QGridLayout() layoutWidget.setLayout(layout) self.openDevBtn = QPushButton('Open Osci') # channel stuff self.measCh = QComboBox() self.chSens = QComboBox() self.triggCh = QComboBox() self.frequency = QLineEdit() self.frequency.setValidator(QIntValidator()) self.recordLen = QLineEdit() self.recordLen.setValidator(QIntValidator()) self.delay = QLineEdit() self.delay.setValidator(QDoubleValidator()) # trigger stuff self.triggLevel = QLineEdit() self.triggLevel.setToolTip( 'http://api.tiepie.com/libtiepie/0.5/triggering_scpch.html#triggering_scpch_level' ) self.triggLevel.setText( '0.5' ) # init value otherwise there's trouble with signal changing index of sensitivity self.triggLevel.setValidator(QDoubleValidator(0., 1., 3)) self.hystereses = QLineEdit() self.hystereses.setText('0.05') self.hystereses.setToolTip( 'http://api.tiepie.com/libtiepie/0.5/triggering_scpch.html#triggering_scpch_hysteresis' ) self.hystereses.setValidator(QDoubleValidator(0., 1., 3)) self.triggKind = QComboBox() # do averages self.averages = QSpinBox() self.averages.setValue(1) self.averages.setRange(1, 10000) # put layout together layout.addWidget(self.openDevBtn, 0, 0) layout.addWidget(QLabel('Measuring Ch'), 1, 0) layout.addWidget(self.measCh, 1, 1) layout.addWidget(QLabel('Ch sensitivity'), 2, 0) layout.addWidget(self.chSens, 2, 1) layout.addWidget(QLabel('Sample freq. (kHz)'), 3, 0) layout.addWidget(self.frequency, 3, 1) layout.addWidget(QLabel('Record length'), 4, 0) layout.addWidget(self.recordLen, 4, 1) layout.addWidget(QLabel('Delay'), 5, 0) layout.addWidget(self.delay, 5, 1) layout.addWidget(QLabel('Trigger Ch'), 6, 0) layout.addWidget(self.triggCh, 6, 1) layout.addWidget(QLabel('Trigger Level (%)'), 7, 0) layout.addWidget(self.triggLevel, 7, 1) layout.addWidget(QLabel('Hystereses'), 8, 0) layout.addWidget(self.hystereses, 8, 1) layout.addWidget(QLabel('Trigger kind'), 9, 0) layout.addWidget(self.triggKind, 9, 1) layout.addWidget(QLabel('Averages'), 10, 0) layout.addWidget(self.averages, 10, 1) layout.setRowStretch(11, 10) layout.setColumnStretch(2, 10) self.addWidget(layoutWidget) # connect UI to get things working self.openDevBtn.released.connect(self.openDev) self.chSens.currentIndexChanged.connect(self._changeSens) self.frequency.returnPressed.connect(self._changeFreq) self.recordLen.returnPressed.connect(self._changeRecordLength) self.triggCh.currentIndexChanged.connect(self._changeTrigCh) self.triggLevel.returnPressed.connect(self._triggLevelChanged) self.triggLevel.textChanged.connect(self._check_state) self.hystereses.returnPressed.connect(self._setHystereses) self.hystereses.textChanged.connect(self._check_state) def openDev(self): # search for devices libtiepie.device_list.update() # try to open an oscilloscope with block measurement support for item in libtiepie.device_list: if item.can_open(libtiepie.DEVICETYPE_OSCILLOSCOPE): self.scp = item.open_oscilloscope() if self.scp.measure_modes & libtiepie.MM_BLOCK: break else: self.scp = None # init UI #print(self.scp.name, 'found') if self.scp is not None: # Set measure mode: self.scp.measure_mode = libtiepie.MM_BLOCK # Set sample frequency: self.scp.sample_frequency = 1e6 # 1 MHz # Set record length: self.scp.record_length = 10000 # 10000 samples # Set pre sample ratio: self.scp.pre_sample_ratio = 0 # 0 % # Set trigger timeout: self.scp.trigger_time_out = 100e-3 # 100 ms # Enable channel 1 for measurement # http://api.tiepie.com/libtiepie/0.5/group__scp__ch__enabled.html self.scp.channels[ 0].enabled = True # by default all channels are enabled self.scp.range = 0.2 self.scp.coupling = libtiepie.CK_DCV # DC Volt # Disable all channel trigger sources for ch in self.scp.channels: ch.trigger.enabled = False # Setup channel trigger on 1 ch = self.scp.channels[0] ch.trigger.enabled = True ch.trigger.kind = libtiepie.TK_RISINGEDGE ch.trigger.levels[0] = 0.5 # 50% ch.trigger.hystereses[0] = 0.05 # 5% # update UI # channel self.measCh.addItems( ['Ch{:d}'.format(i) for i in range(self.scp.channels.count)]) self.chSens.addItems( ['{:.1f} V'.format(i) for i in self.scp.channels[0].ranges]) self.frequency.setValidator( QIntValidator(1, 1e-3 * self.scp.sample_frequency_max)) self.frequency.setText('{:d}'.format( int(self.scp.sample_frequency * 1e-3))) self.recordLen.setValidator( QIntValidator(1, self.scp.record_length_max)) self.recordLen.setText('{:d}'.format(self.scp.record_length)) # trigger self.triggCh.addItems( ['Ch{:d}'.format(i) for i in range(self.scp.channels.count)]) # TODO: doen't work in module anymore!! #self.triggLevel.setText(str(ch.trigger.levels[0])) #self.hystereses.setText(str(ch.trigger.hystereses[0])) self.triggKind.addItems([ '{:s}'.format(i) for i in libtiepie.trigger_kind_str( ch.trigger.kinds).split(', ') ]) self.openDevBtn.setEnabled(False) # tell the world that the scope is connected self.xAxeChanged.emit( 0, 1 / int(self.frequency.text()) * 1e-3 * int(self.recordLen.text())) self.yAxeChanged.emit(-1 * self.scp.range, self.scp.range) self.triggLevelChanged.emit(ch.trigger.levels[0] * 2 * self.scp.range - self.scp.range) self.scpConnected.emit() else: msg = QMessageBox() msg.setIcon(QMessageBox.Critical) msg.setText('No supported device found') msg.exec_() def getData(self): # function called thread for updating plot avg = int(self.averages.text()) with QMutexLocker(self.mutex): x = np.linspace( 0, 1 / self.scp.sample_frequency * self.scp.record_length, self.scp.record_length) y = np.zeros((avg, self.scp.record_length)) for i in range(avg): self.scp.start() while not self.scp.is_data_ready: time.sleep(0.01) y[i, :] = self.scp.get_data()[self.measCh.currentIndex()] return np.column_stack((x, y.mean(axis=0))) #@Slot def _changeSens(self, i): with QMutexLocker(self.mutex): yMax = self.scp.channels[0].ranges[i] self.scp.range = yMax self.yAxeChanged.emit(-1 * yMax, yMax) self.triggLevelChanged.emit( float(self.triggLevel.text()) * 2 * yMax - yMax) def _changeTrigCh(self, newTrig): print('new trigger channel', newTrig) scope = self.scp with QMutexLocker(self.mutex): # Disable all channel trigger sources for ch in scope.channels: ch.trigger.enabled = False # enable trigger on newly selected channel ch = scope.channels[newTrig] ch.trigger.enabled = True ch.trigger.kind = libtiepie.TK_RISINGEDGE ch.trigger.levels[0] = float(self.triggLevel.text()) ch.trigger.hystereses[0] = float(self.hystereses.text()) def _triggLevelChanged(self): with QMutexLocker(self.mutex): idx = self.triggCh.currentIndex() ch = self.scp.channels[idx] ch.trigger.levels[0] = float(self.triggLevel.text()) self.triggLevelChanged.emit( float(self.triggLevel.text()) * 2 * self.scp.range - self.scp.range) def _changeFreq(self): with QMutexLocker(self.mutex): self.scp.sample_frequency = int(self.frequency.text()) * 1e3 self.xAxeChanged.emit( 0, 1 / self.scp.sample_frequency * self.scp.record_length) def _changeRecordLength(self): with QMutexLocker(self.mutex): self.scp.record_length = int(self.recordLen.text()) self.xAxeChanged.emit( 0, 1 / self.scp.sample_frequency * self.scp.record_length) def _setHystereses(self): with QMutexLocker(self.mutex): self.scp.hystereses = float(self.hystereses.text()) def _check_state(self, *args, **kwargs): '''https://snorfalorpagus.net/blog/2014/08/09/validating-user-input-in-pyqt4-using-qvalidator/''' sender = self.sender() validator = sender.validator() state = validator.validate(sender.text(), 0)[0] if state == QValidator.Acceptable: color = '#FFFFFF' # green elif state == QValidator.Intermediate: color = '#fff79a' # yellow else: color = '#f6989d' # red sender.setStyleSheet('QLineEdit { background-color: %s }' % color)
class CentralWidget(QSplitter): def __init__(self, parent): QSplitter.__init__(self, parent) self.name = 'subject' self.age = 26 self.peakL = 559 self.peakM = 530 self.ref_LED = 10 self.LED1 = 15 self.LED2 = 13.9 self.LED3 = 40.45 self.L_OD = 0.35 self.M_OD = 0.22 self.setContentsMargins(10, 10, 10, 10) self.setOrientation(Qt.Vertical) line1 = QSplitter() line1.setOrientation(Qt.Horizontal) line2 = QSplitter() line2.setOrientation(Qt.Horizontal) line3 = QSplitter() line3.setOrientation(Qt.Horizontal) line4 = QSplitter() line4.setOrientation(Qt.Horizontal) line5 = QSplitter() line5.setOrientation(Qt.Horizontal) line6 = QSplitter() line6.setOrientation(Qt.Horizontal) line7 = QSplitter() line7.setOrientation(Qt.Horizontal) line8 = QSplitter() line8.setOrientation(Qt.Horizontal) self.txt1 = QLineEdit(str(self.name)) self.txt1.setMaximumWidth(100) self.txt2 = QLineEdit(str(self.age)) self.txt2.setMaximumWidth(100) self.txt3 = QLineEdit(str(self.peakL)) self.txt3.setMaximumWidth(100) self.txt4 = QLineEdit(str(self.peakM)) self.txt4.setMaximumWidth(100) self.txt5 = QLineEdit(str(self.ref_LED)) self.txt5.setMaximumWidth(100) self.txt6 = QLineEdit() self.txt6.setMaximumWidth(100) self.txt7 = QLineEdit() self.txt7.setMaximumWidth(100) self.txt8 = QLineEdit() self.txt8.setMaximumWidth(100) label1 = QLabel('ID') label2 = QLabel('age') label3 = QLabel('peak L') label4 = QLabel('peak M') label5 = QLabel('ref LED') label6 = QLabel('LED1') label7 = QLabel('LED2') label8 = QLabel('LED3') line1.addWidget(label1) line2.addWidget(label2) line3.addWidget(label3) line4.addWidget(label4) line5.addWidget(label5) line6.addWidget(label6) line7.addWidget(label7) line8.addWidget(label8) line1.addWidget(self.txt1) line2.addWidget(self.txt2) line3.addWidget(self.txt3) line4.addWidget(self.txt4) line5.addWidget(self.txt5) line6.addWidget(self.txt6) line7.addWidget(self.txt7) line8.addWidget(self.txt8) self.load_button = QPushButton('load data') self.load_button.clicked.connect(self.load) self.analyze_button = QPushButton('analyze') self.analyze_button.clicked.connect(self.analyze) # add left side of the bottom panel left_side = QSplitter() left_side.setOrientation(Qt.Vertical) left_side.addWidget(self.load_button) left_side.addWidget(line1) left_side.addWidget(line2) left_side.addWidget(line3) left_side.addWidget(line4) left_side.addWidget(line5) left_side.addWidget(line6) left_side.addWidget(line7) left_side.addWidget(line8) left_side.addWidget(self.analyze_button) # set up right side of the bottom panel self.results = QListWidget(self) self.save_button = QPushButton('save', self.results) self.save_button.clicked.connect(self.save) # add right side of the bottom panel right_side = QSplitter() right_side.setOrientation(Qt.Vertical) right_side.addWidget(self.results) right_side.addWidget(self.save_button) self.addWidget(right_side) # add left and right side to bottom bottom = QSplitter() bottom.addWidget(left_side) bottom.addWidget(right_side) self.plots = CurveWidget(self, xlabel='wavelength (nm)', ylabel='sensitivity') self.addWidget(self.plots) self.addWidget(bottom) self.show_data(PRINT=False) self.setStretchFactor(0, 0) self.setStretchFactor(1, 1) self.setHandleWidth(10) self.setSizes([800, 1]) self.parse_rc_file() def parse_rc_file(self): ''' ''' handle = open(APP_DIR + '\\lm_ratiorc.txt', 'r') params = json.load(handle) self.save_dir = params['SAVE_DIR'] def analyze(self): ''' ''' success = self.get_input_values() if success: # analyze self.show_data() def get_input_values(self): ''' ''' # check that values in try: if len(str(self.txt1.displayText())) < 2: raise ValueError('No subject name') self.name = str(self.txt1.displayText()) self.age = float(self.txt2.displayText()) self.peakL = float(self.txt3.displayText()) self.peakM = float(self.txt4.displayText()) self.ref_LED = float(self.txt5.displayText()) self.LED1 = float(self.txt6.displayText()) self.LED2 = float(self.txt7.displayText()) self.LED3 = float(self.txt8.displayText()) return True except ValueError: message = [] message.append('ERROR: Make sure values were entered \nproperly.') self.results.addItems(message) return False def load(self): ''' ''' # Open a file finder. Load the csv file. name = QFileDialog.getOpenFileName() try: f = open(name, 'r') raw_dat = f.read() f.close() proceed = True except IOError: message = [] message.append('Could not open file.') self.results.addItems(message) proceed = False if proceed: data = {} lines = raw_dat.split('\n') for line in lines: if line != '': d = line.split(',') array = [] for num in d[1:]: if num != '': array.append(num) data[d[0]] = np.asarray(array, dtype=float) if 'name' in data: self.txt1.setText(str(data['name'][0])) else: self.txt1.setText('') if 'age' in data: self.txt2.setText(str(data['age'][0])) else: self.txt2.setText('') if 'L_peak' in data: self.txt3.setText(str(data['L_peak'][0])) else: self.txt3.setText('') if 'M_peak' in data: self.txt4.setText(str(data['M_peak'][0])) else: self.txt4.setText('') if 'ref' in data: self.txt5.setText(str(data['ref'][0])) else: self.txt5.setText('') if 'LED1' in data: self.txt6.setText(str(np.mean(data['LED1']))) else: self.txt6.setText('') if 'LED2' in data: self.txt7.setText(str(np.mean(data['LED2']))) else: self.txt7.setText('') if 'LED3' in data: self.txt8.setText(str(np.mean(data['LED3']))) else: self.txt8.setText('') def save(self): ''' ''' try: self.show_data(save_plot=True, save_data=True, PRINT=True) message = [] message.append('Success! Plot and data saved.') self.results.addItems(message) except: message = [] message.append('ERROR! Plot and data were not \nsaved.') self.results.addItems(message) def show_data(self, save_plot=False, save_data=False, PRINT=True): ''' ''' LM = LMratio() LM.set_parameters(age=self.age, L_peak=self.peakL, M_peak=self.peakM) LM.set_LED_rel_intensities(ref=self.ref_LED, light1=self.LED1, light2=self.LED2, light3=self.LED3) l_frac, error = LM.find_LMratio(PRINT=False) l_percent = l_frac * 100 _data = LM.return_data() data_x = _data['LED_peaks'] data_y = np.log10( np.array([_data['ref'], _data[1], _data[2], _data[3]])) # get fit spectrum = LM.return_spectrum() cones = LM.return_cones() fit = np.log10(LM.return_fit()) L_cones = np.log10(cones['L']) M_cones = np.log10(cones['M']) items = (make.curve(spectrum, L_cones, 'L cones', color='r', linewidth=2), make.curve(spectrum, M_cones, 'M cones', color='g', linewidth=2), make.curve(spectrum, fit, 'fit', color='k', linewidth=2), make.curve(data_x, data_y, linestyle='NoPen', marker="Diamond", markersize=14, markerfacecolor='k') ) self.plots.plot.del_all_items() for item in items: self.plots.plot.add_item(item) self.plots.plot.set_plot_limits(450, 670, -2, 0) self.plots.plot.replot() if PRINT: message = [self.name] message.append('L: ' + str(round(l_percent, 1))) message.append('M: ' + str(round(100 - l_percent, 2))) message.append('error: ' + str(round(error, 4))) self.results.clear() self.results.addItems(message) if save_plot or save_data: directory = self.save_dir + self.name + '\\' if not os.path.exists(directory): os.makedirs(directory) trial = 1 file_name = directory + self.name + '_' + str(trial) while os.path.exists(file_name + '.csv'): trial += 1 file_name = directory + self.name + '_' + str(trial) if save_plot: LM.plot_lm_ratio( save_name=file_name, save_plot=True, show_plot=False) if save_data: LM.save_data_and_params(file_name) del LM
class FitParam(object): def __init__(self, name, value, min, max, logscale=False, steps=5000, format='%.3f', size_offset=0, unit=''): self.name = name self.value = value self.min = min self.max = max self.logscale = logscale self.steps = steps self.format = format self.unit = unit self.prefix_label = None self.lineedit = None self.unit_label = None self.slider = None self.button = None self._widgets = [] self._size_offset = size_offset self._refresh_callback = None self.dataset = FitParamDataSet(title=_("Curve fitting parameter")) def copy(self): """Return a copy of this fitparam""" return self.__class__(self.name, self.value, self.min, self.max, self.logscale, self.steps, self.format, self._size_offset, self.unit) def create_widgets(self, parent, refresh_callback): self._refresh_callback = refresh_callback self.prefix_label = QLabel() font = self.prefix_label.font() font.setPointSize(font.pointSize()+self._size_offset) self.prefix_label.setFont(font) self.button = QPushButton() self.button.setIcon(get_icon('settings.png')) self.button.setToolTip( _("Edit '%s' fit parameter properties") % self.name) self.button.clicked.connect(lambda: self.edit_param(parent)) self.lineedit = QLineEdit() self.lineedit.editingFinished.connect(self.line_editing_finished) self.unit_label = QLabel(self.unit) self.slider = QSlider() self.slider.setOrientation(Qt.Horizontal) self.slider.setRange(0, self.steps-1) self.slider.valueChanged.connect(self.slider_value_changed) self.update(refresh=False) self.add_widgets([self.prefix_label, self.lineedit, self.unit_label, self.slider, self.button]) def add_widgets(self, widgets): self._widgets += widgets def get_widgets(self): return self._widgets def set_scale(self, state): self.logscale = state > 0 self.update_slider_value() def set_text(self, fmt=None): style = "<span style=\'color: #444444\'><b>%s</b></span>" self.prefix_label.setText(style % self.name) if self.value is None: value_str = '' else: if fmt is None: fmt = self.format value_str = fmt % self.value self.lineedit.setText(value_str) self.lineedit.setDisabled( self.value == self.min and self.max == self.min) def line_editing_finished(self): try: self.value = float(self.lineedit.text()) except ValueError: self.set_text() self.update_slider_value() self._refresh_callback() def slider_value_changed(self, int_value): if self.logscale: total_delta = np.log10(1+self.max-self.min) self.value = self.min+10**(total_delta*int_value/(self.steps-1))-1 else: total_delta = self.max-self.min self.value = self.min+total_delta*int_value/(self.steps-1) self.set_text() self._refresh_callback() def update_slider_value(self): if (self.value is None or self.min is None or self.max is None): self.slider.setEnabled(False) if self.slider.parent() and self.slider.parent().isVisible(): self.slider.show() elif self.value == self.min and self.max == self.min: self.slider.hide() else: self.slider.setEnabled(True) if self.slider.parent() and self.slider.parent().isVisible(): self.slider.show() if self.logscale: value_delta = max([np.log10(1+self.value-self.min), 0.]) total_delta = np.log10(1+self.max-self.min) else: value_delta = self.value-self.min total_delta = self.max-self.min intval = int(self.steps*value_delta/total_delta) self.slider.blockSignals(True) self.slider.setValue(intval) self.slider.blockSignals(False) def edit_param(self, parent): update_dataset(self.dataset, self) if self.dataset.edit(parent=parent): restore_dataset(self.dataset, self) if self.value > self.max: self.max = self.value if self.value < self.min: self.min = self.value self.update() def update(self, refresh=True): self.unit_label.setText(self.unit) self.slider.setRange(0, self.steps-1) self.update_slider_value() self.set_text() if refresh: self._refresh_callback()
class Timer( QThread ): def __init__( self, parent ): QThread.__init__(self, parent) self.currsecond = 0 self.asc = True self.initseconds = 0 self.stopped = True self.toolbar_time = parent.addToolBar( 'Time' ) self.toolbar_time.setMovable( False ) self.toolbar_time.setObjectName('Time') self.timer = QLineEdit() self.timer.setStyleSheet("QLineEdit { font-size: 19px; font-family: Courier New; \ border-style: outset; border-radius: 10px; \ font-weight:bold; text-align:center}") self.timer.setFixedWidth( 80 ) self.timer.setReadOnly( True ) self.timer.setAlignment( Qt.AlignHCenter ) left_spacer = QWidget() left_spacer.setSizePolicy( QSizePolicy.Expanding, QSizePolicy.Expanding ) self.toolbar_time.addWidget( left_spacer ) self.toolbar_time.addWidget( self.timer ) self.initialize(0) def run( self ): self.stopped = False while not self.stopped: time.sleep(1) if self.asc is True: self.currsecond += 1 else : if self.currsecond > 0: self.currsecond -= 1 else: self.emit( SIGNAL( 'SessionStop' ) ) self.stop() self.update_timer_display( self.currsecond ) def stop( self ): self.stopped = True def initialize( self, nbseconds ): self.currsecond = nbseconds self.initseconds = nbseconds self.update_timer_display( nbseconds ) if nbseconds > 0: self.asc = False else: self.asc = True @staticmethod def _convertInMinutes( nbseconds ): minutes = nbseconds / 60 seconds = nbseconds - minutes * 60 return minutes, seconds def update_timer_display( self, nbseconds ): minutes, seconds = self._convertInMinutes( nbseconds ) timer_string = "%02d:%02d" % ( minutes, seconds ) self.timer.setText( timer_string ) def getRunningTime( self ): """ Return the duration of the session. """ if self.asc is False: totalsecs = self.initseconds - self.currsecond else: totalsecs = self.currsecond # minutes, seconds = self._convertInMinutes( totalsecs ) return totalsecs
class FitParam(object): def __init__(self, name, value, min, max, logscale=False, steps=5000, format='%.3f', size_offset=0, unit=''): self.name = name self.value = value self.min = min self.max = max self.logscale = logscale self.steps = steps self.format = format self.unit = unit self.prefix_label = None self.lineedit = None self.unit_label = None self.slider = None self.button = None self._widgets = [] self._size_offset = size_offset self._refresh_callback = None self.dataset = FitParamDataSet(title=_("Curve fitting parameter")) def copy(self): """Return a copy of this fitparam""" return self.__class__(self.name, self.value, self.min, self.max, self.logscale, self.steps, self.format, self._size_offset, self.unit) def create_widgets(self, parent, refresh_callback): self._refresh_callback = refresh_callback self.prefix_label = QLabel() font = self.prefix_label.font() font.setPointSize(font.pointSize() + self._size_offset) self.prefix_label.setFont(font) self.button = QPushButton() self.button.setIcon(get_icon('settings.png')) self.button.setToolTip( _("Edit '%s' fit parameter properties") % self.name) self.button.clicked.connect(lambda: self.edit_param(parent)) self.lineedit = QLineEdit() self.lineedit.editingFinished.connect(self.line_editing_finished) self.unit_label = QLabel(self.unit) self.slider = QSlider() self.slider.setOrientation(Qt.Horizontal) self.slider.setRange(0, self.steps - 1) self.slider.valueChanged.connect(self.slider_value_changed) self.update(refresh=False) self.add_widgets([ self.prefix_label, self.lineedit, self.unit_label, self.slider, self.button ]) def add_widgets(self, widgets): self._widgets += widgets def get_widgets(self): return self._widgets def set_scale(self, state): self.logscale = state > 0 self.update_slider_value() def set_text(self, fmt=None): style = "<span style=\'color: #444444\'><b>%s</b></span>" self.prefix_label.setText(style % self.name) if self.value is None: value_str = '' else: if fmt is None: fmt = self.format value_str = fmt % self.value self.lineedit.setText(value_str) self.lineedit.setDisabled(self.value == self.min and self.max == self.min) def line_editing_finished(self): try: self.value = float(self.lineedit.text()) except ValueError: self.set_text() self.update_slider_value() self._refresh_callback() def slider_value_changed(self, int_value): if self.logscale: total_delta = np.log10(1 + self.max - self.min) self.value = self.min + 10**(total_delta * int_value / (self.steps - 1)) - 1 else: total_delta = self.max - self.min self.value = self.min + total_delta * int_value / (self.steps - 1) self.set_text() self._refresh_callback() def update_slider_value(self): if (self.value is None or self.min is None or self.max is None): self.slider.setEnabled(False) if self.slider.parent() and self.slider.parent().isVisible(): self.slider.show() elif self.value == self.min and self.max == self.min: self.slider.hide() else: self.slider.setEnabled(True) if self.slider.parent() and self.slider.parent().isVisible(): self.slider.show() if self.logscale: value_delta = max([np.log10(1 + self.value - self.min), 0.]) total_delta = np.log10(1 + self.max - self.min) else: value_delta = self.value - self.min total_delta = self.max - self.min intval = int(self.steps * value_delta / total_delta) self.slider.blockSignals(True) self.slider.setValue(intval) self.slider.blockSignals(False) def edit_param(self, parent): update_dataset(self.dataset, self) if self.dataset.edit(parent=parent): restore_dataset(self.dataset, self) if self.value > self.max: self.max = self.value if self.value < self.min: self.min = self.value self.update() def update(self, refresh=True): self.unit_label.setText(self.unit) self.slider.setRange(0, self.steps - 1) self.update_slider_value() self.set_text() if refresh: self._refresh_callback()
class PiStageUi(QSplitter): stageConnected = Signal( ) # gets emitted if stage was sucessfully connected stopScan = Signal() xAxeChanged = Signal(object, object) updateCurrPos = Signal(object) def __init__(self, parent): #super(ObjectFT, self).__init__(Qt.Vertical, parent) super().__init__(parent) self.stage = None self.offset = 0. # offset from 0 where t0 is (mm) self.newOff = 0. self.stageRange = (0, 0) layoutWidget = QWidget() layout = QGridLayout() layoutWidget.setLayout(layout) # put layout together self.openStageBtn = QPushButton("Open stage") self.initStageBtn = QPushButton("Init stage") #absolute move #current position self.currentPos = QLabel('') #self.currentPos.setValidator(QDoubleValidator()) #relative move (mm) self.deltaMove_mm = QLineEdit() self.deltaMove_mm.setText('0') self.deltaMove_mm.setValidator(QDoubleValidator()) self.deltaMovePlus_mm = QPushButton('+') self.deltaMoveMinus_mm = QPushButton('-') #relative move (fs) self.deltaMove_fs = QLineEdit() self.deltaMovePlus_fs = QPushButton('+') self.deltaMoveMinus_fs = QPushButton('-') #velocity self.velocityLabel = QLabel('Velocity:') self.velocity = QSlider(Qt.Horizontal) self.velocity.setMinimum(0) self.velocity.setMaximum( 2000) # unit in µm; TODO: try to get max vel. from controller # scan from (fs) self.scanFrom = QLineEdit() self.scanFrom.setText('-100') self.scanFrom.setValidator(QIntValidator()) # scan to (fs) self.scanTo = QLineEdit() self.scanTo.setText('100') self.scanTo.setValidator(QIntValidator()) # scan stepsize (fs) self.scanStep = QLineEdit() self.scanStep.setText('10') self.scanStep.setValidator(QDoubleValidator()) # center here button self.centerBtn = QPushButton('Center here') self.centerBtn.setToolTip('Center scan at current stage position') self.startScanBtn = QPushButton("Start scan") self.stopScanBtn = QPushButton("Stop scan") self.niceBtn = QPushButton('Make it nice') # spacer line hLine = QFrame() hLine.setFrameStyle(QFrame.HLine) hLine.setSizePolicy(QSizePolicy.Minimum, QSizePolicy.Expanding) # put layout together layout.addWidget(self.openStageBtn, 0, 0) layout.addWidget(self.initStageBtn, 0, 1) layout.addWidget(QLabel("Current pos (mm):"), 1, 0) layout.addWidget(self.currentPos, 1, 1) layout.addWidget(self.velocityLabel, 2, 0) layout.addWidget(self.velocity, 3, 0, 1, 2) layout.addWidget(QLabel('Move relative (mm)'), 4, 0) layout.addWidget(self.deltaMove_mm, 5, 0, 1, 2) layout.addWidget(self.deltaMoveMinus_mm, 6, 0) layout.addWidget(self.deltaMovePlus_mm, 6, 1) layout.addWidget(QLabel('Move relative (fs)'), 7, 0) layout.addWidget(self.deltaMove_fs, 8, 0, 1, 2) layout.addWidget(self.deltaMoveMinus_fs, 9, 0) layout.addWidget(self.deltaMovePlus_fs, 9, 1) layout.addWidget(hLine, 10, 0, 1, 2) layout.addWidget(QLabel('Scan from (fs)'), 11, 0) layout.addWidget(self.scanFrom, 11, 1) layout.addWidget(QLabel('Scan to (fs)'), 12, 0) layout.addWidget(self.scanTo, 12, 1) layout.addWidget(QLabel('Stepsize (fs)'), 13, 0) layout.addWidget(self.scanStep, 13, 1) layout.addWidget(self.startScanBtn, 14, 0) layout.addWidget(self.stopScanBtn, 14, 1) layout.addWidget(self.centerBtn, 15, 1) layout.addWidget(self.niceBtn, 16, 1) layout.setRowStretch(17, 10) layout.setColumnStretch(2, 10) self.addWidget(layoutWidget) # make button and stuff functional self.openStageBtn.released.connect(self.connectStage) self.initStageBtn.released.connect(self.initStage) self.scanFrom.returnPressed.connect(self._xAxeChanged) self.scanTo.returnPressed.connect(self._xAxeChanged) self.centerBtn.released.connect(self._centerHere) self.deltaMovePlus_mm.released.connect( lambda x=1: self.moveRel_mm(float(self.deltaMove_mm.text()))) self.deltaMoveMinus_mm.released.connect( lambda x=-1: self.moveRel_mm(float(self.deltaMove_mm.text()), x)) ################ # thread for updating position #self.currPosThr = GenericThread(self.__getCurrPos) self.updateCurrPos.connect(self.__updateCurrPos) self.currPos_thread = QThread() # create the QThread self.currPos_thread.start() # This causes my_worker.run() to eventually execute in my_thread: self.currPos_worker = GenericWorker(self.__getCurrPos) self.currPos_worker.moveToThread(self.currPos_thread) # my_worker.finished.connect(self.xxx) #self.threadPool.append(my_thread) #self.my_worker = my_worker def connectStage(self): gcs = GCSDevice() try: gcs.InterfaceSetupDlg() msg = QMessageBox() msg.setIcon(QMessageBox.Information) msg.setText(gcs.qIDN()) msg.exec_() self.stage = gcs self.openStageBtn.setEnabled(False) except: msg = QMessageBox() msg.setIcon(QMessageBox.Critical) msg.setText('Could not connect stage') msg.exec_() def initStage(self): # TODO put this in thread and show egg clock if self.stage is not None: ## Create and display the splash screen #splash_pix = QPixmap('icons/piController.png') #splash = QSplashScreen(splash_pix, Qt.WindowStaysOnTopHint) #splash.setMask(splash_pix.mask()) #splash.show() # TODO: give choice to select stage pitools.startup(self.stage, stages='M-112.1DG-NEW', refmode='FNL') #splash.close() # TODO: show dialog for waiting self.velocityLabel.setText('Velocity: {:f}mm/s'.format( self.stage.qVEL()['1'])) self.velocity.setValue(int(1000 * self.stage.qVEL()['1'])) self.stageConnected.emit() self._xAxeChanged() self.currentPos.setText('{:.7f}'.format(self.stage.qPOS()['1'])) self.__startCurrPosThr() self.stageRange = (self.stage.qTMN()['1'], self.stage.qTMX()['1']) self.scanStep.validator().setBottom(0) self.initStageBtn.setEnabled(False) else: msg = QMessageBox() msg.setIcon(QMessageBox.Critical) msg.setText('No stage connected') msg.exec_() def gotoPos_mm(self, x): '''Move stage to absolute position in mm''' if self.stageRange[0] <= x <= self.stageRange[1]: self.stage.MOV(self.stage.axes, x) while not pitools.ontarget(self.stage, '1')['1']: time.sleep(0.05) else: print('Requested postition', x, 'outside of range', self.stageRange) def gotoPos_fs(self, x): '''Move stage to absolute position in mm''' self.gotoPos_mm(self._calcAbsPos(x)) def moveRel_mm(self, x=0, sign=1): '''Moves stage relative to current position''' # TODO raise message if outside of range currPos = float(self.currentPos.text()) if self.stageRange[0] <= sign * x + currPos <= self.stageRange[1]: self.stage.MVR(self.stage.axes, sign * x) else: print('Requested postition', x, 'outside of range', self.stageRange) def moveRel_fs(self, x=0, sign=1): '''Moves stage relative to current position; expexts fs''' # TODO raise message if outside of range self.moveRel_mm(self._calcAbsPos(x), sign) def _calcAbsPos(self, x): '''Calculate absolute position on stage from given femtosecond value gets x in fs and returns position mm''' return (x * fsDelay) + self.offset def getDelays_mm(self): '''expects fs and returns mm''' von = self._calcAbsPos(float(self.scanFrom.text())) bis = self._calcAbsPos(float(self.scanTo.text())) #stepSize = int(self.scanStep.text()) stepSize = float(self.scanStep.text()) * fsDelay return np.linspace(von, bis, (np.abs(von) + bis) / stepSize) def getDelays_fs(self): '''expects fs and returns mm''' von = float(self.scanFrom.text()) bis = float(self.scanTo.text()) #stepSize = int(self.scanStep.text()) stepSize = float(self.scanStep.text()) return np.linspace(von, bis, (np.abs(von) + bis) / stepSize) def _xAxeChanged(self): self.xAxeChanged.emit(int(self.scanFrom.text()), int(self.scanTo.text())) def setCenter(self): '''Slot which recieves the new center position in fs and sets offset in mm ''' if self.newOff != 0: self.offset += (self.newOff * fsDelay) print('offset', self.offset, self.newOff) self.newOff = 0. def newOffset(self, newOffset): self.newOff = newOffset def _centerHere(self): self.offset = self.stage.qPOS()['1'] def __startCurrPosThr(self): self.stopCurrPosThr = False #self.currPosThr.start() self.currPos_worker.start.emit() def __stopCurrPosThr(self): self.stopCurrPosThr = True while (self.currPosThr.isRunning()): time.sleep(0.03) def __getCurrPos(self): oldPos = self.stage.qPOS()['1'] while not self.stopCurrPosThr: newPos = self.stage.qPOS()['1'] if oldPos != newPos: oldPos = newPos self.updateCurrPos.emit(newPos) time.sleep(0.5) def __updateCurrPos(self, newPos): self.currentPos.setText('{:.7f}'.format(newPos))
class MaestroUi(QSplitter): connected = Signal() # gets emitted if stage was sucessfully connected newPlotData = Signal(object) updateAvgTxt = Signal(object) def __init__(self, parent): #super(ObjectFT, self).__init__(Qt.Vertical, parent) super().__init__(parent) self.meter = None self.collectData = True # bool for data collection thread self.avgData = Queue() # need data for averaging and set for holding all self.measure = False self.runDataThr = True self.measureData = [] self.startTime = None layoutWidget = QWidget() layout = QGridLayout() layoutWidget.setLayout(layout) ############## # gui elements self.ipEdit = QLineEdit() rx = QRegExp("^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]).){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$") self.ipEdit.setValidator(QRegExpValidator(rx)) self.ipEdit.setText('127.0.0.1') self.portEdit = QLineEdit() self.portEdit.setValidator(QIntValidator(1, 65535, self)) self.portEdit.setText('5000') self.connectBtn = QPushButton('Connect') self.avgSpin = QSpinBox() self.avgSpin.setValue(1) self.avgSpin.setRange(1, 10000) self.currValDisp = QLabel('0.0') self.startMeasBtn = QPushButton('Start aq') self.stopMeasBtn = QPushButton('Stop aq') ############## # put layout together layout.addWidget(QLabel('IP Address:'), 0, 0) layout.addWidget(self.ipEdit, 1, 0) layout.addWidget(QLabel('Port:'), 0, 1) layout.addWidget(self.portEdit, 1, 1) layout.addWidget(self.connectBtn, 2, 1) layout.addWidget(QLabel('Averages'), 4, 0) layout.addWidget(self.avgSpin, 5, 0) layout.addWidget(self.currValDisp, 5, 1) layout.addWidget(self.startMeasBtn, 6, 0) layout.addWidget(self.stopMeasBtn, 6, 1) layout.setRowStretch(7, 10) self.addWidget(layoutWidget) ############## # Network stuff self.tcpClient = QtNetwork.QTcpSocket() self.tcpClient.readyRead.connect(self.__getSocketData) self.tcpClient.error.connect(lambda x: print(x)) ############## # make button and stuff functional self.connectBtn.released.connect(self.connectMeter) self.avgSpin.valueChanged.connect(self.changeAverage) self.startMeasBtn.released.connect(self._startMeasure) self.stopMeasBtn.released.connect(self._stopMeasure) ############## # thread for getting data from socket self.updateAvgTxt.connect(self.__updateAvgTxt) self.dataAq_Thr = QThread() self.dataAq_Thr.start() self.dataAq_worker = GenericWorker(self.__getData) self.dataAq_worker.moveToThread(self.dataAq_Thr) def connectMeter(self): print('connected') self.tcpClient.connectToHost(self.ipEdit.text(), int(self.portEdit.text())) self.tcpClient.write('start\n'.encode()) self.dataAq_worker.start.emit() def _startMeasure(self): self.measure = True self.measureData = [] # reinitialize measure data array time.sleep(0.1) # time to wait for first data to arrive self.startTime = datetime.now() # datetime object def _stopMeasure(self): self.measure = False #@Slot def __updateAvgTxt(self, text): ''' update current value label ''' self.currValDisp.setText(text) def changeAverage(self): shape = int(self.avgSpin.value()) self.dispData = np.zeros(shape) def __getData(self): ''' Function run in thread ''' while self.runDataThr: tmpData = np.array(int(self.avgSpin.text())*[[datetime.now(), 0]]) for i in range(len(tmpData)): tmpData[i] = self.avgData.get() if self.measure: self.measureData.append( (tmpData[i,0].isoformat().encode(), (tmpData[i,0]-self.startTime).total_seconds(), tmpData[i,1])) #print('mean', tmpData.mean()) self.updateAvgTxt.emit(str(tmpData[:,1].mean())) if self.measure: self.newPlotData.emit(np.float_(np.asarray(self.measureData)[:,1:])) self.avgData.task_done() #@Slot() def __getSocketData(self): ''' to be called if network buffer has more data push data to queue ''' self.avgData.put([datetime.now(), float(self.tcpClient.readLine(1024).decode().rstrip())]) def closeEvent(self, event): if self.tcpClient.isOpen(): self.RunDataThr = False self.tcpClient.write('stop\n'.encode()) time.sleep(0.1) self.tcpClient.close() print(self.tcpClient.isOpen()) #if self.console is not None: # self.console.exit_interpreter() event.accept()
class GreatEyesUi(QSplitter): ''' Handling user interface to manage greateys cameras ''' newPlotData = Signal(object, object) message = Signal(object) def __init__(self, parent): super().__init__(parent) self.camera = None self.cameraSettings = None self.aquireData = False self.directory = 'N:/4all/mpsd_drive/xtsfasta/Data' layoutWidget = QWidget() layout = QGridLayout() layoutWidget.setLayout(layout) ############### # GUI elements self.openCamBtn = QPushButton('Connect camera') self.startAquBtn = QPushButton('Start aquisiton') self.readoutSpeedCombo = QComboBox() # this really should not be hard coded but received from dll self.readoutSpeedCombo.addItems(["1 MHz", "1.8 MHz", "2.3 MHz", "2.8 MHz", "250 kHz", "500 kHz"]) self.exposureTimeSpin = QSpinBox() self.exposureTimeSpin.setRange(1, 1e6) self.exposureTimeSpin.setValue(1e3) # default exposure 1s self.exposureTimeSpin.setSingleStep(100) self.exposureTimeSpin.setSuffix(' ms') #self.exposureTimeSpin.setValidator(QIntValidator(1, 2**31)) # ms self.binningXCombo = QComboBox() self.binningXCombo.addItems(["No binning", "Binning of 2 columns", "Binning of 4 columns", "Binning of 8 columns", "Binning of 16 columns", "Binning of 32 columns", "Binning of 64 columns", "Binning of 128 columns", "Full horizontal binning"]) self.binningYCombo = QComboBox() self.binningYCombo.addItems(["No binning", "Binning of 2 lines", "Binning of 4 lines", "Binning of 8 lines", "Binning of 16 lines", "Binning of 32 lines", "Binning of 64 lines", "Binning of 128 lines", "Binning of 256 lines"]) self.temperatureSpin = QSpinBox() self.temperatureSpin.setRange(-100, 20) self.temperatureSpin.setValue(-10) self.temperatureSpin.setSuffix('°C') self.updateInterSpin = QSpinBox() self.updateInterSpin.setRange(1, 3600) self.updateInterSpin.setValue(5) self.updateInterSpin.setSuffix(' s') #self.updateInterSpin.setText("2") #self.updateInterEdit.setValidator(QIntValidator(1, 3600)) self.loi = QSpinBox() self.loi.setRange(1, 511) # one pixel less as the camera has self.deltaPixels = QSpinBox() self.deltaPixels.setRange(0, 256) self.autoSave = QCheckBox("Auto save") self.getDirectory = QPushButton('Choose Dir') self.dirPath = QLineEdit(self.directory) self.comment = QPlainTextEdit() ############## # put elements in layout layout.addWidget(self.openCamBtn, 0, 0) layout.addWidget(self.startAquBtn, 0, 1) layout.addWidget(QLabel('readout speed'), 1, 0) layout.addWidget(self.readoutSpeedCombo, 1, 1) layout.addWidget(QLabel('exposure time'), 2, 0) layout.addWidget(self.exposureTimeSpin, 2, 1) layout.addWidget(QLabel('binning X'), 3, 0) layout.addWidget(self.binningXCombo, 3, 1) layout.addWidget(QLabel('binning Y'), 4, 0) layout.addWidget(self.binningYCombo, 4, 1) layout.addWidget(QLabel('temperature'), 5, 0) layout.addWidget(self.temperatureSpin, 5, 1) layout.addWidget(QLabel('update every n-seconds'), 6, 0) layout.addWidget(self.updateInterSpin, 6, 1) layout.addWidget(QLabel('Pixel of interest'), 7, 0) layout.addWidget(self.loi, 7, 1) layout.addWidget(QLabel('Δ pixels'), 8, 0) layout.addWidget(self.deltaPixels, 8, 1) layout.addWidget(self.autoSave, 9, 1) layout.addWidget(self.getDirectory, 10, 0) layout.addWidget(self.dirPath, 10, 1) layout.addWidget(QLabel('Comment:'), 11, 0) layout.addWidget(self.comment, 12, 0, 1, 2) layout.setRowStretch(13, 10) self.addWidget(layoutWidget) ################# # connect elements for functionality self.openCamBtn.released.connect(self.__openCam) self.getDirectory.released.connect(self.__chooseDir) self.temperatureSpin.valueChanged.connect(self.__setTemperature) self.exposureTimeSpin.valueChanged.connect(self.__setCamParameter) self.readoutSpeedCombo.currentIndexChanged.connect(self.__setCamParameter) self.startAquBtn.released.connect(self.__startCurrImageThr) ################ # thread for updating position self.currImage_thread = QThread() # create the QThread self.currImage_thread.start() # This causes my_worker.run() to eventually execute in my_thread: self.currImage_worker = GenericWorker(self.__getCurrImage) self.currImage_worker.moveToThread(self.currImage_thread) self.startAquBtn.setEnabled(False) self.readoutSpeedCombo.setEnabled(False) self.exposureTimeSpin.setEnabled(False) self.binningXCombo.setEnabled(False) self.binningYCombo.setEnabled(False) self.temperatureSpin.setEnabled(False) self.updateInterSpin.setEnabled(False) def __openCam(self): self.camera = greatEyes() if not self.camera.connected: msg = QMessageBox() msg.setIcon(QMessageBox.Critical) msg.setText('Sorry, could not connect to camera :(\n' + self.camera.status) msg.exec_() return self.openCamBtn.setText('Connected') self.message.emit('Camera connected') self.openCamBtn.setStyleSheet('QPushButton {color: green;}') self.readoutSpeedCombo.setEnabled(True) self.exposureTimeSpin.setEnabled(True) self.binningXCombo.setEnabled(False) self.binningYCombo.setEnabled(False) self.temperatureSpin.setEnabled(True) self.updateInterSpin.setEnabled(True) self.openCamBtn.setEnabled(False) self.startAquBtn.setEnabled(True) def __chooseDir(self): self.directory = QFileDialog.getExistingDirectory(self, "Choose directory", self.directory) self.dirPath.setText(self.directory) def __startCurrImageThr(self): if not self.aquireData: self.aquireData = True self.currImage_worker.start.emit() self.startAquBtn.setText('Stop aquisition') self.message.emit('Starting aqusition') else: self.__stopCurrImageThr() self.startAquBtn.setText('Start aquisition') self.message.emit('Stopping aqusition') def __stopCurrImageThr(self): self.aquireData = False #while(self.currPosThr.isRunning()): # time.sleep(0.03) def __getCurrImage(self): #from scipy import mgrid #import numpy as np #X, Y = mgrid[-256:256, -1024:1025] i = self.updateInterSpin.value() while self.aquireData: # seconds over which to record a new image imageIntervall = self.updateInterSpin.value() # sleep for n seconds to check if intervall was changed sleepy = 1 if i >= imageIntervall: # dummy image #z = np.exp(-0.5*(X**2+Y**2)/np.random.uniform(30000, 40000))*np.cos(0.1*X+0.1*Y) z = self.camera.getImage() timeStamp = datetime.datetime.now() self.cameraSettings = { 'temperature': self.camera.getTemperature(), 'exposure_time': self.exposureTimeSpin.value(), 'readout_speed': self.readoutSpeedCombo.currentText() 'time_stamp': timeStamp} self.newPlotData.emit(z, timeStamp) i = 0 # restart counter i += sleepy time.sleep(sleepy) def __setTemperature(self, temp): self.camera.setTemperture(temp) self.message.emit('Temperature set to {:d}°C'.format(temp)) def __setCamParameter(self, param): self.camera.setCameraParameter( self.readoutSpeedCombo.currentIndex(), self.exposureTimeSpin.value(), 0, 0) self.message.emit('Readout: {:s}, Exposure: {:d}, binningX: 0, binningY: 0'.format(self.readoutSpeedCombo.currentText(), self.exposureTimeSpin.value()))