{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "import scanpy as sc\n",
    "\n",
    "import pickle \n",
    "\n",
    "data_location = \"../data/aging_brain/\"\n",
    "\n",
    "young = pickle.load(open(data_location + \"aging_brain_young.pickle\",mode='rb'))\n",
    "old = pickle.load(open(data_location + \"aging_brain_old.pickle\",mode='rb'))\n",
    "filtered = pickle.load(open(data_location + \"aging_brain_filtered.pickle\",mode='rb'))\n",
    "\n",
    "batch_encoding = np.loadtxt(data_location + 'aging_batch_encoding.tsv')\n",
    "batch_encoding = batch_encoding.astype(dtype=bool)\n",
    "\n",
    "young_mask = np.zeros(37069,dtype=bool)\n",
    "old_mask = np.zeros(37069,dtype=bool)\n",
    "\n",
    "young_mask[:young.shape[0]] = True\n",
    "old_mask[young.shape[0]:] = True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys\n",
    "sys.path.append('../src')\n",
    "import tree_reader as tr \n",
    "import lumberjack\n",
    "\n",
    "forest = lumberjack.fit(\n",
    "    young.X,\n",
    "#     old.X,\n",
    "    header=young.var_names,\n",
    "    trees=100,\n",
    "    braids=3,\n",
    "    ifs=700,\n",
    "    ofs=700,\n",
    "    ss=500,\n",
    "    depth=9,\n",
    "    leaves=50,\n",
    "    sfr=0,\n",
    "    norm='l2',\n",
    "# run with variance    \n",
    "    reduce_input=\"true\",\n",
    "    reduce_output=\"false\",\n",
    ")\n",
    "\n",
    "# forest.set_cache(True)\n",
    "# forest.backup(data_location + \"scanpy_cmp_aging_brain_true_l1\")\n",
    "\n",
    "# with open(\"scanpy_cmp_aging_brain_trim_prediction\", mode='bw') as f:\n",
    "#     pickle.dump(forest.old_predictions, f)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.ensemble import ExtraTreesRegressor as Ex\n",
    "\n",
    "iris = np.loadtxt('/Users/bbrener1/battle/rusty_forest_4/data/iris/iris.tsv')\n",
    "\n",
    "model = Ex(min_samples_split=10).fit(iris,iris)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def encode_extra_trees(estimator, data):\n",
    "    node_sample_encoding = np.array(\n",
    "        estimator.decision_path(data)[0].todense()).T\n",
    "    node_label_encoding = np.vstack(\n",
    "        [t.tree_.value[:, :, 0] for t in estimator.estimators_])\n",
    "    return node_sample_encoding, node_label_encoding"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "nse,nle = encode_extra_trees(model,iris)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "dir(model.estimators_[0].tree_)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# type(model.estimators_[0].tree_.children_left)\n",
    "model.estimators_[0].tree_.node_count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# np.mean(young.X,axis=0)\n",
    "# plt.figure()\n",
    "# plt.hist(np.var(young.X,axis=0))\n",
    "# plt.show()\n",
    "plt.figure()\n",
    "plt.hist(np.mean(young.X,axis=0))\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "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.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}

