Esempio n. 1
0
    def plug_in_body(self, body=u"", conditions=None, cmv=None,
                     ids_path=None, subtree=True, attr=None):
        """Translate the model into a set of "if" statemets in tableau syntax

        `depth` controls the size of indentation. As soon as a value is missing
        that node is returned without further evaluation.

        """

        if cmv is None:
            cmv = []

        if body:
             alternate = u"ELSEIF"
        else:
            if conditions is None:
                conditions = []
            alternate = u"IF"

        children = filter_nodes(self.children, ids=ids_path,
                                subtree=subtree)
        if children:

            field = split(children)
            has_missing_branch = (missing_branch(children) or
                                  none_value(children))
            # the missing is singled out as a special case only when there's
            # no missing branch in the children list
            one_branch = not has_missing_branch or \
                self.fields[field]['optype'] in COMPOSED_FIELDS
            if (one_branch and
                    not self.fields[field]['name'] in cmv):
                body += self.missing_check_code(field, alternate, cmv,
                                                conditions, attr=attr)
                alternate = u"ELSEIF"

            for child in children:
                pre_condition = u""
                post_condition = u""
                if has_missing_branch and child.predicate.value is not None:
                    pre_condition = self.missing_prefix_code(child, field, cmv)
                    post_condition = u")"

                child.split_condition_code(field, conditions,
                                           pre_condition, post_condition)

                body = child.plug_in_body(body, conditions[:], cmv=cmv[:],
                                          ids_path=ids_path, subtree=subtree,
                                          attr=attr)
                del conditions[-1]
        else:
            if attr is None:
                value = value_to_print( \
                    self.output, self.fields[self.objective_id]['optype'])
            else:
                value = getattr(self, attr)
            body += u"%s %s THEN" % (alternate, " AND ".join(conditions))
            body += u" %s\n" % value

        return body
Esempio n. 2
0
    def plug_in_body(self, body=u"", conditions=None, cmv=None,
                     ids_path=None, subtree=True, attr=None):
        """Translate the model into a set of "if" statemets in tableau syntax

        `depth` controls the size of indentation. As soon as a value is missing
        that node is returned without further evaluation.

        """

        if cmv is None:
            cmv = []

        if body:
             alternate = u"ELSEIF"
        else:
            if conditions is None:
                conditions = []
            alternate = u"IF"

        children = filter_nodes(self.children, ids=ids_path,
                                subtree=subtree)
        if children:

            field = split(children)
            has_missing_branch = (missing_branch(children) or
                                  none_value(children))
            # the missing is singled out as a special case only when there's
            # no missing branch in the children list
            one_branch = not has_missing_branch or \
                self.fields[field]['optype'] in COMPOSED_FIELDS
            if (one_branch and
                    not self.fields[field]['name'] in cmv):
                body += self.missing_check_code(field, alternate, cmv,
                                                conditions, attr=attr)
                alternate = u"ELSEIF"

            for child in children:
                pre_condition = u""
                post_condition = u""
                if has_missing_branch and child.predicate.value is not None:
                    pre_condition = self.missing_prefix_code(child, field, cmv)
                    post_condition = u")"

                child.split_condition_code(field, conditions,
                                           pre_condition, post_condition)

                body = child.plug_in_body(body, conditions[:], cmv=cmv[:],
                                          ids_path=ids_path, subtree=subtree,
                                          attr=attr)
                del conditions[-1]
        else:
            if attr is None:
                value = value_to_print( \
                    self.output, self.fields[self.objective_id]['optype'])
            else:
                value = getattr(self, attr)
            body += u"%s %s THEN" % (alternate, " AND ".join(conditions))
            body += u" %s\n" % value

        return body
