fake_cut.py 2.47 KB
Newer Older
Chetan Sharma's avatar
Chetan Sharma committed
1
from models import T_lin, F_lin, T_lin_full, F_lin_full
Chetan Sharma's avatar
Chetan Sharma committed
2
3
from objects import Conditions, Data
import numpy as np
Chetan Sharma's avatar
Chetan Sharma committed
4
5
import shelve
import os
Chetan Sharma's avatar
Chetan Sharma committed
6
from ml import UnifiedLinearModel
Chetan Sharma's avatar
Chetan Sharma committed
7

Chetan Sharma's avatar
Chetan Sharma committed
8
9
import logging
log = logging.getLogger(__name__)
Chetan Sharma's avatar
Chetan Sharma committed
10

Chetan Sharma's avatar
Chetan Sharma committed
11

Chetan Sharma's avatar
Chetan Sharma committed
12
class Fake_Cut:
Chetan Sharma's avatar
Chetan Sharma committed
13
14
15
16
    """
    Fake cutting process. Returns results using prebaked parameters and specified noise levels.

    Args:
Chetan Sharma's avatar
Chetan Sharma committed
17
18
19
        params: list of params
        T_func: A function to use for torques
        F_func: A function to use for forces
Chetan Sharma's avatar
Chetan Sharma committed
20
21
22
23
        error: list of standard deviations of format [o_T, o_Fy]. Simulates the sensor being "wrong" sometimes.
        error: list of standard deviations of format [o_T, o_Fy]. Simulates the sensor being noisy.
    """

Chetan Sharma's avatar
Chetan Sharma committed
24
    def __init__(self, params, T_func, F_func, error, noise):
Chetan Sharma's avatar
Chetan Sharma committed
25
        self.params = params
Chetan Sharma's avatar
Chetan Sharma committed
26
27
        self.T_func = T_func
        self.F_func = F_func
Chetan Sharma's avatar
Chetan Sharma committed
28
29
        self.error = error
        self.noise = noise
Chetan Sharma's avatar
Chetan Sharma committed
30
        log.info("Initialized fake cut with params: " + str(self.params))
Chetan Sharma's avatar
Chetan Sharma committed
31

Chetan Sharma's avatar
Chetan Sharma committed
32
33
    def face_layer(self, *args, **kwargs):
        log.info("Face cut called")
Chetan Sharma's avatar
Chetan Sharma committed
34
35
        pass

Chetan Sharma's avatar
Chetan Sharma committed
36
37
38
39
40
    def begin_layer(self, *args, **kwargs):
        log.info("Begin cut called")
        pass

    def cut(self, conditions: Conditions, *args, **kwargs):
Chetan Sharma's avatar
Chetan Sharma committed
41
42
43
44
45
46
47
48
        """Performs a simulated cut with noise

        Args:
            conditions (Conditions): Conditions for this fake cut

        Returns:
            Data: A fake data blob with noise
        """
Chetan Sharma's avatar
Chetan Sharma committed
49
        # use prediction as output
Chetan Sharma's avatar
Chetan Sharma committed
50
51
        T = self.T_func(conditions, *self.params)
        _, Fy = self.F_func(conditions, *self.params)
Chetan Sharma's avatar
Chetan Sharma committed
52
53
54
55
56
57
58
59
60
        # add sensor error
        T_error = np.random.normal(T, self.error[0])
        Fy_error = np.random.normal(Fy, self.error[1])
        # add sensor noise
        T_noisy = np.random.normal(T_error, self.noise[0], (100))
        Fy_noisy = np.random.normal(Fy_error, self.noise[1], (100))
        # generate fake times
        t = np.linspace(0, 1, 100)
        # return fake reading
Chetan Sharma's avatar
Chetan Sharma committed
61
62
        data = Data(*conditions.unpack(),
                    np.array([t, T_noisy]).T, np.array([t, Fy_noisy]).T)
Chetan Sharma's avatar
Chetan Sharma committed
63
        return data
Chetan Sharma's avatar
Chetan Sharma committed
64

Chetan Sharma's avatar
Chetan Sharma committed
65
66
    def scale_coefs(self, scale):
        self.params = [scale * p for p in self.params]
Chetan Sharma's avatar
Chetan Sharma committed
67
68


Chetan Sharma's avatar
Chetan Sharma committed
69
70
71
72
73
74
class ReplayCut(Fake_Cut):
    def __init__(self, replay_data, model, T_func, F_func, error, noise):
        self.model = model
        with shelve.open(os.path.join("saved_cuts", "db")) as db:
            data = db[replay_data]
            self.model.ingest_data(data)
Chetan Sharma's avatar
Chetan Sharma committed
75
        super().__init__(self.model.params, T_func, F_func, error, noise)