Exemplo n.º 1
0
    def test_subs(self):
        fvw = SymField([self.v, self.w])
        fxy = SymField([self.x, self.y])

        assert len(fvw - fxy) == 2
        assert isinstance(fvw - fxy, SymField)
        assert isinstance(fvw - 2, SymField)
Exemplo n.º 2
0
    def __init__(self):
        self.X_operator = lambda Field, regressors: Field
        self.y_operator = lambda Field: Field

        self.domain = Domain()

        self.field = Field()
        self.regressors = Field()
        self.sym_regressors = SymField()
        self.sym_field = SymField()
Exemplo n.º 3
0
 def test_constructor(self):
     fv = SymField()
     assert len(fv) == 0
     fv = SymField(self.sym_v)
     assert len(fv) == 1
     fv.append(SymField(self.sym_x))
     assert len(fv) == 2
     fv.append(self.sym_x)
     assert len(fv) == 3
Exemplo n.º 4
0
    def __mul__(self, other):
        if isinstance(other, Poly):
            return Poly(other.polynomial_order + self.polynomial_order)

        # in this case the operation over fields is not element by element; it meshes all so it should change.
        elif isinstance(other, Field):
            return Field(self.successive_multiplications(other.data))
        elif isinstance(other, SymField):
            return SymField(self.successive_multiplications(other.data))
        else:
            return Operator.__mul__(self, other)
Exemplo n.º 5
0
 def sym_var_transform(self, X, y='deprecated', copy=None):
     if isinstance(X, SymVariable):
         X = SymField(X)
     # if isinstance(X, Variable):
     #     X = Field(X)
     if isinstance(X, SymField):
         new_field = X * 1
         if self.with_mean:
             new_field = new_field - self.mean_
         if self.with_std:
             new_field = new_field / self.scale_
         return new_field
Exemplo n.º 6
0
 def sym_var_inverse_transform(self, X, copy=None):
     if isinstance(X, SymVariable):
         X = SymField(X)
     if isinstance(X, Variable):
         X = Field(X)
     if isinstance(X, (SymField, Field)):
         new_field = X * 1
         if self.with_std:
             new_field = new_field * self.scale_
         if self.with_mean:
             new_field = new_field + self.mean_
         return new_field
Exemplo n.º 7
0
    def test_dot(self):
        fvw = SymField([self.v, self.w])
        fxy = SymField([self.x, self.y])

        assert len(fvw.dot(fxy)) == 1
        assert isinstance(fvw.dot(fxy), SymVariable)
        assert str(fvw.dot(fxy)) == "v(x, y)*x(x) + w(x, y)*y(y)".replace(
            ' ', '')
Exemplo n.º 8
0
    def successive_multiple_delays(self, other):
        if isinstance(other, (Variable)):
            temp_other = Field([other])
        elif isinstance(other, SymVariable):
            temp_other = SymField([other])
        else:
            temp_other = other

        delayed_vars = []
        for ax_name, delays_list in self.delays_dict.items():
            for var in temp_other.data:
                for delay in delays_list:
                    delayed_vars.append(
                        Delay(axis_name=ax_name, delay=delay) * var)

        return delayed_vars
Exemplo n.º 9
0
 def __mul__(self, other):
     if isinstance(other, Variable):
         return self.var_operator_func(other)
     elif isinstance(other, SymVariable):
         return self.sym_var_operator_func(other)
     elif isinstance(other, Field):
         return Field([self * var for var in other.data])
     elif isinstance(other, SymField):
         return SymField([self * sym_var for sym_var in other.data])
     elif isinstance(other, Domain):
         return self.domain_operator_func(other)
     elif isinstance(other, Operator):
         o = Operator()
         o.var_operator_func = lambda var: self.var_operator_func(
             other.var_operator_func(var))
         o.sym_var_operator_func = lambda sym_var: self.sym_var_operator_func(
             other.sym_var_operator_func(sym_var))
         o.backward_lag = other.backward_lag + self.backward_lag  # TODO: not necessarily will always work, rethink
         o.forward_lag = other.forward_lag + self.forward_lag
         return o
Exemplo n.º 10
0
 def test_simplify(self):
     fvw = SymField([self.v, self.w])
     print(fvw)
     fvw.simplify()
     print(fvw)
Exemplo n.º 11
0
 def test_evaluate(self):
     fvw = SymField([self.v, self.w])
     assert all(fvw.evaluate({"x": 0, "y": 0}) == np.ones(2))
Exemplo n.º 12
0
 def test_matmul(self):
     fvw = SymField([self.v, self.w])
     a = fvw.matmul(np.array([[0, 1], [0, 1]]))
     assert len(a) == 2
     assert isinstance(a, SymField)
     assert str(a) == '[0, 1.0*v(x,y)+1.0*w(x,y)]'
Exemplo n.º 13
0
 def sym_var_operator_func(self, sym_var):
     return SymField(self.successive_multiplications([sym_var]))
Exemplo n.º 14
0
    def test_add(self):
        fvw = SymField([self.v, self.w])
        fxy = SymField([self.x, self.y])

        assert len(fvw + fxy) == 2
        assert isinstance(fvw + fxy, SymField)
