RelativeValue.py

From XPUB & Lens-Based wiki

A class to work with noisy data values (like sensors), or really any value where the importance is the value relative to time, not the momentary absolute value.

class RelativeValue:
	"""
	A relative value keeps track of it's history in the form
	of a minimum and maximum value,

	The "slider" attribute that represents where the
	current value is relative to the min (0.0), and max (1.0).

	For example, if the slider value is 0.5, this means that the current value
	is halfway between the lowest and highest value ever seen.

	"""
	def __init__(self, value=0.0):
		self.value = value
		self.min = None
		self.max = None
		self.slider = 0.0
		self.update()

	def update(self):
		if (self.min == None or self.value < self.min):
			self.min = self.value
		if (self.max == None or self.value > self.max):
			self.max = self.value
		if (self.min != self.max):
			self.slider = (self.value - self.min) / (self.max - self.min)

	def setvalue(self, value):
		self.value = value
		self.update()