Example #1
0
def init_classifier_impl(field_code: str, init_script: str):
    if init_script is not None:
        init_script = init_script.strip()

    if not init_script:
        from sklearn import tree as sklearn_tree
        return sklearn_tree.DecisionTreeClassifier()

    from sklearn import tree as sklearn_tree
    from sklearn import neural_network as sklearn_neural_network
    from sklearn import neighbors as sklearn_neighbors
    from sklearn import svm as sklearn_svm
    from sklearn import gaussian_process as sklearn_gaussian_process
    from sklearn.gaussian_process import kernels as sklearn_gaussian_process_kernels
    from sklearn import ensemble as sklearn_ensemble
    from sklearn import naive_bayes as sklearn_naive_bayes
    from sklearn import discriminant_analysis as sklearn_discriminant_analysis
    from sklearn import linear_model as sklearn_linear_model

    eval_locals = {
        'sklearn_linear_model': sklearn_linear_model,
        'sklearn_tree': sklearn_tree,
        'sklearn_neural_network': sklearn_neural_network,
        'sklearn_neighbors': sklearn_neighbors,
        'sklearn_svm': sklearn_svm,
        'sklearn_gaussian_process': sklearn_gaussian_process,
        'sklearn_gaussian_process_kernels': sklearn_gaussian_process_kernels,
        'sklearn_ensemble': sklearn_ensemble,
        'sklearn_naive_bayes': sklearn_naive_bayes,
        'sklearn_discriminant_analysis': sklearn_discriminant_analysis
    }
    return eval_script(
        'classifier init script of field {0}'.format(field_code), init_script,
        eval_locals)
Example #2
0
    def calc_formula(cls, field_code: str, formula: str,
                     depends_on_field_to_value: Dict[str, Any]) -> Any:
        if not formula or not formula.strip():
            return None

        try:
            return eval_script(script_title=f'{field_code} formula',
                               script_code=formula,
                               eval_locals=depends_on_field_to_value)
        except ScriptError as se:
            raise DocumentFieldFormulaError(field_code, formula,
                                            depends_on_field_to_value) from se
Example #3
0
    def calc_formula(cls,
                     field_code: str,
                     formula: str,
                     depends_on_field_to_value: Dict[str, Any],
                     convert_decimals_to_floats: bool = False) -> Any:
        if not formula or not formula.strip():
            return None
        if convert_decimals_to_floats:
            depends_on_field_to_value = {c: TypedField.replace_decimals_with_floats_in_python_value_of_any_type(v)
                                         for c, v in depends_on_field_to_value.items()} \
                if depends_on_field_to_value else {}
        try:
            if settings.DEBUG_SLOW_DOWN_FIELD_FORMULAS_SEC:
                import time
                time.sleep(settings.DEBUG_SLOW_DOWN_FIELD_FORMULAS_SEC)

            return eval_script(script_title=f'{field_code} formula',
                               script_code=formula,
                               eval_locals=depends_on_field_to_value)
        except ScriptError as se:
            raise DocumentFieldFormulaError(field_code, formula,
                                            depends_on_field_to_value) from se