haydn-jones commited on
Commit
09a13d5
1 Parent(s): c56c1ae

Upload generate_ds.ipynb

Browse files
Files changed (1) hide show
  1. utils/generate_ds.ipynb +327 -0
utils/generate_ds.ipynb ADDED
@@ -0,0 +1,327 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": 1,
6
+ "metadata": {},
7
+ "outputs": [],
8
+ "source": [
9
+ "from datasets import load_dataset"
10
+ ]
11
+ },
12
+ {
13
+ "cell_type": "code",
14
+ "execution_count": 2,
15
+ "metadata": {},
16
+ "outputs": [],
17
+ "source": [
18
+ "data_files = {\n",
19
+ " \"train\": \"./train.txt\",\n",
20
+ " \"val\": \"./val.txt\",\n",
21
+ " \"test\": \"./test.txt\",\n",
22
+ "}\n",
23
+ "ds = load_dataset(\"text\", data_files=data_files)"
24
+ ]
25
+ },
26
+ {
27
+ "cell_type": "code",
28
+ "execution_count": 3,
29
+ "metadata": {},
30
+ "outputs": [],
31
+ "source": [
32
+ "ds['train'] = ds['train'].rename_column('text', 'SMILE')\n",
33
+ "ds['val'] = ds['val'].rename_column('text', 'SMILE')\n",
34
+ "ds['test'] = ds['test'].rename_column('text', 'SMILE')"
35
+ ]
36
+ },
37
+ {
38
+ "cell_type": "code",
39
+ "execution_count": 4,
40
+ "metadata": {},
41
+ "outputs": [],
42
+ "source": [
43
+ "import selfies as sf\n",
44
+ "\n",
45
+ "def try_convert(row):\n",
46
+ " selfie = None\n",
47
+ " try:\n",
48
+ " selfie = sf.encoder(row['SMILE'])\n",
49
+ " except:\n",
50
+ " pass\n",
51
+ "\n",
52
+ " return {'SELFIE': selfie}\n",
53
+ "\n",
54
+ "# Alongside the SMILES, we also need to convert them to SELFIES\n",
55
+ "# ds['train'] = ds['train'].add_column('SELFIE', ds['train'].map(try_convert, num_proc=8))"
56
+ ]
57
+ },
58
+ {
59
+ "cell_type": "code",
60
+ "execution_count": 5,
61
+ "metadata": {},
62
+ "outputs": [],
63
+ "source": [
64
+ "ds['train'] = ds['train'].map(try_convert, num_proc=8)\n",
65
+ "ds['val'] = ds['val'].map(try_convert, num_proc=8)\n",
66
+ "ds['test'] = ds['test'].map(try_convert, num_proc=8)"
67
+ ]
68
+ },
69
+ {
70
+ "cell_type": "code",
71
+ "execution_count": 6,
72
+ "metadata": {},
73
+ "outputs": [],
74
+ "source": [
75
+ "# Drop the rows where the conversion failed\n",
76
+ "ds['train'] = ds['train'].filter(lambda row: row['SELFIE'] is not None)\n",
77
+ "ds['val'] = ds['val'].filter(lambda row: row['SELFIE'] is not None)\n",
78
+ "ds['test'] = ds['test'].filter(lambda row: row['SELFIE'] is not None)"
79
+ ]
80
+ },
81
+ {
82
+ "cell_type": "code",
83
+ "execution_count": 21,
84
+ "metadata": {},
85
+ "outputs": [],
86
+ "source": [
87
+ "from tokenizers import Tokenizer\n",
88
+ "\n",
89
+ "tokenizer = Tokenizer.from_pretrained(\"haydn-jones/GuacamolSELFIETokenizer\")"
90
+ ]
91
+ },
92
+ {
93
+ "cell_type": "code",
94
+ "execution_count": 22,
95
+ "metadata": {},
96
+ "outputs": [
97
+ {
98
+ "data": {
99
+ "application/vnd.jupyter.widget-view+json": {
100
+ "model_id": "0ebfc58c2d8a46419df052346f288eff",
101
+ "version_major": 2,
102
+ "version_minor": 0
103
+ },
104
+ "text/plain": [
105
+ "Filter (num_proc=8): 0%| | 0/1273077 [00:00<?, ? examples/s]"
106
+ ]
107
+ },
108
+ "metadata": {},
109
+ "output_type": "display_data"
110
+ },
111
+ {
112
+ "data": {
113
+ "application/vnd.jupyter.widget-view+json": {
114
+ "model_id": "af4f73ef62ad40a7992a6f99887eaa1a",
115
+ "version_major": 2,
116
+ "version_minor": 0
117
+ },
118
+ "text/plain": [
119
+ "Filter (num_proc=8): 0%| | 0/79567 [00:00<?, ? examples/s]"
120
+ ]
121
+ },
122
+ "metadata": {},
123
+ "output_type": "display_data"
124
+ },
125
+ {
126
+ "data": {
127
+ "application/vnd.jupyter.widget-view+json": {
128
+ "model_id": "b49b45c72f3d445abf74c3694979a34b",
129
+ "version_major": 2,
130
+ "version_minor": 0
131
+ },
132
+ "text/plain": [
133
+ "Filter (num_proc=8): 0%| | 0/238698 [00:00<?, ? examples/s]"
134
+ ]
135
+ },
136
+ "metadata": {},
137
+ "output_type": "display_data"
138
+ }
139
+ ],
140
+ "source": [
141
+ "unk_id = tokenizer.token_to_id('<UNK>')\n",
142
+ "\n",
143
+ "# Drop any rows where the tokenization has an <UNK> token\n",
144
+ "ds['train'] = ds['train'].filter(lambda row: unk_id not in tokenizer.encode(row['SELFIE']).ids, num_proc=8)\n",
145
+ "ds['val'] = ds['val'].filter(lambda row: unk_id not in tokenizer.encode(row['SELFIE']).ids, num_proc=8)\n",
146
+ "ds['test'] = ds['test'].filter(lambda row: unk_id not in tokenizer.encode(row['SELFIE']).ids, num_proc=8)"
147
+ ]
148
+ },
149
+ {
150
+ "cell_type": "code",
151
+ "execution_count": 24,
152
+ "metadata": {},
153
+ "outputs": [
154
+ {
155
+ "data": {
156
+ "application/vnd.jupyter.widget-view+json": {
157
+ "model_id": "168a1aa5665f47529aea44c5f2bbbf9f",
158
+ "version_major": 2,
159
+ "version_minor": 0
160
+ },
161
+ "text/plain": [
162
+ "Saving the dataset (0/1 shards): 0%| | 0/1273077 [00:00<?, ? examples/s]"
163
+ ]
164
+ },
165
+ "metadata": {},
166
+ "output_type": "display_data"
167
+ },
168
+ {
169
+ "data": {
170
+ "application/vnd.jupyter.widget-view+json": {
171
+ "model_id": "bb77c4370aee45ec9a3cb614d1b21b93",
172
+ "version_major": 2,
173
+ "version_minor": 0
174
+ },
175
+ "text/plain": [
176
+ "Saving the dataset (0/1 shards): 0%| | 0/79564 [00:00<?, ? examples/s]"
177
+ ]
178
+ },
179
+ "metadata": {},
180
+ "output_type": "display_data"
181
+ },
182
+ {
183
+ "data": {
184
+ "application/vnd.jupyter.widget-view+json": {
185
+ "model_id": "d966547c0f8847e5aff55fbb117a33d9",
186
+ "version_major": 2,
187
+ "version_minor": 0
188
+ },
189
+ "text/plain": [
190
+ "Saving the dataset (0/1 shards): 0%| | 0/238694 [00:00<?, ? examples/s]"
191
+ ]
192
+ },
193
+ "metadata": {},
194
+ "output_type": "display_data"
195
+ }
196
+ ],
197
+ "source": [
198
+ "ds.save_to_disk('./guacamol')"
199
+ ]
200
+ },
201
+ {
202
+ "cell_type": "code",
203
+ "execution_count": 26,
204
+ "metadata": {},
205
+ "outputs": [
206
+ {
207
+ "data": {
208
+ "application/vnd.jupyter.widget-view+json": {
209
+ "model_id": "e52e2a9926b94dec81514575a0600a39",
210
+ "version_major": 2,
211
+ "version_minor": 0
212
+ },
213
+ "text/plain": [
214
+ "Uploading the dataset shards: 0%| | 0/1 [00:00<?, ?it/s]"
215
+ ]
216
+ },
217
+ "metadata": {},
218
+ "output_type": "display_data"
219
+ },
220
+ {
221
+ "data": {
222
+ "application/vnd.jupyter.widget-view+json": {
223
+ "model_id": "c65e4593a4d4434eb5017997844ff50d",
224
+ "version_major": 2,
225
+ "version_minor": 0
226
+ },
227
+ "text/plain": [
228
+ "Creating parquet from Arrow format: 0%| | 0/1274 [00:00<?, ?ba/s]"
229
+ ]
230
+ },
231
+ "metadata": {},
232
+ "output_type": "display_data"
233
+ },
234
+ {
235
+ "data": {
236
+ "application/vnd.jupyter.widget-view+json": {
237
+ "model_id": "336e610ebd324b34a793c7f373f24769",
238
+ "version_major": 2,
239
+ "version_minor": 0
240
+ },
241
+ "text/plain": [
242
+ "Uploading the dataset shards: 0%| | 0/1 [00:00<?, ?it/s]"
243
+ ]
244
+ },
245
+ "metadata": {},
246
+ "output_type": "display_data"
247
+ },
248
+ {
249
+ "data": {
250
+ "application/vnd.jupyter.widget-view+json": {
251
+ "model_id": "0b5bc569aa7c4a9c880899f6728a9d88",
252
+ "version_major": 2,
253
+ "version_minor": 0
254
+ },
255
+ "text/plain": [
256
+ "Creating parquet from Arrow format: 0%| | 0/80 [00:00<?, ?ba/s]"
257
+ ]
258
+ },
259
+ "metadata": {},
260
+ "output_type": "display_data"
261
+ },
262
+ {
263
+ "data": {
264
+ "application/vnd.jupyter.widget-view+json": {
265
+ "model_id": "2028affe9f43476caf7e785417329a65",
266
+ "version_major": 2,
267
+ "version_minor": 0
268
+ },
269
+ "text/plain": [
270
+ "Uploading the dataset shards: 0%| | 0/1 [00:00<?, ?it/s]"
271
+ ]
272
+ },
273
+ "metadata": {},
274
+ "output_type": "display_data"
275
+ },
276
+ {
277
+ "data": {
278
+ "application/vnd.jupyter.widget-view+json": {
279
+ "model_id": "f3eedbe390574443b69528830d8039af",
280
+ "version_major": 2,
281
+ "version_minor": 0
282
+ },
283
+ "text/plain": [
284
+ "Creating parquet from Arrow format: 0%| | 0/239 [00:00<?, ?ba/s]"
285
+ ]
286
+ },
287
+ "metadata": {},
288
+ "output_type": "display_data"
289
+ }
290
+ ],
291
+ "source": [
292
+ "repo_id = \"haydn-jones/Guacamol\"\n",
293
+ "\n",
294
+ "# Push the dataset to the repo\n",
295
+ "ds.push_to_hub(repo_id, token=\"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\")"
296
+ ]
297
+ },
298
+ {
299
+ "cell_type": "code",
300
+ "execution_count": null,
301
+ "metadata": {},
302
+ "outputs": [],
303
+ "source": []
304
+ }
305
+ ],
306
+ "metadata": {
307
+ "kernelspec": {
308
+ "display_name": "ddpm",
309
+ "language": "python",
310
+ "name": "python3"
311
+ },
312
+ "language_info": {
313
+ "codemirror_mode": {
314
+ "name": "ipython",
315
+ "version": 3
316
+ },
317
+ "file_extension": ".py",
318
+ "mimetype": "text/x-python",
319
+ "name": "python",
320
+ "nbconvert_exporter": "python",
321
+ "pygments_lexer": "ipython3",
322
+ "version": "3.11.6"
323
+ }
324
+ },
325
+ "nbformat": 4,
326
+ "nbformat_minor": 2
327
+ }