-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmake_model_dicts.py
87 lines (69 loc) · 3.29 KB
/
make_model_dicts.py
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
import numpy as np
from scipy.interpolate import interp1d
from math import ceil, floor
import sys
class ModelMaker:
'''
The ModelMaker Class generates different models from 21 given measured data points.
'''
def __init__(self, data):
'''
Initializes a ModelMaker with 20 measured data points. The data points correspond to percentages in steps of 5 starting with 0 and ending at 95.
Argument:
data measured data as a list of floats
'''
if len(data) != 20:
raise ValueError
self.data = data
self.percentages = list(range(0, 100, 5))
self.evaluation_points = [0.5*i for i in range(1, 191)] # points in steps of 0.5
def make_spline(self, kind):
'''
Generates a spline model.
Argument:
kind 'linear' or 'cubic' kind of spline
Returns:
a dictionary with key-value pairs matching distances in cm to percentages in [0,1) representing the model
'''
spline = interp1d(self.percentages, self.data, kind=kind)
evaluation_results = spline(self.evaluation_points)
return self.inverse_funcion(evaluation_results)
def make_polynom(self, degree):
'''
Generates a polynomial model.
Argument:
degree degree of the polynomial, e.g. 1=linear, 3 = cubic
Returns:
a dictionary with key-value pairs matching distances in cm to percentages in [0,1) representing the model
'''
polynom = np.polyfit(self.percentages, self.data, degree)
evaluation_results = np.polyval(polynom, self.evaluation_points)
return self.inverse_funcion(evaluation_results)
def inverse_funcion(self, evaluation_results):
'''
Inverts a function given by dicrete points
Argument:
evaluation_results values that result from the evaluation of the spline/polynom a the evaluation points
Returns:
a dictionary with key-value pairs matching distances in cm to percentages in [0,1) representing the function of a model
'''
inverted = dict()
mini, maxi = ceil(min(evaluation_results)), floor(max(evaluation_results))
# searches for each point in the range interval [mini, maxi] a nearest value in the evaluation_results
for i in range(mini, maxi+1):
# gets evaluation_result with minimal distance to i
inverted[i] = self.evaluation_points[np.abs(evaluation_results - i).argmin()]
return inverted
if __name__ == '__main__':
# 20 measured data points are needed as command line parameters for generating models
if len(sys.argv) != 21:
print("Not enough data")
data = list(map(float, sys.argv[1:])) # makes arguments floats
model_maker = ModelMaker(data)
# generate a python file with 4 objects to save the 4 models
text = "spline_cubic = " + repr(model_maker.make_spline("linear"))
text += "\n" + "spline_lin = " + repr(model_maker.make_spline("cubic"))
text += "\n" + "pol_lin = " + repr(model_maker.make_polynom(1))
text += "\n" + "pol_cubic = " + repr(model_maker.make_polynom(3))
with open("model_dicts.py", "w") as file:
file.write(text)