|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": null, |
| 6 | + "metadata": {}, |
| 7 | + "outputs": [], |
| 8 | + "source": [ |
| 9 | + "import os\n", |
| 10 | + "import numpy as np\n", |
| 11 | + "from os.path import expanduser\n", |
| 12 | + "from fcs_signal_prediction.predict import predict_signal\n", |
| 13 | + "from fcs_signal_prediction.utils import data_utils as du\n", |
| 14 | + "\n", |
| 15 | + "\n", |
| 16 | + "%load_ext autoreload\n", |
| 17 | + "%reload_ext autoreload\n", |
| 18 | + "%autoreload 2\n" |
| 19 | + ] |
| 20 | + }, |
| 21 | + { |
| 22 | + "cell_type": "code", |
| 23 | + "execution_count": null, |
| 24 | + "metadata": {}, |
| 25 | + "outputs": [], |
| 26 | + "source": [ |
| 27 | + "DATA_CONVERGE_PROJECT=\"sd2e-project-43\"\n", |
| 28 | + "\n", |
| 29 | + "data_converge_base = os.path.join(expanduser(\"~\"), 'sd2e-projects', DATA_CONVERGE_PROJECT)\n", |
| 30 | + "experiment_dir = os.path.realpath(os.path.join(data_converge_base, 'reactor_outputs', 'complete'))\n", |
| 31 | + "experiment_dir_contents = [os.path.realpath(os.path.join(experiment_dir, x)) for x in os.listdir(experiment_dir)]\n", |
| 32 | + "\n", |
| 33 | + "experiment_dir_contents" |
| 34 | + ] |
| 35 | + }, |
| 36 | + { |
| 37 | + "cell_type": "code", |
| 38 | + "execution_count": null, |
| 39 | + "metadata": {}, |
| 40 | + "outputs": [], |
| 41 | + "source": [ |
| 42 | + "#experiment_id = \"YeastSTATES-CRISPR-Short-Duration-Time-Series-20191208\"\n", |
| 43 | + "experiment_id=\"YeastSTATES-OR-Gate-CRISPR-Dose-Response\"\n", |
| 44 | + "process_dir = os.path.join(experiment_dir, experiment_id)\n", |
| 45 | + "experiment_id_dir_contents = [os.path.realpath(os.path.join(process_dir, x)) for x in os.listdir(process_dir)]\n", |
| 46 | + "experiment_id_dir_contents.sort()\n", |
| 47 | + "last_process = experiment_id_dir_contents[-1]" |
| 48 | + ] |
| 49 | + }, |
| 50 | + { |
| 51 | + "cell_type": "code", |
| 52 | + "execution_count": null, |
| 53 | + "metadata": {}, |
| 54 | + "outputs": [], |
| 55 | + "source": [ |
| 56 | + "id_col = 'sample_id'\n", |
| 57 | + "df = du.get_data(last_process, du.get_record(last_process))\n", |
| 58 | + "meta = du.get_meta(last_process, du.get_record(last_process))\n", |
| 59 | + "#df = du.get_meta(last_process, du.get_record(last_process))\n", |
| 60 | + "df = df.merge(meta[[id_col, 'strain_name']])" |
| 61 | + ] |
| 62 | + }, |
| 63 | + { |
| 64 | + "cell_type": "code", |
| 65 | + "execution_count": null, |
| 66 | + "metadata": {}, |
| 67 | + "outputs": [], |
| 68 | + "source": [ |
| 69 | + "channels = list(df.columns)\n", |
| 70 | + "channels.remove(id_col)\n", |
| 71 | + "channels.remove(\"strain_name\")\n", |
| 72 | + "\n", |
| 73 | + "high_control = 'CRISPR_CEN.PK2_positive_control_NOR_00_24864'\n", |
| 74 | + "low_control = 'CRISPR_CEN.PK2_negative_control_23970'\n", |
| 75 | + "strain_col = \"strain_name\"" |
| 76 | + ] |
| 77 | + }, |
| 78 | + { |
| 79 | + "cell_type": "code", |
| 80 | + "execution_count": null, |
| 81 | + "metadata": {}, |
| 82 | + "outputs": [], |
| 83 | + "source": [ |
| 84 | + "predictions = predict_signal(df, experiment_id, low_control, high_control, id_col, channels, strain_col)\n", |
| 85 | + "mean_prediction = predictions.groupby([id_col]).agg({\"predicted_output\" : [np.mean, np.std]}).reset_index()\n", |
| 86 | + "mean_prediction.columns = mean_prediction.columns.map('_'.join)\n", |
| 87 | + "mean_prediction = mean_prediction.rename(columns={id_col+\"_\": id_col})\n", |
| 88 | + "result = meta.merge(mean_prediction, on=id_col)\n", |
| 89 | + "result" |
| 90 | + ] |
| 91 | + }, |
| 92 | + { |
| 93 | + "cell_type": "code", |
| 94 | + "execution_count": null, |
| 95 | + "metadata": {}, |
| 96 | + "outputs": [], |
| 97 | + "source": [ |
| 98 | + "def get_log_gfp(df):\n", |
| 99 | + " df['log BL1-A'] = df['BL1-A'].apply(lambda x: x+1.0).apply(np.log10)\n", |
| 100 | + " return df.replace([np.inf, -np.inf], np.nan).dropna()\n", |
| 101 | + "\n", |
| 102 | + "\n", |
| 103 | + "log_df = get_log_gfp(df).sample(n=int(10e5))" |
| 104 | + ] |
| 105 | + }, |
| 106 | + { |
| 107 | + "cell_type": "code", |
| 108 | + "execution_count": null, |
| 109 | + "metadata": {}, |
| 110 | + "outputs": [], |
| 111 | + "source": [ |
| 112 | + "import matplotlib.pyplot as plt\n", |
| 113 | + "\n", |
| 114 | + "samples = log_df.sample_id.unique()\n", |
| 115 | + "num_samples = len(samples)\n", |
| 116 | + "\n", |
| 117 | + "fig, ax = plt.subplots(num_samples, 1, figsize=(3, 3*num_samples))\n", |
| 118 | + "\n", |
| 119 | + "num=100000\n", |
| 120 | + "\n", |
| 121 | + "high_df = log_df.loc[log_df.strain_name == high_control]#.sample(n=num)\n", |
| 122 | + "low_df = log_df.loc[log_df.strain_name == low_control]#.sample(n=num)\n", |
| 123 | + "\n", |
| 124 | + "for i, sample in enumerate(samples):\n", |
| 125 | + " #print(i)\n", |
| 126 | + " sample_df = log_df.loc[log_df.sample_id == sample]\n", |
| 127 | + " \n", |
| 128 | + " #sample_df = sample_df.sample(n=min(num, len(sample_df)))\n", |
| 129 | + "\n", |
| 130 | + " ax[i].hist(high_df['log BL1-A'], label=\"high\")\n", |
| 131 | + " ax[i].hist(low_df['log BL1-A'], label=\"low\")\n", |
| 132 | + " ax[i].hist(sample_df['log BL1-A'], label=\"sample\")\n", |
| 133 | + " ax[i].set_xlim(0, 5)\n", |
| 134 | + " ax[i].text(6, 0, \"\\n\".join(result[result.sample_id==sample][[\"predicted_output_mean\", \"predicted_output_std\"]].transpose()[0:].to_string().split(\"\\n\")[1:]))\n", |
| 135 | + " ax[i].set_title(sample)\n", |
| 136 | + "\n", |
| 137 | + "# if i >= 9: \n", |
| 138 | + "# break\n", |
| 139 | + "#fig.legend()\n", |
| 140 | + "\n", |
| 141 | + "#result[result.sample_id==sample][[\"predicted_output_mean\", \"predicted_output_std\"]]\n", |
| 142 | + "\n" |
| 143 | + ] |
| 144 | + }, |
| 145 | + { |
| 146 | + "cell_type": "code", |
| 147 | + "execution_count": null, |
| 148 | + "metadata": {}, |
| 149 | + "outputs": [], |
| 150 | + "source": [] |
| 151 | + } |
| 152 | + ], |
| 153 | + "metadata": { |
| 154 | + "kernelspec": { |
| 155 | + "display_name": "Python 3", |
| 156 | + "language": "python", |
| 157 | + "name": "python3" |
| 158 | + } |
| 159 | + }, |
| 160 | + "nbformat": 4, |
| 161 | + "nbformat_minor": 2 |
| 162 | +} |
0 commit comments