def test_04_execute_pipeline(self):
        try:
            with open("pp_pipeline.py", "w") as pipeline_f:
                pipeline_f.write(TestAutoAIOutputConsumption.pipeline_content)

            import importlib.util

            import lale.operators

            spec = importlib.util.spec_from_file_location(
                "pp_pipeline", "pp_pipeline.py")
            pipeline_module = importlib.util.module_from_spec(spec)
            spec.loader.exec_module(pipeline_module)
            TestAutoAIOutputConsumption.pp_pipeline = pipeline_module.pipeline
            assert isinstance(
                TestAutoAIOutputConsumption.pp_pipeline,
                lale.operators.TrainablePipeline,
            )
        except Exception as e:
            assert False, f"{e}"
        finally:
            try:
                os.remove("pp_pipeline.py")
            except OSError:
                println_pos("Couldn't remove pp_pipeline.py file")
    def test_08_refine_model_with_lale(self):
        from lale import wrap_imported_operators
        from lale.lib.lale import Hyperopt

        wrap_imported_operators()
        try:
            println_pos(
                f"type(prefix_model) {type(TestAutoAIOutputConsumption.prefix_model)}"
            )
            println_pos(f"type(LR) {type(LR)}")
            # This is for classifiers, regressors needs to have different operators & different scoring metrics (e.g 'r2')
            new_model = TestAutoAIOutputConsumption.prefix_model >> (LR | Tree
                                                                     | KNN)
            train_X = TestAutoAIOutputConsumption.training_df.drop(
                ["Risk"], axis=1).values
            train_y = TestAutoAIOutputConsumption.training_df["Risk"].values
            hyperopt = Hyperopt(estimator=new_model,
                                cv=2,
                                max_evals=3,
                                scoring="roc_auc")
            hyperopt_pipelines = hyperopt.fit(train_X, train_y)
            TestAutoAIOutputConsumption.refined_model = (
                hyperopt_pipelines.get_pipeline())
        except Exception as e:
            assert False, f"Exception was thrown during model refinery: {e}"
 def test_01_load_pickled_model(self):
     try:
         TestAutoAIOutputConsumption.model = joblib.load(
             TestAutoAIOutputConsumption.pickled_model_path)
         println_pos(
             f"type(model) {type(TestAutoAIOutputConsumption.model)}")
         println_pos(f"model {str(TestAutoAIOutputConsumption.model)}")
     except Exception as e:
         assert False, f"Exception was thrown during model pickle: {e}"
 def test_03_print_pipeline(self):
     lale_pipeline = TestAutoAIOutputConsumption.model
     wrapped_pipeline = wrap_pipeline_segments(lale_pipeline)
     TestAutoAIOutputConsumption.pipeline_content = wrapped_pipeline.pretty_print(
     )
     assert type(TestAutoAIOutputConsumption.pipeline_content) == str
     assert len(TestAutoAIOutputConsumption.pipeline_content) > 0
     println_pos(
         f'pretty-printed """{TestAutoAIOutputConsumption.pipeline_content}"""'
     )
     assert ("lale.wrap_imported_operators()"
             in TestAutoAIOutputConsumption.pipeline_content)