def __init__(self, channelDict={}, **kwargs): super(ChannelLibrary, self).__init__(channelDict=channelDict, **kwargs) self.load_from_library() self.logicalChannelManager = DictManager( itemDict=self.channelDict, displayFilter=lambda x: isinstance(x, LogicalChannel), possibleItems=NewLogicalChannelList) self.physicalChannelManager = DictManager( itemDict=self.channelDict, displayFilter=lambda x: isinstance(x, PhysicalChannel), possibleItems=NewPhysicalChannelList) if self.libFile: self.fileWatcher = FileWatcher.LibraryFileWatcher( self.libFile, self.update_from_file)
def __init__(self, channelDict={}, **kwargs): super(ChannelLibrary, self).__init__(channelDict=channelDict, **kwargs) self.load_from_library() self.logicalChannelManager = DictManager(itemDict=self.channelDict, displayFilter=lambda x : isinstance(x, LogicalChannel), possibleItems=NewLogicalChannelList) self.physicalChannelManager = DictManager(itemDict=self.channelDict, displayFilter=lambda x : isinstance(x, PhysicalChannel), possibleItems=NewPhysicalChannelList) if self.libFile: self.fileWatcher = FileWatcher.LibraryFileWatcher(self.libFile, self.update_from_file)
def __init__(self, **kwargs): super(InstrumentLibrary, self).__init__(**kwargs) self.load_from_library() if self.libFile: self.fileWatcher = FileWatcher.LibraryFileWatcher( self.libFile, self.update_from_file) #Setup the dictionary managers for the different instrument types self.AWGs = DictManager( itemDict=self.instrDict, displayFilter=lambda x: isinstance(x, AWGs.AWG), possibleItems=AWGs.AWGList) self.sources = DictManager( itemDict=self.instrDict, displayFilter=lambda x: isinstance( x, MicrowaveSources.MicrowaveSource), possibleItems=MicrowaveSources.MicrowaveSourceList) self.others = DictManager( itemDict=self.instrDict, displayFilter=lambda x: not isinstance(x, AWGs.AWG) and not isinstance(x, MicrowaveSources.MicrowaveSource), possibleItems=newOtherInstrs)
def __init__(self, **kwargs): super(SweepLibrary, self).__init__(**kwargs) self.load_from_library() self.sweepManager = DictManager(itemDict=self.sweepDict, possibleItems=newSweepClasses)
class ChannelLibrary(Atom): # channelDict = Dict(Str, Channel) channelDict = Typed(dict) logicalChannelManager = Typed(DictManager) physicalChannelManager = Typed(DictManager) libFile = Str() fileWatcher = Typed(FileWatcher.LibraryFileWatcher) def __init__(self, channelDict={}, **kwargs): super(ChannelLibrary, self).__init__(channelDict=channelDict, **kwargs) self.load_from_library() self.logicalChannelManager = DictManager( itemDict=self.channelDict, displayFilter=lambda x: isinstance(x, LogicalChannel), possibleItems=NewLogicalChannelList) self.physicalChannelManager = DictManager( itemDict=self.channelDict, displayFilter=lambda x: isinstance(x, PhysicalChannel), possibleItems=NewPhysicalChannelList) if self.libFile: self.fileWatcher = FileWatcher.LibraryFileWatcher( self.libFile, self.update_from_file) #Dictionary methods def __getitem__(self, key): return self.channelDict[key] def __setitem__(self, key, value): self.channelDict[key] = value def __delitem__(self, key): del self.channelDict[key] # def __len__(self): # return len(self.channelDict) # def __iter__(self): # for x in self.channelDict: # yield x def __contains__(self, key): return key in self.channelDict def keys(self): return self.channelDict.keys() def values(self): return self.channelDict.values() def write_to_file(self): import JSONHelpers if self.libFile: #Pause the file watcher to stop cicular updating insanity if self.fileWatcher: self.fileWatcher.pause() with open(self.libFile, 'w') as FID: json.dump(self, FID, cls=JSONHelpers.LibraryEncoder, indent=2, sort_keys=True) if self.fileWatcher: self.fileWatcher.resume() def json_encode(self, matlabCompatible=False): return {"channelDict": self.channelDict} def load_from_library(self): import JSONHelpers if self.libFile: try: with open(self.libFile, 'r') as FID: tmpLib = json.load(FID, cls=JSONHelpers.ChannelDecoder) if not isinstance(tmpLib, ChannelLibrary): raise ValueError('Failed to load channel library') # connect objects labeled by strings for chan in tmpLib.channelDict.values(): if hasattr(chan, 'physChan'): chan.physChan = tmpLib[ chan. physChan] if chan.physChan in tmpLib.channelDict else None if hasattr(chan, 'gateChan'): chan.gateChan = tmpLib[ chan. gateChan] if chan.gateChan in tmpLib.channelDict else None self.channelDict.update(tmpLib.channelDict) except IOError: print('No channel library found.') except ValueError: print('Failed to load channel library.') def update_from_file(self): """ Only update relevant parameters Helps avoid both stale references from replacing whole channel objects (as in load_from_library) and the overhead of recreating everything. """ if self.libFile: with open(self.libFile, 'r') as FID: try: allParams = json.load(FID)['channelDict'] except ValueError: print( 'Failed to update channel library from file. Probably is just half-written.' ) return # update & insert for chName, chParams in allParams.items(): if chName in self.channelDict: self.update_from_json(chName, chParams) else: # load class from name and update from json className = chParams['x__class__'] moduleName = chParams['x__module__'] mod = importlib.import_module(moduleName) cls = getattr(mod, className) self.channelDict[chName] = cls() self.update_from_json(chName, chParams) # remove for chName in self.channelDict.keys(): if chName not in allParams: del self.channelDict[chName] def update_from_json(self, chName, chParams): # ignored or specially handled parameters ignoreList = [ 'pulseParams', 'physChan', 'gateChan', 'AWG', 'generator', 'x__class__', 'x__module__' ] if 'pulseParams' in chParams.keys(): paramDict = { k.encode('ascii'): v for k, v in chParams['pulseParams'].items() } shapeFunName = paramDict.pop('shapeFun', None) if shapeFunName: paramDict['shapeFun'] = getattr(PulseShapes, shapeFunName) self.channelDict[chName].pulseParams = paramDict if 'physChan' in chParams.keys(): self.channelDict[chName].physChan = self.channelDict[ chParams['physChan']] if chParams[ 'physChan'] in self.channelDict else None if 'gateChan' in chParams.keys(): self.channelDict[chName].gateChan = self.channelDict[ chParams['gateChan']] if chParams[ 'gateChan'] in self.channelDict else None # TODO: how do we follow changes to selected AWG or generator/source? for paramName in chParams: if paramName not in ignoreList: setattr(self.channelDict[chName], paramName, chParams[paramName]) def on_awg_change(self, oldName, newName): print "Change AWG", oldName, newName for chName in self.channelDict: if (isinstance(self.channelDict[chName], PhysicalMarkerChannel) or isinstance(self.channelDict[chName], PhysicalQuadratureChannel)): awgName, awgChannel = chName.rsplit('-', 1) if awgName == oldName: newLabel = "{0}-{1}".format(newName, awgChannel) print "Changing {0} to {1}".format(chName, newLabel) self.physicalChannelManager.name_changed(chName, newLabel)
def __init__(self, **kwargs): super(MeasFilterLibrary, self).__init__(**kwargs) self.load_from_library() self.filterManager = DictManager(itemDict=self.filterDict, possibleItems=measFilterList)
class ChannelLibrary(Atom): # channelDict = Dict(Str, Channel) channelDict = Typed(dict) logicalChannelManager = Typed(DictManager) physicalChannelManager = Typed(DictManager) libFile = Str() fileWatcher = Typed(FileWatcher.LibraryFileWatcher) def __init__(self, channelDict={}, **kwargs): super(ChannelLibrary, self).__init__(channelDict=channelDict, **kwargs) self.load_from_library() self.logicalChannelManager = DictManager(itemDict=self.channelDict, displayFilter=lambda x : isinstance(x, LogicalChannel), possibleItems=NewLogicalChannelList) self.physicalChannelManager = DictManager(itemDict=self.channelDict, displayFilter=lambda x : isinstance(x, PhysicalChannel), possibleItems=NewPhysicalChannelList) if self.libFile: self.fileWatcher = FileWatcher.LibraryFileWatcher(self.libFile, self.update_from_file) #Dictionary methods def __getitem__(self, key): return self.channelDict[key] def __setitem__(self, key, value): self.channelDict[key] = value def __delitem__(self, key): del self.channelDict[key] # def __len__(self): # return len(self.channelDict) # def __iter__(self): # for x in self.channelDict: # yield x def __contains__(self, key): return key in self.channelDict def keys(self): return self.channelDict.keys() def values(self): return self.channelDict.values() def write_to_file(self): import JSONHelpers if self.libFile: #Pause the file watcher to stop cicular updating insanity if self.fileWatcher: self.fileWatcher.pause() with open(self.libFile, 'w') as FID: json.dump(self, FID, cls=JSONHelpers.LibraryEncoder, indent=2, sort_keys=True) if self.fileWatcher: self.fileWatcher.resume() def json_encode(self, matlabCompatible=False): return {"channelDict":self.channelDict} def load_from_library(self): import JSONHelpers if self.libFile: try: with open(self.libFile, 'r') as FID: tmpLib = json.load(FID, cls=JSONHelpers.ChannelDecoder) if not isinstance(tmpLib, ChannelLibrary): raise ValueError('Failed to load channel library') # connect objects labeled by strings for chan in tmpLib.channelDict.values(): if hasattr(chan, 'physChan'): chan.physChan = tmpLib[chan.physChan] if chan.physChan in tmpLib.channelDict else None if hasattr(chan, 'gateChan'): chan.gateChan = tmpLib[chan.gateChan] if chan.gateChan in tmpLib.channelDict else None self.channelDict.update(tmpLib.channelDict) except IOError: print('No channel library found.') except ValueError: print('Failed to load channel library.') def update_from_file(self): """ Only update relevant parameters Helps avoid both stale references from replacing whole channel objects (as in load_from_library) and the overhead of recreating everything. """ if self.libFile: with open(self.libFile, 'r') as FID: try: allParams = json.load(FID)['channelDict'] except ValueError: print('Failed to update channel library from file. Probably is just half-written.') return # update & insert for chName, chParams in allParams.items(): if chName in self.channelDict: self.update_from_json(chName, chParams) else: # load class from name and update from json className = chParams['x__class__'] moduleName = chParams['x__module__'] mod = importlib.import_module(moduleName) cls = getattr(mod, className) self.channelDict[chName] = cls() self.update_from_json(chName, chParams) # remove for chName in self.channelDict.keys(): if chName not in allParams: del self.channelDict[chName] def update_from_json(self,chName, chParams): # ignored or specially handled parameters ignoreList = ['pulseParams', 'physChan', 'gateChan', 'AWG', 'generator', 'x__class__', 'x__module__'] if 'pulseParams' in chParams.keys(): paramDict = {k.encode('ascii'):v for k,v in chParams['pulseParams'].items()} shapeFunName = paramDict.pop('shapeFun', None) if shapeFunName: paramDict['shapeFun'] = getattr(PulseShapes, shapeFunName) self.channelDict[chName].pulseParams = paramDict if 'physChan' in chParams.keys(): self.channelDict[chName].physChan = self.channelDict[chParams['physChan']] if chParams['physChan'] in self.channelDict else None if 'gateChan' in chParams.keys(): self.channelDict[chName].gateChan = self.channelDict[chParams['gateChan']] if chParams['gateChan'] in self.channelDict else None # TODO: how do we follow changes to selected AWG or generator/source? for paramName in chParams: if paramName not in ignoreList: setattr(self.channelDict[chName], paramName, chParams[paramName]) def on_awg_change(self, oldName, newName): print "Change AWG", oldName, newName for chName in self.channelDict: if (isinstance(self.channelDict[chName], PhysicalMarkerChannel) or isinstance(self.channelDict[chName], PhysicalQuadratureChannel)): awgName, awgChannel = chName.rsplit('-',1) if awgName == oldName: newLabel = "{0}-{1}".format(newName,awgChannel) print "Changing {0} to {1}".format(chName, newLabel) self.physicalChannelManager.name_changed(chName, newLabel)