Beispiel #1
0
def patch_booleans(history, trace=TRACE):
    if trace:
        print('In patch_booleans(%d,%s)' % (len(history),
                                            (history or ('', ))[0]))
    fromHistoryBuffer = len(history) and hasattr(history[0], 'time')
    patch = 0

    for h in history:
        v = h[1] if not fromHistoryBuffer else h.value
        if v is None:
            continue
        if isinstance(v, int) or isinstance(v, float):
            break
        if isinstance(v,basestring) and \
            (not isNumber(v) and v.lower() not in ('true','false','none')):
            break
        if isinstance(v, basestring):
            if isNumber(v) and v in ('1', '0', '1.0', '0.0'): patch = 1
            if v.lower() in ('true', 'false', 'none'): patch = 2
            break
        if isinstance(v, bool):
            patch = 1
            break

    if patch:
        for i, h in enumerate(history):
            v = h.value if fromHistoryBuffer else h[1]
            if patch == 2:
                v = v.lower().strip() in ('true', '1')
            if fromHistoryBuffer:
                h.value = int(v)
            else:
                history[i] = (h[0], int(v))

    return history
Beispiel #2
0
def patch_booleans(history,trace=TRACE):
    if trace: 
        print('In patch_booleans(%d,%s)'%(len(history),(history or ('',))[0]))
    fromHistoryBuffer = len(history) and hasattr(history[0],'time')
    patch = 0

    for h in history: 
        v = h[1] if not fromHistoryBuffer else h.value
        if v is None: 
            continue
        if isinstance(v,int) or isinstance(v,float): 
            break
        if isinstance(v,basestring) and \
            (not isNumber(v) and v.lower() not in ('true','false','none')): 
            break
        if isinstance(v,basestring):
            if isNumber(v) and v in ('1','0','1.0','0.0'): patch=1
            if v.lower() in ('true','false','none'): patch=2
            break
        if isinstance(v,bool):
            patch = 1
            break

    if patch:
        for i,h in enumerate(history):
            v = h.value if fromHistoryBuffer else h[1]
            if patch==2: 
                v = v.lower().strip() in ('true','1')
            if fromHistoryBuffer: 
                h.value = int(v)
            else: 
                history[i] = (h[0],int(v))

    return history
Beispiel #3
0
 def __contains__(self, key):
     if key in self.mappings:
         return True
     elif fun.isNumber(key):
         #Return a mapped addresses
         return any(m.has_address(key) for m in self.mappings.values())
     else:
         return False
Beispiel #4
0
    def reader_hook(self, attribute, values):
        """This method will be executed by the ReaderProcess to process the queried data."""
        try:
            print('>' * 80)
            print(time.ctime() + ' In reader_hook(%s,[%d])' %
                  (attribute, len(values)))

            MAXDIM = 1024 * 1024 * 1024
            #First create the attributes
            epoch, data, aname = [], [], attribute.replace('/', '__')
            values = decimate_values(values)
            [(epoch.append(v[0]), data.append(v[1])) for v in values]
            writable = PyTango.AttrWriteType.READ

            #Adding time attribute
            m, atformat, dims = None, PyTango.SpectrumAttr, [MAXDIM]
            for d in data:
                if d is not None:
                    if fn.isSequence(d):
                        atformat, dims = PyTango.ImageAttr, [MAXDIM, MAXDIM]
                        m = d[0]
                    else:
                        m = d
                    break

            attype = PyTango.DevDouble if (
                fn.isNumber(m) or fn.isBool(m)) else PyTango.DevString
            self.add_attribute(
                PyTango.ImageAttr(aname, attype, writable, MAXDIM, MAXDIM),
                self.read_dyn_attr, None, self.is_dyn_attr_allowed)

            self.add_attribute(
                PyTango.SpectrumAttr(aname + '_t', PyTango.DevDouble, writable,
                                     MAXDIM), self.read_dyn_attr, None,
                self.is_dyn_attr_allowed)

            self.add_attribute(
                PyTango.SpectrumAttr(aname + '_d', PyTango.DevString, writable,
                                     MAXDIM), self.read_dyn_attr, None,
                self.is_dyn_attr_allowed)

            self.add_attribute(atformat(aname + '_r', attype, writable,
                                        *dims), self.read_dyn_attr, None,
                               self.is_dyn_attr_allowed)

            #Then add the data to Cache values, so IsDataReady will return True
            t = fn.now()
            self.RemoveCachedAttribute(attribute)
            self.AttrData[attribute] = (t, atformat, attype, values)
            print('Done: %s,%s,%s,%s,%d' %
                  (attribute, t, atformat, attype, len(values)))
        except:
            print(traceback.format_exc())
Beispiel #5
0
 def __getitem__(self, key):
     if fun.isNumber(key):
         key = int(key)
         for v in self.mappings.values():  #Return a mapped addresses
             if v.has_address(key):
                 return v.get_address(key)
         if self.default is not Exception:
             return self.default
         raise Exception("AdressNotMapped: '%s'" % key)
     elif key in self.mappings:
         return self.mappings[key]  #Return a Map
     else:
         raise Exception("KeyError: '%s'" % key)
Beispiel #6
0
 def __setitem__(self, key, value):
     if fun.isNumber(key):
         raise Exception('NotAllowed!')
     else:
         self.mappings[key].set(value)
