File size: 22,896 Bytes
b311d31
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<br>\n",
    "<p align=\"center\">\n",
    "<img src=\"./logo.png\" alt= \"MXgap logo\" width=600>\n",
    "</p>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h1 style=\"text-align: center;\"> MXgap: A Machine Learning Program to predict MXene Bandgaps\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`mxgap` is a computational tool designed to streamline electronic structure calculations for MXenes using hybrid functionals like PBE0. By employing Machine Learning (ML) models, `mxgap` predicts the PBE0 bandgap based on features extracted from a PBE calculation. Aside from its CLI interface, it can also be used as an imported module. In this Notebook some examples are found."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1. Getting Started"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Predictions can be made either using the `run_prediction()` or the `ML_prediction()` functions. The `run_prediction()` receives the same arguments as in the CLI and does input validation, and the runs `ML_prediction()` internally. While the `ML_prediction()` will directly run the prediction with the ML model chosen. Both will return the prediction (or predictions when choosing a C+R model combination) in a list, and write a file (`mxgap.info`) in the selected path folder with a report of the calculation."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "For example, to use the best model available (a combination of GBC classifier and RFR regressor):"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "====================================================================\n",
      "                            MXgap Report                           \n",
      "====================================================================\n",
      "\n",
      "Date:            2025-02-19 12:48:34\n",
      "Model Used:      GBC+RFR_onlygap\n",
      "Folder Path:     .\n",
      "CONTCAR file:    ./CONTCAR\n",
      "DOSCAR file:     ./DOSCAR\n",
      "Output Path:     ./mxgap.info\n",
      "\n",
      "====================================================================\n",
      "    \n",
      "Predicted ML_isgap  =  1 (Semiconductor)\n",
      "Predicted ML_gap    =  1.961\n",
      "\n",
      "Finished successfully in 1.22s\n"
     ]
    }
   ],
   "source": [
    "from mxgap import run_prediction\n",
    "\n",
    "path         = \".\"                  # Path to the folder where the CONTCAR and DOSCAR are present\n",
    "model        = \"GBC+RFR_onlygap\"    # \"best\" or \"default\" can also be used to get the best model.\n",
    "prediction   =  run_prediction(path = path, model = model)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The direct paths for the CONTCAR and DOSCAR can also be given, with the files argument:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "====================================================================\n",
      "                            MXgap Report                           \n",
      "====================================================================\n",
      "\n",
      "Date:            2025-02-19 12:48:45\n",
      "Model Used:      GBC+RFR_onlygap\n",
      "Folder Path:     None\n",
      "CONTCAR file:    ./CONTCAR\n",
      "DOSCAR file:     ./DOSCAR\n",
      "Output Path:     ./mxgap.info\n",
      "\n",
      "====================================================================\n",
      "    \n",
      "Predicted ML_isgap  =  1 (Semiconductor)\n",
      "Predicted ML_gap    =  1.961\n",
      "\n",
      "Finished successfully in 0.11s\n"
     ]
    }
   ],
   "source": [
    "from mxgap import run_prediction\n",
    "\n",
    "files        = [\"./CONTCAR\",\"./DOSCAR\"]     # List with the CONTCAR and DOSCAR files\n",
    "model        = \"GBC+RFR_onlygap\"            # \"best\" or \"default\" can also be used to get the best model.\n",
    "prediction   =  run_prediction(files = files, model = model)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "And the same can be done with the `ML_prediction()` function:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Predicted ML_isgap  =  1 (Semiconductor)\n",
      "Predicted ML_gap    =  1.961\n"
     ]
    }
   ],
   "source": [
    "from mxgap import ML_prediction\n",
    "\n",
    "contcar_path    = \"./CONTCAR\"            # Path to the CONTCAR file\n",
    "doscar_path     = \"./DOSCAR\"             # Path to the DOSCAR file\n",
    "model           = \"GBC+RFR_onlygap\"      # ML model\n",
    "prediction      =  ML_prediction(contcar_path,doscar_path,model)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If a classifier is used, return_proba=True can be passed to the function to also extract the probability of semiconductor class (p>=0.5: Semiconductor, p<0.5: Metallic), given by sklearn model.predict_proba():"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "No ML model detected. The GBC+RFR_onlygap model (most accurate) will be used.\n",
      "\n",
      "====================================================================\n",
      "                            MXgap Report                           \n",
      "====================================================================\n",
      "\n",
      "Date:            2025-02-19 12:48:51\n",
      "Model Used:      GBC+RFR_onlygap\n",
      "Folder Path:     .\n",
      "CONTCAR file:    ./CONTCAR\n",
      "DOSCAR file:     ./DOSCAR\n",
      "Output Path:     ./mxgap.info\n",
      "\n",
      "====================================================================\n",
      "    \n",
      "Predicted ML_isgap  =  1 (Semiconductor)\n",
      "Class probability   =  0.999\n",
      "Predicted ML_gap    =  1.961\n",
      "\n",
      "Finished successfully in 0.12s\n"
     ]
    }
   ],
   "source": [
    "from mxgap import run_prediction\n",
    "\n",
    "path         = \".\"                  # Path to the folder where the CONTCAR and DOSCAR are present\n",
    "prediction   =  run_prediction(path = path, return_proba=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "There are several models available, between Classifiers and Regressors (and can be combined). Generally, the models that are not trained with DOS information (_notDOS) are faster and do not require the DOSCAR file, but the results are less accurate. We recommend using the default model \"GBC+RFR_onlygap\", which is a combination of a Classifier (metallic/semiconductor) and a Regressor (bandgap prediction). More info about the ML models in the models/ folder."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Classifiers\t     Regressors (full)    Regressors (only gap) Regressors (edges)\n",
      "GBC                  GBR                  GBR_onlygap          \tGBR_edges           \n",
      "RFC                  RFR                  RFR_onlygap          \tRFR_edges           \n",
      "SVC                  SVR                  SVR_onlygap          \tSVR_edges           \n",
      "MLPC                 MLPR                 MLPR_onlygap         \tMLPR_edges          \n",
      "LR                   KRR                  KRR_onlygap          \tKRR_edges            \n",
      "GBC_notDOS           GBR_notDOS           GBR_onlygap_notDOS   \tGBR_edges_notDOS    \n",
      "RFC_notDOS           RFR_notDOS           RFR_onlygap_notDOS   \tRFR_edges_notDOS    \n",
      "SVC_notDOS           SVR_notDOS           SVR_onlygap_notDOS   \tSVR_edges_notDOS    \n",
      "MLPC_notDOS          MLPR_notDOS          MLPR_onlygap_notDOS  \tMLPR_edges_notDOS   \n",
      "LR_notDOS            KRR_notDOS           KRR_onlygap_notDOS   \tKRR_edges_notDOS    \n",
      "\n"
     ]
    }
   ],
   "source": [
    "from mxgap.utils import load_models_list\n",
    "\n",
    "models_list, models_list_string = load_models_list()\n",
    "print(models_list_string)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2. Batch calculations"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The program can be used in batch to quickly screen different MXenes. Here is done for the examples available in the `test/examples/` folder, but you can use whatever paths you need:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "from mxgap import run_prediction\n",
    "\n",
    "examples_folder = \"../mxgap/test/examples/\" \n",
    "paths   = [examples_folder + e for e in os.listdir(examples_folder)]\n",
    "\n",
    "for mxene_path in paths:\n",
    "    print(mxene_path)\n",
    "    prediction = run_prediction(mxene_path)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3. Feature extraction"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If needed, you can easily extract the feature arrays that the ML models uses to predict the bandgap:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 1.          0.          0.          4.00767035  6.52237803  1.78952712\n",
      "  1.78952712  2.92510078  2.92510078  3.9986449   3.9986449   2.74218817\n",
      "  2.74218817 57.          3.          6.          1.1         0.5575462\n",
      "  2.43        1.95        6.         14.          2.55        1.26211361\n",
      "  1.7         0.7        17.         17.          3.          3.16\n",
      "  3.61272528  1.75        1.        ]\n",
      "[-2.74500000e+00 -2.10400000e+00  6.41000000e-01  0.00000000e+00\n",
      "  0.00000000e+00  0.00000000e+00  0.00000000e+00  0.00000000e+00\n",
      "  0.00000000e+00  0.00000000e+00  1.61024000e+00  5.43692000e+00\n",
      "  1.81104800e+01  9.98550000e+00  4.97735000e+00  1.30065220e+00\n",
      "  3.16461800e+01  2.04537200e+01  2.33658200e+01  3.86112040e+00\n",
      "  0.00000000e+00  0.00000000e+00  0.00000000e+00  0.00000000e+00\n",
      "  0.00000000e+00  0.00000000e+00  0.00000000e+00  0.00000000e+00\n",
      "  0.00000000e+00  0.00000000e+00  0.00000000e+00  0.00000000e+00\n",
      "  0.00000000e+00  0.00000000e+00  0.00000000e+00  0.00000000e+00\n",
      "  0.00000000e+00  0.00000000e+00  0.00000000e+00  0.00000000e+00\n",
      "  0.00000000e+00  0.00000000e+00  6.07590000e-03  5.93588600e-01\n",
      "  1.31054400e+00  2.08536800e+00  1.14187200e+01  4.94334000e+00\n",
      "  7.30122000e+00  1.18353400e+01  6.63112000e+00  6.48510000e+00\n",
      "  7.42786400e+00  0.00000000e+00  0.00000000e+00  0.00000000e+00\n",
      "  0.00000000e+00  0.00000000e+00  0.00000000e+00  3.99274000e-01\n",
      "  8.94706000e-01  1.18128600e+00  1.46785000e+00  1.76110800e+00\n",
      "  1.95160000e+00  2.89672000e+00  2.35256000e+00  1.68090000e+00\n",
      "  1.61701600e+00  1.44153800e+00  7.78752000e-01  6.21248000e-01\n",
      "  7.60290000e-01  2.36160800e+00  5.01682000e+00  4.22500000e+00\n",
      "  4.37516000e+00  2.96292000e+00  3.73232000e+00  1.44625020e+01\n",
      "  1.96832000e+01  2.36281060e+01  4.44126962e+01  1.70180400e+01\n",
      "  0.00000000e+00  0.00000000e+00  0.00000000e+00  0.00000000e+00\n",
      "  0.00000000e+00  0.00000000e+00  0.00000000e+00  0.00000000e+00\n",
      "  0.00000000e+00  0.00000000e+00  0.00000000e+00  0.00000000e+00\n",
      "  0.00000000e+00  0.00000000e+00  0.00000000e+00  0.00000000e+00\n",
      "  0.00000000e+00  0.00000000e+00  0.00000000e+00]\n"
     ]
    }
   ],
   "source": [
    "from mxgap.features import get_elemental_array, get_doscar_array\n",
    "\n",
    "# Non-normalized arrays from CONTCAR and DOSCAR files\n",
    "contcar_array   = get_elemental_array(\"./CONTCAR\")    # periodic table + structural features from the CONTCAR file\n",
    "doscar_array    = get_doscar_array(\"./DOSCAR\")        # DOS features extracted from the DOSCAR\n",
    "print(contcar_array,doscar_array,sep=\"\\n\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The ML models actually recieve a normalized version of these arrays, achieved with the `make_data_array()` function, which takes care of everything:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0.00000000e+00,  0.00000000e+00,  0.00000000e+00,  9.09631933e-01,\n",
       "        2.51629672e-01,  5.91180702e-01,  5.91425345e-01,  7.28368395e-01,\n",
       "        7.30668148e-01,  8.51341178e-01,  8.53155363e-01,  6.02698551e-01,\n",
       "        6.33307511e-01,  6.79245283e-01,  0.00000000e+00,  1.00000000e+00,\n",
       "       -1.05263158e-01,  5.72541818e-01,  1.42307692e+00,  1.33333333e+00,\n",
       "        0.00000000e+00,  0.00000000e+00,  0.00000000e+00,  1.00000000e+00,\n",
       "        1.00000000e+00,  1.00000000e+00,  3.07692308e-01,  1.00000000e+00,\n",
       "        5.00000000e-01,  5.63829787e-01,  1.00000000e+00,  6.77083333e-01,\n",
       "        6.52173913e-01,  2.06422535e-01,  2.78647887e-01,  3.02930057e-01,\n",
       "        0.00000000e+00,  0.00000000e+00,  0.00000000e+00,  0.00000000e+00,\n",
       "        0.00000000e+00,  0.00000000e+00,  0.00000000e+00,  1.61024000e+00,\n",
       "        5.43692000e+00,  1.81104800e+01,  9.98550000e+00,  4.97735000e+00,\n",
       "        1.30065220e+00,  3.16461800e+01,  2.04537200e+01,  2.33658200e+01,\n",
       "        3.86112040e+00,  0.00000000e+00,  0.00000000e+00,  0.00000000e+00,\n",
       "        0.00000000e+00,  0.00000000e+00,  0.00000000e+00,  0.00000000e+00,\n",
       "        0.00000000e+00,  0.00000000e+00,  0.00000000e+00,  0.00000000e+00,\n",
       "        0.00000000e+00,  0.00000000e+00,  0.00000000e+00,  0.00000000e+00,\n",
       "        0.00000000e+00,  0.00000000e+00,  0.00000000e+00,  0.00000000e+00,\n",
       "        0.00000000e+00,  0.00000000e+00,  0.00000000e+00,  6.07590000e-03,\n",
       "        5.93588600e-01,  1.31054400e+00,  2.08536800e+00,  1.14187200e+01,\n",
       "        4.94334000e+00,  7.30122000e+00,  1.18353400e+01,  6.63112000e+00,\n",
       "        6.48510000e+00,  7.42786400e+00,  0.00000000e+00,  0.00000000e+00,\n",
       "        0.00000000e+00,  0.00000000e+00,  0.00000000e+00,  0.00000000e+00,\n",
       "        3.99274000e-01,  8.94706000e-01,  1.18128600e+00,  1.46785000e+00,\n",
       "        1.76110800e+00,  1.95160000e+00,  2.89672000e+00,  2.35256000e+00,\n",
       "        1.68090000e+00,  1.61701600e+00,  1.44153800e+00,  7.78752000e-01,\n",
       "        6.21248000e-01,  7.60290000e-01,  2.36160800e+00,  5.01682000e+00,\n",
       "        4.22500000e+00,  4.37516000e+00,  2.96292000e+00,  3.73232000e+00,\n",
       "        1.44625020e+01,  1.96832000e+01,  2.36281060e+01,  4.44126962e+01,\n",
       "        1.70180400e+01,  0.00000000e+00,  0.00000000e+00,  0.00000000e+00,\n",
       "        0.00000000e+00,  0.00000000e+00,  0.00000000e+00,  0.00000000e+00,\n",
       "        0.00000000e+00,  0.00000000e+00,  0.00000000e+00,  0.00000000e+00,\n",
       "        0.00000000e+00,  0.00000000e+00,  0.00000000e+00,  0.00000000e+00,\n",
       "        0.00000000e+00,  0.00000000e+00,  0.00000000e+00,  0.00000000e+00])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from mxgap.features import make_data_array\n",
    "from mxgap.utils import load_normalization\n",
    "\n",
    "# Final feature array, the one that the model actually reads\n",
    "norm_x_contcar, norm_x_doscar, norm_y = load_normalization()    # We need normalization constants\n",
    "data_array = make_data_array(\"CONTCAR\",\"DOSCAR\",needDOS=True,norm_x_contcar=norm_x_contcar,norm_x_doscar=norm_x_doscar)\n",
    "data_array\n",
    "# The DOS part is acctually not normalized, to conserve the different number of electrons between systems"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 4. Prediction from data"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The program is designed for VASP outputs (CONTCAR and DOSCAR), but if you use a different software, you can manually extract the feature arrays and use them with the `prediction_from_data()` function.  \n",
    "\n",
    "The `elemental_array`, which includes periodic table and structural features, can be extracted directly from the geometry file using the `get_elemental_array()` function. Since this function utilizes ASE, it should correctly extract the feature array as long as the geometry file is supported by ASE and represents a p(1×1) cell.  \n",
    "\n",
    "On the other hand, the DOS must be parsed manually to create the `dos_array`. This requires extracting the total DOS, energy (corrected with \\(E_f\\)!), and Fermi level (\\(E_f\\)). Functions from the `mxgap.dos` module can then be used to extract key information such as bandgap and histogram. In the end, the `dos_array` can be created using `np.concatenate([[VBM, CBM, Eg], DOS_hist])`.  \n",
    "\n",
    "Below is an example using FHI-AIMS output files:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "from mxgap.ML import prediction_from_data\n",
    "from mxgap.features import get_elemental_array\n",
    "from mxgap.dos import get_bandgap, make_histogram\n",
    "\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Predicted ML_isgap  =  1 (Semiconductor)\n",
      "Class probability   =  0.999\n",
      "Predicted ML_gap    =  1.961\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[1, 1.961, 0.999]"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model = \"GBC+RFR_onlygap\"   # Optional. By default it will select the best model\n",
    "\n",
    "# FHI-AIMS files\n",
    "geometry_file = \"geometry.in\"\n",
    "dos_file = \"KS_DOS_total.dat\"\n",
    "\n",
    "# Getting elemental array (periodic table + structure)\n",
    "# Since this is done through ASE, the function already accepts different formats than VASP\n",
    "elemental_array = get_elemental_array(geometry_file)\n",
    "\n",
    "# Getting dos array (VBM_PBE, CBM_PBE, Eg_PBE, DOS_hist)\n",
    "# For this, read the DOS file and extract the DOS and E (corrected with Ef!)\n",
    "E,dos_up,dos_down = np.loadtxt(dos_file).T  # FHI-AIMS already gives you the Ef corrected energies\n",
    "DOS = dos_up + dos_down\n",
    "\n",
    "with open(dos_file, \"r\") as f:\n",
    "    f.readline()                        # Skip first line\n",
    "    second_line = f.readline().split()  # Read and split second line\n",
    "Ef = float(second_line[-2])\n",
    "\n",
    "# Functions from mxgap.dos can be used to extract the bandgap and make the histogram\n",
    "Eg = get_bandgap(E,DOS)\n",
    "VBM,CBM = round(Ef,3), round(Ef+Eg,3)\n",
    "DOS_hist, E_hist = make_histogram(E,DOS)\n",
    "dos_array = np.concatenate([[VBM, CBM, Eg],DOS_hist])\n",
    "\n",
    "# Run prediction from the created arrays\n",
    "prediction_from_data(elemental_array,dos_array,model=model,return_proba=True)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 5. Structure"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "To get all the structural informaiton, a `Structure()` object class was created that inherites from `ase.Atoms`. This has all the properties of `ase.Atoms` plus some extra functionality thought for MXenes, like get the stacking and hollows, add a termination to the surface, get the *M*, *X*, *T* positions or symbols separately, ... Here are some examples:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from mxgap.structure import Structure\n",
    "\n",
    "structure = Structure(\"CONTCAR\")\n",
    "\n",
    "## Sets vacuum to M2X or M2XT2 structure.\n",
    "structure.add_vacuum(vacuum=30)\n",
    "\n",
    "## Shifts the slab a certain amount\n",
    "structure.shift(3)\n",
    "\n",
    "## Shifts to zero/origin all the atoms \n",
    "structure.to_zero()\n",
    "\n",
    "## Separated M, X, T atoms\n",
    "M_pos,X_pos,T_pos = structure.getMXT()                          # By positions\n",
    "M_symbols,X_symbols,T_symbols =structure.getMXT(symbols=True)   # By symbols\n",
    "\n",
    "## Get stacking and T hollow position\n",
    "stack, hollows = structure.get_stack_hollows()\n",
    "\n",
    "## Adds Termination to structure.\n",
    "structure.addT(\"O\",hollow=\"HX\")\n",
    "structure.addT(\"H\",hollow=\"HX\")\n",
    "\n",
    "## Write as a new POSCAR file\n",
    "structure.write(\"POSCAR_new\",\"vasp\",direct=True)\n",
    "\n",
    "## Convert to FHI-AIMS geometry.in\n",
    "structure.write(\"geometry.in\",\"aims\",scaled=True)\n",
    "\n",
    "## Extracts geometry parameters (lattice parameter and width, with extra=True also bond distances, etc)\n",
    "geom = structure.get_geom(extra=True)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "mxgap",
   "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.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}