File size: 9,602 Bytes
6f1e670
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Introduction\n",
    "\n",
    "In the previous notebook, we saw how to use the ```splitters```, ```featurizers```, ```predictors```, and ```proposers``` modules to train and deploy models to perform property prediction. In this notebook, we will see how to test multiple methods of data splits, featurizations, and models at once. We will initialize several different ```splitters```, ```featurizers```, and ```predictors``` and test them all at once."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "from model.splitters import *\n",
    "from model.featurizers import *\n",
    "from model.predictors import *\n",
    "from model.proposers import *"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Setting up\n",
    "\n",
    "First, define the following variables as before:\n",
    "- ```experiment_name```: the name of the experiment\n",
    "\n",
    "- ```protein_name```: the name of the protein\n",
    "\n",
    "- ```wt_file```: the path to the wildtype sequence\n",
    "\n",
    "- ```training_dataset_fname```: the path to the training dataset"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "experiment_name = \"example_experiment\"\n",
    "protein_name = \"example_protein\"\n",
    "wt_file = \"../../../data/example_protein/apex.fasta\"\n",
    "training_dataset_fname = '../../../data/example_protein/example_dataset.csv'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Non-neural network models\n",
    "\n",
    "We will show an example for non-neural network models first."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "First, we initialize our desired splitters and save them in a list."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "random_splitter = RandomProteinSplitter(protein_name, training_dataset_fname, wt_file, csv_has_header=True, use_cache=True, y_scaling=False, val_split=None)\n",
    "round_splitter = RoundProteinSplitter(protein_name, training_dataset_fname, wt_file, csv_has_header=True, use_cache=True, y_scaling=False, val_split=None)\n",
    "\n",
    "random_splitter.split_data(test_size=0.2)\n",
    "round_splitter.split_data(min_test_round=1, max_train_round=0)\n",
    "\n",
    "splitters = [random_splitter, round_splitter]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Next, we initialize our desired featurizers."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "onehot = OneHotFeaturizer(protein=protein_name, use_cache=True)\n",
    "georgiev = GeorgievFeaturizer(protein=protein_name, use_cache=True)\n",
    "\n",
    "featurizers = [onehot, georgiev]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Next, we initialize our desired predictors in the form of a list."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "predictors = [RidgeRegressor, RandomForestRegressor]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Finally, we run the following function ```run_model_experiments``` to train and test all the models. The function will return a pandas dataframe with the results, which are also saved in the directory where the training dataset is located in the folder ```model_cache/dataset_name/results```\n",
    "\n",
    "```run_model_experiments``` takes the following arguments:\n",
    "\n",
    "- ```splitters```: A list of splitters to use for the experiment.\n",
    "\n",
    "- ```featurizers```: A list of featurizers to use for the experiment.\n",
    "\n",
    "- ```predictors```: A list of predictors to use for the experiment.\n",
    "\n",
    "- ```experiment_name```: The name of the experiment.\n",
    "\n",
    "- ```use_cache```: Whether to use the cache for the splitters, featurizers, and predictors.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "results = run_model_experiments(splitters, featurizers, predictors, experiment_name,use_cache=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "results"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Neural network models\n",
    "\n",
    "Now, we will show an example for neural network models. You will need to have a wandb account to run this example.\n",
    "\n",
    "For neural network models, we used wandb to perform hyperparameter sweeps, which include comparing different methods of splitting and featurizing.\n",
    "\n",
    "We will initialize the same splitters and featurizers as before, and train various fully connected neural networks with different architectures and hyperparameters."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "random_splitter = RandomProteinSplitter(protein_name, training_dataset_fname, wt_file, csv_has_header=True, use_cache=True, y_scaling=False, val_split=0.15)\n",
    "round_splitter = RoundProteinSplitter(protein_name, training_dataset_fname, wt_file, csv_has_header=True, use_cache=True, y_scaling=False, val_split=0.15)\n",
    "\n",
    "random_splitter.split_data(test_size=0.2)\n",
    "round_splitter.split_data(min_test_round=1, max_train_round=0)\n",
    "\n",
    "splitters = [random_splitter, round_splitter]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "onehot = OneHotFeaturizer(protein=protein_name, use_cache=True) # just as an example, we will just use onehot featurizer\n",
    "\n",
    "featurizers = [onehot]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "models = [Fcn]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "For neural network models, we use the function ```run_nn_model_experiments``` to train and test all the models. Unlike the previous function, the results of the model training are saved on the wandb server.\n",
    "\n",
    "```run_nn_model_experiments``` takes the following arguments:\n",
    "\n",
    "- ```splitters```: A list of splitters to use for the experiment.\n",
    "\n",
    "- ```featurizers```: A list of featurizers to use for the experiment.\n",
    "\n",
    "- ```models```: A list of models to use for the experiment.\n",
    "\n",
    "- ```experiment_name```: The name of the experiment.\n",
    "\n",
    "- ```use_cache```: Whether to use the cache for the splitters, featurizers, and predictors.\n",
    "\n",
    "- ```sweep_depth```: The depth of the hyperparameter sweep. Options are 'test', 'standard', and 'custom'. 'test' is to test the training on a single hyperparameter configuration. 'standard' will test a standard set of hyperparameter configurations. 'custom' is a modified version of 'standard' that can be modified to test a subset of hyperparameter configurations.\n",
    "\n",
    "\n",
    "- ```search_method```: The method of hyperparameter search. Options are 'test', 'grid', and 'bayes'. 'test' is to test the training on a single hyperparameter configuration. 'grid' is to test the training on all possible hyperparameter configurations. 'bayes' is to test the training on a bayesian search of hyperparameter configurations.\n",
    "\n",
    "- ```count```: The number of different hyperparameter configurations to test. This is only used if ```search_method``` is 'bayes'.\n",
    "\n",
    "If the user wants to modify the hyperparameter sweeps to test other hyperparameters, then the configuration files for the sweep configs can be found in ```model/predictors/sweep_configs```."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "run_nn_model_experiments(splitters, \n",
    "                         featurizers, \n",
    "                         models, \n",
    "                         experiment_name=experiment_name,\n",
    "                         use_cache=False,\n",
    "                         sweep_depth='test', \n",
    "                         search_method='test',\n",
    "                         count=1\n",
    "                         )"
   ]
  }
 ],
 "metadata": {
  "environment": {
   "kernel": "multievolve",
   "name": "workbench-notebooks.m128",
   "type": "gcloud",
   "uri": "us-docker.pkg.dev/deeplearning-platform-release/gcr.io/workbench-notebooks:m128"
  },
  "kernelspec": {
   "display_name": "multievolve",
   "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.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}