ctheodoris
commited on
Commit
•
40624e8
1
Parent(s):
ae4e0e0
Delete prior nb
Browse files
examples/multi-task_cell_classification.ipynb
DELETED
@@ -1,395 +0,0 @@
|
|
1 |
-
{
|
2 |
-
"cells": [
|
3 |
-
{
|
4 |
-
"cell_type": "markdown",
|
5 |
-
"metadata": {},
|
6 |
-
"source": [
|
7 |
-
"# Geneformer Multi-Task Cell Classifier Tutorial\n",
|
8 |
-
"\n",
|
9 |
-
"This tutorial demonstrates how to use the Geneformer Multi-Task Cell Classifier and optimizatize hyperparameter for fine-tuning"
|
10 |
-
]
|
11 |
-
},
|
12 |
-
{
|
13 |
-
"cell_type": "markdown",
|
14 |
-
"metadata": {},
|
15 |
-
"source": [
|
16 |
-
"## 1. Installation and Imports\n",
|
17 |
-
"\n",
|
18 |
-
"First import the necessary modules."
|
19 |
-
]
|
20 |
-
},
|
21 |
-
{
|
22 |
-
"cell_type": "code",
|
23 |
-
"execution_count": 1,
|
24 |
-
"metadata": {},
|
25 |
-
"outputs": [],
|
26 |
-
"source": [
|
27 |
-
"from geneformer import MTLClassifier\n"
|
28 |
-
]
|
29 |
-
},
|
30 |
-
{
|
31 |
-
"cell_type": "markdown",
|
32 |
-
"metadata": {},
|
33 |
-
"source": [
|
34 |
-
"## 2. Set up Paths and Parameters\n",
|
35 |
-
"\n",
|
36 |
-
"Now, let's set up the necessary paths and parameters for our classifier. We'll also define our task columns, which are specific columns from our dataset that represent the classification tasks we want to train the model on."
|
37 |
-
]
|
38 |
-
},
|
39 |
-
{
|
40 |
-
"cell_type": "code",
|
41 |
-
"execution_count": 2,
|
42 |
-
"metadata": {},
|
43 |
-
"outputs": [],
|
44 |
-
"source": [
|
45 |
-
"# Define paths\n",
|
46 |
-
"pretrained_path = \"/path/to/pretrained/Geneformer/model\" \n",
|
47 |
-
"# input data is tokenized rank value encodings generated by Geneformer tokenizer (see tokenizing_scRNAseq_data.ipynb)\n",
|
48 |
-
"train_path = \"/path/to/train/data.dataset\"\n",
|
49 |
-
"val_path = \"/path/to/val/data.dataset\"\n",
|
50 |
-
"test_path = \"/path/to/test/data.dataset\"\n",
|
51 |
-
"results_dir = \"/path/to/results/directory\"\n",
|
52 |
-
"model_save_path = \"/path/to/model/save/path\"\n",
|
53 |
-
"tensorboard_log_dir = \"/path/to/tensorboard/log/dir\"\n",
|
54 |
-
"\n",
|
55 |
-
"# Define tasks and hyperparameters\n",
|
56 |
-
"# task_columns should be a list of column names from your dataset\n",
|
57 |
-
"# Each column represents a specific classification task (e.g. cell type, disease state)\n",
|
58 |
-
"task_columns = [\"cell_type\", \"disease_state\"] # Example task columns\n",
|
59 |
-
"\n",
|
60 |
-
"hyperparameters = {\n",
|
61 |
-
" \"learning_rate\": {\"type\": \"float\", \"low\": 1e-5, \"high\": 1e-3, \"log\": True},\n",
|
62 |
-
" \"warmup_ratio\": {\"type\": \"float\", \"low\": 0.005, \"high\": 0.01},\n",
|
63 |
-
" \"weight_decay\": {\"type\": \"float\", \"low\": 0.01, \"high\": 0.1},\n",
|
64 |
-
" \"dropout_rate\": {\"type\": \"float\", \"low\": 0.0, \"high\": 0.7},\n",
|
65 |
-
" \"lr_scheduler_type\": {\"type\": \"categorical\", \"choices\": [\"cosine\"]},\n",
|
66 |
-
" \"task_weights\": {\"type\": \"float\", \"low\": 0.1, \"high\": 2.0}\n",
|
67 |
-
"}"
|
68 |
-
]
|
69 |
-
},
|
70 |
-
{
|
71 |
-
"cell_type": "markdown",
|
72 |
-
"metadata": {},
|
73 |
-
"source": [
|
74 |
-
"In the code above, we've defined `task_columns` as `[\"cell_type\", \"disease_state\"]`. This means our model will be trained to classify cells based on two tasks:\n",
|
75 |
-
"1. Identifying the cell type\n",
|
76 |
-
"2. Determining the disease state\n",
|
77 |
-
"3. Note: \"unique_cell_id\" is a required column in the dataset for logging and inference purposes\n",
|
78 |
-
"\n",
|
79 |
-
"These column names should correspond to actual columns in your dataset. Each column should contain the labels for that specific classification task.\n",
|
80 |
-
"\n",
|
81 |
-
"For example, your dataset might look something like this:\n",
|
82 |
-
"\n",
|
83 |
-
" | unique_cell_id | input_ids | ... | cell_type | disease_state |\n",
|
84 |
-
" |----------------|-----------|-----|-----------|---------------|\n",
|
85 |
-
" | cell1 | ... | ... | neuron | healthy |\n",
|
86 |
-
" | cell2 | ... | ... | astrocyte | diseased |\n",
|
87 |
-
" | ... | ... | ... | ... | ... |\n",
|
88 |
-
"The model will learn to predict classes within 'cell_type' and 'disease_state' "
|
89 |
-
]
|
90 |
-
},
|
91 |
-
{
|
92 |
-
"cell_type": "markdown",
|
93 |
-
"metadata": {},
|
94 |
-
"source": [
|
95 |
-
"## 3. Initialize the MTLClassifier\n",
|
96 |
-
"\n",
|
97 |
-
"Now, let's create an instance of the MTLClassifier with our defined parameters and task columns."
|
98 |
-
]
|
99 |
-
},
|
100 |
-
{
|
101 |
-
"cell_type": "code",
|
102 |
-
"execution_count": 3,
|
103 |
-
"metadata": {},
|
104 |
-
"outputs": [],
|
105 |
-
"source": [
|
106 |
-
"mc = MTLClassifier(\n",
|
107 |
-
" task_columns=task_columns, # Our defined classification tasks\n",
|
108 |
-
" study_name=\"MTLClassifier_example\",\n",
|
109 |
-
" pretrained_path=pretrained_path,\n",
|
110 |
-
" train_path=train_path,\n",
|
111 |
-
" val_path=val_path,\n",
|
112 |
-
" test_path=test_path,\n",
|
113 |
-
" model_save_path=model_save_path,\n",
|
114 |
-
" results_dir=results_dir,\n",
|
115 |
-
" tensorboard_log_dir=tensorboard_log_dir,\n",
|
116 |
-
" hyperparameters=hyperparameters,\n",
|
117 |
-
" n_trials=15, # Number of trials for hyperparameter optimization\n",
|
118 |
-
" epochs=1, # Number of training epochs (1 suggested to prevent overfitting)\n",
|
119 |
-
" batch_size=8, # Adjust based on available GPU memory\n",
|
120 |
-
" seed=42\n",
|
121 |
-
")"
|
122 |
-
]
|
123 |
-
},
|
124 |
-
{
|
125 |
-
"cell_type": "markdown",
|
126 |
-
"metadata": {},
|
127 |
-
"source": [
|
128 |
-
"## 4. Run Hyperparameter Optimization\n",
|
129 |
-
"\n",
|
130 |
-
"Now, let's run the Optuna study to optimize our hyperparameters for both classification tasks."
|
131 |
-
]
|
132 |
-
},
|
133 |
-
{
|
134 |
-
"cell_type": "code",
|
135 |
-
"execution_count": 4,
|
136 |
-
"metadata": {},
|
137 |
-
"outputs": [],
|
138 |
-
"source": [
|
139 |
-
"mc.run_optuna_study()"
|
140 |
-
]
|
141 |
-
},
|
142 |
-
{
|
143 |
-
"cell_type": "markdown",
|
144 |
-
"metadata": {},
|
145 |
-
"source": [
|
146 |
-
"## 5. Evaluate the Model on Test Data\n",
|
147 |
-
"\n",
|
148 |
-
"After optimization, we can evaluate our model on the test dataset. This will provide performance metrics for both classification tasks. CSV containing following keys will be generated in specified results directiory \"Cell ID, task(1...n) True,task(1.,.n) Pred,task(1...n) Probabilities\""
|
149 |
-
]
|
150 |
-
},
|
151 |
-
{
|
152 |
-
"cell_type": "code",
|
153 |
-
"execution_count": 5,
|
154 |
-
"metadata": {},
|
155 |
-
"outputs": [],
|
156 |
-
"source": [
|
157 |
-
"mc.load_and_evaluate_test_model()"
|
158 |
-
]
|
159 |
-
},
|
160 |
-
{
|
161 |
-
"cell_type": "markdown",
|
162 |
-
"metadata": {},
|
163 |
-
"source": [
|
164 |
-
"## 6. (Optional) Manual Hyperparameter Tuning\n",
|
165 |
-
"\n",
|
166 |
-
"If you prefer to set hyperparameters manually, you can use the following approach:"
|
167 |
-
]
|
168 |
-
},
|
169 |
-
{
|
170 |
-
"cell_type": "code",
|
171 |
-
"execution_count": 6,
|
172 |
-
"metadata": {},
|
173 |
-
"outputs": [],
|
174 |
-
"source": [
|
175 |
-
"manual_hyperparameters = {\n",
|
176 |
-
" \"learning_rate\": 0.001,\n",
|
177 |
-
" \"warmup_ratio\": 0.01,\n",
|
178 |
-
" \"weight_decay\": 0.1,\n",
|
179 |
-
" \"dropout_rate\": 0.1,\n",
|
180 |
-
" \"lr_scheduler_type\": \"cosine\",\n",
|
181 |
-
" \"task_weights\": [1, 1], # Weights for each task (cell_type, disease_state)\n",
|
182 |
-
" \"max_layers_to_freeze\": 2\n",
|
183 |
-
"}\n",
|
184 |
-
"\n",
|
185 |
-
"mc_manual = MTLClassifier(\n",
|
186 |
-
" task_columns=task_columns,\n",
|
187 |
-
" study_name=\"mtl_manual\",\n",
|
188 |
-
" pretrained_path=pretrained_path,\n",
|
189 |
-
" train_path=train_path,\n",
|
190 |
-
" val_path=val_path,\n",
|
191 |
-
" test_path=test_path,\n",
|
192 |
-
" model_save_path=model_save_path,\n",
|
193 |
-
" results_dir=results_dir,\n",
|
194 |
-
" tensorboard_log_dir=tensorboard_log_dir,\n",
|
195 |
-
" manual_hyperparameters=manual_hyperparameters,\n",
|
196 |
-
" use_manual_hyperparameters=True,\n",
|
197 |
-
" epochs=10,\n",
|
198 |
-
" batch_size=32,\n",
|
199 |
-
" seed=42\n",
|
200 |
-
")\n",
|
201 |
-
"\n",
|
202 |
-
"mc_manual.run_manual_tuning()"
|
203 |
-
]
|
204 |
-
},
|
205 |
-
{
|
206 |
-
"cell_type": "markdown",
|
207 |
-
"metadata": {},
|
208 |
-
"source": [
|
209 |
-
"# Geneformer In Silico Perturber Tutorial (MTL Quantized)\n",
|
210 |
-
"This demonstrates how to use the Geneformer In Silico Perturber with a Multi-Task Learning (MTL) model in a quantized configuration to optimize runtime and memory."
|
211 |
-
]
|
212 |
-
},
|
213 |
-
{
|
214 |
-
"cell_type": "code",
|
215 |
-
"execution_count": 7,
|
216 |
-
"metadata": {},
|
217 |
-
"outputs": [],
|
218 |
-
"source": [
|
219 |
-
"from geneformer import InSilicoPerturber, EmbExtractor, InSilicoPerturberStats"
|
220 |
-
]
|
221 |
-
},
|
222 |
-
{
|
223 |
-
"cell_type": "code",
|
224 |
-
"execution_count": 8,
|
225 |
-
"metadata": {},
|
226 |
-
"outputs": [],
|
227 |
-
"source": [
|
228 |
-
"# Define paths\n",
|
229 |
-
"model_directory = \"/path/to/model/save/path\"\n",
|
230 |
-
"input_data_file = \"/path/to/input/data.dataset\"\n",
|
231 |
-
"output_directory = \"/path/to/output/directory\"\n",
|
232 |
-
"output_prefix = \"mtl_quantized_perturbation\"\n",
|
233 |
-
"\n",
|
234 |
-
"# Define parameters\n",
|
235 |
-
"perturb_type = \"delete\" # or \"overexpress\"\n",
|
236 |
-
"\n",
|
237 |
-
"# Define cell states to model\n",
|
238 |
-
"cell_states_to_model = {\n",
|
239 |
-
" \"state_key\": \"disease_state\", \n",
|
240 |
-
" \"start_state\": \"disease\", \n",
|
241 |
-
" \"goal_state\": \"control\"\n",
|
242 |
-
"}\n",
|
243 |
-
"\n",
|
244 |
-
"# Define filter data\n",
|
245 |
-
"filter_data_dict = {\n",
|
246 |
-
" \"cell_type\": [\"Fibroblast\"]\n",
|
247 |
-
"}"
|
248 |
-
]
|
249 |
-
},
|
250 |
-
{
|
251 |
-
"cell_type": "markdown",
|
252 |
-
"metadata": {},
|
253 |
-
"source": [
|
254 |
-
"## 3. Extract State Embeddings\n",
|
255 |
-
"\n",
|
256 |
-
"Before we initialize the InSilicoPerturber, we need to extract the state embeddings using the EmbExtractor."
|
257 |
-
]
|
258 |
-
},
|
259 |
-
{
|
260 |
-
"cell_type": "code",
|
261 |
-
"execution_count": 9,
|
262 |
-
"metadata": {},
|
263 |
-
"outputs": [],
|
264 |
-
"source": [
|
265 |
-
"# Initialize EmbExtractor\n",
|
266 |
-
"embex = EmbExtractor(\n",
|
267 |
-
" filter_data_dict=filter_data_dict,\n",
|
268 |
-
" max_ncells=1000, # Number of cells to perturb\n",
|
269 |
-
" emb_layer=0, # Use the second to last layer\n",
|
270 |
-
" emb_mode = \"cls\",\n",
|
271 |
-
" summary_stat=\"exact_mean\",\n",
|
272 |
-
" forward_batch_size=8, # Adjust based on available GPU memory\n",
|
273 |
-
" nproc=4\n",
|
274 |
-
")\n",
|
275 |
-
"\n",
|
276 |
-
"# Extract state embeddings\n",
|
277 |
-
"state_embs_dict = embex.get_state_embs(\n",
|
278 |
-
" cell_states_to_model,\n",
|
279 |
-
" model_directory=model_directory,\n",
|
280 |
-
" input_data_file=input_data_file,\n",
|
281 |
-
" output_directory=output_directory,\n",
|
282 |
-
" output_prefix=output_prefix\n",
|
283 |
-
")"
|
284 |
-
]
|
285 |
-
},
|
286 |
-
{
|
287 |
-
"cell_type": "markdown",
|
288 |
-
"metadata": {},
|
289 |
-
"source": [
|
290 |
-
"## 4. Initialize the InSilicoPerturber\n",
|
291 |
-
"\n",
|
292 |
-
"Now that we have our state embeddings, let's create an instance of the InSilicoPerturber with MTL and quantized configurations."
|
293 |
-
]
|
294 |
-
},
|
295 |
-
{
|
296 |
-
"cell_type": "code",
|
297 |
-
"execution_count": 10,
|
298 |
-
"metadata": {},
|
299 |
-
"outputs": [],
|
300 |
-
"source": [
|
301 |
-
"# Initialize InSilicoPerturber\n",
|
302 |
-
"isp = InSilicoPerturber(\n",
|
303 |
-
" perturb_type=perturb_type,\n",
|
304 |
-
" genes_to_perturb=\"all\", # Perturb all genes\n",
|
305 |
-
" model_type=\"MTLCellClassifier-Quantized\", # Use MTL model\n",
|
306 |
-
" emb_mode=\"cls\", # Use CLS token embedding\n",
|
307 |
-
" cell_states_to_model=cell_states_to_model,\n",
|
308 |
-
" state_embs_dict=state_embs_dict,\n",
|
309 |
-
" max_ncells=1000, # Number of cells to perturb\n",
|
310 |
-
" emb_layer=0, \n",
|
311 |
-
" forward_batch_size=8, # Adjust based on available GPU memory\n",
|
312 |
-
" nproc=1\n",
|
313 |
-
")"
|
314 |
-
]
|
315 |
-
},
|
316 |
-
{
|
317 |
-
"cell_type": "markdown",
|
318 |
-
"metadata": {},
|
319 |
-
"source": [
|
320 |
-
"## 5. Run In Silico Perturbation\n",
|
321 |
-
"\n",
|
322 |
-
"Run the in silico perturbation on the dataset."
|
323 |
-
]
|
324 |
-
},
|
325 |
-
{
|
326 |
-
"cell_type": "code",
|
327 |
-
"execution_count": 11,
|
328 |
-
"metadata": {},
|
329 |
-
"outputs": [],
|
330 |
-
"source": [
|
331 |
-
"# Run perturbation and output intermediate files\n",
|
332 |
-
"isp.perturb_data(\n",
|
333 |
-
" model_directory=model_directory,\n",
|
334 |
-
" input_data_file=input_data_file,\n",
|
335 |
-
" output_directory=output_directory,\n",
|
336 |
-
" output_prefix=output_prefix\n",
|
337 |
-
")"
|
338 |
-
]
|
339 |
-
},
|
340 |
-
{
|
341 |
-
"cell_type": "markdown",
|
342 |
-
"metadata": {},
|
343 |
-
"source": [
|
344 |
-
"## 6. Process Results with InSilicoPerturberStats\n",
|
345 |
-
"\n",
|
346 |
-
"After running the perturbation, we'll use InSilicoPerturberStats to process the intermediate files and generate the final statistics."
|
347 |
-
]
|
348 |
-
},
|
349 |
-
{
|
350 |
-
"cell_type": "code",
|
351 |
-
"execution_count": 12,
|
352 |
-
"metadata": {},
|
353 |
-
"outputs": [],
|
354 |
-
"source": [
|
355 |
-
"# Initialize InSilicoPerturberStats\n",
|
356 |
-
"ispstats = InSilicoPerturberStats(\n",
|
357 |
-
" mode=\"goal_state_shift\",\n",
|
358 |
-
" genes_perturbed=\"all\",\n",
|
359 |
-
" combos=0,\n",
|
360 |
-
" anchor_gene=None,\n",
|
361 |
-
" cell_states_to_model=cell_states_to_model\n",
|
362 |
-
")\n",
|
363 |
-
"\n",
|
364 |
-
"# Process stats and output final .csv\n",
|
365 |
-
"ispstats.get_stats(\n",
|
366 |
-
" input_data_file,\n",
|
367 |
-
" None,\n",
|
368 |
-
" output_directory,\n",
|
369 |
-
" output_prefix\n",
|
370 |
-
")"
|
371 |
-
]
|
372 |
-
}
|
373 |
-
],
|
374 |
-
"metadata": {
|
375 |
-
"kernelspec": {
|
376 |
-
"display_name": "Python 3 (ipykernel)",
|
377 |
-
"language": "python",
|
378 |
-
"name": "python3"
|
379 |
-
},
|
380 |
-
"language_info": {
|
381 |
-
"codemirror_mode": {
|
382 |
-
"name": "ipython",
|
383 |
-
"version": 3
|
384 |
-
},
|
385 |
-
"file_extension": ".py",
|
386 |
-
"mimetype": "text/x-python",
|
387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