Exemplo n.º 15
0
    def test_str(self):
        fvw = SymField([self.v, self.w])
        fxy = SymField([self.x, self.y])

        assert str(fvw) == "[v(x,y), w(x,y)]"
        assert str(fxy) == "[x(x), y(y)]"
Exemplo n.º 16
0
 def get_Xy_eq(self):
     X = self.get_X()
     X = SymField([SymVariable(x.name, SymVariable.get_init_info_from_variable(x)[1], x.domain) for x in X.data])
     Y = self.get_y()
     Y = SymField([SymVariable(y.name, SymVariable.get_init_info_from_variable(y)[1], y.domain) for y in Y.data])
     return X, Y
Exemplo n.º 17
0
 def filter_ysymvars_in_Xsymvars(y_field, x_field):  # type: (SymField, SymField) -> SymField
     y_sym_expressions = [str(sym_var) for sym_var in y_field.data]
     return SymField([symvar for symvar in x_field.data if str(symvar) not in y_sym_expressions])
Exemplo n.º 18
0
 def sym_var_operator_func(self, sym_var):
     return SymField(self.successive_multiple_derivatives(sym_var))
Exemplo n.º 19
0
class DataManager:
    def __init__(self):
        self.X_operator = lambda Field, regressors: Field
        self.y_operator = lambda Field: Field

        self.domain = Domain()

        self.field = Field()
        self.regressors = Field()
        self.sym_regressors = SymField()
        self.sym_field = SymField()

    def set_domain(self, domain_info=None):
        if domain_info is None:
            # in case none is passed then it will get the maximum domain from the variables of the field.
            self.field.set_domain()
            self.domain = self.field.domain
        elif isinstance(domain_info, dict):
            if ("lower_limits_dict" in domain_info.keys()) and ("upper_limits_dict" in domain_info.keys()) and \
                    ("step_width_dict" in domain_info.keys()):
                lower_limits_dict = domain_info["lower_limits_dict"]
                upper_limits_dict = domain_info["upper_limits_dict"]
                step_width_dict = domain_info["step_width_dict"]
            elif all([isinstance(element, np.ndarray) for element in domain_info.values()]):
                lower_limits_dict = {}
                upper_limits_dict = {}
                step_width_dict = {}
                for axis_name, range_vals in domain_info.items():
                    lower_limits_dict[axis_name] = range_vals[0]
                    upper_limits_dict[axis_name] = range_vals[-1]
                    step_width_dict[axis_name] = range_vals[1] - range_vals[0]
            else:
                Exception("imput should be or dict of ranges or dict of limits and steps")
            self.domain = Domain(lower_limits_dict, upper_limits_dict, step_width_dict)
        elif isinstance(domain_info, Domain):
            self.domain = copy.deepcopy(domain_info)

    def add_variables(self, variables):  # type:((list| Variable)) -> None
        self.field.append(variables)
        self.sym_field.append(variables)

    def add_field(self, field):
        self.field.append(field)
        self.sym_field.append(field)

    def add_regressors(self, regressors):  # : (Variable, Field)
        self.regressors.append(regressors)
        self.sym_regressors.append(regressors)

    def set_X_operator(self, operator):
        self.X_operator = operator

    def set_y_operator(self, operator):
        self.y_operator = operator

    @staticmethod
    def filter_yvars_in_Xvars(y_field, x_field):  # type: (Field, Field) -> Field
        y_var_names = [var.get_full_name() for var in y_field.data]
        return Field([var for var in x_field.data if var.get_full_name() not in y_var_names])

    @staticmethod
    def filter_ysymvars_in_Xsymvars(y_field, x_field):  # type: (SymField, SymField) -> SymField
        y_sym_expressions = [str(sym_var) for sym_var in y_field.data]
        return SymField([symvar for symvar in x_field.data if str(symvar) not in y_sym_expressions])

    def get_X_sym(self, split_operator=Identity()):
        """
        gets the simbolic expression of X
        :return:
        """
        sym_X = self.X_operator(split_operator * self.sym_field,
                                split_operator * self.sym_regressors if self.sym_regressors != [] else self.sym_regressors)

        return self.filter_ysymvars_in_Xsymvars(self.get_y_sym(split_operator), x_field=sym_X)

    def get_y_sym(self, split_operator=Identity()):
        """
        gets the simbolic expression of y
        :return:
        """
        return self.y_operator(split_operator * self.sym_field)

    def get_X(self, split_operator=Identity()):
        X = self.X_operator(split_operator * self.field,
                            split_operator * self.regressors if self.sym_regressors != [] else self.regressors)
        return self.filter_yvars_in_Xvars(self.get_y(split_operator), x_field=X)

    def get_y(self, split_operator=Identity()):
        return self.y_operator(split_operator * self.field)

    def get_X_dframe(self, split_operator=Identity()):
        return self.get_X(split_operator).to_pandas()

    def get_y_dframe(self, split_operator=Identity()):
        return self.get_y(split_operator).to_pandas()

    def get_Xy_eq(self):
        X = self.get_X()
        X = SymField([SymVariable(x.name, SymVariable.get_init_info_from_variable(x)[1], x.domain) for x in X.data])
        Y = self.get_y()
        Y = SymField([SymVariable(y.name, SymVariable.get_init_info_from_variable(y)[1], y.domain) for y in Y.data])
        return X, Y