def saveDevices(self, setupItem): output = [] childIndex = 0 deviceItems = [] while childIndex < setupItem.childCount(): deviceItems.append(setupItem.child(childIndex)) childIndex += 1 if not deviceItems: return '' output.append('devices = dict(\n') for name, info in iteritems( self.deviceWidgets[setupItem.setup.abspath]): output.append(' ' + name + ' = device(') # class string must be first parameter. Also mustn't have a key. output.append(repr(info.parameters['Class'].getValue()) + ',\n') indent = len(name) + 14 for _, params in iteritems(info.parameters): # skip class as it has already been added if not params.param == 'Class': if isinstance(params.getValue(), string_types): prepend = indent * ' ' + str(params.param) + ' = ' if params.isUnknownValue: param = str(params.getValue()) + ',\n' else: param = repr(str(params.getValue())) + ',\n' else: prepend = indent * ' ' + str(params.param) + ' = ' param = str(params.getValue()) + ',\n' output.append(prepend + param) output.append((indent - 1) * ' ' + '),\n') output.append(')\n\n') return ''.join(output)
def structure(self, name, metainfo): val = { "type": "group", "name": name, "attributes": { "NX_class": self.nxclass }, "children": [] } # Add the children if self.children: for ename, entry in iteritems(self.children): if isinstance(entry, NexusElementBase): for child_dict in entry.structure(ename, metainfo): if child_dict: val["children"].append(child_dict) else: session.log.info("%s does not provide value!!", ename) # Add the attributes if self.attrs: attr_dict = {} for attr_name, attr in iteritems(self.attrs): # It is important to check if the type is of NXAttribute # Subclasses can directly change the self.attrs dict if isinstance(attr, NXAttribute): for attr_structure in attr.structure(attr_name, metainfo): if attr_structure: attr_dict.update(attr_structure) val["attributes"].update(attr_dict) return [val]
def loadSettings(self, settings): self.autoconnect = settings.value('autoconnect', True, bool) self.connpresets = {} # new setting key, with dictionary values for (k, v) in iteritems(settings.value('connpresets_new', {})): self.connpresets[k] = ConnectionData(**v) # if it was empty, try old setting key with list values if not self.connpresets: for (k, v) in iteritems(settings.value('connpresets', {})): self.connpresets[k] = ConnectionData(host=v[0], port=int(v[1]), user=v[2], password=None) self.lastpreset = settings.value('lastpreset', '') if self.lastpreset in self.connpresets: self.conndata = self.connpresets[self.lastpreset].copy() self.instrument = settings.value('instrument', self.gui_conf.name) self.confirmexit = settings.value('confirmexit', True, bool) self.warnwhenadmin = settings.value('warnwhenadmin', True, bool) self.showtrayicon = settings.value('showtrayicon', True, bool) self.autoreconnect = settings.value('autoreconnect', True, bool) self.autosavelayout = settings.value('autosavelayout', True, bool) self.allowoutputlinewrap = settings.value('allowoutputlinewrap', False, bool) self.update()
def _rollover(self): """Must be called with self._cat_lock held.""" self.log.info('midnight passed, data file rollover started') ltime = localtime() # set the days and midnight time correctly self._year = str(ltime[0]) self._currday = '%02d-%02d' % ltime[1:3] self._midnight = mktime(ltime[:3] + (0, ) * (8 - 3) + (ltime[8], )) self._nextmidnight = self._midnight + 86400 # roll over all file descriptors for category, (fd, _, db) in iteritems(self._cat): if fd: fd.close() self._cat[category][0] = None fd = self._create_fd(category) for subkey, entry in iteritems(db): if entry.value: fd.write('%s\t%s\t%s\t%s\n' % (subkey, entry.time, (entry.ttl or entry.expired) and '-' or '+', entry.value)) # don't keep fds open for *all* files with keys, rather reopen # those that are necessary when new updates come in fd.close() # set the 'lastday' symlink to the current day directory self._set_lastday()
def doRead(self, maxage=0): # read all tuning devices devs = { key: value.read(maxage) for key, value in iteritems(self._tunedevs) } # find correct echotime for current device setup in currently active # tunewave table for echotime, tunedevs in iteritems(self.currenttable): self.log.debug('checking if we are at echotime %s', echotime) success = True for tunedev, value in iteritems(tunedevs): # XXX: horrible hack if tunedev.endswith('reg_amp'): continue # fuzzy matching necessary due to maybe oscillating devices prec = getattr(self._tunedevs[tunedev], 'precision', 0) if not self._fuzzy_match(value, devs.get(tunedev, None), prec): self.log.debug( '-> no, because %s is at %s not %s ' '(prec = %s)', tunedev, devs.get(tunedev, None), value, prec) success = False break if success: self.log.debug('-> YES!') return echotime # return 0 as echotime and show additional info in status string # if the echo time could not be determined return 0.0
def _test(): from nicos.pycompat import iteritems for t, vals in iteritems(valid): for hkl in vals: # this def is necessary to get uniq descriptions in the # test output. def tf1(t, mode, hkl): _testbravais(t, mode, hkl) tf1.description = 'Bravais allowed %s %s' % (t, hkl) yield tf1, t, True, hkl for t, vals in iteritems(invalid): for hkl in vals: # this def is necessary to get uniq descriptions in the # test output. def tf2(t, mode, hkl): _testbravais(t, mode, hkl) tf2.description = 'Bravais forbidden %s %s' % (t, hkl) yield tf2, t, False, hkl for sym in symbols: # this def is necessary to get uniq descriptions in the # test output. def tf3(sym): _testlaue(sym) tf3.description = 'Laue uniqds for %s' % sym yield tf3, sym
def setOptimalWidth(self): # get necessary width to align labels maxWidth = 100 # default minimum 100 for _, widget in iteritems(self.parameters): labelWidth = widget.labelParam.sizeHint().width() if labelWidth > maxWidth: maxWidth = labelWidth for _, widget in iteritems(self.parameters): widget.labelParam.setMinimumWidth(maxWidth)
def doInit(self, mode): # check that an offset is defined for each z distance for _selpos, selpresets in iteritems(self.presets): for _pname, preset in iteritems(selpresets): if preset['z'] not in self.offsets: raise ConfigurationError( self, 'no detector offset found in configuration ' 'for detector distance of %.2f m' % preset['z']) MappedMoveable.doInit(self, mode) # apply mapping of last selector pos in case it changed if mode == MASTER: self._updateMapping(self.mapkey)
def _validate_table(self, table): """Validates the structure of a single tunewave table and and all the included device values (using the valuetype of the particular device). """ # Structure of a single tunewave table: {echotime: {tunedev : value}} result = {} for echotime, tunedevs in iteritems(table): echotime = float(echotime) result[echotime] = {} for tunedev, value in iteritems(tunedevs): result[echotime][tunedev] = self._tunedevs[tunedev].valuetype( value) return result
def trimResult(self): """Trim objects that are not required to be kept after finish().""" BaseDataset.trimResult(self) # remove arrays from memory in cached datasets for (key, value) in iteritems(self.results): if value is not None: self.results[key] = (value[0], [])
def init_from_history(self, history, starttime, endtime, valueindex=()): ltime = 0 lvalue = None maxdelta = max(2 * self.interval, 11) data = np.zeros((max(self.minsize, 3 * len(history) + 2), 2)) i = 0 vtime = value = None # stops pylint from complaining for vtime, value in history: if value is None: continue if valueindex: try: value = functools.reduce(operator.getitem, valueindex, value) except (TypeError, IndexError): continue delta = vtime - ltime if not isinstance(value, number_types): # if it's a string, create a new unique integer value for the string if isinstance(value, string_types): value = self.string_mapping.setdefault( value, len(self.string_mapping)) # other values we can't use else: continue value = value * self.scale + self.offset if delta < self.interval: # value comes too fast -> ignore lvalue = value continue if delta > maxdelta and lvalue is not None: # synthesize one or two points inbetween if vtime - self.interval > ltime + self.interval: data[i] = ltime + self.interval, lvalue i += 1 data[i] = vtime - self.interval, lvalue i += 1 data[i] = ltime, lvalue = max(vtime, starttime), value i += 1 if i and data[i - 1, 1] != value: # In case the final value was discarded because it came too fast, # add it anyway, because it will potentially be the last one for # longer, and synthesized. data[i] = vtime, value i += 1 elif i and data[i - 1, 0] < endtime - self.interval: # In case the final value is very old, synthesize a point # right away at the end of the interval. data[i] = endtime, value i += 1 # resize in-place (possible since we have not given out references) data.resize((max(self.minsize, i * 2), 2)) self.data = data self.n = self.real_n = i self.last_y = lvalue if self.string_mapping: self.info = ', '.join( '%g=%s' % (v * self.scale + self.offset, k) for (k, v) in sorted(iteritems(self.string_mapping), key=lambda x: x[1]))
def doSetPreset(self, **preset): self._user_comment = preset.pop('info', '') preset = self._getPreset(preset) if not preset: # keep old settings return for master in self._masters: master.ismaster = False self._channel_presets = {} for (name, value) in iteritems(preset): if name in self._presetkeys and name != 'live': dev = self._presetkeys[name][0] dev.setChannelPreset(name, value) self._channel_presets.setdefault(dev, []).append((name, value)) self._getMasters() if set(self._masters) != set(self._channel_presets): if not self._masters: self.log.warning('no master configured, detector may not stop') else: self.log.warning( 'master setting for devices %s ignored by ' 'detector', ', '.join(set(self._channel_presets) - set(self._masters))) self.log.debug(" presets: %s", preset) self.log.debug("presetkeys: %s", self._presetkeys) self.log.debug(" masters: %s", self._masters) self.log.debug(" slaves: %s", self._slaves)
def _addvalues(self, values): with self._statslock: for devname, (timestamp, value) in iteritems(values): self.values[devname] = value if timestamp is None: self.canonical_values[devname] = value continue elif isinstance(value, number_types): # collect statistics current = self._valuestats.setdefault(devname, []) if not current: # first value: record timestamp and value current.extend( [0, 0, 0, value, value, timestamp, value]) else: oldtime, oldvalue = current[-2:] dt = timestamp - oldtime if dt >= 0: current[0] += dt current[1] += dt * oldvalue current[2] += dt * oldvalue**2 current[3] = min(current[3], value) current[4] = max(current[4], value) current[5] = timestamp current[6] = value
def _mapReadValue(self, pos): """maps a tuple to one of the configured values""" hasprec = bool(self.precision) if hasprec: precisions = self.precision if len(precisions) == 1: precisions = [precisions[0]] * len(self.devices) for name, values in iteritems(self.mapping): if hasprec: for p, v, prec in zip(pos, values, precisions): if prec: if abs(p - v) > prec: break elif p != v: break else: # if there was no break we end here... return name else: if tuple(pos) == tuple(values): return name if self.fallback is not None: return self.fallback raise PositionError( self, 'unknown position of %s: %s' % (', '.join(str(d) for d in self.devices), ', '.join( d.format(p) for (p, d) in zip(pos, self.devices))))
def ask_wc(self, key, ts, time, ttl): ret = set() # look for matching keys for cat, (_, lock, db) in listitems(self._cat): prefix = cat + '/' if cat != 'nocat' else '' with lock: for subkey, entry in iteritems(db): if key not in prefix + subkey: continue # check for removed keys if entry.value is None: continue # check for expired keys op = entry.expired and OP_TELLOLD or OP_TELL if entry.ttl: if ts: ret.add('%r+%s@%s%s%s\n' % (entry.time, entry.ttl, prefix + subkey, op, entry.value)) else: ret.add(prefix + subkey + op + entry.value + '\n') elif ts: ret.add('%r@%s%s%s\n' % (entry.time, prefix + subkey, op, entry.value)) else: ret.add(prefix + subkey + op + entry.value + '\n') return [''.join(ret)]
def _add_row(self, row, row_data, diff_table): """Add a row to the table and add/highlight differences.""" echotime = row_data['echotime'] for header, value in iteritems(row_data): # Use a QTextEdit to be able to use HTML/CSS inside the content # (used for displaying the differences) widget = QTextEdit(str(value)) if diff_table is not None: diff_value = '' show_diff = False if echotime in diff_table: # If the echotime exists on both tables, only show the # differences diff_value = diff_table[echotime].get(header, '') if header != 'echotime' and diff_value != value: show_diff = True else: # If the echotime is completely new, highlight the whole # row show_diff = True if show_diff: self._add_diff(widget, diff_value) widget.setReadOnly(True) # Don't show any frame as there is the more fancy table grid widget.setFrameStyle(QFrame.NoFrame) self.tableWidget.setCellWidget(row, self._header_labels.index(header), widget)
def _fill_table(self): """Request the currently selected table from the server side table storage and display it in the table widget.""" table = self.client.eval( '%s.getTable("%s", %s)' % (self._dev, self.measurement_mode, self.wavelength)) # Disable table sorting while filling to avoid jumping rows self.tableWidget.setSortingEnabled(False) # Clear table and preserve the column headers self.tableWidget.setRowCount(0) row = 0 for echotime, devices in iteritems(table): # Create dict from readonly dict cols = dict(devices) # Add echotime to row data cols['echotime'] = echotime self._add_row(row, cols) row += 1 # Reenable table sorting self.tableWidget.horizontalHeader().setSortIndicator( 0, Qt.AscendingOrder) self.tableWidget.setSortingEnabled(True) self._adjust_table_sizes()
def add_value(self, vtime, value, real=True, use_scale=True): if not isinstance(value, number_types): if isinstance(value, string_types): value = self.string_mapping.setdefault( value, len(self.string_mapping)) self.info = ', '.join( '%g=%s' % (v * self.scale + self.offset, k) for (k, v) in sorted(iteritems(self.string_mapping), key=lambda x: x[1])) else: return elif use_scale: value = value * self.scale + self.offset n, real_n = self.n, self.real_n arrsize = self.data.shape[0] self.last_y = value # do not add value if it comes too fast if real_n > 0 and self.data[real_n - 1, 0] > vtime - self.interval: return self._last_update_time = currenttime() # double array size if array is full if n >= arrsize: # keep array around the size of maxsize if arrsize >= self.maxsize: # don't add more points, make existing ones more sparse data = self.data[:real_n] new_data = lttb.downsample(data[data[:, 0].argsort()], n_out=arrsize // 2) n = self.n = self.real_n = new_data.shape[0] # can resize in place here new_data.resize(self.data, (n * 2, 2)) self.data = new_data else: # can't resize in place self.data = np.resize(self.data, (2 * arrsize, 2)) # fill next entry if not real and real_n < n - 1: # do not generate endless amounts of synthesized points, # two are enough (one at the beginning, one at the end of # the interval without real points) self.data[n - 1] = vtime, value else: self.data[n] = vtime, value self.n += 1 if real: self.real_n = self.n # check sliding window if self.window: i = -1 threshold = vtime - self.window while self.data[i + 1, 0] < threshold and i < n: if self.data[i + 2, 0] > threshold: self.data[i + 1, 0] = threshold break i += 1 if i >= 0: self.data[0:n - i] = self.data[i + 1:n + 1].copy() self.n -= i + 1 self.real_n -= i + 1 self.signal_obj.timeSeriesUpdate.emit(self)
def on_fileSystemWatcher_fileChanged(self, filename): if self.saving: return editor = watcher = None for editor, watcher in iteritems(self.watchers): if watcher is self.sender(): break else: return if editor.isModified(): # warn the user self.warnText.setText( 'The file %r has changed on disk, but has also been edited' ' here.\nPlease use either File-Reload to load the' ' version on disk or File-Save to save this version.' % self.filenames[editor]) self.warnWidget.show() else: # reload without asking try: with io.open(self.filenames[editor], 'r', encoding='utf-8') as f: text = f.read() except Exception: return editor.setText(text) editor.setModified(False) # re-add the filename to the watcher if it was deleted # (happens for programs that do delete-write on save) if not watcher.files(): watcher.addPath(self.filenames[editor])
def createAttributes(self, h5obj, sinkhandler): if not hasattr(self, 'attrs'): return for key, val in iteritems(self.attrs): if isinstance(val, str): val = NXAttribute(val, 'string') val.create(key, h5obj, sinkhandler)
def complete(self, command, word): def select(candidates, word): return [c for c in candidates if c.startswith(word)] try: # XXX could complete "?" too if command.startswith(('!', '?')) or command.endswith('?'): return [] if command.startswith(':'): return self.complete(command[1:].strip(), word) commands = self.tokenize(command, partial=True) tokens = commands[-1] # only last command is interesting if not word: tokens.append('') command = tokens[0] if len(tokens) == 1: # complete command return select([ n for (n, o) in iteritems(self.session.namespace) if hasattr(o, 'is_usercommand') or isinstance(o, Device) ], word) cmdobj = self.session.namespace.get(command) if isinstance(cmdobj, Device): return [] if not hasattr(cmdobj, 'is_usercommand'): return [] return self.complete_command(cmdobj, tokens[1:], word) except Exception as err: self.session.log.debug('error during completion: %s', err) return []
def _refresh_presets(self): pmenu = self.presetmenu pmenu.clear() delmenu = QMenu('Delete', self) try: for preset, info in iteritems(self.presetdict): paction = QAction(preset, self) pdelaction = QAction(preset, self) info = info.copy() def launchpreset(on, info=info): self._createViewFromDialog(info) def delpreset(on, name=preset, act=paction, delact=pdelaction): pmenu.removeAction(act) delmenu.removeAction(delact) self.presetdict.pop(name, None) self._refresh_presets() paction.triggered[bool].connect(launchpreset) pmenu.addAction(paction) pdelaction.triggered[bool].connect(delpreset) delmenu.addAction(pdelaction) except AttributeError: self.presetdict = {} if self.presetdict: pmenu.addSeparator() pmenu.addMenu(delmenu) else: pmenu.addAction('(no presets created)')
def _handleScanArgs(args, kwargs, scaninfo): preset, detlist, envlist, move, multistep = {}, [], None, [], [] for arg in args: if isinstance(arg, string_types): scaninfo = arg + ' - ' + scaninfo elif isinstance(arg, number_types): preset['t'] = arg elif isinstance(arg, Measurable): detlist.append(arg) elif isinstance(arg, Readable): if envlist is None: envlist = [] envlist.append(arg) else: raise UsageError('unsupported scan argument: %r' % arg) for key, value in iteritems(kwargs): if key in session.devices and isinstance(session.devices[key], Moveable): # Here, don't replace 'list' by '(list, tuple)' # (tuples are reserved for valid device values) if isinstance(value, list): if multistep and len(value) != len(multistep[-1][1]): raise UsageError('all multi-step arguments must have the ' 'same length') multistep.append((session.devices[key], value)) else: move.append((session.devices[key], value)) elif key == 'info' and isinstance(value, string_types): scaninfo = value + ' - ' + scaninfo else: preset[key] = value return preset, scaninfo, detlist, envlist, move, multistep
def showRoiWindow(self, roikey): key = roikey + '/roi' widget = self._getLiveWidget(roikey) region = self.widget._rois[key] if not widget: widget = LiveWidget(None) widget.setWindowTitle(roikey) widget.setColormap(self.widget.getColormap()) widget.setCenterMark(self.actionMarkCenter.isChecked()) widget.logscale(self.actionLogScale.isChecked()) widget.gr.setAdjustSelection(False) # don't use adjust on ROIs for name, roi in iteritems(self.rois): widget.setROI(name, roi) width = max(region.x) - min(region.x) height = max(region.y) - min(region.y) if width > height: dwidth = 500 dheight = 500 * height // width else: dheight = 500 dwidth = 500 * width // height widget.resize(dwidth, dheight) widget.closed.connect(self.on_roiWindowClosed) widget.setWindowForRoi(region) widget.update() widget.show() widget.activateWindow() self._livewidgets[key] = widget
def CreateAllDevices(**kwargs): """Try to create all possible devices in the current setup. This is useful when a setup failed to load many devices, and another attempt should be made. Example: >>> CreateAllDevices() Note: Devices that are marked as lowlevel will not be automatically created, unless you set the lowlevel flag like: >>> CreateAllDevices(lowlevel=True) see also: `CreateDevice`, `RemoveDevice` """ lowlevel = kwargs.get('lowlevel', False) if lowlevel and not session.checkUserLevel(ADMIN): session.log.error('Creating all lowlevel devices is only allowed ' 'for admin users') lowlevel = False session.startMultiCreate() try: for devname, (_, devconfig) in iteritems(session.configured_devices): if devconfig.get('lowlevel', False) and not lowlevel: continue try: session.createDevice(devname, explicit=True) except NicosError: session.log.exception('error creating %s', devname) finally: session.endMultiCreate()
def ListSetups(listall=False): """Print a list of setups. Example: >>> ListSetups() To list also low-level and simulation setups: >>> ListSetups(True) see also: `NewSetup`, `AddSetup`, `RemoveSetup` """ session.log.info('Available setups:') items = [] for name, info in iteritems(session.getSetupInfo()): if info is None: items.append((name, '', '<could not be read, check syntax>', '')) continue if info['group'] in ('special', 'configdata'): continue if info['group'] == 'lowlevel' and not listall: continue items.append((name, name in session.loaded_setups and 'yes' or '', info['description'], ', '.join(sorted(info['devices'])))) items.sort() printTable(('name', 'loaded', 'description', 'devices'), items, session.log.info)
def ask_wc(self, key, ts, time, ttl): ret = set() with self._db_lock: # look for matching keys for dbkey, entries in iteritems(self._db): if key not in dbkey: continue lastent = entries[-1] # check for removed keys if lastent.value is None: continue if dbkey.startswith('nocat/'): dbkey = dbkey[6:] # check for expired keys if lastent.ttl: remaining = lastent.time + lastent.ttl - currenttime() op = remaining > 0 and OP_TELL or OP_TELLOLD if ts: ret.add('%r+%s@%s%s%s\n' % (lastent.time, lastent.ttl, dbkey, op, lastent.value)) else: ret.add(dbkey + op + lastent.value + '\n') elif ts: ret.add('%r@%s%s%s\n' % (lastent.time, dbkey, OP_TELL, lastent.value)) else: ret.add(dbkey + OP_TELL + lastent.value + '\n') return ret
def __init__(self, value, dtype=None, **attr): self.value = value self.dtype = dtype self.attrs = {} for key, val in iteritems(attr): if not isinstance(val, NXAttribute): val = NXAttribute(val) self.attrs[key] = val
def __call__(self, val=None): val = val if val is not None else {} if not isinstance(val, dict): raise ValueError('value needs to be a dict') ret = {} for k, v in iteritems(val): ret[self.keyconv(k)] = self.valconv(v) return readonlydict(ret)
def transferSettings(self): import epics for epicsid, pvs in iteritems(self.pv_values): for pvname, pvvalue in pvs: fullpvname = '%s:%s_W' % (epicsid, pvname) self.log.debug('setting %s = %s' % (fullpvname, pvvalue)) epics.caput(fullpvname, pvvalue) session.delay(2)