{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Pipeline\n\nThis example requires full licence, and the program will break if you use the trial licence.\n\nThis example demonstrates how to load data, process it,\ntrain models, and evaluate their performance using a pipeline.\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Installation\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# To install the required package, use the following command:\n# !pip install modeva"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Authentication\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# To get authentication, use the following command: (To get full access please replace the token to your own token)\n# from modeva.utils.authenticate import authenticate\n# authenticate(auth_code='eaaa4301-b140-484c-8e93-f9f633c8bacb')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Import required modules\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "from modeva import DataSet\nfrom modeva import TestSuite\nfrom modeva.models import MoLGBMClassifier\nfrom modeva.automation.pipeline import Pipeline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "----------------------------------------------------------\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "def load_data(name, inactive_features, target_feature, task_type, test_ratio):\n    ds = DataSet(name=name)\n    ds.load(name)\n    ds.reset_preprocess()\n    ds.impute_missing()\n    ds.scale_numerical(method=\"minmax\")\n    ds.encode_categorical(method=\"ordinal\")\n    ds.preprocess()\n    ds.set_inactive_features(features=inactive_features)\n    ds.set_target(feature=target_feature)\n    ds.set_task_type(task_type)\n    ds.set_random_split(test_ratio=test_ratio)\n    return ds\n\ndef train_model(ds):\n    model = MoLGBMClassifier(name=\"LGBM\", max_depth=2, n_estimators=100, verbose=-1)\n    model.fit(ds.train_x, ds.train_y.ravel())\n    \n    model_tuned = MoLGBMClassifier(eta=0.928576,\n                                   max_depth=2,\n                                   linear_tree=False,\n                                   name=\"LGMB-Tuned\",\n                                   verbose=-1)\n    model_tuned.fit(ds.train_x, ds.train_y)\n    return model, model_tuned\n\ndef test_model(ds, model, model_tuned):\n    tsc = TestSuite(ds, models=[model, model_tuned])\n\n    result1 = tsc.compare_accuracy_table(train_dataset=\"train\",\n                                         test_dataset=\"test\",\n                                         metric=(\"AUC\", \"LogLoss\"))\n    result1.plot(figsize=(6.5, 4))\n\n    result2 = tsc.compare_robustness(noise_levels=(0.1, 0.2, 0.3, 0.4),\n                                     perturb_method=\"quantile\", metric=\"AUC\")\n    result2.plot(figsize=(6.5, 4))\n    return result1, result2"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Initialize the pipeline with steps\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "exp = Pipeline(name='pipeline1')\n\nexp.add_step(\n    func=load_data,\n    func_inputs={'name': 'SimuCredit',\n                 \"inactive_features\": (\"Race\", \"Gender\"),\n                 \"target_feature\": \"Status\",\n                 \"task_type\": \"Classification\",\n                 \"test_ratio\": 0.33},\n    name='load_data',\n    save_data=True,\n) # save output\n\nexp.add_step(\n    func=train_model,\n    func_inputs={}, # automatically map from previous step\n    name='train_model', parent='load_data',\n    save_model=True,\n)\n\nexp.add_step(\n    func=test_model,\n    func_inputs={}, # automatically map from previous step\n    name='test_model', parent=['load_data', 'train_model'],\n    save_testsuite=True,\n)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Run the pipeline\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "exp.run()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Save the pipeline results (optional)\np.save()\n"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.11.11"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}