Esempio n. 1
0
 def transform(self, **Xy):
     """
     Parameter
     ---------
     Xy: dictionary
         parameters for fit and transform
     """
     if conf.KW_SPLIT_TRAIN_TEST in Xy:
         Xy_train, Xy_test = train_test_split(Xy)
         res = self.estimator.fit(**_sub_dict(Xy_train, self.in_args_fit))
         # catch args_transform in ds, transform, store output in a dict
         Xy_out_tr = _as_dict(self.estimator.transform(
                     **_sub_dict(Xy_train, self.in_args_transform)),
                     keys=self.in_args_transform)
         Xy_out_te = _as_dict(self.estimator.transform(**_sub_dict(Xy_test,
                         self.in_args_transform)),
                         keys=self.in_args_transform)
         Xy_out = train_test_merge(Xy_out_tr, Xy_out_te)
     else:
         res = self.estimator.fit(**_sub_dict(Xy, self.in_args_fit))
         # catch args_transform in ds, transform, store output in a dict
         Xy_out = _as_dict(self.estimator.transform(**_sub_dict(Xy,
                                              self.in_args_transform)),
                        keys=self.in_args_transform)
     # update ds with transformed values
     Xy.update(Xy_out)
     return Xy
Esempio n. 2
0
 def transform(self, **Xy):
     """
     Parameter
     ---------
     Xy: dictionary
         parameters for transform
     """
     if conf.KW_SPLIT_TRAIN_TEST in Xy:
         Xy_train, Xy_test = train_test_split(Xy)
         # catch args_transform in ds, transform, store output in a dict
         Xy_out_tr = self.wrapped_node.transform(
             **_sub_dict(Xy_train, self.in_args_transform))
         Xy_out_te = self.wrapped_node.transform(
             **_sub_dict(Xy_test, self.in_args_transform))
         if type(Xy_out_tr) is not dict or type(Xy_out_te) is not dict:
             raise ValueError("%s.transform should return a dictionary" %
                              (self.wrapped_node.__class__.__name__))
         Xy_out = train_test_merge(Xy_out_tr, Xy_out_te)
     else:
         # catch args_transform in ds, transform, store output in a dict
         Xy_out = self.wrapped_node.transform(
             **_sub_dict(Xy, self.in_args_transform))
         if type(Xy_out) is not dict:
             raise ValueError("%s.transform should return a dictionary" %
                              (self.wrapped_node.__class__.__name__))
     return Xy_out
Esempio n. 3
0
 def transform(self, **Xy):
     """
     Parameter
     ---------
     Xy: dictionary
         parameters for transform
     """
     if conf.KW_SPLIT_TRAIN_TEST in Xy:
         Xy_train, Xy_test = train_test_split(Xy)
         # catch args_transform in ds, transform, store output in a dict
         Xy_out_tr = self.wrapped_node.transform(**_sub_dict(
             Xy_train,
             self.in_args_transform))
         Xy_out_te = self.wrapped_node.transform(**_sub_dict(
             Xy_test,
             self.in_args_transform))
         if type(Xy_out_tr) is not dict or type(Xy_out_te) is not dict:
             raise ValueError("%s.transform should return a dictionary"
                              % (self.wrapped_node.__class__.__name__))
         Xy_out = train_test_merge(Xy_out_tr, Xy_out_te)
     else:
         # catch args_transform in ds, transform, store output in a dict
         Xy_out = self.wrapped_node.transform(**_sub_dict(Xy,
                                              self.in_args_transform))
         if type(Xy_out) is not dict:
             raise ValueError("%s.transform should return a dictionary"
                              % (self.wrapped_node.__class__.__name__))
     return Xy_out
