def __init__(self, channels, logger=Logger('buffer'), **kwargs): """Create a new `DataBuffer` """ if isinstance(channels, str): channels = [channels] self.channels = ChannelList.from_names(*channels) self.data = self.DictClass() self.logger = logger
def add_channels(self, *channels, **fetchargs): """Add one of more channels to this `DataBuffer` Parameters ---------- *channels : `str`, `~gwpy.detector.Channel` one or more channels to add to the buffer. Any channels that already exist in the buffer will be ignored **fetchargs keyword arguments to pass to the `fetch()` method. """ # find new channels channels = ChannelList.from_names(*channels) new = [] for c in channels: if c not in self.channels: new.append(c) self.channels.append(c) self.data[c] = self.ListClass() # fetch data for new channels for seg in self.segments: self.get((seg[0], seg[1]), channels=new, **fetchargs)
def from_ini(filepath, ifo=None): """Configure a new Monitor from an INI file Parameters ---------- filepath : `str` path to INI-format configuration file ifo : `str`, optional prefix of relevant interferometer. This is only required if '%(ifo)s' interpolation is used in the INI file. The default value is taken from the ``IFO`` environment variable, if found. Returns ------- monitor : `Monitor` a new monitor of the type given in the INI file Raises ------ ValueError if the configuration file cannot be read OR if IFO interpolation is used, but the `ifo` is not given or found """ # get ifo ifo = ifo or os.getenv('IFO', None) # read configuration file if isinstance(filepath, str): filepath = filepath.split(',') cp = ConfigParser() readok = cp.read(filepath) if not len(readok) == len(filepath): failed = [cf for cf in filepath if cf not in readok] raise ValueError("Failed to read configuration file %r" % failed[0]) # get basic params basics = dict(cp.items('monitor', raw=True)) type_ = basics.pop('type') channels = map(str, ChannelList.from_names(basics.pop('channels', ''))) combinations = basics.pop('combinations', None) basics = dict((key, safe_eval(val)) for (key, val) in basics.iteritems()) # get type monitor = get_monitor(type_) # get plotting parameters pparams = dict((key, safe_eval(val)) for (key, val) in cp.items('plot')) # get channel and reference curve names sections = cp.sections() if not channels: channels = [c for c in sections if c not in ['monitor', 'plot'] if c[:4] not in ['ref:', 'com:']] references = [c for c in sections if c not in ['monitor', 'plot'] if c[:4] == 'ref:'] if combinations is None: combinations = [c for c in sections if c not in ['monitor', 'plot'] if c[:4] == 'com:'] # get channel parameters cparams = {} for i, channel in enumerate(channels): # get channel section _params = cp.items(channel) # interpolate ifo if r'%(ifo)s' in channel and not ifo: raise ValueError("Cannot interpolate IFO in channel name without " "IFO environment variable or --ifo on command " "line") channels[i] = channel % {'ifo': ifo} for param, val in _params: val = safe_eval(val) if param not in cparams: cparams[param] = [] while len(cparams[param]) < len(channels): cparams[param].append(None) cparams[param][i] = val # get reference parameters # reference parameters will be sent to the monitor in a dictionary # with the path of each reference as keys and with the name and the other # parameters as a dictionary for each key rparams = OrderedDict() for reference in references: rparamsi = OrderedDict([('name', reference[4:])]) for param, val in cp.items(reference): val = safe_eval(val) if param == 'path': refpath = val else: rparamsi[param] = val try: if os.path.isdir(refpath): # Section is a directory: # import all references in folder (assumes 'dat' format) for f in os.listdir(refpath): if os.path.splitext(f)[1] in ['.txt', '.dat', '.gz']: refpath += f rparamsi.setdefault( 'label', os.path.basename(refpath).split('.') [0].replace('_', r' ')) rparams[refpath] = rparamsi else: rparamsi.setdefault( 'label', os.path.basename(refpath).split('.')[0] .replace('_', r' ')) rparams[refpath] = rparamsi except NameError: raise ValueError('Cannot load reference {0} plot if no ' 'parameter "path" is defined' .format(reference)) # get combination parameters # IN PROGRESS combparams = OrderedDict() for comb in combinations: # get combination section _params = cp.items(comb) deflabel = comb[4:] combparamsi = OrderedDict([('label', deflabel)]) for param, val in _params: val = safe_eval(val) combparamsi[param] = val combparams[deflabel] = combparamsi params = dict(basics.items() + pparams.items() + cparams.items()) if rparams: params['reference'] = rparams if combparams: params['combination'] = combparams return monitor(*channels, **params)
def test_find(self): cl = ChannelList.from_names(*self.NAMES) self.assertEqual(cl.find(self.NAMES[2]), 2) self.assertRaises(ValueError, cl.find, 'blah')
def test_from_names(self): cl = ChannelList.from_names(*self.NAMES) self.assertListEqual(cl, map(Channel, self.NAMES)) cl2 = ChannelList.from_names(','.join(self.NAMES)) self.assertListEqual(cl, cl2)
def from_ini(filepath, ifo=None): """Configure a new Monitor from an INI file Parameters ---------- filepath : `str` path to INI-format configuration file ifo : `str`, optional prefix of relevant interferometer. This is only required if '%(ifo)s' interpolation is used in the INI file. The default value is taken from the ``IFO`` environment variable, if found. Returns ------- monitor : `Monitor` a new monitor of the type given in the INI file Raises ------ ValueError if the configuration file cannot be read OR if IFO interpolation is used, but the `ifo` is not given or found """ # get ifo ifo = ifo or os.getenv('IFO', None) # read configuration file if isinstance(filepath, str): filepath = filepath.split(',') cp = ConfigParser() readok = cp.read(filepath) if not len(readok) == len(filepath): failed = [cf for cf in filepath if cf not in readok] raise ValueError("Failed to read configuration file %r" % failed[0]) # get basic params basics = dict(cp.items('monitor', raw=True)) type_ = basics.pop('type') channels = map(str, ChannelList.from_names(basics.pop('channels', ''))) combinations = basics.pop('combinations', None) basics = dict((key, safe_eval(val)) for (key, val) in basics.iteritems()) # get type monitor = get_monitor(type_) # get plotting parameters pparams = dict((key, safe_eval(val)) for (key, val) in cp.items('plot')) # get channel and reference curve names sections = cp.sections() if not channels: channels = [ c for c in sections if c not in ['monitor', 'plot'] if c[:4] not in ['ref:', 'com:'] ] references = [ c for c in sections if c not in ['monitor', 'plot'] if c[:4] == 'ref:' ] if combinations is None: combinations = [ c for c in sections if c not in ['monitor', 'plot'] if c[:4] == 'com:' ] # get channel parameters cparams = {} for i, channel in enumerate(channels): # get channel section _params = cp.items(channel) # interpolate ifo if r'%(ifo)s' in channel and not ifo: raise ValueError("Cannot interpolate IFO in channel name without " "IFO environment variable or --ifo on command " "line") channels[i] = channel % {'ifo': ifo} for param, val in _params: val = safe_eval(val) if param not in cparams: cparams[param] = [] while len(cparams[param]) < len(channels): cparams[param].append(None) cparams[param][i] = val # get reference parameters # reference parameters will be sent to the monitor in a dictionary # with the path of each reference as keys and with the name and the other # parameters as a dictionary for each key rparams = OrderedDict() for reference in references: rparamsi = OrderedDict([('name', reference[4:])]) for param, val in cp.items(reference): val = safe_eval(val) if param == 'path': refpath = val else: rparamsi[param] = val try: if os.path.isdir(refpath): # Section is a directory: # import all references in folder (assumes 'dat' format) for f in os.listdir(refpath): if os.path.splitext(f)[1] in ['.txt', '.dat', '.gz']: refpath += f rparamsi.setdefault( 'label', os.path.basename(refpath).split('.') [0].replace('_', r' ')) rparams[refpath] = rparamsi else: rparamsi.setdefault( 'label', os.path.basename(refpath).split('.')[0].replace( '_', r' ')) rparams[refpath] = rparamsi except NameError: raise ValueError( 'Cannot load reference {0} plot if no ' 'parameter "path" is defined'.format(reference)) # get combination parameters # IN PROGRESS combparams = OrderedDict() for comb in combinations: # get combination section _params = cp.items(comb) deflabel = comb[4:] combparamsi = OrderedDict([('label', deflabel)]) for param, val in _params: val = safe_eval(val) combparamsi[param] = val combparams[deflabel] = combparamsi params = dict(basics.items() + pparams.items() + cparams.items()) if rparams: params['reference'] = rparams if combparams: params['combination'] = combparams return monitor(*channels, **params)