Package PyFoam :: Package Basics :: Module TimeLineCollection
[hide private]
[frames] | no frames]

Source Code for Module PyFoam.Basics.TimeLineCollection

  1  """Collection of array of timelines""" 
  2   
  3  from PyFoam.Error import error 
  4   
5 -def mean(a,b):
6 """Mean value of a and b""" 7 return 0.5*(a+b)
8
9 -def signedMax(a,b):
10 """Absolute Maximum of a and b with the sign preserved""" 11 if a<0. or b<0.: 12 return min(a,b) 13 else: 14 return max(a,b)
15
16 -class TimeLineCollection(object):
17 18 possibleAccumulations=["first", "last", "min", "max", "average"] 19
20 - def __init__(self,deflt=0.,extendCopy=False,splitThres=None,splitFun=None,accumulation="first"):
21 """@param deflt: default value for timelines if none has been defined before 22 @param extendCopy: Extends the timeline by cpying the last element 23 @param splitThres: Threshold after which the number of points is halved 24 @param splitFun: Function that is used for halving. If none is specified the mean function is used 25 @param accumulation: if more than one value is given at any time-step, how to accumulate them (possible values: "first", "last", "min", "max", "average") 26 """ 27 28 self.cTime=None 29 self.times=[] 30 self.values={} 31 self.setDefault(deflt) 32 self.setExtend(extendCopy) 33 self.thres=None 34 self.fun=None 35 36 if not (accumulation in TimeLineCollection.possibleAccumulations): 37 error("Value",accumulation,"not in list of possible values:",TimeLineCollection.possibleAccumulations) 38 self.accumulation=accumulation 39 self.occured={} 40 41 self.setSplitting(splitThres=splitThres,splitFun=splitFun)
42
43 - def setSplitting(self,splitThres=None,splitFun=None):
44 """Sets the parameters for splitting""" 45 if splitThres: 46 self.thres=splitThres 47 if (self.thres % 2)==1: 48 self.thres+=1 49 50 if splitFun: 51 self.fun=splitFun 52 elif not self.fun: 53 self.fun=mean
54
55 - def setDefault(self,deflt):
56 """@param deflt: default value to be used""" 57 self.defaultValue=float(deflt)
58
59 - def setExtend(self,mode):
60 """@param mode: whether or not to extend the timeline by copying or setting the default value""" 61 self.extendCopy=mode
62
63 - def nr(self):
64 """Number of elements in timelines""" 65 return len(self.times)
66
67 - def setTime(self,time):
68 """Sets the time. If time is new all the timelines are extended 69 @param time: the new current time""" 70 71 dTime=float(time) 72 73 if dTime!=self.cTime: 74 self.cTime=dTime 75 self.times.append(self.cTime) 76 for v in self.values.values(): 77 if len(v)>0 and self.extendCopy: 78 val=v[-1] 79 else: 80 val=self.defaultValue 81 v.append(val) 82 if self.thres: 83 if len(self.times)==self.thres: 84 self.times=self.split(self.times,mean) 85 for k in self.values.keys(): 86 self.values[k]=self.split(self.values[k],self.fun) 87 self.occured={}
88
89 - def split(self,array,func):
90 """Makes the array smaller by joining every two points 91 @param array: the field to split 92 @param func: The function to use for joining two points""" 93 94 newLen=len(array)/2 95 newArray=[0.]*newLen 96 97 for i in range(newLen): 98 newArray[i]=func(array[2*i],array[2*i+1]) 99 100 return newArray
101
102 - def getTimes(self):
103 """@return: A list of the time values""" 104 return self.times
105
106 - def getValueNames(self):
107 """@return: A list with the names of the safed values""" 108 return self.values.keys()
109
110 - def getValues(self,name):
111 """Gets a timeline 112 @param name: Name of the timeline 113 @return: List with the values""" 114 115 if not self.values.has_key(name): 116 self.values[name]=self.nr()*[self.defaultValue] 117 return self.values[name]
118
119 - def setValue(self,name,value):
120 """Sets the value of the last element in a timeline 121 @param name: name of the timeline 122 @param value: the last element""" 123 data=self.getValues(name) 124 val=float(value) 125 if len(data)>0: 126 if not self.occured.has_key(name): 127 newValue=val 128 self.occured[name]=1 129 else: 130 oldValue=data[-1] 131 n=self.occured[name] 132 self.occured[name]+=1 133 if self.accumulation=="first": 134 newValue=oldValue 135 elif self.accumulation=="last": 136 newValue=val 137 elif self.accumulation=="max": 138 newValue=max(val,oldValue) 139 elif self.accumulation=="min": 140 newValue=min(val,oldValue) 141 elif self.accumulation=="average": 142 newValue=(n*oldValue+val)/(n+1) 143 else: 144 error("Unimplemented accumulator",self.accumulation) 145 146 data[-1]=newValue
147