def __init__(self, size, width=5, brush=None, pen=None, suffix='m', offset=None): GraphicsObject.__init__(self) GraphicsWidgetAnchor.__init__(self) self.setFlag(self.ItemHasNoContents) self.setAcceptedMouseButtons(QtCore.Qt.NoButton) if brush is None: brush = getConfigOption('foreground') self.brush = fn.mkBrush(brush) self.pen = fn.mkPen(pen) self._width = width self.size = size if offset == None: offset = (0, 0) self.offset = offset self.bar = QtGui.QGraphicsRectItem() self.bar.setPen(self.pen) self.bar.setBrush(self.brush) self.bar.setParentItem(self) self.text = TextItem(text=fn.siFormat(size, suffix=suffix), anchor=(0.5, 1), angle=90, color=(0, 0, 0)) self.text.setParentItem(self)
def updateText(self, prev=None): # get the number of decimal places to print decimals = self.opts.get('decimals') # temporarily disable validation self.skipValidate = True # if we're supposed to add a prefix to the label if self.opts['siPrefix']: # special case: if it's zero use the previous prefix if self.val == 0 and prev is not None: (s, p) = fn.siScale(prev) # NOTE: Could have the user specify a format string and use it here txt = ("%."+str(decimals)+"g %s%s") % (0, p, self.opts['suffix']) else: # NOTE: Could have the user specify a format string and send it as an argument here txt = fn.siFormat(float(self.val), precision=decimals, suffix=self.opts['suffix']) # otherwise, format the string manually else: # NOTE: Could have the user specify a format string and use it here txt = ('%.'+str(decimals)+'g%s') % (self.val , self.opts['suffix']) # actually set the text self.lineEdit().setText(txt) self.lastText = txt # re-enable the validation self.skipValidate = False
def updateText(self, prev=None): #print "Update text." self.skipValidate = True if self.opts['siPrefix']: if self.val == 0 and prev is not None: (s, p) = fn.siScale(prev) txt = "0.0 %s%s" % (p, self.opts['suffix']) else: txt = fn.siFormat(float(self.val), suffix=self.opts['suffix']) else: txt = '%g%s' % (self.val, self.opts['suffix']) self.lineEdit().setText(txt) self.lastText = txt self.skipValidate = False
def updateText(self, prev=None): #print "Update text." self.skipValidate = True if self.opts['siPrefix']: if self.val == 0 and prev is not None: (s, p) = fn.siScale(prev) txt = "0.0 %s%s" % (p, self.opts['suffix']) else: txt = fn.siFormat(float(self.val), suffix=self.opts['suffix']) else: txt = '%g%s' % (self.val , self.opts['suffix']) self.lineEdit().setText(txt) self.lastText = txt self.skipValidate = False
def updateText(self, prev=None): self.skipValidate = True if self.opts['siPrefix']: if self.val == 0 and prev is not None: (s, p) = fn.siScale(prev) txt = "0.0 %s%s" % (p, self.opts['suffix']) else: txt = fn.siFormat(float(self.val), precision=self.opts['decimals'], suffix=self.opts['suffix']) else: fmt = "%." + str(self.opts['decimals']) + "g%s" txt = fmt % (self.val, self.opts['suffix']) self.lineEdit().setText(txt) self.lastText = txt self.skipValidate = False
def updateText(self, prev=None): # print("Update text.") self.skipValidate = True if self.opts['siPrefix']: if self.val == 0 and prev is not None: (s, p) = fn.siScale(prev) txt = "0.0 {0!s}{1!s}".format(p, self.opts['suffix']) else: txt = fn.siFormat(float(self.val), precision=self.opts['decimals'] + 1, suffix=self.opts['suffix']) else: txt = '{0:.14g}{1!s}'.format(self.val, self.opts['suffix']) self.lineEdit().setText(txt) self.lastText = txt self.skipValidate = False
def updateText(self, prev=None): # print("Update text.") self.skipValidate = True if self.opts['siPrefix']: if self.val == 0 and prev is not None: (s, p) = fn.siScale(prev) txt = "0.0 {0!s}{1!s}".format(p, self.opts['suffix']) else: txt = fn.siFormat(float(self.val), precision=self.opts['decimals']+1, suffix=self.opts['suffix'] ) else: txt = '{0:.14g}{1!s}'.format(self.val , self.opts['suffix']) self.lineEdit().setText(txt) self.lastText = txt self.skipValidate = False
def __init__(self, size, width=5, brush=None, pen=None, suffix='m'): GraphicsObject.__init__(self) GraphicsWidgetAnchor.__init__(self) self.setFlag(self.ItemHasNoContents) self.setAcceptedMouseButtons(QtCore.Qt.NoButton) if brush is None: brush = pg.getConfigOption('foreground') self.brush = fn.mkBrush(brush) self.pen = fn.mkPen(pen) self._width = width self.size = size self.bar = QtGui.QGraphicsRectItem() self.bar.setPen(self.pen) self.bar.setBrush(self.brush) self.bar.setParentItem(self) self.text = TextItem(text=fn.siFormat(size, suffix=suffix), anchor=(0.5,1)) self.text.setParentItem(self)
def create_formatted_output(param_dict, num_sig_digits=5): """ Display a parameter set nicely in SI units. @param dict param_dict: dictionary with entries being again dictionaries with two needed keywords 'value' and 'unit' and one optional keyword 'error'. Add the proper items to the specified keywords. Note, that if no error is specified, no proper rounding (and therefore displaying) can be guaranteed. @param int num_sig_digits: optional, the number of significant digits will be taken, if the rounding procedure was not successful at all. That will ensure at least that not all the digits are displayed. According to that the error will be displayed. @return str: a string, which is nicely formatted. Note: If you want that the values are displayed in a certain order, then use OrderedDict from the collections package. Note2: The absolute tolerance to a zero is set to 1e-18. Example of a param dict: param_dict = {'Rabi frequency': {'value':123.43, 'error': 0.321, 'unit': 'Hz'}, 'ODMR contrast': {'value':2.563423, 'error': 0.523, 'unit': '%'}, 'Fidelity': {'value':0.783, 'error': 0.2222, 'unit': ''}} If you want to access on the value of the Fidelity, then you can do that via: param_dict['Fidelity']['value'] or on the error of the ODMR contrast: param_dict['ODMR contrast']['error'] """ if (fn is None): raise Exception('This function requires pyqtgraph.') output_str = '' atol = 1e-18 # absolute tolerance for the detection of zero. for entry in param_dict: if param_dict[entry].get('error') is not None: value, error, digit = round_value_to_error( param_dict[entry]['value'], param_dict[entry]['error']) if (np.isclose(value, 0.0, atol=atol) or np.isnan(error) or np.isclose(error, 0.0, atol=atol) or np.isinf(error)): sc_fact, unit_prefix = fn.siScale(param_dict[entry]['value']) str_val = '{0:.{1}e}'.format( param_dict[entry]['value'], num_sig_digits - 1) if np.isnan(np.float(str_val)): value = np.NAN elif np.isinf(np.float(str_val)): value = np.inf else: value = float('{0:.{1}e}'.format( param_dict[entry]['value'], num_sig_digits - 1)) else: # the factor 10 moves the displayed digit by one to the right, # so that the values from 100 to 0.1 are displayed within one # range, rather then from the value 1000 to 1, which is # default. sc_fact, unit_prefix = fn.siScale(error * 10) output_str += '{0}: {1} \u00B1 {2} {3}{4} \n'.format(entry, round( value * sc_fact, num_sig_digits - 1), round( error * sc_fact, num_sig_digits - 1), unit_prefix, param_dict[entry][ 'unit'], ) else: output_str += '{0}: '.format(entry) + fn.siFormat(param_dict[entry]['value'], precision=num_sig_digits, suffix=param_dict[entry]['unit']) + '\n' return output_str