{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!ls {data_location}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Predictability of Features and Samples\n",
    "\n",
    "In this notebook we'll ask a basic question:\n",
    "\n",
    "How predictable are different features and samples in general?\n",
    "\n",
    "PCA makes the best possible orthogonal representation of a dataset using up to n different linear components, so it's the platonic ideal of how well a dataset is represented by a multivariate normal distribution with some covariance matrix. \n",
    "\n",
    "So let's ask ourselves, how much information can we recover from various scRNAseq datasests if we project them into a lower-dimensional subspace using PCA and then recover them? "
   ]
  },
  {
   "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",
    "\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\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",
    "\n",
    "# filter_mask = np.loadtxt(data_location + \"filtered_feature_mask.txt\").astype(dtype=bool)\n",
    "\n",
    "# young_filtered = young.X.T[filter_mask].T\n",
    "# old_filtered = old.X.T[filter_mask].T"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.decomposition import PCA\n",
    "\n",
    "model = PCA(n_components=25).fit(young.X)\n",
    "transformed = model.transform(young.X)\n",
    "recovered = model.inverse_transform(transformed)\n",
    "\n",
    "centered = young.X - np.mean(young.X,axis=0)\n",
    "null_squared_residual = np.power(centered,2)\n",
    "\n",
    "pca_residual = young.X - recovered\n",
    "pca_squared_residual = np.power(pca_residual,2)\n",
    "\n",
    "pca_recovered_per_sample = np.sum(pca_squared_residual,axis=1)\n",
    "pca_recovered_fraction_per_sample = np.sum(pca_squared_residual,axis=1) / np.sum(null_squared_residual,axis=1)\n",
    "print(np.sum(null_squared_residual))\n",
    "print(np.sum(pca_squared_residual))\n",
    "\n",
    "print(f\"Remaining variance:{(np.sum(pca_squared_residual) / np.sum(null_squared_residual))}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0: Remaining variance:1.0\n",
      "1: Remaining variance:0.8692488670349121\n",
      "2: Remaining variance:0.758476972579956\n",
      "3: Remaining variance:0.6877858638763428\n",
      "4: Remaining variance:0.6321733593940735\n",
      "5: Remaining variance:0.6113985180854797\n",
      "6: Remaining variance:0.5953540205955505\n",
      "7: Remaining variance:0.5854095816612244\n",
      "8: Remaining variance:0.5772488117218018\n",
      "9: Remaining variance:0.5703732967376709\n",
      "10: Remaining variance:0.5647286772727966\n",
      "11: Remaining variance:0.5592596530914307\n",
      "12: Remaining variance:0.5543373227119446\n",
      "13: Remaining variance:0.5496830344200134\n",
      "14: Remaining variance:0.5452210307121277\n",
      "15: Remaining variance:0.5414120554924011\n",
      "16: Remaining variance:0.5379151105880737\n",
      "17: Remaining variance:0.534825325012207\n",
      "18: Remaining variance:0.5319852828979492\n",
      "19: Remaining variance:0.5294577479362488\n",
      "20: Remaining variance:0.5269657373428345\n",
      "21: Remaining variance:0.5245851874351501\n",
      "22: Remaining variance:0.5222638845443726\n",
      "23: Remaining variance:0.5200920104980469\n",
      "24: Remaining variance:0.5179393887519836\n",
      "25: Remaining variance:0.51596599817276\n",
      "26: Remaining variance:0.5141041278839111\n",
      "27: Remaining variance:0.5123263001441956\n",
      "28: Remaining variance:0.5106368660926819\n",
      "29: Remaining variance:0.5090409517288208\n",
      "30: Remaining variance:0.5074819326400757\n",
      "31: Remaining variance:0.5059599876403809\n",
      "32: Remaining variance:0.5045190453529358\n",
      "33: Remaining variance:0.5030720233917236\n",
      "34: Remaining variance:0.5015971064567566\n",
      "35: Remaining variance:0.5002066493034363\n",
      "36: Remaining variance:0.4988354742527008\n",
      "37: Remaining variance:0.49738404154777527\n",
      "38: Remaining variance:0.4960356056690216\n",
      "39: Remaining variance:0.49468255043029785\n",
      "40: Remaining variance:0.4933839738368988\n",
      "41: Remaining variance:0.49206802248954773\n",
      "42: Remaining variance:0.4907509982585907\n",
      "43: Remaining variance:0.48943570256233215\n",
      "44: Remaining variance:0.48806801438331604\n",
      "45: Remaining variance:0.4867989122867584\n",
      "46: Remaining variance:0.48557090759277344\n",
      "47: Remaining variance:0.4841751158237457\n",
      "48: Remaining variance:0.48292937874794006\n",
      "49: Remaining variance:0.48166894912719727\n",
      "50: Remaining variance:0.48036813735961914\n",
      "51: Remaining variance:0.47924062609672546\n",
      "52: Remaining variance:0.47791290283203125\n",
      "53: Remaining variance:0.47663551568984985\n",
      "54: Remaining variance:0.47536909580230713\n",
      "55: Remaining variance:0.47409531474113464\n",
      "56: Remaining variance:0.4728451073169708\n",
      "57: Remaining variance:0.4717200994491577\n",
      "58: Remaining variance:0.4704384207725525\n",
      "59: Remaining variance:0.4691973030567169\n",
      "60: Remaining variance:0.46800121665000916\n",
      "61: Remaining variance:0.4667389392852783\n",
      "62: Remaining variance:0.4655531644821167\n",
      "63: Remaining variance:0.464366614818573\n",
      "64: Remaining variance:0.46310940384864807\n",
      "65: Remaining variance:0.4619869589805603\n",
      "66: Remaining variance:0.46072983741760254\n",
      "67: Remaining variance:0.4595184326171875\n",
      "68: Remaining variance:0.4583347737789154\n",
      "69: Remaining variance:0.45720401406288147\n",
      "70: Remaining variance:0.4560345411300659\n",
      "71: Remaining variance:0.4548739194869995\n",
      "72: Remaining variance:0.4536670744419098\n",
      "73: Remaining variance:0.45255279541015625\n",
      "74: Remaining variance:0.45133182406425476\n",
      "75: Remaining variance:0.4501909017562866\n",
      "76: Remaining variance:0.4491017758846283\n",
      "77: Remaining variance:0.4478946328163147\n",
      "78: Remaining variance:0.44674184918403625\n",
      "79: Remaining variance:0.4456769824028015\n",
      "80: Remaining variance:0.4444604218006134\n",
      "81: Remaining variance:0.4433758854866028\n",
      "82: Remaining variance:0.4423079490661621\n",
      "83: Remaining variance:0.44106853008270264\n",
      "84: Remaining variance:0.4400748312473297\n",
      "85: Remaining variance:0.4389554262161255\n",
      "86: Remaining variance:0.43786871433258057\n",
      "87: Remaining variance:0.43680399656295776\n",
      "88: Remaining variance:0.4356449246406555\n",
      "89: Remaining variance:0.4346434772014618\n",
      "90: Remaining variance:0.4334925711154938\n",
      "91: Remaining variance:0.4324246942996979\n",
      "92: Remaining variance:0.4314056634902954\n",
      "93: Remaining variance:0.4302714765071869\n",
      "94: Remaining variance:0.4292720556259155\n",
      "95: Remaining variance:0.4281669557094574\n",
      "96: Remaining variance:0.42712152004241943\n",
      "97: Remaining variance:0.4260901212692261\n",
      "98: Remaining variance:0.4250199794769287\n",
      "99: Remaining variance:0.4241145849227905\n",
      "100: Remaining variance:0.42301565408706665\n",
      "101: Remaining variance:0.42199984192848206\n",
      "102: Remaining variance:0.42106813192367554\n",
      "103: Remaining variance:0.4200410842895508\n",
      "104: Remaining variance:0.4190862774848938\n",
      "105: Remaining variance:0.4181439280509949\n",
      "106: Remaining variance:0.4171839654445648\n"
     ]
    },
    {
     "ename": "KeyboardInterrupt",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mKeyboardInterrupt\u001b[0m                         Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-4-3bb2d4d58296>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m     12\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     13\u001b[0m     \u001b[0mrecovered_residual\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0myoung\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mX\u001b[0m \u001b[0;34m-\u001b[0m \u001b[0mrecovered\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 14\u001b[0;31m     \u001b[0mrecovered_squared_residual\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpower\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mrecovered_residual\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     15\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     16\u001b[0m     \u001b[0mpca_recovered_per_sample\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msum\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mrecovered_squared_residual\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0maxis\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mKeyboardInterrupt\u001b[0m: "
     ]
    }
   ],
   "source": [
    "from sklearn.decomposition import PCA\n",
    "\n",
    "centered = young.X - np.mean(young.X,axis=0)\n",
    "null_squared_residual = np.power(centered,2)\n",
    "\n",
    "fractions = []\n",
    "\n",
    "for i in range(100):\n",
    "    model = PCA(n_components=i).fit(young.X)\n",
    "    transformed = model.transform(young.X)\n",
    "    recovered = model.inverse_transform(transformed)\n",
    "\n",
    "    recovered_residual = young.X - recovered\n",
    "    recovered_squared_residual = np.power(recovered_residual,2)\n",
    "\n",
    "    pca_recovered_per_sample = np.sum(recovered_squared_residual,axis=1)\n",
    "    pca_recovered_fraction_per_sample = np.sum(recovered_squared_residual,axis=1) / np.sum(null_squared_residual,axis=1)\n",
    "#     print(np.sum(null_squared_residual))\n",
    "#     print(np.sum(recovered_squared_residual))\n",
    "\n",
    "    fraction = np.sum(recovered_squared_residual) / np.sum(null_squared_residual)\n",
    "    fractions.append(fraction)\n",
    "    print(f\"{i}: Remaining variance:{fraction}\")\n",
    "\n",
    "# centered = young_filtered - np.mean(young_filtered,axis=0)\n",
    "# null_squared_residual = np.power(centered,2)\n",
    "\n",
    "# fractions = []\n",
    "\n",
    "# for i in range(351):\n",
    "#     model = PCA(n_components=i).fit(young_filtered)\n",
    "#     transformed = model.transform(young_filtered)\n",
    "#     recovered = model.inverse_transform(transformed)\n",
    "\n",
    "#     recovered_residual = young_filtered - recovered\n",
    "#     recovered_squared_residual = np.power(recovered_residual,2)\n",
    "\n",
    "#     pca_recovered_per_sample = np.sum(recovered_squared_residual,axis=1)\n",
    "#     pca_recovered_fraction_per_sample = np.sum(recovered_squared_residual,axis=1) / np.sum(null_squared_residual,axis=1)\n",
    "# #     print(np.sum(null_squared_residual))\n",
    "# #     print(np.sum(recovered_squared_residual))\n",
    "\n",
    "#     fraction = np.sum(recovered_squared_residual) / np.sum(null_squared_residual)\n",
    "#     fractions.append(fraction)\n",
    "#     print(f\"{i}: Remaining variance:{fraction}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 100516 characters]\n",
      "text/plain": [
       "<Figure size 1800x1200 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "No handles with labels found to put in legend.\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 90472 characters]\n",
      "text/plain": [
       "<Figure size 1800x1200 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.figure()\n",
    "plt.title(\"Fraction of Variance Unexplained, PCA\")\n",
    "plt.plot(fractions[:60],label=\"PCA FVU\")\n",
    "plt.plot([0,60],[.55,.55],\"--\",label=\"Forest FVU\")\n",
    "plt.xlabel(\"Number of PCs\")\n",
    "plt.ylabel(\"FVU\")\n",
    "plt.legend()\n",
    "plt.show()\n",
    "\n",
    "fractions = np.array(fractions)\n",
    "diff = fractions[:-1]-fractions[1:]\n",
    "\n",
    "plt.figure()a\n",
    "plt.title(\"Fraction of Variance Explained per PC\")\n",
    "plt.plot(diff[:60])\n",
    "plt.xlabel(\"PCs\")\n",
    "plt.ylabel(\"Added Power\")\n",
    "plt.legend()\n",
    "plt.show()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "# for i,pc in enumerate(transformed.T):\n",
    "#     plt.figure()\n",
    "#     plt.title(i)\n",
    "#     plt.scatter(*young.obsm[\"X_umap\"].T,c=pc,s=3,alpha=.4,cmap='bwr',vmin=-20,vmax=20)\n",
    "#     plt.colorbar()\n",
    "#     plt.show()\n",
    "\n",
    "# f1 = \"Ctsd\"\n",
    "# f2 = \"H2-Ab1\"\n",
    "\n",
    "# f1_index = forest.truth_dictionary.feature_dictionary[f1]\n",
    "# f2_index = forest.truth_dictionary.feature_dictionary[f2]\n",
    "\n",
    "# for i,component in enumerate(model.components_):\n",
    "#     print(f\"{i}: {f1}:{component[f1_index]},{f2}:{component[f2_index]}\")\n",
    "\n",
    "# plt.figure()\n",
    "# plt.scatter(model.components_[:,f1_index],model.components_[:,f2_index])\n",
    "# plt.plot([.2,-.2],[-.2,.2],color='red')\n",
    "# plt.show()\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "feature_null = np.sum(null_squared_residual,axis=0) + 1\n",
    "feature_absolute_null = np.sum(np.abs(centered),axis=0) + 1\n",
    "sample_null = np.sum(null_squared_residual,axis=1) + 1\n",
    "sample_absolute_null = np.sum(np.abs(centered),axis=1) + 1\n",
    "\n",
    "pca_feature_error = np.sum(pca_squared_residual,axis=0) + 1\n",
    "pca_feature_remaining = pca_feature_error/feature_null\n",
    "\n",
    "pca_absolute_feature_error = np.sum(recovered_residual)\n",
    "\n",
    "pca_sample_error = np.sum(pca_squared_residual,axis=1) + 1\n",
    "pca_sample_remaining = pca_sample_error / sample_null\n",
    "\n",
    "\n",
    "plt.figure()\n",
    "plt.title(\"Fraction of Variance Unexplained, Per Feature\")\n",
    "plt.hist(pca_feature_remaining,bins=50)\n",
    "plt.ylabel(\"Frequency\")\n",
    "plt.xlabel(\"Fraction of Variance Unexplained\")\n",
    "plt.show()\n",
    "\n",
    "plt.figure()\n",
    "plt.title(\"Fraction of Variance Unexplained, Per Sample\")\n",
    "plt.hist(pca_sample_remaining,bins=50)\n",
    "plt.ylabel(\"Frequency\")\n",
    "plt.xlabel(\"Fraction of Variance Unexplained\")\n",
    "plt.show()\n",
    "\n",
    "print(f\"PCA Variance Unexplained:{np.sum(recovered_squared_residual)/np.sum(null_squared_residual)}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# SUM OF ABSOLUTE DEVIATIONS VERSION\n",
    "\n",
    "# feature_null = np.sum(np.abs(centered),axis=0) + 1\n",
    "# sample_null = np.sum(np.abs(centered),axis=1) + 1\n",
    "\n",
    "# pca_feature_error = np.sum(np.abs(recovered_residual),axis=0) + 1\n",
    "# pca_feature_remaining = pca_feature_error/feature_null\n",
    "\n",
    "# pca_sample_error = np.sum(np.abs(recovered_residual),axis=1) + 1\n",
    "# pca_sample_remaining = pca_sample_error / sample_null\n",
    "\n",
    "\n",
    "# plt.figure()\n",
    "# plt.title(\"Fraction of Variance Unexplained, Per Feature\")\n",
    "# plt.hist(pca_feature_remaining,bins=50)\n",
    "# plt.ylabel(\"Frequency\")\n",
    "# plt.xlabel(\"Fraction of Variance Unexplained\")\n",
    "# plt.show()\n",
    "\n",
    "# plt.figure()\n",
    "# plt.title(\"Fraction of Variance Unexplained, Per Sample\")\n",
    "# plt.hist(pca_sample_remaining,bins=50)\n",
    "# plt.ylabel(\"Frequency\")\n",
    "# plt.xlabel(\"Fraction of Variance Unexplained\")\n",
    "# plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
<<<<<<< Updated upstream
   "outputs": [],
   "source": [
    "!ls ../../data/aging_brain/"
=======
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "21_7_14_figure_forest                sub_forest_19_cached\r\n",
      "GSE129788_RAW.tar                    sub_forest_1_cached\r\n",
      "aging_batch_encoding.tsv             sub_forest_2\r\n",
      "aging_brain_filtered.pickle          sub_forest_20\r\n",
      "aging_brain_old.pickle               sub_forest_20_cached\r\n",
      "aging_brain_young.pickle             sub_forest_21\r\n",
      "cv_forest                            sub_forest_21_cached\r\n",
      "cv_forest_11                         sub_forest_22\r\n",
      "cv_forest_14                         sub_forest_22_cached\r\n",
      "cv_forest_trimmed                    sub_forest_23\r\n",
      "cv_forest_trimmed_extra              sub_forest_23_cached\r\n",
      "filtered_feature_mask.txt            sub_forest_24\r\n",
      "filtered_gene_header.txt             sub_forest_24_cached\r\n",
      "full_clustering                      sub_forest_25\r\n",
      "full_clustering_predicted            sub_forest_25_cached\r\n",
      "full_clustering_predicted_trimmed    sub_forest_26\r\n",
      "full_custering_predicted             sub_forest_26_cached\r\n",
      "full_custering_predicted_cached      sub_forest_27\r\n",
      "full_mad                             sub_forest_27_cached\r\n",
      "full_mad_clustered                   sub_forest_28\r\n",
      "\u001b[34mrestricted_sub_forest\u001b[m\u001b[m                sub_forest_28_cached\r\n",
      "scanpy_aging_brain.ipynb             sub_forest_29\r\n",
      "scanpy_aging_brain_3.ipynb           sub_forest_29_cached\r\n",
      "scanpy_aging_brain_cv_forest         sub_forest_2_cached\r\n",
      "scanpy_aging_brain_cv_forest_compact sub_forest_3\r\n",
      "scanpy_aging_brain_random_cv_forest  sub_forest_30\r\n",
      "scanpy_aging_brain_restricted_1      sub_forest_30_cached\r\n",
      "sub_forest_0                         sub_forest_31\r\n",
      "sub_forest_0_cached                  sub_forest_31_cached\r\n",
      "sub_forest_1                         sub_forest_32\r\n",
      "sub_forest_10                        sub_forest_32_cached\r\n",
      "sub_forest_10_cached                 sub_forest_33\r\n",
      "sub_forest_11                        sub_forest_33_cached\r\n",
      "sub_forest_11_cached                 sub_forest_34\r\n",
      "sub_forest_12                        sub_forest_34_cached\r\n",
      "sub_forest_12_cached                 sub_forest_3_cached\r\n",
      "sub_forest_13                        sub_forest_4\r\n",
      "sub_forest_13_cached                 sub_forest_4_cached\r\n",
      "sub_forest_14                        sub_forest_5\r\n",
      "sub_forest_14_cached                 sub_forest_5_cached\r\n",
      "sub_forest_15                        sub_forest_6\r\n",
      "sub_forest_15_cached                 sub_forest_6_cached\r\n",
      "sub_forest_16                        sub_forest_7\r\n",
      "sub_forest_16_cached                 sub_forest_7_cached\r\n",
      "sub_forest_17                        sub_forest_8\r\n",
      "sub_forest_17_cached                 sub_forest_8_cached\r\n",
      "sub_forest_18                        sub_forest_9\r\n",
      "sub_forest_18_cached                 sub_forest_9_cached\r\n",
      "sub_forest_19\r\n"
     ]
    }
   ],
   "source": [
    "!ls {data_location}"
>>>>>>> Stashed changes
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Setting context\n",
      "Input:(16028, 2000)\n",
      "Output:(16028, 2000)\n",
      "Generating trees\n",
      "Running /Users/bbrener1/miniconda3/lib/python3.7/site-packages/rusty_axe/bin/rf_5\n",
      "Command: /Users/bbrener1/miniconda3/lib/python3.7/site-packages/rusty_axe/bin/rf_5 -ic /var/folders/_k/81hqlgss0tbf2l0t7wm4t_200000gn/T/tmp9e8fvi9m/input.counts -oc /var/folders/_k/81hqlgss0tbf2l0t7wm4t_200000gn/T/tmp9e8fvi9m/output.counts -o /var/folders/_k/81hqlgss0tbf2l0t7wm4t_200000gn/T/tmp9e8fvi9m/tmp -auto -ifh /var/folders/_k/81hqlgss0tbf2l0t7wm4t_200000gn/T/tmp9e8fvi9m/tmp.ifh -ofh /var/folders/_k/81hqlgss0tbf2l0t7wm4t_200000gn/T/tmp9e8fvi9m/tmp.ofh -trees 100 -ifs 700 -ofs 700 -ss 300 -depth 8 -leaves 100 -dispersion_mode ssme -sfr 0 -standardize true -norm l1 -reduction 10 -p 8 -reduce_input true -reduce_output true\n",
      "Read parameters\n",
      "\n",
      "0 0 1.8931357860565186 0 0 0 2.507997751235962 0 0 0 0\n",
      "100\n",
      "\n",
      "0 0 0 0 0 0 0 0 0 0 200\n",
      "300\n",
      "\n",
      "0 0 0 0 0 0 0 0 0 0 400\n",
      "500\n",
      "\n",
      "0 0 0 0 0 0 0 0 0 0 600\n",
      "700\n",
      "\n",
      "0 0 0 0 2.0017569065093994 0 0 0 0 0 800\n",
      "900\n",
      "\n",
      "0 0 0 0 0 0 0 1.5864790678024292 0 0 1000\n",
      "1100\n",
      "\n",
      "0 0 0 0 0 0 0 3.541308879852295 0 0 1200\n",
      "1300\n",
      "\n",
      "0 0 0 0 0 0 1.5142695903778076 0 0 0 1400\n",
      "1500\n",
      "\n",
      "0 0 0 0 0 0 0 0 0 0 1600\n",
      "1700\n",
      "\n",
      "0 0 0 0 0 0 0 0 0 0 1800\n",
      "1900\n",
      "\n",
      "0 0 0 0 0 0 0 0 0 0 2000\n",
      "2100\n",
      "\n",
      "0 0 0 0 1.4800764322280884 0 0 1.4800764322280884 0 0 2200\n",
      "2300\n",
      "\n",
      "0 0 0 0 1.5150998830795288 0 0 0 0 0 2400\n",
      "2500\n",
      "\n",
      "0 0 0 0 0 0 0 3.6586053371429443 0 0 2600\n",
      "2700\n",
      "\n",
      "0 0 0 0 0 0 0 2.3695943355560303 0 1.442442536354065 2800\n",
      "2900\n",
      "\n",
      "0 0 0 0 0 0 0 0 0 0 3000\n",
      "3100\n",
      "\n",
      "0 0 0 0 0 0 3.000828981399536 0 0 0 3200\n",
      "3300\n",
      "\n",
      "0 0 0 0 0 0 0 0 0 0 3400\n",
      "3500\n",
      "\n",
      "0 0 0 0 0 0 0 0 0 0 3600\n",
      "3700\n",
      "\n",
      "0 0 0 0 0 0 0 1.862636685371399 0 0 3800\n",
      "3900\n",
      "\n",
      "0 0 0 0 2.229140281677246 0 0 0 0 0 4000\n",
      "4100\n",
      "\n",
      "0 0 0 0 0 0 0 0 0 0 4200\n",
      "4300\n",
      "\n",
      "0 0 0 0 0 0 0 1.5815516710281372 0 0 4400\n",
      "4500\n",
      "\n",
      "0 0 0 0 0 0 0 0 0 0 4600\n",
      "4700\n",
      "\n",
      "0 1.214314579963684 0 0 0 0 0 0 0 0 4800\n",
      "4900\n",
      "\n",
      "0 0 0 0 0 0 3.1836531162261963 0 0 0 5000\n",
      "5100\n",
      "\n",
      "0 0 0 0 0 0 0 1.5914435386657715 0 0 5200\n",
      "5300\n",
      "\n",
      "0 0 0 0 0 0 0 0 0 0 5400\n",
      "5500\n",
      "\n",
      "0 0 0 0 0 0 0 0 0 0 5600\n",
      "5700\n",
      "\n",
      "0 0 0 0 0 0 0 0 0 0 5800\n",
      "5900\n",
      "\n",
      "0 0 0 0 0 0 0 1.6191076040267944 0 0 6000\n",
      "6100\n",
      "\n",
      "0 0 0 0 1.1573530435562134 0 0 0 0 0 6200\n",
      "6300\n",
      "\n",
      "0 0 0 0 0.5733009576797485 0 0 0 0 0 6400\n",
      "6500\n",
      "\n",
      "0 0 0 0 0 0 0 1.8346844911575317 0 1.8346844911575317 6600\n",
      "6700\n",
      "\n",
      "0 0 0 0 0 0 0 3.685375928878784 0 0 6800\n",
      "6900\n",
      "\n",
      "0 0 0 0 1.9447110891342163 0 0 3.6095566749572754 0 0 7000\n",
      "7100\n",
      "\n",
      "0 0 0 0 0 0 3.0982682704925537 0 0 0 7200\n",
      "7300\n",
      "\n",
      "0 0 0 0 0 0 0 0 0 0 7400\n",
      "7500\n",
      "\n",
      "0 0 0 0 0 0 0 0 0 0 7600\n",
      "7700\n",
      "\n",
      "0 0 0 0 0 0 0 0 0 0 7800\n",
      "7900\n",
      "\n",
      "0 0 0 0 0 0 0 0 0 0 8000\n",
      "8100\n",
      "\n",
      "0 0 0 0 0 0 0 0 0 0 8200\n",
      "8300\n",
      "\n",
      "0 0 0 0 0 0 0 1.0990124940872192 0 0 8400\n",
      "8500\n",
      "\n",
      "0 0 0 0 1.7238681316375732 0 1.7238681316375732 3.1793389320373535 0 0 8600\n",
      "8700\n",
      "\n",
      "0 1.8960157632827759 0 1.8960157632827759 0 0 0 3.5540859699249268 0 0 8800\n",
      "8900\n",
      "\n",
      "0 0 0 0 0 0 0 0 0 1.6033854484558105 9000\n",
      "9100\n",
      "\n",
      "0 0 0 0 0 0 0 0 0 0 9200\n",
      "9300\n",
      "\n",
      "0 0 0 0 0 0 0 0 0 0 9400\n",
      "9500\n",
      "\n",
      "0 0 0 0 0 0 0 0 0 0 9600\n",
      "9700\n",
      "\n",
      "0 0 0 0 0 0 0 0 1.674572467803955 0 9800\n",
      "9900\n",
      "\n",
      "0 0 0 0 0 0 0 0 0 0 10000\n",
      "10100\n",
      "\n",
      "0 0 0 0 1.3285235166549683 0 0 0 0 0 10200\n",
      "10300\n",
      "\n",
      "1.8285188674926758 1.8285188674926758 0 0 0 0 0 0 0 0 10400\n",
      "10500\n",
      "\n",
      "0 0 0 0 0 0 0 1.8828281164169312 0 1.0498963594436646 10600\n",
      "10700\n",
      "\n",
      "0 0 0 0 1.0425875186920166 0 0 2.8638525009155273 0 1.0425875186920166 10800\n",
      "10900\n",
      "\n",
      "0 0 0 0 0 0 0 0 0 0 11000\n",
      "11100\n",
      "\n",
      "0 0 0 0 0 0 0 0 0 0 11200\n",
      "11300\n",
      "\n",
      "0 0 0 0 0 0 0 0 0 0 11400\n",
      "11500\n",
      "\n",
      "0 0 0 0 0 0 0 0 0 0 11600\n",
      "11700\n",
      "\n",
      "0 0 0 0 0 0 0 0 0 0.8006526231765747 11800\n",
      "11900\n",
      "\n",
      "0 0 0 0 0 0 0 0 0 0 12000\n",
      "12100\n",
      "\n",
      "0 0 0 1.996789574623108 3.0005266666412354 0 0 0 0 1.996789574623108 12200\n",
      "12300\n",
      "\n",
      "0 0 0 0 0 0 0 1.679168939590454 0 0 12400\n",
      "12500\n",
      "\n",
      "0 0 0 0 0 0 0 0 0 0 12600\n",
      "12700\n",
      "\n",
      "0 1.8413492441177368 0 0 0 0 0 2.8282079696655273 0 0 12800\n",
      "12900\n",
      "\n",
      "0 0 0 0 0 0 0 2.3510587215423584 0 0 13000\n",
      "13100\n",
      "\n",
      "0 0 0 1.3673235177993774 0 0 0 1.3673235177993774 0 0 13200\n",
      "13300\n",
      "\n",
      "0 0 0 0 0 0 2.957416296005249 0 0 0 13400\n",
      "13500\n",
      "\n",
      "0 0 0 0 0 0 0 0 0 0 13600\n",
      "13700\n",
      "\n",
      "0 0 0 0 0 0 0 0 0 0 13800\n",
      "13900\n",
      "\n",
      "0 0 0 0 0 0 0 0 0 0 14000\n",
      "14100\n",
      "\n",
      "0 0 0 0 0 0 0 0 0 0 14200\n",
      "14300\n",
      "\n",
      "0 0 0 0 0 0 0.7154713869094849 0 0 0 14400\n",
      "14500\n",
      "\n",
      "0 0 0 0 0 0 0 0 0 0 14600\n",
      "14700\n",
      "\n",
      "0 0 0 0 0 0 0 0 0 0 14800\n",
      "14900\n",
      "\n",
      "0 0 0 0 1.0648548603057861 0 1.0648548603057861 1.0648548603057861 0 0 15000\n",
      "15100\n",
      "\n",
      "2.0710577964782715 1.197271466255188 0 0 0 0 0 0 0 0 15200\n",
      "15300\n",
      "\n",
      "0 2.195094108581543 0 0 1.6075206995010376 0 0 4.341439247131348 0 0 15400\n",
      "15500\n",
      "\n",
      "0 0 0 0 0 0 0 2.571714401245117 0 0 15600\n",
      "15700\n",
      "\n",
      "0 0 0 2.1566731929779053 0 0 0 0 0 0 15800\n",
      "15900\n",
      "\n",
      "0.9007956981658936 1.683558464050293 0.9007956981658936 0 0 0 1.3668876886367798 0 0 0 16000\n",
      "===========\n",
      "16028,2000\n",
      "\n",
      "0 0 1.8931357860565186 0 0 0 2.507997751235962 0 0 0 0\n",
      "100\n",
      "\n",
      "0 0 0 0 0 0 0 0 0 0 200\n",
      "300\n",
      "\n",
      "0 0 0 0 0 0 0 0 0 0 400\n",
      "500\n",
      "\n",
      "0 0 0 0 0 0 0 0 0 0 600\n",
      "700\n",
      "\n",
      "0 0 0 0 2.0017569065093994 0 0 0 0 0 800\n",
      "900\n",
      "\n",
      "0 0 0 0 0 0 0 1.5864790678024292 0 0 1000\n",
      "1100\n",
      "\n",
      "0 0 0 0 0 0 0 3.541308879852295 0 0 1200\n",
      "1300\n",
      "\n",
      "0 0 0 0 0 0 1.5142695903778076 0 0 0 1400\n",
      "1500\n",
      "\n",
      "0 0 0 0 0 0 0 0 0 0 1600\n",
      "1700\n",
      "\n",
      "0 0 0 0 0 0 0 0 0 0 1800\n",
      "1900\n",
      "\n",
      "0 0 0 0 0 0 0 0 0 0 2000\n",
      "2100\n",
      "\n",
      "0 0 0 0 1.4800764322280884 0 0 1.4800764322280884 0 0 2200\n",
      "2300\n",
      "\n",
      "0 0 0 0 1.5150998830795288 0 0 0 0 0 2400\n",
      "2500\n",
      "\n",
      "0 0 0 0 0 0 0 3.6586053371429443 0 0 2600\n",
      "2700\n",
      "\n",
      "0 0 0 0 0 0 0 2.3695943355560303 0 1.442442536354065 2800\n",
      "2900\n",
      "\n",
      "0 0 0 0 0 0 0 0 0 0 3000\n",
      "3100\n",
      "\n",
      "0 0 0 0 0 0 3.000828981399536 0 0 0 3200\n",
      "3300\n",
      "\n",
      "0 0 0 0 0 0 0 0 0 0 3400\n",
      "3500\n",
      "\n",
      "0 0 0 0 0 0 0 0 0 0 3600\n",
      "3700\n",
      "\n",
      "0 0 0 0 0 0 0 1.862636685371399 0 0 3800\n",
      "3900\n",
      "\n",
      "0 0 0 0 2.229140281677246 0 0 0 0 0 4000\n",
      "4100\n",
      "\n",
      "0 0 0 0 0 0 0 0 0 0 4200\n",
      "4300\n",
      "\n",
      "0 0 0 0 0 0 0 1.5815516710281372 0 0 4400\n",
      "4500\n",
      "\n",
      "0 0 0 0 0 0 0 0 0 0 4600\n",
      "4700\n",
      "\n",
      "0 1.214314579963684 0 0 0 0 0 0 0 0 4800\n",
      "4900\n",
      "\n",
      "0 0 0 0 0 0 3.1836531162261963 0 0 0 5000\n",
      "5100\n",
      "\n",
      "0 0 0 0 0 0 0 1.5914435386657715 0 0 5200\n",
      "5300\n",
      "\n",
      "0 0 0 0 0 0 0 0 0 0 5400\n",
      "5500\n",
      "\n",
      "0 0 0 0 0 0 0 0 0 0 5600\n",
      "5700\n",
      "\n",
      "0 0 0 0 0 0 0 0 0 0 5800\n",
      "5900\n",
      "\n",
      "0 0 0 0 0 0 0 1.6191076040267944 0 0 6000\n",
      "6100\n",
      "\n",
      "0 0 0 0 1.1573530435562134 0 0 0 0 0 6200\n",
      "6300\n",
      "\n",
      "0 0 0 0 0.5733009576797485 0 0 0 0 0 6400\n",
      "6500\n",
      "\n",
      "0 0 0 0 0 0 0 1.8346844911575317 0 1.8346844911575317 6600\n",
      "6700\n",
      "\n",
      "0 0 0 0 0 0 0 3.685375928878784 0 0 6800\n",
      "6900\n",
      "\n",
      "0 0 0 0 1.9447110891342163 0 0 3.6095566749572754 0 0 7000\n",
      "7100\n",
      "\n",
      "0 0 0 0 0 0 3.0982682704925537 0 0 0 7200\n",
      "7300\n",
      "\n",
      "0 0 0 0 0 0 0 0 0 0 7400\n",
      "7500\n",
      "\n",
      "0 0 0 0 0 0 0 0 0 0 7600\n",
      "7700\n",
      "\n",
      "0 0 0 0 0 0 0 0 0 0 7800\n",
      "7900\n",
      "\n",
      "0 0 0 0 0 0 0 0 0 0 8000\n",
      "8100\n",
      "\n",
      "0 0 0 0 0 0 0 0 0 0 8200\n",
      "8300\n",
      "\n",
      "0 0 0 0 0 0 0 1.0990124940872192 0 0 8400\n",
      "8500\n",
      "\n",
      "0 0 0 0 1.7238681316375732 0 1.7238681316375732 3.1793389320373535 0 0 8600\n",
      "8700\n",
      "\n",
      "0 1.8960157632827759 0 1.8960157632827759 0 0 0 3.5540859699249268 0 0 8800\n",
      "8900\n",
      "\n",
      "0 0 0 0 0 0 0 0 0 1.6033854484558105 9000\n",
      "9100\n",
      "\n",
      "0 0 0 0 0 0 0 0 0 0 9200\n",
      "9300\n",
      "\n",
      "0 0 0 0 0 0 0 0 0 0 9400\n",
      "9500\n",
      "\n",
      "0 0 0 0 0 0 0 0 0 0 9600\n",
      "9700\n",
      "\n",
      "0 0 0 0 0 0 0 0 1.674572467803955 0 9800\n",
      "9900\n",
      "\n",
      "0 0 0 0 0 0 0 0 0 0 10000\n",
      "10100\n",
      "\n",
      "0 0 0 0 1.3285235166549683 0 0 0 0 0 10200\n",
      "10300\n",
      "\n",
      "1.8285188674926758 1.8285188674926758 0 0 0 0 0 0 0 0 10400\n",
      "10500\n",
      "\n",
      "0 0 0 0 0 0 0 1.8828281164169312 0 1.0498963594436646 10600\n",
      "10700\n",
      "\n",
      "0 0 0 0 1.0425875186920166 0 0 2.8638525009155273 0 1.0425875186920166 10800\n",
      "10900\n",
      "\n",
      "0 0 0 0 0 0 0 0 0 0 11000\n",
      "11100\n",
      "\n",
      "0 0 0 0 0 0 0 0 0 0 11200\n",
      "11300\n",
      "\n",
      "0 0 0 0 0 0 0 0 0 0 11400\n",
      "11500\n",
      "\n",
      "0 0 0 0 0 0 0 0 0 0 11600\n",
      "11700\n",
      "\n",
      "0 0 0 0 0 0 0 0 0 0.8006526231765747 11800\n",
      "11900\n",
      "\n",
      "0 0 0 0 0 0 0 0 0 0 12000\n",
      "12100\n",
      "\n",
      "0 0 0 1.996789574623108 3.0005266666412354 0 0 0 0 1.996789574623108 12200\n",
      "12300\n",
      "\n",
      "0 0 0 0 0 0 0 1.679168939590454 0 0 12400\n",
      "12500\n",
      "\n",
      "0 0 0 0 0 0 0 0 0 0 12600\n",
      "12700\n",
      "\n",
      "0 1.8413492441177368 0 0 0 0 0 2.8282079696655273 0 0 12800\n",
      "12900\n",
      "\n",
      "0 0 0 0 0 0 0 2.3510587215423584 0 0 13000\n",
      "13100\n",
      "\n",
      "0 0 0 1.3673235177993774 0 0 0 1.3673235177993774 0 0 13200\n",
      "13300\n",
      "\n",
      "0 0 0 0 0 0 2.957416296005249 0 0 0 13400\n",
      "13500\n",
      "\n",
      "0 0 0 0 0 0 0 0 0 0 13600\n",
      "13700\n",
      "\n",
      "0 0 0 0 0 0 0 0 0 0 13800\n",
      "13900\n",
      "\n",
      "0 0 0 0 0 0 0 0 0 0 14000\n",
      "14100\n",
      "\n",
      "0 0 0 0 0 0 0 0 0 0 14200\n",
      "14300\n",
      "\n",
      "0 0 0 0 0 0 0.7154713869094849 0 0 0 14400\n",
      "14500\n",
      "\n",
      "0 0 0 0 0 0 0 0 0 0 14600\n",
      "14700\n",
      "\n",
      "0 0 0 0 0 0 0 0 0 0 14800\n",
      "14900\n",
      "\n",
      "0 0 0 0 1.0648548603057861 0 1.0648548603057861 1.0648548603057861 0 0 15000\n",
      "15100\n",
      "\n",
      "2.0710577964782715 1.197271466255188 0 0 0 0 0 0 0 0 15200\n",
      "15300\n",
      "\n",
      "0 2.195094108581543 0 0 1.6075206995010376 0 0 4.341439247131348 0 0 15400\n",
      "15500\n",
      "\n",
      "0 0 0 0 0 0 0 2.571714401245117 0 0 15600\n",
      "15700\n",
      "\n",
      "0 0 0 2.1566731929779053 0 0 0 0 0 0 15800\n",
      "15900\n",
      "\n",
      "0.9007956981658936 1.683558464050293 0.9007956981658936 0 0 0 1.3668876886367798 0 0 0 16000\n",
      "===========\n",
      "16028,2000\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Reading header: /var/folders/_k/81hqlgss0tbf2l0t7wm4t_200000gn/T/tmp9e8fvi9m/tmp.ifh\n",
      "Read 2000 lines\n",
      "Reading header: /var/folders/_k/81hqlgss0tbf2l0t7wm4t_200000gn/T/tmp9e8fvi9m/tmp.ofh\n",
      "Read 2000 lines\n",
      "Computing tree 6\n",
      "Computing tree 7\n",
      "Computing tree 25\n",
      "Computing tree 10\n",
      "Computing tree 12\n",
      "Computing tree 0\n",
      "Computing tree 50\n",
      "Computing tree 9\n",
      "Computing tree 1\n",
      "Computing tree 3\n",
      "Computing tree 8\n",
      "Computing tree 11\n",
      "Computing tree 13\n",
      "Computing tree 51\n",
      "Computing tree 26\n",
      "Computing tree 75\n",
      "Computing tree 4\n",
      "Computing tree 2\n",
      "Computing tree 5\n",
      "Computing tree 14\n",
      "Computing tree 76\n",
      "Computing tree 62\n",
      "Computing tree 27\n",
      "Computing tree 52\n",
      "Computing tree 87\n",
      "Computing tree 93\n",
      "Computing tree 15\n",
      "Computing tree 90\n",
      "Computing tree 77\n",
      "Computing tree 28\n",
      "Computing tree 63\n",
      "Computing tree 53\n",
      "Computing tree 91\n",
      "Computing tree 16\n",
      "Computing tree 88\n",
      "Computing tree 94\n",
      "Computing tree 78\n",
      "Computing tree 29\n",
      "Computing tree 54\n",
      "Computing tree 64\n",
      "Computing tree 92\n",
      "Computing tree 17\n",
      "Computing tree 95\n",
      "Computing tree 79\n",
      "Computing tree 89\n",
      "Computing tree 55\n",
      "Computing tree 65\n",
      "Computing tree 30\n",
      "Computing tree 96\n",
      "Computing tree 37\n",
      "Computing tree 80\n",
      "Computing tree 18\n",
      "Computing tree 56\n",
      "Computing tree 21\n",
      "Computing tree 31\n",
      "Computing tree 66\n",
      "Computing tree 97\n",
      "Computing tree 38\n",
      "Computing tree 81\n",
      "Computing tree 19\n",
      "Computing tree 22\n",
      "Computing tree 57\n",
      "Computing tree 67\n",
      "Computing tree 32\n",
      "Computing tree 39\n",
      "Computing tree 20\n",
      "Computing tree 98\n",
      "Computing tree 82\n",
      "Computing tree 58\n",
      "Computing tree 23\n",
      "Computing tree 33\n",
      "Computing tree 68\n",
      "Computing tree 99\n",
      "Computing tree 40\n",
      "Computing tree 34\n",
      "Computing tree 24\n",
      "Computing tree 83\n",
      "Computing tree 59\n",
      "Computing tree 43\n",
      "Computing tree 69\n",
      "Computing tree 60\n",
      "Computing tree 41\n",
      "Computing tree 35\n",
      "Computing tree 61\n",
      "Computing tree 71\n",
      "Computing tree 84\n",
      "Computing tree 44\n",
      "Computing tree 36\n",
      "Computing tree 70\n",
      "Computing tree 42\n",
      "Computing tree 73\n",
      "Computing tree 46\n",
      "Computing tree 85\n",
      "Computing tree 72\n",
      "Computing tree 45\n",
      "Computing tree 86\n",
      "Computing tree 74\n",
      "Computing tree 48\n",
      "Computing tree 49\n",
      "Computing tree 47\n",
      "\n",
      "\n",
      "Rust observes: /Users/bbrener1/miniconda3/lib/python3.7/site-packages/rusty_axe/bin/rf_5\n",
      "\n",
      "CHECK OUTPUT\n",
      "['tmp.tree_42.compact', 'tmp.tree_52.compact', 'tmp.tree_3.compact', 'tmp.tree_99.compact', 'tmp.tree_89.compact', 'tmp.tree_20.compact', 'tmp.tree_30.compact', 'tmp.tree_16.compact', 'input.counts', 'tmp.tree_64.compact', 'tmp.tree_74.compact', 'tmp.tree_80.compact', 'tmp.tree_90.compact', 'tmp.tree_39.compact', 'tmp.tree_29.compact', 'tmp.tree_17.compact', 'tmp.tree_75.compact', 'tmp.tree_65.compact', 'tmp.tree_28.compact', 'tmp.tree_38.compact', 'tmp.tree_91.compact', 'tmp.tree_81.compact', 'tmp.tree_2.compact', 'tmp.tree_53.compact', 'tmp.tree_43.compact', 'tmp.tree_31.compact', 'tmp.tree_21.compact', 'tmp.tree_88.compact', 'tmp.tree_98.compact', 'tmp.tree_67.compact', 'tmp.tree_77.compact', 'tmp.tree_15.compact', 'tmp.tree_9.compact', 'tmp.tree_58.compact', 'tmp.tree_48.compact', 'tmp.tree_83.compact', 'tmp.tree_93.compact', 'tmp.ifh', 'tmp.tree_23.compact', 'tmp.tree_33.compact', 'tmp.tree_0.compact', 'tmp.tree_41.compact', 'tmp.tree_51.compact', 'output.counts', 'tmp.tree_32.compact', 'tmp.tree_22.compact', 'tmp.tree_50.compact', 'tmp.tree_40.compact', 'tmp.tree_1.compact', 'tmp.tree_76.compact', 'tmp.tree_66.compact', 'tmp.tree_14.compact', 'tmp.tree_49.compact', 'tmp.tree_59.compact', 'tmp.tree_8.compact', 'tmp.tree_92.compact', 'tmp.tree_82.compact', 'tmp.tree_87.compact', 'tmp.tree_97.compact', 'tmp.tree_63.compact', 'tmp.tree_73.compact', 'tmp.tree_11.compact', 'tmp.tree_27.compact', 'tmp.tree_37.compact', 'tmp.tree_45.compact', 'tmp.tree_55.compact', 'tmp.tree_4.compact', 'tmp.tree_18.compact', 'tmp.tree_36.compact', 'tmp.tree_26.compact', 'tmp.tree_5.compact', 'tmp.tree_54.compact', 'tmp.tree_44.compact', 'tmp.tree_19.compact', 'tmp.tree_96.compact', 'tmp.tree_86.compact', 'tmp.tree_72.compact', 'tmp.tree_62.compact', 'tmp.tree_10.compact', 'tmp.tree_7.compact', 'tmp.tree_46.compact', 'tmp.tree_56.compact', 'tmp.tree_24.compact', 'tmp.tree_34.compact', 'tmp.ofh', 'tmp.tree_79.compact', 'tmp.tree_69.compact', 'tmp.tree_84.compact', 'tmp.tree_94.compact', 'tmp.tree_12.compact', 'tmp.tree_60.compact', 'tmp.tree_70.compact', 'tmp.tree_95.compact', 'tmp.tree_85.compact', 'tmp.tree_13.compact', 'tmp.tree_71.compact', 'tmp.tree_61.compact', 'tmp.tree_57.compact', 'tmp.tree_47.compact', 'tmp.tree_6.compact', 'tmp.tree_35.compact', 'tmp.tree_25.compact', 'tmp.tree_68.compact', 'tmp.tree_78.compact']\n",
      "Looking for clusters:/var/folders/_k/81hqlgss0tbf2l0t7wm4t_200000gn/T/tmp9e8fvi9m/tmp.clusters\n",
      "Loading /var/folders/_k/81hqlgss0tbf2l0t7wm4t_200000gn/T/tmp9e8fvi9m/tmp.tree_0.compact\n",
      "Loading /var/folders/_k/81hqlgss0tbf2l0t7wm4t_200000gn/T/tmp9e8fvi9m/tmp.tree_1.compact\n",
      "Loading /var/folders/_k/81hqlgss0tbf2l0t7wm4t_200000gn/T/tmp9e8fvi9m/tmp.tree_10.compact\n",
      "Loading /var/folders/_k/81hqlgss0tbf2l0t7wm4t_200000gn/T/tmp9e8fvi9m/tmp.tree_11.compact\n",
      "Loading /var/folders/_k/81hqlgss0tbf2l0t7wm4t_200000gn/T/tmp9e8fvi9m/tmp.tree_12.compact\n",
      "Loading /var/folders/_k/81hqlgss0tbf2l0t7wm4t_200000gn/T/tmp9e8fvi9m/tmp.tree_13.compact\n",
      "Loading /var/folders/_k/81hqlgss0tbf2l0t7wm4t_200000gn/T/tmp9e8fvi9m/tmp.tree_14.compact\n",
      "Loading /var/folders/_k/81hqlgss0tbf2l0t7wm4t_200000gn/T/tmp9e8fvi9m/tmp.tree_15.compact\n",
      "Loading /var/folders/_k/81hqlgss0tbf2l0t7wm4t_200000gn/T/tmp9e8fvi9m/tmp.tree_16.compact\n",
      "Loading /var/folders/_k/81hqlgss0tbf2l0t7wm4t_200000gn/T/tmp9e8fvi9m/tmp.tree_17.compact\n",
      "Loading /var/folders/_k/81hqlgss0tbf2l0t7wm4t_200000gn/T/tmp9e8fvi9m/tmp.tree_18.compact\n",
      "Loading /var/folders/_k/81hqlgss0tbf2l0t7wm4t_200000gn/T/tmp9e8fvi9m/tmp.tree_19.compact\n",
      "Loading /var/folders/_k/81hqlgss0tbf2l0t7wm4t_200000gn/T/tmp9e8fvi9m/tmp.tree_2.compact\n",
      "Loading /var/folders/_k/81hqlgss0tbf2l0t7wm4t_200000gn/T/tmp9e8fvi9m/tmp.tree_20.compact\n",
      "Loading /var/folders/_k/81hqlgss0tbf2l0t7wm4t_200000gn/T/tmp9e8fvi9m/tmp.tree_21.compact\n",
      "Loading /var/folders/_k/81hqlgss0tbf2l0t7wm4t_200000gn/T/tmp9e8fvi9m/tmp.tree_22.compact\n",
      "Loading /var/folders/_k/81hqlgss0tbf2l0t7wm4t_200000gn/T/tmp9e8fvi9m/tmp.tree_23.compact\n",
      "Loading /var/folders/_k/81hqlgss0tbf2l0t7wm4t_200000gn/T/tmp9e8fvi9m/tmp.tree_24.compact\n",
      "Loading /var/folders/_k/81hqlgss0tbf2l0t7wm4t_200000gn/T/tmp9e8fvi9m/tmp.tree_25.compact\n",
      "Loading /var/folders/_k/81hqlgss0tbf2l0t7wm4t_200000gn/T/tmp9e8fvi9m/tmp.tree_26.compact\n",
      "Loading /var/folders/_k/81hqlgss0tbf2l0t7wm4t_200000gn/T/tmp9e8fvi9m/tmp.tree_27.compact\n",
      "Loading /var/folders/_k/81hqlgss0tbf2l0t7wm4t_200000gn/T/tmp9e8fvi9m/tmp.tree_28.compact\n",
      "Loading /var/folders/_k/81hqlgss0tbf2l0t7wm4t_200000gn/T/tmp9e8fvi9m/tmp.tree_29.compact\n",
      "Loading /var/folders/_k/81hqlgss0tbf2l0t7wm4t_200000gn/T/tmp9e8fvi9m/tmp.tree_3.compact\n",
      "Loading /var/folders/_k/81hqlgss0tbf2l0t7wm4t_200000gn/T/tmp9e8fvi9m/tmp.tree_30.compact\n",
      "Loading /var/folders/_k/81hqlgss0tbf2l0t7wm4t_200000gn/T/tmp9e8fvi9m/tmp.tree_31.compact\n",
      "Loading /var/folders/_k/81hqlgss0tbf2l0t7wm4t_200000gn/T/tmp9e8fvi9m/tmp.tree_32.compact\n",
      "Loading /var/folders/_k/81hqlgss0tbf2l0t7wm4t_200000gn/T/tmp9e8fvi9m/tmp.tree_33.compact\n",
      "Loading /var/folders/_k/81hqlgss0tbf2l0t7wm4t_200000gn/T/tmp9e8fvi9m/tmp.tree_34.compact\n",
      "Loading /var/folders/_k/81hqlgss0tbf2l0t7wm4t_200000gn/T/tmp9e8fvi9m/tmp.tree_35.compact\n",
      "Loading /var/folders/_k/81hqlgss0tbf2l0t7wm4t_200000gn/T/tmp9e8fvi9m/tmp.tree_36.compact\n",
      "Loading /var/folders/_k/81hqlgss0tbf2l0t7wm4t_200000gn/T/tmp9e8fvi9m/tmp.tree_37.compact\n",
      "Loading /var/folders/_k/81hqlgss0tbf2l0t7wm4t_200000gn/T/tmp9e8fvi9m/tmp.tree_38.compact\n",
      "Loading /var/folders/_k/81hqlgss0tbf2l0t7wm4t_200000gn/T/tmp9e8fvi9m/tmp.tree_39.compact\n",
      "Loading /var/folders/_k/81hqlgss0tbf2l0t7wm4t_200000gn/T/tmp9e8fvi9m/tmp.tree_4.compact\n",
      "Loading /var/folders/_k/81hqlgss0tbf2l0t7wm4t_200000gn/T/tmp9e8fvi9m/tmp.tree_40.compact\n",
      "Loading /var/folders/_k/81hqlgss0tbf2l0t7wm4t_200000gn/T/tmp9e8fvi9m/tmp.tree_41.compact\n",
      "Loading /var/folders/_k/81hqlgss0tbf2l0t7wm4t_200000gn/T/tmp9e8fvi9m/tmp.tree_42.compact\n",
      "Loading /var/folders/_k/81hqlgss0tbf2l0t7wm4t_200000gn/T/tmp9e8fvi9m/tmp.tree_43.compact\n",
      "Loading /var/folders/_k/81hqlgss0tbf2l0t7wm4t_200000gn/T/tmp9e8fvi9m/tmp.tree_44.compact\n",
      "Loading /var/folders/_k/81hqlgss0tbf2l0t7wm4t_200000gn/T/tmp9e8fvi9m/tmp.tree_45.compact\n",
      "Loading /var/folders/_k/81hqlgss0tbf2l0t7wm4t_200000gn/T/tmp9e8fvi9m/tmp.tree_46.compact\n",
      "Loading /var/folders/_k/81hqlgss0tbf2l0t7wm4t_200000gn/T/tmp9e8fvi9m/tmp.tree_47.compact\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Loading /var/folders/_k/81hqlgss0tbf2l0t7wm4t_200000gn/T/tmp9e8fvi9m/tmp.tree_48.compact\n",
      "Loading /var/folders/_k/81hqlgss0tbf2l0t7wm4t_200000gn/T/tmp9e8fvi9m/tmp.tree_49.compact\n",
      "Loading /var/folders/_k/81hqlgss0tbf2l0t7wm4t_200000gn/T/tmp9e8fvi9m/tmp.tree_5.compact\n",
      "Loading /var/folders/_k/81hqlgss0tbf2l0t7wm4t_200000gn/T/tmp9e8fvi9m/tmp.tree_50.compact\n",
      "Loading /var/folders/_k/81hqlgss0tbf2l0t7wm4t_200000gn/T/tmp9e8fvi9m/tmp.tree_51.compact\n",
      "Loading /var/folders/_k/81hqlgss0tbf2l0t7wm4t_200000gn/T/tmp9e8fvi9m/tmp.tree_52.compact\n",
      "Loading /var/folders/_k/81hqlgss0tbf2l0t7wm4t_200000gn/T/tmp9e8fvi9m/tmp.tree_53.compact\n",
      "Loading /var/folders/_k/81hqlgss0tbf2l0t7wm4t_200000gn/T/tmp9e8fvi9m/tmp.tree_54.compact\n",
      "Loading /var/folders/_k/81hqlgss0tbf2l0t7wm4t_200000gn/T/tmp9e8fvi9m/tmp.tree_55.compact\n",
      "Loading /var/folders/_k/81hqlgss0tbf2l0t7wm4t_200000gn/T/tmp9e8fvi9m/tmp.tree_56.compact\n",
      "Loading /var/folders/_k/81hqlgss0tbf2l0t7wm4t_200000gn/T/tmp9e8fvi9m/tmp.tree_57.compact\n",
      "Loading /var/folders/_k/81hqlgss0tbf2l0t7wm4t_200000gn/T/tmp9e8fvi9m/tmp.tree_58.compact\n",
      "Loading /var/folders/_k/81hqlgss0tbf2l0t7wm4t_200000gn/T/tmp9e8fvi9m/tmp.tree_59.compact\n",
      "Loading /var/folders/_k/81hqlgss0tbf2l0t7wm4t_200000gn/T/tmp9e8fvi9m/tmp.tree_6.compact\n",
      "Loading /var/folders/_k/81hqlgss0tbf2l0t7wm4t_200000gn/T/tmp9e8fvi9m/tmp.tree_60.compact\n",
      "Loading /var/folders/_k/81hqlgss0tbf2l0t7wm4t_200000gn/T/tmp9e8fvi9m/tmp.tree_61.compact\n",
      "Loading /var/folders/_k/81hqlgss0tbf2l0t7wm4t_200000gn/T/tmp9e8fvi9m/tmp.tree_62.compact\n",
      "Loading /var/folders/_k/81hqlgss0tbf2l0t7wm4t_200000gn/T/tmp9e8fvi9m/tmp.tree_63.compact\n",
      "Loading /var/folders/_k/81hqlgss0tbf2l0t7wm4t_200000gn/T/tmp9e8fvi9m/tmp.tree_64.compact\n",
      "Loading /var/folders/_k/81hqlgss0tbf2l0t7wm4t_200000gn/T/tmp9e8fvi9m/tmp.tree_65.compact\n",
      "Loading /var/folders/_k/81hqlgss0tbf2l0t7wm4t_200000gn/T/tmp9e8fvi9m/tmp.tree_66.compact\n",
      "Loading /var/folders/_k/81hqlgss0tbf2l0t7wm4t_200000gn/T/tmp9e8fvi9m/tmp.tree_67.compact\n",
      "Loading /var/folders/_k/81hqlgss0tbf2l0t7wm4t_200000gn/T/tmp9e8fvi9m/tmp.tree_68.compact\n",
      "Loading /var/folders/_k/81hqlgss0tbf2l0t7wm4t_200000gn/T/tmp9e8fvi9m/tmp.tree_69.compact\n",
      "Loading /var/folders/_k/81hqlgss0tbf2l0t7wm4t_200000gn/T/tmp9e8fvi9m/tmp.tree_7.compact\n",
      "Loading /var/folders/_k/81hqlgss0tbf2l0t7wm4t_200000gn/T/tmp9e8fvi9m/tmp.tree_70.compact\n",
      "Loading /var/folders/_k/81hqlgss0tbf2l0t7wm4t_200000gn/T/tmp9e8fvi9m/tmp.tree_71.compact\n",
      "Loading /var/folders/_k/81hqlgss0tbf2l0t7wm4t_200000gn/T/tmp9e8fvi9m/tmp.tree_72.compact\n",
      "Loading /var/folders/_k/81hqlgss0tbf2l0t7wm4t_200000gn/T/tmp9e8fvi9m/tmp.tree_73.compact\n",
      "Loading /var/folders/_k/81hqlgss0tbf2l0t7wm4t_200000gn/T/tmp9e8fvi9m/tmp.tree_74.compact\n",
      "Loading /var/folders/_k/81hqlgss0tbf2l0t7wm4t_200000gn/T/tmp9e8fvi9m/tmp.tree_75.compact\n",
      "Loading /var/folders/_k/81hqlgss0tbf2l0t7wm4t_200000gn/T/tmp9e8fvi9m/tmp.tree_76.compact\n",
      "Loading /var/folders/_k/81hqlgss0tbf2l0t7wm4t_200000gn/T/tmp9e8fvi9m/tmp.tree_77.compact\n",
      "Loading /var/folders/_k/81hqlgss0tbf2l0t7wm4t_200000gn/T/tmp9e8fvi9m/tmp.tree_78.compact\n",
      "Loading /var/folders/_k/81hqlgss0tbf2l0t7wm4t_200000gn/T/tmp9e8fvi9m/tmp.tree_79.compact\n",
      "Loading /var/folders/_k/81hqlgss0tbf2l0t7wm4t_200000gn/T/tmp9e8fvi9m/tmp.tree_8.compact\n",
      "Loading /var/folders/_k/81hqlgss0tbf2l0t7wm4t_200000gn/T/tmp9e8fvi9m/tmp.tree_80.compact\n",
      "Loading /var/folders/_k/81hqlgss0tbf2l0t7wm4t_200000gn/T/tmp9e8fvi9m/tmp.tree_81.compact\n",
      "Loading /var/folders/_k/81hqlgss0tbf2l0t7wm4t_200000gn/T/tmp9e8fvi9m/tmp.tree_82.compact\n",
      "Loading /var/folders/_k/81hqlgss0tbf2l0t7wm4t_200000gn/T/tmp9e8fvi9m/tmp.tree_83.compact\n",
      "Loading /var/folders/_k/81hqlgss0tbf2l0t7wm4t_200000gn/T/tmp9e8fvi9m/tmp.tree_84.compact\n",
      "Loading /var/folders/_k/81hqlgss0tbf2l0t7wm4t_200000gn/T/tmp9e8fvi9m/tmp.tree_85.compact\n",
      "Loading /var/folders/_k/81hqlgss0tbf2l0t7wm4t_200000gn/T/tmp9e8fvi9m/tmp.tree_86.compact\n",
      "Loading /var/folders/_k/81hqlgss0tbf2l0t7wm4t_200000gn/T/tmp9e8fvi9m/tmp.tree_87.compact\n",
      "Loading /var/folders/_k/81hqlgss0tbf2l0t7wm4t_200000gn/T/tmp9e8fvi9m/tmp.tree_88.compact\n",
      "Loading /var/folders/_k/81hqlgss0tbf2l0t7wm4t_200000gn/T/tmp9e8fvi9m/tmp.tree_89.compact\n",
      "Loading /var/folders/_k/81hqlgss0tbf2l0t7wm4t_200000gn/T/tmp9e8fvi9m/tmp.tree_9.compact\n",
      "Loading /var/folders/_k/81hqlgss0tbf2l0t7wm4t_200000gn/T/tmp9e8fvi9m/tmp.tree_90.compact\n",
      "Loading /var/folders/_k/81hqlgss0tbf2l0t7wm4t_200000gn/T/tmp9e8fvi9m/tmp.tree_91.compact\n",
      "Loading /var/folders/_k/81hqlgss0tbf2l0t7wm4t_200000gn/T/tmp9e8fvi9m/tmp.tree_92.compact\n",
      "Loading /var/folders/_k/81hqlgss0tbf2l0t7wm4t_200000gn/T/tmp9e8fvi9m/tmp.tree_93.compact\n",
      "Loading /var/folders/_k/81hqlgss0tbf2l0t7wm4t_200000gn/T/tmp9e8fvi9m/tmp.tree_94.compact\n",
      "Loading /var/folders/_k/81hqlgss0tbf2l0t7wm4t_200000gn/T/tmp9e8fvi9m/tmp.tree_95.compact\n",
      "Loading /var/folders/_k/81hqlgss0tbf2l0t7wm4t_200000gn/T/tmp9e8fvi9m/tmp.tree_96.compact\n",
      "Loading /var/folders/_k/81hqlgss0tbf2l0t7wm4t_200000gn/T/tmp9e8fvi9m/tmp.tree_97.compact\n",
      "Loading /var/folders/_k/81hqlgss0tbf2l0t7wm4t_200000gn/T/tmp9e8fvi9m/tmp.tree_98.compact\n",
      "Loading /var/folders/_k/81hqlgss0tbf2l0t7wm4t_200000gn/T/tmp9e8fvi9m/tmp.tree_99.compact\n",
      "Saving forest\n",
      "../../data/aging_brain/pca_comparison_forest\n"
     ]
    }
   ],
   "source": [
    "\n",
    "# import sys\n",
    "# sys.path.append('../')\n",
    "# from rusty_axe import tree_reader as tr \n",
    "from rusty_axe import lumberjack\n",
    "\n",
    "cv_forest = lumberjack.fit(\n",
    "    young.X,\n",
    "    header=young.var_names,\n",
    "    trees=100,\n",
    "    ifs=700,\n",
    "    ofs=700,\n",
    "    ss=300,\n",
    "    depth=8,\n",
    "    leaves=100,\n",
    "    dispersion_mode='ssme',\n",
    "    sfr=0,\n",
    "    standardize = 'true',\n",
    "    norm='l1',\n",
    "    reduction = 10,\n",
    "    p=8,\n",
    "    reduce_input='true',\n",
    "    reduce_output='true'\n",
    ")\n",
    "cv_forest.set_cache(True)\n",
    "cv_forest.backup(data_location + \"pca_comparison_forest\")\n",
    "\n",
    "# started at 5:50"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "ename": "FileNotFoundError",
     "evalue": "[Errno 2] No such file or directory: '../../data/aging_brain/scanpy_cmp_aging_brain_true_l1'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mFileNotFoundError\u001b[0m                         Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-8-7f0e6591e6f4>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m      7\u001b[0m \u001b[0mdata_location\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m\"../../data/aging_brain/\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      8\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 9\u001b[0;31m \u001b[0mforest\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mtr\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mForest\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mload\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdata_location\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0;34m'scanpy_cmp_aging_brain_true_l1'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     10\u001b[0m \u001b[0mforest\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0marguments\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/battle/rf_5/rusty_axe/tree_reader.py\u001b[0m in \u001b[0;36mload\u001b[0;34m(location)\u001b[0m\n\u001b[1;32m    403\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    404\u001b[0m     \u001b[0;32mdef\u001b[0m \u001b[0mload\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mlocation\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 405\u001b[0;31m         \u001b[0;32mwith\u001b[0m \u001b[0mopen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mlocation\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mmode\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m'br'\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0mf\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    406\u001b[0m             \u001b[0;32mreturn\u001b[0m \u001b[0mpickle\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mload\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mf\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    407\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mFileNotFoundError\u001b[0m: [Errno 2] No such file or directory: '../../data/aging_brain/scanpy_cmp_aging_brain_true_l1'"
     ]
    }
   ],
   "source": [
    "import sys\n",
    "# sys.path.append('/localscratch/bbrener1/rusty_forest_v3/src')\n",
    "sys.path.append('../../')\n",
    "import rusty_axe.lumberjack as lumberjack\n",
    "\n",
    "data_location = \"../../data/aging_brain/\"\n",
    "\n",
    "forest = lumberjack.load(data_location + 'selection_forest')\n",
    "forest.arguments"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "forest.self_prediction = forest.predict(forest.output)\n",
    "forest.self_prediction.prediction_report()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "forest_residuals = forest.self_prediction.residuals()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "forest_squared_residuals = np.power(forest_residuals,2)\n",
    "\n",
    "forest_feature_error = np.sum(forest_squared_residuals,axis=0) + 1\n",
    "forest_feature_remaining = forest_feature_error/feature_null\n",
    "\n",
    "forest_sample_error = np.sum(forest_squared_residuals,axis=1) + 1\n",
    "forest_sample_remaining = forest_sample_error/sample_null\n",
    "\n",
    "\n",
    "plt.figure()\n",
    "plt.title(\"Fraction of Variance Unexplained, Per Feature\")\n",
    "plt.hist(forest_feature_remaining,bins=50)\n",
    "plt.ylabel(\"Frequency\")\n",
    "plt.xlabel(\"Fraction of Variance Unexplained\")\n",
    "plt.show()\n",
    "\n",
    "plt.figure()\n",
    "plt.title(\"Fraction of Variance Unexplained, Per Sample\")\n",
    "plt.hist(forest_sample_remaining,bins=50)\n",
    "plt.ylabel(\"Frequency\")\n",
    "plt.xlabel(\"Fraction of Variance Unexplained\")\n",
    "plt.show()\n",
    "\n",
    "# print(f\"Forest Variance Unexplained:{np.sum(forest_squared_residuals)/np.sum(null_squared_residual)}\")\n",
    "\n",
    "\n",
    "# delta_sort = np.argsort(pca_feature_remaining-forest_feature_remaining)\n",
    "\n",
    "# print(f\"PCA best:{forest.output_features[delta_sort[:20]]}\")\n",
    "# print(f\"Forest best:{forest.output_features[delta_sort[-20:]]}\")\n",
    "\n",
    "# for fb in delta_sort[-20:]:\n",
    "#     print(f\"Forest best: {forest.output_features[fb]}\")\n",
    "#     print(f\"Forest: {forest_feature_remaining[fb]}\")\n",
    "#     print(f\"PCA:{pca_feature_remaining[fb]}\")\n",
    "\n",
    "# ctsd_index = forest.truth_dictionary.feature_dictionary[\"Ctsd\"]\n",
    "\n",
    "# print(forest_feature_remaining[ctsd_index])\n",
    "# print(pca_feature_remaining[ctsd_index])\n",
    "\n",
    "feature_mean = np.mean(young.X,axis=0)\n",
    "feature_mean.shape\n",
    "\n",
    "sample_mean = np.mean(young.X,axis=1)\n",
    "\n",
    "# h2_index = forest.truth_dictionary.feature_dictionary[\"H2-Ab1\"]\n",
    "\n",
    "# plt.figure()\n",
    "# plt.scatter(*forest.coordinates().T,c=recovered_residual[:,h2_index],s=2,cmap='bwr')\n",
    "# plt.colorbar()\n",
    "# plt.show()\n",
    "\n",
    "# Cat 1a,:  S100a9, S100a8, Wfdc21,Retnlg, Lcn2,Ngp,Camp,Mmp8,Hp, Ltf, Slpi, Trem3\n",
    "# Cat 1b: Plac8, \n",
    "# Cat 1c: H2-Eb1,H2-Aa,H2-Ab1,\n",
    "\n",
    "\n",
    "# Cat 2a: Slc22a6,Slc6a13,Fmod\n",
    "\n",
    "# Cat3: Myoc"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.figure(figsize=(4,4))\n",
    "plt.title(\"Fraction of Variance Unexplained Per Feature, Forest Vs PCA\")\n",
    "plt.scatter(pca_feature_remaining,forest_feature_remaining,s=3,c=feature_mean)\n",
    "plt.colorbar(label=\"Mean Expression\")\n",
    "plt.plot([0,1],[0,1],color='red')\n",
    "plt.xlabel(\"PCA FVU\")\n",
    "plt.ylabel(\"Forest FVU\")\n",
    "plt.show()\n",
    "\n",
    "# plt.figure(figsize=(4,4))\n",
    "# plt.title(\"Fraction of Variance Unexplained Per Sample, Forest Vs PCA\")\n",
    "# plt.scatter(pca_sample_remaining,forest_sample_remaining,s=3,c=sample_mean)\n",
    "# plt.plot([0,1],[0,1],color='red')\n",
    "# plt.xlabel(\"PCA FVU\")\n",
    "# plt.ylabel(\"Forest FVU\")\n",
    "# plt.show()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.figure()\n",
    "plt.title(\"Forest Error Vs PCA Error\")\n",
    "plt.scatter(*young.obsm[\"X_umap\"].T,s=2,c=forest_sample_remaining-pca_sample_remaining,cmap='seismic',vmin=-.5,vmax=.5)\n",
    "plt.colorbar(label=\"Forest FVU - PCA FVU\")\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "gene = \"Hp\"\n",
    "gene_index = forest.truth_dictionary.feature_dictionary[gene]\n",
    "\n",
    "print(forest_feature_remaining[gene_index])\n",
    "print(pca_feature_remaining[gene_index])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.figure()\n",
    "plt.scatter(forest_squared_residuals.flatten(),pca_squared_residual.flatten(),s=1,alpha=.3)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "random_mask = np.random.random(forest_squared_residuals.flatten().shape) < .001\n",
    "\n",
    "plt.figure()\n",
    "plt.title(\"All Squared Residuals, PCA vs URF\\n Subsampled and Truncated\")\n",
    "plt.scatter(forest_squared_residuals.flatten()[random_mask],recovered_squared_residual.flatten()[random_mask],s=1,alpha=.3)\n",
    "plt.xlabel(\"Unsupervised Random Forest\")\n",
    "plt.ylabel(\"PCA\")\n",
    "plt.xlim(0,10)\n",
    "plt.ylim(0,10)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "plt.figure()\n",
    "plt.title(\"Model Residuals, PCA vs URF\\n Subsampled\")\n",
    "plt.scatter(forest_residuals.flatten()[random_mask],recovered_residual.flatten()[random_mask],s=1,alpha=.3)\n",
    "plt.xlabel(\"Unsupervised Random Forest\")\n",
    "plt.ylabel(\"PCA\")\n",
    "plt.plot([-5,5],[0,0],\"--\",color=\"gray\")\n",
    "plt.plot([0,0],[-4,4],\"--\",color=\"gray\")\n",
    "plt.xlim(-4,4)\n",
    "plt.ylim(-4,4)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.figure(figsize=(4,4))\n",
    "plt.title(\"Fraction of Variance Unexplained Per Feature, Forest Vs PCA\")\n",
    "plt.scatter(pca_feature_remaining,forest_feature_remaining,s=3,c=np.log(feature_mean))\n",
    "plt.colorbar(label=\"Mean Expression\")\n",
    "plt.plot([0,1],[0,1],color='red')\n",
    "plt.xlabel(\"PCA FVU\")\n",
    "plt.ylabel(\"Forest FVU\")\n",
    "plt.show()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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": 4
}