Beispiel #7
0
def decimation(history,method,window='0',logger_obj=None, N=MAX_RESOLUTION):
    """
    Nones and NaNs are always removed if this method is called
    
    history: array of data
    method: method, callable or interval, from fandango.arrays
    window: string for time
    logger_obj: ArchivedTrendLogger or similar
    N: max array size to return
    """
    t0 = time.time()
    l0 = len(history)
    if not l0:
        return history
    
    trace = getattr(logger_obj,'warning',fandango.printf)
    try:
        if not isinstance(window,(int,float)):
            window = str2time(window or '0') 
    except: 
        window = 0

    if isNumber(method) and not window:
        method, window = fandango.arrays.pickfirst, float(method)
        
    start_date,stop_date = float(history[0][0]),float(history[-1][0])

    ## Decimation by data_has_changed is ALWAYS done
    if len(history): #method is not None
        nv = []
        #sq = isSequence(history[0][1])
        for i,v in enumerate(history):
            if (v[1] not in (None,NaN)# is not None and (sq or not isNaN(v[1]))
                    and (i in (0,l0-1,l0-2) or 
                        data_has_changed(history[i-1],v,history[i+1]))
                    ):
                nv.append(v)
        t1 = time.time()
        if l0!=len(nv):
            trace('Removed %d repeated values in %fs'
              %(l0-len(nv),t1-t0))

        t0,i,c,lh = t1,0,0,len(history)
        while i<len(history):
            if history[c] in (None,NaN):
                history.pop(c)
            else:
                c+=1
            i+=1
        t1 = time.time()
        if l0!=len(history):
            trace('Removed %d (None,NaN) values in %fs'
              %(l0-len(history),t1-t0))
        history = nv   
        
    if (method and isCallable(method) and len(history)):
        # Data is filtered applying an averaging at every "window" interval.
        # As range() only accept integers the minimum window is 1 second.
        # It means that filtering 3 hours will implicitly prune millis data.        
        #DATA FROM EVAL IS ALREADY FILTERED; SHOULD NOT PASS THROUGH HERE
        
        if type(history[0][-1]) not in (int,float,bool):
            method = fandango.arrays.pickfirst
            trace('Decimation forced to %s' % method)
        
        wmin = max(1.,(stop_date-start_date)/(2*MAX_RESOLUTION))
        wauto = max(1.,(stop_date-start_date)/(10*N))
        trace('WMIN,WUSER,WAUTO = %s,%s,%s'%(wmin,window,wauto))
        window = wauto if not window else max((wmin,window))
        
        if len(history) > float(stop_date-start_date)/window:
            history = fandango.arrays.filter_array(
                data=history,window=window,method=method)
            t2 = time.time()
            trace('Decimated %d values to %d in %f seconds '
                  '(%s,%s)'
                  %(l0,len(history),t2-t1,method,window))
    else:
        #trace('Decimation(%s) is not callable' % method)
        pass
            
    return history
    def reader_hook(self,attribute,values):
        """This method will be executed by the ReaderProcess to process the queried data.""" 
        try:
            print('>'*80)
            print(time.ctime()+' In reader_hook(%s,[%d])'
                  %(attribute,len(values)))
            self.counter-=1
            print(self.counter)
            
            MAXDIM = 1024*1024*1024
            #First create the attributes
            epoch,data,aname = [],[],attribute.replace('/','__')
            values = decimate_values(values)
            [(epoch.append(v[0]),data.append(v[1])) for v in values]
            writable = PyTango.AttrWriteType.READ

            #Adding time attribute
            m,atformat,dims = None,PyTango.SpectrumAttr,[MAXDIM]
            for d in data:
              if d is not None:
                if fn.isSequence(d):
                  atformat,dims = PyTango.ImageAttr,[MAXDIM,MAXDIM]
                  m = d[0]
                else:
                  m = d
                break

            attype = PyTango.DevDouble if (fn.isNumber(m) or fn.isBool(m)) else PyTango.DevString
            self.add_attribute(
                PyTango.ImageAttr(aname,attype,writable,MAXDIM,MAXDIM),
                self.read_dyn_attr,None,self.is_dyn_attr_allowed)
            
            self.add_attribute(
                PyTango.SpectrumAttr(aname+'_t',PyTango.DevDouble, writable,MAXDIM),
                self.read_dyn_attr,None,self.is_dyn_attr_allowed)

            self.add_attribute(
                PyTango.SpectrumAttr(aname+'_d',PyTango.DevString, writable,MAXDIM),
                self.read_dyn_attr,None,self.is_dyn_attr_allowed)
            
            #ARRAY
            self.add_attribute(atformat(aname+'_r',attype, writable,*dims),
                               self.read_dyn_attr,None,self.is_dyn_attr_allowed)
            
            #LAST VALUE
            self.add_attribute(PyTango.Attr(aname+'_l',attype,PyTango.AttrWriteType.READ),
                               self.read_dyn_attr,None,self.is_dyn_attr_allowed)   
            
            #LAST DATE
            self.add_attribute(
                PyTango.Attr(aname+'_ld',PyTango.DevString,PyTango.AttrWriteType.READ),
                               self.read_dyn_attr,None,self.is_dyn_attr_allowed)              
            
            self.add_attribute(
                PyTango.Attr(aname+'_rg',PyTango.DevString,PyTango.AttrWriteType.READ),
                               self.read_dyn_attr,None,self.is_dyn_attr_allowed)                 
            
            #Then add the data to Cache values, so IsDataReady will return True
            t = fn.now()
            self.RemoveCachedAttribute(attribute)
            self.AttrData[attribute] = (t,atformat,attype,values)
            print('Done: %s,%s,%s,%s,%d'%(attribute,t,atformat,attype,len(values)))
        except:
            print(traceback.format_exc())