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
}