Esempio n. 3
0
    def plug_in_body(self,
                     depth=1,
                     cmv=None,
                     input_map=False,
                     ids_path=None,
                     subtree=True):
        """Translate the model into a set of "if" python statements.

        `depth` controls the size of indentation. As soon as a value is missing
        that node is returned without further evaluation.

        """
        if cmv is None:
            cmv = []
        body = u""
        term_analysis_fields = []
        item_analysis_fields = []

        children = filter_nodes(self.children, ids=ids_path, subtree=subtree)
        if children:

            # field used in the split
            field = split(children)

            has_missing_branch = (missing_branch(children)
                                  or none_value(children))
            # the missing is singled out as a special case only when there's
            # no missing branch in the children list
            one_branch = not has_missing_branch or \
                self.fields[field]['optype'] in COMPOSED_FIELDS
            if (one_branch and not self.fields[field]['slug'] in cmv):
                body += self.missing_check_code(field, depth, input_map, cmv)

            for child in children:
                field = child.predicate.field
                pre_condition = u""
                # code when missing_splits has been used
                if has_missing_branch and child.predicate.value is not None:
                    pre_condition = self.missing_prefix_code(
                        child, field, input_map, cmv)

                # complete split condition code
                body += child.split_condition_code( \
                    field, depth, input_map, pre_condition,
                    term_analysis_fields, item_analysis_fields)

                # value to be determined in next node
                next_level = child.plug_in_body(depth + 1,
                                                cmv=cmv[:],
                                                input_map=input_map,
                                                ids_path=ids_path,
                                                subtree=subtree)

                body += next_level[0]
                term_analysis_fields.extend(next_level[1])
                item_analysis_fields.extend(next_level[2])
        else:
            value = value_to_print(self.output, "numeric")
            body = u"%sreturn {\"prediction\":%s" % (INDENT * depth, value)
            if hasattr(self, "probability"):
                body += u", \"probability\": %s" % self.probability
            body += u"}\n"

        return body, term_analysis_fields, item_analysis_fields
Esempio n. 4
0
    def plug_in_body(self,
                     depth=0,
                     cmv=None,
                     ids_path=None,
                     subtree=True,
                     body=u"",
                     attr=None):
        """Translate the model into a mysql function

        `depth` controls the size of indentation. As soon as a value is missing
        that node is returned without further evaluation.
        `attr` is used to decide the value returned by the function. When
        it's set to None, the prediction is returned. When set to the
        name of an attribute (e.g. 'confidence') this attribute is returned

        """

        if cmv is None:
            cmv = []

        if body:
            alternate = u",\n%sIF (" % (depth * INDENT)
        else:
            alternate = u"IF ("
        post_missing_body = u""

        children = filter_nodes(self.children, ids=ids_path, subtree=subtree)
        if children:

            # field used in the split
            field = split(children)

            has_missing_branch = (missing_branch(children)
                                  or none_value(children))
            # the missing is singled out as a special case only when there's
            # no missing branch in the children list
            if (not has_missing_branch
                    and not self.fields[field]['name'] in cmv):
                body += self.missing_check_code(field, alternate, cmv, attr)
                depth += 1
                alternate = u",\n%sIF (" % (depth * INDENT)
                post_missing_body += u")"

            for child in children:
                pre_condition = u""
                # code when missing splits has been used
                if has_missing_branch and child.predicate.value is not None:
                    pre_condition = self.missing_prefix_code(child, field, cmv)

                # complete split condition code
                body += child.split_condition_code( \
                    field, alternate, pre_condition)

                depth += 1
                alternate = u",\n%sIF (" % (depth * INDENT)
                body = child.plug_in_body(depth,
                                          cmv=cmv[:],
                                          ids_path=ids_path,
                                          subtree=subtree,
                                          body=body,
                                          attr=attr)
            body += u", NULL))" + post_missing_body
            post_missing_body = u""
        else:
            if attr is None:
                value = value_to_print( \
                    self.output, self.fields[self.objective_id]['optype'])
            else:
                value = getattr(self, attr)
            body += u", %s" % (value)

        return body
Esempio n. 5
0
    def plug_in_body(self, depth=0, cmv=None,
                     ids_path=None, subtree=True, body=u"", attr=None):
        """Translate the model into a mysql function

        `depth` controls the size of indentation. As soon as a value is missing
        that node is returned without further evaluation.
        `attr` is used to decide the value returned by the function. When
        it's set to None, the prediction is returned. When set to the
        name of an attribute (e.g. 'confidence') this attribute is returned

        """

        if cmv is None:
            cmv = []

        if body:
            alternate = u",\n%sIF (" % (depth * INDENT)
        else:
            alternate = u"IF ("
        post_missing_body = u""


        children = filter_nodes(self.children, ids=ids_path,
                                subtree=subtree)
        if children:

            # field used in the split
            field = split(children)

            has_missing_branch = (missing_branch(children) or
                                  none_value(children))
            # the missing is singled out as a special case only when there's
            # no missing branch in the children list
            if (not has_missing_branch and
                    not self.fields[field]['name'] in cmv):
                body += self.missing_check_code(field, alternate, cmv, attr)
                depth += 1
                alternate = u",\n%sIF (" % (depth * INDENT)
                post_missing_body += u")"

            for child in children:
                pre_condition = u""
                # code when missing splits has been used
                if has_missing_branch and child.predicate.value is not None:
                    pre_condition = self.missing_prefix_code(child, field, cmv)

                # complete split condition code
                body += child.split_condition_code( \
                    field, alternate, pre_condition)

                depth += 1
                alternate = u",\n%sIF (" % (depth * INDENT)
                body = child.plug_in_body(depth, cmv=cmv[:],
                                          ids_path=ids_path, subtree=subtree,
                                          body=body, attr=attr)
            body += u", NULL))" + post_missing_body
            post_missing_body = u""
        else:
            if attr is None:
                value = value_to_print( \
                    self.output, self.fields[self.objective_id]['optype'])
            else:
                value = getattr(self, attr)
            body += u", %s" % (value)

        return body
