1 from .Utilities
import readValuePairsFromFile
2 from collections
import deque, OrderedDict
7 def __init__(self, filename=None, minXValue=None):
9 self.
data = OrderedDict()
13 self.
errors = OrderedDict()
15 def readFromFile(self, filename, minXValue):
16 self.
data = readValuePairsFromFile(filename, minXValue)
18 def addPoint(self, point, value, error=None):
19 self.
data[point] = value
24 self.
data = OrderedDict(sorted(self.
data.items()))
27 def getNearestPoint(self, x):
30 iterator = iter(self.
data.items())
31 firstX, firstY = next(iterator)
34 return (firstX, firstY)
38 lastDistance = x - firstX
40 for currentX, currentY
in iterator:
41 newDistance = x - currentX
44 if abs(newDistance) < lastDistance:
45 return (currentX, currentY)
50 lastDistance = x - currentX
54 def plot(self, plotEveryNthPoint=1):
57 for x, y
in sorted(self.
data.items()):
58 if counter % plotEveryNthPoint == 0:
59 gnuplotData += str(x) +
"\t" + str(y) +
"\n"
63 gnuplot = subprocess.Popen([
'gnuplot'], stdin=subprocess.PIPE)
65 gnuplotCommand =
"set terminal wxt persist\n"
66 gnuplotCommand +=
"plot '-' with linespoints notitle\n"
67 gnuplotCommand += gnuplotData
70 gnuplot.stdin.write(gnuplotCommand.encode(
"UTF-8"))
72 def getArithmeticMean(self):
75 for _, y
in self.
data.items():
81 def getRootMeanSquaredDeviationFromArithmeticMean(self):
86 for _, y
in self.
data.items():
88 sum_ += (y - mean) ** 2
90 return math.sqrt(sum_ / count)
92 def getSimpleMovingAverageDict(self, windowsize):
96 averages = OrderedDict()
98 for x, y
in self.
data.items():
101 firstX = window[0][0]
102 if x - firstX >= windowsize:
109 averages.update({x: sum_ / count})
115 def getAverageFromSimpleMovingAverage(self, windowsize):
120 for _, y
in averages.items():
126 def getLocalExtremaByComparisonFunction(self, comparisonFunction, includeBoundaries):
131 items = {key: value
for key, value
in enumerate(self.
data.items())}
139 if not includeBoundaries:
142 if index + 1
not in items:
148 if comparisonFunction(y, items[index + 1][1]):
153 if index + 1
not in items:
154 if not includeBoundaries:
157 if comparisonFunction(y, items[index - 1][1]):
162 if not comparisonFunction(y, items[index - 1][1]):
164 if not comparisonFunction(y, items[index + 1][1]):
171 def getLocalMaxima(self, includeBoundaries=True):
172 greaterThan =
lambda x, y: x > y
175 def getLocalMinima(self, includeBoundaries=True):
176 lessThan =
lambda x, y: x < y
179 def getLocalExtrema(self, includeBoundaries=True):
182 return OrderedDict(sorted(minima.items() + maxima.items()))
185 def getData(self, sortFirst=True):
191 def getKeysAndValues(self, sortFirst=True):
195 return zip(*self.
data.items())
197 def getKeysAndValuesAndErrors(self, sortFirst=True):
204 for key, value
in self.
data.items():
213 return [keys, values, errors]
216 return len(self.
data)
218 def save(self, filename, sortFirst=True):
220 Saves the data to the given filename.
222 @param[in] filename The file path to save to.
223 @param[in] sortFirst Whether to sort the points first, so that their x coordinate is ascending.
226 self.
writeTo(open(filename,
'w'), sortFirst)
228 def writeTo(self, stream, sortFirst=True):
230 Writes the data to the given object.
232 @param[in] stream The object to write to.
233 @param[in] sortFirst Whether to sort the points first, so that their x coordinate is ascending.
239 for x, y
in self.
data.items():
240 stream.write(str(x) +
"\t" + str(y))
242 stream.write(
"\t" + str(self.
errors[x]))