{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "from yaml import load, Loader\n",
    "\n",
    "import os, sys\n",
    "os.chdir('/home/phyto/Abil/')  # Provide your path here\n",
    "\n",
    "sys.path.insert(0, './abil/')\n",
    "from abil.predict import predict"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open('./examples/configuration/2-phase.yml', 'r') as f:\n",
    "    model_config = load(f, Loader=Loader)\n",
    "\n",
    "predictors = model_config['predictors']\n",
    "d = pd.read_csv(\"./examples/data/training.csv\")\n",
    "species = 'Emiliania huxleyi'\n",
    "d = d[d[species]>0]\n",
    "\n",
    "d.dropna(subset=predictors, inplace=True)\n",
    "d.dropna(subset=species, inplace=True)\n",
    "\n",
    "X_train = d[predictors]\n",
    "y = d[species]\n",
    "X_predict = pd.read_csv(\"./examples/data/prediction.csv\")\n",
    "X_predict.set_index([\"time\", \"depth\", \"lat\", \"lon\"], inplace=True)\n",
    "\n",
    "X_predict = X_predict[X_train.columns]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'R2': 'r2', 'MAE': 'neg_mean_absolute_error', 'RMSE': 'neg_root_mean_squared_error', 'tau': make_scorer(tau_scoring), 'tau_p': make_scorer(tau_scoring_p)}\n",
      "initialized prediction\n",
      "number of models in ensemble:3\n",
      "predicting regressor\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "exporting rf prediction to: /home/phyto/Abil/examples/ModelOutput/rf/predictions/\n",
      "predicting regressor\n",
      "exporting xgb prediction to: /home/phyto/Abil/examples/ModelOutput/xgb/predictions/\n",
      "predicting regressor\n",
      "exporting knn prediction to: /home/phyto/Abil/examples/ModelOutput/knn/predictions/\n",
      "0.04\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[Parallel(n_jobs=2)]: Using backend LokyBackend with 2 concurrent workers.\n",
      "[Parallel(n_jobs=2)]: Done   3 out of   3 | elapsed:    2.1s finished\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "exporting ensemble scores to: /home/phyto/Abil/examples/ModelOutput/ens/scoring/\n",
      "\n",
      "exported MAPIE CI50 prediction to: /home/phyto/Abil/examples/ModelOutput/mapie/predictions/50/Emiliania huxleyi.nc\n",
      "exported MAPIE 95_LL prediction to: /home/phyto/Abil/examples/ModelOutput/mapie/predictions/95_LL/Emiliania huxleyi.nc\n",
      "exported MAPIE 95_HL prediction to: /home/phyto/Abil/examples/ModelOutput/mapie/predictions/95_HL/Emiliania huxleyi.nc\n",
      "finished\n",
      "execution time: 11.210785865783691 seconds\n"
     ]
    }
   ],
   "source": [
    "m = predict(X_train=X_train, y=y, X_predict=X_predict, \n",
    "    model_config=model_config, n_jobs=2)\n",
    "m.make_prediction(prediction_inference=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'R2': 'r2', 'MAE': 'neg_mean_absolute_error', 'RMSE': 'neg_root_mean_squared_error', 'tau': make_scorer(tau_scoring), 'tau_p': make_scorer(tau_scoring_p)}\n",
      "initialized prediction\n",
      "number of models in ensemble:3\n",
      "predicting regressor\n",
      "exporting rf prediction to: /home/phyto/Abil/examples/ModelOutput/rf/predictions/\n",
      "predicting regressor\n",
      "exporting xgb prediction to: /home/phyto/Abil/examples/ModelOutput/xgb/predictions/\n",
      "predicting regressor\n",
      "exporting knn prediction to: /home/phyto/Abil/examples/ModelOutput/knn/predictions/\n",
      "0.04\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[Parallel(n_jobs=2)]: Using backend LokyBackend with 2 concurrent workers.\n",
      "[Parallel(n_jobs=2)]: Done   3 out of   3 | elapsed:    2.1s finished\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "exporting ensemble scores to: /home/phyto/Abil/examples/ModelOutput/ens/scoring/\n",
      "\n",
      "exported MAPIE CI50 prediction to: /home/phyto/Abil/examples/ModelOutput/mapie/predictions/50/Emiliania huxleyi.nc\n",
      "exported MAPIE 95_LL prediction to: /home/phyto/Abil/examples/ModelOutput/mapie/predictions/95_LL/Emiliania huxleyi.nc\n",
      "exported MAPIE 95_HL prediction to: /home/phyto/Abil/examples/ModelOutput/mapie/predictions/95_HL/Emiliania huxleyi.nc\n",
      "finished\n",
      "execution time: 11.940204381942749 seconds\n"
     ]
    }
   ],
   "source": [
    "m = predict(X_train=X_train, y=y, X_predict=X_predict, \n",
    "    model_config=model_config, n_jobs=2)\n",
    "m.make_prediction(prediction_inference=True)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "my-geopandas-env-2",
   "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.4"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