Esempio n. 6
0
    def plug_in_body(self, depth=1, cmv=None, ids_path=None, subtree=True):
        """Translate the model into a set of "if" r statements.

        `depth` controls the size of indentation. If `cmv` (control missing
        values) is set to True then as soon as a value is missing to
        evaluate a predicate the output at that node is returned without
        further evaluation.

        """
        metric = "error" if self.regression else "confidence"
        if cmv is None:
            cmv = []
        body = u""
        term_analysis_fields = []
        item_analysis_fields = []
        field_obj = self.fields[self.objective_id]
        children = filter_nodes(self.children, ids=ids_path,
                                subtree=subtree)

        if children:
            # field used in the split
            field = split(children)

            has_missing_branch = missing_branch(children)
            # the missing is singled out as a special case only when there's
            # no missing branch in the children list
            one_branch = not (has_missing_branch or
                self.fields[field]['optype'] in COMPOSED_FIELDS)
            if (one_branch and
                    not self.fields[field]['dotted'] in cmv):
                body += self.missing_check_code(field, depth, cmv,
                                                metric)

            for child in self.children:
                field = child.predicate.field
                pre_condition = u""
                # code when missing_splits has been used
                if has_missing_branch and field not in COMPOSED_FIELDS \
                        and child.predicate.value is not None:
                    pre_condition = self.missing_prefix_code(child, field, cmv)

                # complete split condition code
                body += child.split_condition_code( \
                    field, depth, pre_condition,
                    term_analysis_fields, item_analysis_fields)

                # value to be determined in next node
                next_level = child.plug_in_body(depth + 1, cmv=cmv[:],
                                                ids_path=ids_path,
                                                subtree=subtree)
                body += next_level[0]
                body += u"%s}\n" % (INDENT * depth)
                term_analysis_fields.extend(next_level[1])
                item_analysis_fields.extend(next_level[2])
        else:
            value = value_to_print(self.output,
                                   self.fields[self.objective_id]['optype'])
            body = u"%sreturn(list(prediction=%s, %s=%s))\n" % \
                  (INDENT * depth,
                   value, metric, self.confidence)
        return body, term_analysis_fields, item_analysis_fields
Esempio n. 7
0
    def plug_in_body(self, depth=1, cmv=None, input_map=False,
                     ids_path=None, subtree=True):
        """Translate the model into a set of "if" python statements.

        `depth` controls the size of indentation. As soon as a value is missing
        that node is returned without further evaluation.

        """
        if cmv is None:
            cmv = []
        body = u""
        term_analysis_fields = []
        item_analysis_fields = []

        children = filter_nodes(self.children, ids=ids_path,
                                subtree=subtree)
        if children:

            # field used in the split
            field = split(children)

            has_missing_branch = (missing_branch(children) or
                                  none_value(children))
            # the missing is singled out as a special case only when there's
            # no missing branch in the children list
            one_branch = not has_missing_branch or \
                self.fields[field]['optype'] in COMPOSED_FIELDS
            if (one_branch and
                not self.fields[field]['slug'] in cmv):
                body += self.missing_check_code(field, depth, input_map, cmv)

            for child in children:
                field = child.predicate.field
                pre_condition = u""
                # code when missing_splits has been used
                if has_missing_branch and child.predicate.value is not None:
                    pre_condition = self.missing_prefix_code(child, field,
                                                             input_map, cmv)

                # complete split condition code
                body += child.split_condition_code( \
                    field, depth, input_map, pre_condition,
                    term_analysis_fields, item_analysis_fields)

                # value to be determined in next node
                next_level = child.plug_in_body(depth + 1,
                                                cmv=cmv[:],
                                                input_map=input_map,
                                                ids_path=ids_path,
                                                subtree=subtree)

                body += next_level[0]
                term_analysis_fields.extend(next_level[1])
                item_analysis_fields.extend(next_level[2])
        else:
            value = value_to_print(self.output, "numeric")
            body = u"%sreturn {\"prediction\":%s" % (INDENT * depth, value)
            if hasattr(self, "probability"):
                body += u", \"probability\": %s" % self.probability
            body += u"}\n"

        return body, term_analysis_fields, item_analysis_fields