ml.py 2.81 KB
Newer Older
Chetan Sharma's avatar
Chetan Sharma committed
1
2
3
4
5
6
7
import abc
import numpy as np

from sklearn import linear_model
from matplotlib import pyplot as plt

from models import T_lin, F_lin, T_x_vector, Fy_x_vector
Chetan Sharma's avatar
Chetan Sharma committed
8
from objects import Data, Conditions, EndMill, Prediction
Chetan Sharma's avatar
Chetan Sharma committed
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

class Model(abc.ABC):
    """
    Represents a model that can be trained with one training datum at a time.
    """
    @abc.abstractmethod
    def ingest_datum(self, datum):
        """
        Ingests one datum.
        Args:
            datum : A Data object.
        """
        pass

    def ingest_data(self, data):
        return list(map(data, self.ingest_datum))

    @abc.abstractmethod
    def predict_one(self, conditions):
        """
        Predicts milling forces using the model as it currently is.
        """
        pass

    def predict(self, conditions):
        return list(map(conditions, self.predict_one))


class LinearModel(Model):
    def __init__(self):
        self.training_T_x = list()
        self.training_T_y = list()
        self.training_Fy_x = list()
        self.training_Fy_y = list()
Chetan Sharma's avatar
Chetan Sharma committed
43
44
        self.regressor_T = linear_model.LinearRegression(fit_intercept = False)
        self.regressor_Fy = linear_model.LinearRegression(fit_intercept = False)
Chetan Sharma's avatar
Chetan Sharma committed
45
46
47
48
        self.params = np.array([0,0,0,0])

    def ingest_datum(self, datum):
        # decompose
Chetan Sharma's avatar
Chetan Sharma committed
49
50
        _, _, _, _, _, Ts, Fys = datum.unpack()
        T, Fy = np.median(Ts[1, :]), np.median(Fys[1, :])
Chetan Sharma's avatar
Chetan Sharma committed
51
52

        # get linear coefficients
Chetan Sharma's avatar
Chetan Sharma committed
53
54
55
56
57
        T_x = T_x_vector(datum.conditions())
        Fy_x = Fy_x_vector(datum.conditions())

        print("T", T)
        print("coef times real K_tc", T_x[1] * 858494934.9591976)
Chetan Sharma's avatar
Chetan Sharma committed
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72

        # add to training set
        self.training_T_x.append(T_x)
        self.training_T_y.append(T)
        self.training_Fy_x.append(Fy_x)
        self.training_Fy_y.append(Fy) 
        self.update()

    def update(self):
        # convert force into numpy arrays for convenience
        training_Fy_x = np.array(self.training_Fy_x)
        training_Fy_y = np.array(self.training_Fy_y)

        # calculate best fit from data
        self.regressor_T.fit(self.training_T_x, self.training_T_y)
Chetan Sharma's avatar
Chetan Sharma committed
73
74
        K_tc, K_te = self.regressor_T.coef_
        print(K_tc, K_te)
Chetan Sharma's avatar
Chetan Sharma committed
75
76
77
78
79
80
        self.params[0], self.params[1] = K_tc, K_te

        # transform Fy into a smaller linear problem and fit
        intercepts = training_Fy_x @ np.array([K_tc, K_te, 0, 0])[np.newaxis].T
        training_Fy_y_no_intercepts = training_Fy_y - intercepts
        self.regressor_Fy.fit(training_Fy_x[:, 2:], training_Fy_y_no_intercepts)
Chetan Sharma's avatar
Chetan Sharma committed
81
82
        K_rc, K_re = self.regressor_Fy.coef_
        self.params[0], self.params[1] = K_rc, K_re
Chetan Sharma's avatar
Chetan Sharma committed
83
84
85
86
87

    def predict_one(self, conditions):
        # decompose

        # evaluate
Chetan Sharma's avatar
Chetan Sharma committed
88
89
        T = T_lin(conditions, *self.params[:2])
        F = F_lin(conditions, *self.params)
Chetan Sharma's avatar
Chetan Sharma committed
90
91

        # repack and return
Chetan Sharma's avatar
Chetan Sharma committed
92
        return Prediction(conditions, T, F)
Chetan Sharma's avatar
Chetan Sharma committed
93
94