Esempio n. 4
0
 def __init__(self, *nodes):
     super(Methods, self).__init__()
     for node in nodes:
         node_cp = copy.deepcopy(node)
         node_cp = node_cp if isinstance(node_cp, BaseNode) else \
             LeafEstimator(node_cp)
         self.add_child(node_cp)
     curr_nodes = self.children
     leaves_key = [l.get_key() for l in self.walk_leaves()]
     curr_nodes_key = [c.get_key() for c in curr_nodes]
     while len(leaves_key) != len(set(leaves_key)) and curr_nodes:
         curr_nodes_state = [c.get_parameters() for c in curr_nodes]
         curr_nodes_next = list()
         for key in set(curr_nodes_key):
             collision_indices = _list_indices(curr_nodes_key, key)
             if len(collision_indices) == 1:  # no collision for this cls
                 continue
             diff_arg_keys = dict_diff(*[curr_nodes_state[i] for i
                                         in collision_indices]).keys()
             for curr_node_idx in collision_indices:
                 if diff_arg_keys:
                     curr_nodes[curr_node_idx].signature_args = \
                         _sub_dict(curr_nodes_state[curr_node_idx],
                                   diff_arg_keys)
                 curr_nodes_next += curr_nodes[curr_node_idx].children
         curr_nodes = curr_nodes_next
         curr_nodes_key = [c.get_key() for c in curr_nodes]
         leaves_key = [l.get_key() for l in self.walk_leaves()]
     leaves_key = [l.get_key() for l in self.walk_leaves()]
     if len(leaves_key) != len(set(leaves_key)):
         raise ValueError("Some methods are identical, they could not be "
                 "differentiated according to their arguments")
Esempio n. 5
0
 def __init__(self, *nodes):
     super(Methods, self).__init__()
     for node in nodes:
         node_cp = copy.deepcopy(node)
         node_cp = NodeFactory.build(node_cp)
         self.add_child(node_cp)
     curr_nodes = self.children
     leaves_key = [l.get_key() for l in self.walk_leaves()]
     curr_nodes_key = [c.get_key() for c in curr_nodes]
     while len(leaves_key) != len(set(leaves_key)) and curr_nodes:
         curr_nodes_state = [c.get_parameters() for c in curr_nodes]
         curr_nodes_next = list()
         for key in set(curr_nodes_key):
             collision_indices = _list_indices(curr_nodes_key, key)
             if len(collision_indices) == 1:  # no collision for this cls
                 continue
             diff_arg_keys = dict_diff(
                 *[curr_nodes_state[i] for i in collision_indices]).keys()
             for curr_node_idx in collision_indices:
                 if diff_arg_keys:
                     curr_nodes[curr_node_idx].signature_args = \
                         _sub_dict(curr_nodes_state[curr_node_idx],
                                   diff_arg_keys)
                 curr_nodes_next += curr_nodes[curr_node_idx].children
         curr_nodes = curr_nodes_next
         curr_nodes_key = [c.get_key() for c in curr_nodes]
         leaves_key = [l.get_key() for l in self.walk_leaves()]
     leaves_key = [l.get_key() for l in self.walk_leaves()]
     if len(leaves_key) != len(set(leaves_key)):
         raise ValueError("Some methods are identical, they could not be "
                          "differentiated according to their arguments")
Esempio n. 6
0
 def transform(self, **Xy):
     """
     Parameter
     ---------
     Xy: dictionary
         parameters for fit and transform
     """
     if conf.KW_SPLIT_TRAIN_TEST in Xy:
         Xy_train, Xy_test = train_test_split(Xy)
         Xy_out = dict()
         # Train fit
         res = self.estimator.fit(**_sub_dict(Xy_train, self.in_args_fit))
         # Train predict
         Xy_out_tr = _as_dict(self.estimator.predict(**_sub_dict(Xy_train,
                                              self.in_args_predict)),
                        keys=self.out_args_predict)
         Xy_out_tr = _dict_suffix_keys(Xy_out_tr,
             suffix=conf.SEP + conf.TRAIN + conf.SEP + conf.PREDICTION)
         Xy_out.update(Xy_out_tr)
         # Test predict
         Xy_out_te = _as_dict(self.estimator.predict(**_sub_dict(Xy_test,
                                              self.in_args_predict)),
                        keys=self.out_args_predict)
         Xy_out_te = _dict_suffix_keys(Xy_out_te,
             suffix=conf.SEP + conf.TEST + conf.SEP + conf.PREDICTION)
         Xy_out.update(Xy_out_te)
         ## True test
         Xy_test_true = _sub_dict(Xy_test, self.out_args_predict)
         Xy_out_true = _dict_suffix_keys(Xy_test_true,
             suffix=conf.SEP + conf.TEST + conf.SEP + conf.TRUE)
         Xy_out.update(Xy_out_true)
     else:
         res = self.estimator.fit(**_sub_dict(Xy, self.in_args_fit))
         # catch args_transform in ds, transform, store output in a dict
         Xy_out = _as_dict(self.estimator.predict(**_sub_dict(Xy,
                                              self.in_args_predict)),
                        keys=self.out_args_predict)
         Xy_out = _dict_suffix_keys(Xy_out,
             suffix=conf.SEP + conf.PREDICTION)
         ## True test
         Xy_true = _sub_dict(Xy, self.out_args_predict)
         Xy_out_true = _dict_suffix_keys(Xy_true,
             suffix=conf.SEP + conf.TRUE)
         Xy_out.update(Xy_out_true)
     return Xy_out
