{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "### OCP Data Preprocessing Tutorial\n", "\n", "\n", "This notebook provides an overview of converting ASE Atoms objects to PyTorch Geometric Data objects. To better understand the raw data contained within OC20, check out the following tutorial first: https://github.com/Open-Catalyst-Project/ocp/blob/master/docs/source/tutorials/data_visualization.ipynb" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "from ocpmodels.preprocessing import AtomsToGraphs\n", "import ase.io\n", "from ase.build import bulk\n", "from ase.build import fcc100, add_adsorbate, molecule\n", "from ase.constraints import FixAtoms\n", "from ase.calculators.emt import EMT\n", "from ase.optimize import BFGS" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Generate toy dataset: Relaxation of CO on Cu" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "adslab = fcc100(\"Cu\", size=(2, 2, 3))\n", "ads = molecule(\"CO\")\n", "add_adsorbate(adslab, ads, 3, offset=(1, 1))\n", "cons = FixAtoms(indices=[atom.index for atom in adslab if (atom.tag == 3)])\n", "adslab.set_constraint(cons)\n", "adslab.center(vacuum=13.0, axis=2)\n", "adslab.set_pbc(True)\n", "adslab.set_calculator(EMT())\n", "dyn = BFGS(adslab, trajectory=\"CuCO_adslab.traj\", logfile=None)\n", "dyn.run(fmax=0, steps=1000)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1001\n" ] } ], "source": [ "raw_data = ase.io.read(\"CuCO_adslab.traj\", \":\")\n", "print(len(raw_data))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Convert Atoms object to Data object\n", "\n", "The AtomsToGraphs class takes in several arguments to control how Data objects created:\n", "\n", "- max_neigh (int): Maximum number of neighbors a given atom is allowed to have, discarding the furthest\n", "- radius (float): Cutoff radius to compute nearest neighbors around\n", "- r_energy (bool): Write energy to Data object\n", "- r_forces (bool): Write forces to Data object\n", "- r_distances (bool): Write distances between neighbors to Data object\n", "- r_edges (bool): Write neigbhor edge indices to Data object\n", "- r_fixed (bool): Write indices of fixed atoms to Data object" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "a2g = AtomsToGraphs(\n", " max_neigh=50,\n", " radius=6,\n", " r_energy=True,\n", " r_forces=True,\n", " r_distances=False,\n", " r_edges=True,\n", " r_fixed=True,\n", ")" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "data_objects = a2g.convert_all(raw_data, disable_tqdm=True)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Data(atomic_numbers=[14], cell=[1, 3, 3], cell_offsets=[636, 3], edge_index=[2, 636], fixed=[14], force=[14, 3], natoms=14, pos=[14, 3], y=3.989314410668539)" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data = data_objects[0]\n", "data" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tensor([29., 29., 29., 29., 29., 29., 29., 29., 29., 29., 29., 29., 8., 6.])" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data.atomic_numbers" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tensor([[[ 5.1053, 0.0000, 0.0000],\n", " [ 0.0000, 5.1053, 0.0000],\n", " [ 0.0000, 0.0000, 32.6100]]])" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data.cell" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tensor([[ 1, 2, 2, ..., 4, 6, 3],\n", " [ 0, 0, 0, ..., 13, 13, 13]])" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data.edge_index #neighbor idx, source idx" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tensor([45., 45., 45., 46., 49., 49., 49., 49., 50., 49., 49., 50., 26., 35.])" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from torch_geometric.utils import degree\n", "# Degree corresponds to the number of neighbors a given node has. Note there is no more than max_neigh neighbors for\n", "# any given node.\n", "\n", "degree(data.edge_index[1]) " ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tensor([1., 1., 1., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data.fixed" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tensor([[ 9.9356e-16, 4.5465e-15, 1.1354e-01],\n", " [ 2.6749e-15, 3.7696e-15, 1.1344e-01],\n", " [ 8.4481e-16, 2.7062e-16, 1.1344e-01],\n", " [-6.6623e-18, 6.6196e-17, 1.1294e-01],\n", " [-8.5221e-03, -8.5221e-03, -1.1496e-02],\n", " [ 8.5221e-03, -8.5221e-03, -1.1496e-02],\n", " [-8.5221e-03, 8.5221e-03, -1.1496e-02],\n", " [ 8.5221e-03, 8.5221e-03, -1.1496e-02],\n", " [ 1.9082e-17, 9.6277e-16, -1.0431e-01],\n", " [-2.0583e-15, -4.3021e-16, -6.6610e-02],\n", " [-5.5511e-17, -2.3592e-15, -6.6610e-02],\n", " [-2.9409e-17, -4.3038e-15, -3.3250e-01],\n", " [ 3.3204e-19, 6.7763e-21, -3.4247e-01],\n", " [-4.5103e-17, -5.2042e-17, 5.0512e-01]])" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data.force" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tensor([[ 0.0000, 0.0000, 13.0000],\n", " [ 2.5527, 0.0000, 13.0000],\n", " [ 0.0000, 2.5527, 13.0000],\n", " [ 2.5527, 2.5527, 13.0000],\n", " [ 1.2763, 1.2763, 14.8050],\n", " [ 3.8290, 1.2763, 14.8050],\n", " [ 1.2763, 3.8290, 14.8050],\n", " [ 3.8290, 3.8290, 14.8050],\n", " [ 0.0000, 0.0000, 16.6100],\n", " [ 2.5527, 0.0000, 16.6100],\n", " [ 0.0000, 2.5527, 16.6100],\n", " [ 2.5527, 2.5527, 16.6100],\n", " [ 2.5527, 2.5527, 19.6100],\n", " [ 2.5527, 2.5527, 18.4597]])" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data.pos" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3.989314410668539" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data.y" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Adding additional info to your Data objects\n", "\n", "In addition to the above information, the OCP repo requires several other pieces of information for your data to work\n", "with the provided trainers:\n", "\n", "- sid (int): A unique identifier for a particular system. Does not affect your model performance, used for prediction saving \n", "- fid (int) (S2EF only): If training for the S2EF task, your data must also contain a unique frame identifier for atoms objects coming from the same system.\n", "- tags (tensor): Tag information - 0 for adsorbate, 1 for surface, 2 for subsurface. Optional, can be used for training.\n", "\n", "\n", "Other information may be added her as well if you choose to incorporate other information in your models/frameworks" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [], "source": [ "data_objects = []\n", "for idx, system in enumerate(raw_data):\n", " data = a2g.convert(system)\n", " data.fid = idx\n", " data.sid = 0 # All data points come from the same system, arbitrarly define this as 0\n", " data_objects.append(data)" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Data(atomic_numbers=[14], cell=[1, 3, 3], cell_offsets=[636, 3], edge_index=[2, 636], fid=100, fixed=[14], force=[14, 3], natoms=14, pos=[14, 3], sid=0, y=3.968355893395719)" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data = data_objects[100]\n", "data" ] }, { "cell_type": "code", "execution_count": 73, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 73, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data.sid" ] }, { "cell_type": "code", "execution_count": 74, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "100" ] }, "execution_count": 74, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data.fid" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Resources:\n", "\n", "- https://github.com/Open-Catalyst-Project/ocp/blob/6604e7130ea41fabff93c229af2486433093e3b4/ocpmodels/preprocessing/atoms_to_graphs.py\n", "- https://github.com/Open-Catalyst-Project/ocp/blob/master/scripts/preprocess_ef.py" ] } ], "metadata": { "kernelspec": { "display_name": "ocp-models", "language": "python", "name": "ocp-models" }, "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.8.10" } }, "nbformat": 4, "nbformat_minor": 5 }