{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# pH prediction using the 88 soils dataset \n\nThe next microbiome example considers a\n`Soil dataset <https://github.com/Leo-Simpson/c-lasso/tree/master/examples/pH_data>`_ .\n\nThe data are generated thanks to a `qiime2 workflow <https://github.com/Leo-Simpson/c-lasso/blob/master/examples/pH_data/qiime2/read%20data.ipynb>`_\nsimilar to `a gneiss tutorial <https://github.com/biocore/gneiss/blob/master/ipynb/88soils/88soils-qiime2-tutorial.ipynb>`_.\n\nThis workflow treat `some files <https://github.com/Leo-Simpson/c-lasso/blob/master/examples/pH_data/qiime2/originals>`_ \ntaken from `gneiss GitHub <https://github.com/biocore/gneiss/tree/master/ipynb/88soils>`_.\n\n\nThe task is to predict pH concentration in the soil from microbial abundance data.\n\nA similar analysis is also done in `Tree-Aggregated Predictive Modeling of Microbiome Data <https://www.biorxiv.org/content/10.1101/2020.09.01.277632v1>`_.\n `on another dataset <https://royalsocietypublishing.org/doi/full/10.1098/rspb.2014.1988>`_\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import sys, os\nfrom os.path import join, dirname, abspath\n\nclasso_dir = dirname(dirname(abspath(\"__file__\")))\nsys.path.append(classo_dir)\nfrom classo import classo_problem\nimport numpy as np\nimport pandas as pd"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Load data\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "t = pd.read_csv(\"pH_data/qiime2/news/table.csv\", index_col=0)\nmetadata = pd.read_table(\n    \"pH_data/qiime2/originals/88soils_modified_metadata.txt\", index_col=0\n)\ny_uncent = metadata[\"ph\"].values\n\n\nX = t.values\nlabel = t.columns\n\n\n# second option to load the data\n# import scipy.io as sio\n# pH = sio.loadmat(\"pH_data/matlab/pHData.mat\")\n# tax = sio.loadmat(\"pH_data/matlab/taxTablepHData.mat\")[\"None\"][0]\n# X, y_uncent = pH[\"X\"], pH[\"Y\"].T[0]\n# label = None\n\ny = y_uncent - np.mean(y_uncent)  # Center Y\nprint(X.shape)\nprint(y.shape)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Set up c-lassso problem\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "problem = classo_problem(X, y, label=label)\n\nproblem.model_selection.StabSelparameters.method = \"lam\"\nproblem.model_selection.PATH = True\nproblem.model_selection.LAMfixed = True\nproblem.model_selection.PATHparameters.n_active = X.shape[1] + 1"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Solve for R1\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "problem.formulation.concomitant = False\nproblem.solve()\nprint(problem, problem.solution)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Solve for R2\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "problem.formulation.huber = True\nproblem.solve()\nprint(problem, problem.solution)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Solve for R3\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "problem.formulation.concomitant = True\nproblem.formulation.huber = False\nproblem.solve()\nprint(problem, problem.solution)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Solve for R4\nRemark : we reset the numerical method here,\nbecause it has been automatically set to '\u00a8Path-Alg'\nfor previous computations, but for R4, \"DR\" is much better\nas explained in the documentation, R4 \"Path-Alg\" is a method for fixed lambda\nbut is (paradoxically) bad to compute the lambda-path\nbecause of the absence of possible warm-start in this method\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "problem.model_selection.PATHparameters.numerical_method = \"DR\"\nproblem.formulation.huber = True\nproblem.solve()\nprint(problem, problem.solution)"
      ]
    }
  ],
  "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.9.1"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}