Esempio n. 7
0
    def transform(self, **Xy):
        """
        Parameter
        ---------
        Xy: dictionary
            parameters for fit and transform
        """
        is_fit_predict = False
        is_fit_transform = False
        if (hasattr(self.wrapped_node, "transform") and
                hasattr(self.wrapped_node, "predict")):
            if not self.children:
                # leaf node
                is_fit_predict = True
            else:
                # internal node
                is_fit_transform = True
        elif hasattr(self.wrapped_node, "transform"):
            is_fit_transform = True
        elif hasattr(self.wrapped_node, "predict"):
            is_fit_predict = True

        if is_fit_transform:
            Xy_train, Xy_test = train_test_split(Xy)
            if Xy_train is not Xy_test:
                res = self.wrapped_node.fit(**_sub_dict(Xy_train,
                                                        self.in_args_fit))
                Xy_out_tr = self._wrapped_node_transform(**Xy_train)
                Xy_out_te = self._wrapped_node_transform(**Xy_test)
                Xy_out = train_test_merge(Xy_out_tr, Xy_out_te)
            else:
                res = self.wrapped_node.fit(**_sub_dict(Xy, self.in_args_fit))
                Xy_out = self._wrapped_node_transform(**Xy)
            # update ds with transformed values
            Xy.update(Xy_out)
            return Xy
        elif is_fit_predict:
            Xy_train, Xy_test = train_test_split(Xy)
            if Xy_train is not Xy_test:
                Xy_out = dict()
                res = self.wrapped_node.fit(**_sub_dict(Xy_train,
                                            self.in_args_fit))
                Xy_out_tr = self._wrapped_node_predict(**Xy_train)
                Xy_out_tr = _dict_suffix_keys(
                    Xy_out_tr,
                    suffix=conf.SEP + conf.TRAIN + conf.SEP + conf.PREDICTION)
                Xy_out.update(Xy_out_tr)
                # Test predict
                Xy_out_te = self._wrapped_node_predict(**Xy_test)
                Xy_out_te = _dict_suffix_keys(
                    Xy_out_te,
                    suffix=conf.SEP + conf.TEST + conf.SEP + conf.PREDICTION)
                Xy_out.update(Xy_out_te)
                ## True test
                Xy_test_true = _sub_dict(Xy_test, self.out_args_predict)
                Xy_out_true = _dict_suffix_keys(
                    Xy_test_true,
                    suffix=conf.SEP + conf.TEST + conf.SEP + conf.TRUE)
                Xy_out.update(Xy_out_true)
            else:
                res = self.wrapped_node.fit(**_sub_dict(Xy, self.in_args_fit))
                Xy_out = self._wrapped_node_predict(**Xy)
                Xy_out = _dict_suffix_keys(
                    Xy_out,
                    suffix=conf.SEP + conf.PREDICTION)
                ## True test
                Xy_true = _sub_dict(Xy, self.out_args_predict)
                Xy_out_true = _dict_suffix_keys(
                    Xy_true,
                    suffix=conf.SEP + conf.TRUE)
                Xy_out.update(Xy_out_true)
            return Xy_out
        else:
            raise ValueError("%s should implement either transform or predict"
                             % self.wrapped_node.__class__.__name__)
