def test_constructor(self):
     fv = Field()
     assert len(fv) == 0
     fv = Field(self.v)
     assert len(fv) == 1
     fv.append(Field(self.w))
     assert len(fv) == 2
 def test_topandas(self):
     fv = Field(self.v)
     fv.append(Field(self.w))
     print(fv.to_pandas().shape)
     assert fv.to_pandas().shape == (self.domain.get_shape("y")["y"] *
                                     self.domain.get_shape("x")["x"], 2)
Exemple #3
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