-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunction_model_worker.py
More file actions
363 lines (295 loc) · 10.8 KB
/
function_model_worker.py
File metadata and controls
363 lines (295 loc) · 10.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
import pandas as pd
from sqlalchemy import create_engine
class CoreFunction:
"""
A class to handle core functions.
"""
def __init__(self, csv_path):
"""
Constructs a CoreFunction object.
Parameters:
csv_path (str): The path to the input CSV file.
"""
self.data_frames = []
try:
self.csv_data = pd.read_csv(csv_path)
except FileNotFoundError:
print(f"There is an issue while reading file {csv_path}")
raise
x_values = self.csv_data["x"]
for name_of_column, data_of_column in self.csv_data.items():
if "x" in name_of_column:
continue
subset = pd.concat([x_values, data_of_column], axis=1)
function = Function.from_dataframe(name_of_column, subset)
self.data_frames.append(function)
def to_sql(self, file_name, suffix):
"""
Converts the CSV data to SQL and saves it to disk.
Parameters:
file_name (str): The name of the output database file.
suffix (str): The suffix to add to the column names in the database.
"""
db_engine = create_engine('sqlite:///output-data/solution.db', echo=False)
csv_data_copied = self.csv_data.copy()
csv_data_copied.columns = [name.capitalize() + suffix for name in csv_data_copied.columns]
csv_data_copied.set_index(csv_data_copied.columns[0], inplace=True)
csv_data_copied.to_sql(
file_name,
db_engine,
if_exists="replace",
index=True,
)
@property
def functions(self):
"""
Returns the data frames.
"""
return self.data_frames
def __iter__(self):
"""
Makes the object iterable.
"""
return CoreFunctionIterator(self)
def __repr__(self):
"""
Returns a string representation of the object.
"""
return f"Contains {len(self.functions)} number of functions"
class CoreFunctionIterator():
"""
An iterator that iterates through the functions in a CoreFunctionObject.
Attributes:
-----------
index : int
The index of the current function in the iteration.
coreFunctionObject : CoreFunctionObject
The CoreFunctionObject containing the functions to iterate over.
Methods:
--------
__next__() -> function:
Returns the next function in the iteration.
"""
def __init__(self, coreFunctionObj):
"""
Initializes the iteration by setting the index to 0 and saving a reference to the
CoreFunctionObject to iterate over.
Parameters:
-----------
coreFunctionObj : CoreFunctionObject
The CoreFunctionObject containing the functions to iterate over.
"""
self.index = 0
self.coreFunctionObject = coreFunctionObj
def __next__(self):
"""
Returns the next function in the iteration, and increments the index.
Raises:
-------
StopIteration
If there are no more functions in the iteration.
Returns:
--------
function
The next function in the iteration.
"""
if self.index < len(self.coreFunctionObject.functions):
valueRequested = self.coreFunctionObject.functions[self.index]
self.index = self.index + 1
return valueRequested
raise StopIteration
class Function:
"""
A class representing a mathematical function.
Attributes:
----------
_name : str
The name of the function.
dataframe : pandas.DataFrame
A dataframe containing the function's X and Y values.
Methods:
-------
locateYBasedOnX(x):
Returns the Y value based on the given X value.
name():
Returns the name of the function.
from_dataframe(name, dataframe):
Creates a new Function object from a pandas DataFrame.
__iter__():
Returns an iterator that iterates over the function's Y values.
__sub__(second):
Subtracts the Y values of two functions and returns the result.
__repr__():
Returns a string representation of the Function object.
"""
def __init__(self, name):
"""
Initialize Function object with given name.
:param name: Name of the function
"""
self._name = name
self.dataframe = pd.DataFrame()
def locateYBasedOnX(self, x):
"""
Returns the Y value based on X value from the data frame. If the value is not found, it raises an IndexError.
:param x: X value
:return: Y value
"""
searchKey = self.dataframe["x"] == x
try:
return self.dataframe.loc[searchKey].iat[0, 1]
except IndexError:
raise IndexError("Y value not found for given X value.")
@property
def name(self):
"""
Returns the name of the function.
"""
return self._name
def __iter__(self):
"""
Returns FunctionIterator object for iteration.
"""
return FunctionIterator(self)
def __sub__(self, second):
"""
Subtracts two data frames and returns the resulting data frame.
:param second: Function object to subtract
:return: Data frame resulting from subtraction
"""
return self.dataframe - second.dataframe
@classmethod
def from_dataframe(cls, name, dataframe):
"""
Returns a Function object from given data frame.
:param name: Name of the function
:param dataframe: Data frame
:return: Function object
"""
dataFunction = cls(name)
dataFunction.dataframe = dataframe
dataFunction.dataframe.columns = ["x", "y"]
return dataFunction
def __repr__(self):
"""
Returns a string representation of the Function object.
"""
return "This is Function for {}".format(self.name)
class IdealFunction(Function):
"""
This class calculates the ideal function based on the passed function data
and training function with a given error tolerance.
Attributes:
training_function (Function): The training function used to calculate the ideal function.
error (float): The error tolerance for the ideal function.
toleranceValue (float): The tolerance factor for the largest deviation of the ideal function.
_tolerance (float): The current tolerance value for the ideal function.
Methods:
determineLargestDeviation: Calculates the largest deviation between the training function
and the ideal function.
tolerance: Property that returns the current tolerance value.
tolerance.setter: Setter for the tolerance value.
toleranceFactor: Property that returns the current tolerance factor.
toleranceFactor.setter: Setter for the tolerance factor.
largestDeviation: Property that returns the largest deviation between the training function
and the ideal function.
"""
def __init__(self, functionData, trainingFunction, error):
"""
Initializes the IdealFunction instance.
Args:
functionData (Function): The function data used to create the ideal function.
trainingFunction (Function): The training function used to calculate the ideal function.
error (float): The error tolerance for the ideal function.
"""
super().__init__(functionData.name)
self.dataframe = functionData.dataframe
self.training_function = trainingFunction
self.error = error
self.toleranceValue = 1
self._tolerance = 1
def determineLargestDeviation(self, idealFunction, trainFunction):
"""
Calculates the largest deviation between the training function and the ideal function.
Args:
idealFunction (Function): The ideal function.
trainFunction (Function): The training function.
Returns:
The largest deviation between the training function and the ideal function.
"""
distance = trainFunction - idealFunction
distance["y"] = distance["y"].abs()
return max(distance["y"])
@property
def tolerance(self):
"""
Returns the current tolerance value for the ideal function.
Returns:
The current tolerance value.
"""
self._tolerance = self.toleranceFactor * self.largestDeviation
return self._tolerance
@tolerance.setter
def tolerance(self, value):
"""
Sets the tolerance value for the ideal function.
Args:
value (float): The new tolerance value.
"""
self._tolerance = value
@property
def toleranceFactor(self):
"""
Returns the current tolerance factor for the ideal function.
Returns:
The current tolerance factor.
"""
return self.toleranceValue
@toleranceFactor.setter
def toleranceFactor(self, value):
"""
Sets the tolerance factor for the ideal function.
Args:
value (float): The new tolerance factor.
"""
self.toleranceValue = value
@property
def largestDeviation(self):
"""
Calculates the largest deviation between the training function and the ideal function.
Returns:
The largest deviation between the training function and the ideal function.
"""
return self.determineLargestDeviation(self, self.training_function)
class FunctionIterator:
"""
Iterator class that returns a dictionary describing a point on a function.
Args:
function (Function): The function to iterate over.
Yields:
dict: A dictionary containing the x and y values of the point.
Raises:
StopIteration: When there are no more points to iterate over.
"""
def __init__(self, function):
"""
Initializes an instance of FunctionIterator.
Args:
function (Function): The function to iterate over.
"""
self._function = function
self._index = 0
def __next__(self):
"""
Returns the next point on the function.
Yields:
dict: A dictionary containing the x and y values of the point.
Raises:
StopIteration: When there are no more points to iterate over.
"""
if self._index < len(self._function.dataframe):
value_requested_series = (self._function.dataframe.iloc[self._index])
point = {"x": value_requested_series.x, "y": value_requested_series.y}
self._index += 1
return point
raise StopIteration