Esempio n. 8
0
 def _wrapped_node_predict(self, **Xy):
     Xy_out = _as_dict(self.wrapped_node.predict(
         **_sub_dict(Xy, self.in_args_predict)),
         keys=self.out_args_predict)
     return Xy_out
Esempio n. 9
0
 def _wrapped_node_transform(self, **Xy):
     Xy_out = _as_dict(self.wrapped_node.transform(
         **_sub_dict(Xy, self.in_args_transform)),
         keys=self.in_args_transform)
     return Xy_out
Esempio n. 10
0
    def transform(self, **Xy):
        """
        Parameter
        ---------
        Xy: dictionary
            parameters for fit and transform
        """
        is_fit_predict = False
        is_fit_transform = False
        if (hasattr(self.wrapped_node, "transform") and
                hasattr(self.wrapped_node, "predict")):
            if not self.children:
                # leaf node
                is_fit_predict = True
            else:
                # internal node
                is_fit_transform = True
        elif hasattr(self.wrapped_node, "transform"):
            is_fit_transform = True
        elif hasattr(self.wrapped_node, "predict"):
            is_fit_predict = True

        if is_fit_transform:
            Xy_train, Xy_test = train_test_split(Xy)
            if Xy_train is not Xy_test:
                res = self.wrapped_node.fit(**_sub_dict(Xy_train,
                                                        self.in_args_fit))
                Xy_out_tr = self._wrapped_node_transform(**Xy_train)
                Xy_out_te = self._wrapped_node_transform(**Xy_test)
                Xy_out = train_test_merge(Xy_out_tr, Xy_out_te)
            else:
                res = self.wrapped_node.fit(**_sub_dict(Xy, self.in_args_fit))
                Xy_out = self._wrapped_node_transform(**Xy)
            # update ds with transformed values
            Xy.update(Xy_out)
            return Xy
        elif is_fit_predict:
            Xy_train, Xy_test = train_test_split(Xy)
            if Xy_train is not Xy_test:
                Xy_out = dict()
                res = self.wrapped_node.fit(**_sub_dict(Xy_train,
                                            self.in_args_fit))
                Xy_out_tr = self._wrapped_node_predict(**Xy_train)
                Xy_out_tr = _dict_suffix_keys(
                    Xy_out_tr,
                    suffix=conf.SEP + conf.TRAIN + conf.SEP + conf.PREDICTION)
                Xy_out.update(Xy_out_tr)
                # Test predict
                Xy_out_te = self._wrapped_node_predict(**Xy_test)
                Xy_out_te = _dict_suffix_keys(
                    Xy_out_te,
                    suffix=conf.SEP + conf.TEST + conf.SEP + conf.PREDICTION)
                Xy_out.update(Xy_out_te)
                ## True test
                Xy_test_true = _sub_dict(Xy_test, self.out_args_predict)
                Xy_out_true = _dict_suffix_keys(
                    Xy_test_true,
                    suffix=conf.SEP + conf.TEST + conf.SEP + conf.TRUE)
                Xy_out.update(Xy_out_true)
            else:
                res = self.wrapped_node.fit(**_sub_dict(Xy, self.in_args_fit))
                Xy_out = self._wrapped_node_predict(**Xy)
                Xy_out = _dict_suffix_keys(
                    Xy_out,
                    suffix=conf.SEP + conf.PREDICTION)
                ## True test
                Xy_true = _sub_dict(Xy, self.out_args_predict)
                Xy_out_true = _dict_suffix_keys(
                    Xy_true,
                    suffix=conf.SEP + conf.TRUE)
                Xy_out.update(Xy_out_true)
            return Xy_out
        else:
            raise ValueError("%s should implement either transform or predict"
                             % self.wrapped_node.__class__.__name__)
Esempio n. 11
0
 def _wrapped_node_predict(self, **Xy):
     Xy_out = _as_dict(self.wrapped_node.predict(
         **_sub_dict(Xy, self.in_args_predict)),
         keys=self.out_args_predict)
     return Xy_out
Esempio n. 12
0
 def _wrapped_node_transform(self, **Xy):
     Xy_out = _as_dict(self.wrapped_node.transform(
         **_sub_dict(Xy, self.in_args_transform)),
         keys=self.in_args_transform)
     return Xy_out