Skip to content

Commit

Permalink
Merge pull request #418 from lbluque/notebooks
Browse files Browse the repository at this point in the history
Example notebook cosmetic edits
  • Loading branch information
lbluque authored Sep 16, 2023
2 parents f50e2a4 + bf02e1f commit 2a47d17
Show file tree
Hide file tree
Showing 9 changed files with 249 additions and 184 deletions.
20 changes: 11 additions & 9 deletions docs/src/notebooks/ce-fit-w-centering.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -1019,14 +1019,16 @@
"ltol, stol, atol = 0.15, 0.20, 15\n",
"basis = 'sinusoid'\n",
"\n",
"cs = ClusterSubspace.from_cutoffs(structure=prim,\n",
" cutoffs=cutoffs,\n",
" ltol=ltol, \n",
" stol=stol, \n",
" angle_tol=atol,\n",
" supercell_size=('O2-'),\n",
" basis=basis, \n",
" orthonormal=False)\n",
"cs = ClusterSubspace.from_cutoffs(\n",
" structure=prim,\n",
" cutoffs=cutoffs,\n",
" ltol=ltol, \n",
" stol=stol, \n",
" angle_tol=atol,\n",
" supercell_size=('O2-'),\n",
" basis=basis, \n",
" orthonormal=False\n",
")\n",
"cs.add_external_term(EwaldTerm())"
]
},
Expand Down Expand Up @@ -1755,7 +1757,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.5"
"version": "3.10.11"
}
},
"nbformat": 4,
Expand Down
24 changes: 22 additions & 2 deletions docs/src/notebooks/cluster-visualization.ipynb

Large diffs are not rendered by default.

64 changes: 40 additions & 24 deletions docs/src/notebooks/creating-a-ce-w-electrostatics.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,12 @@
"metadata": {},
"outputs": [],
"source": [
"subspace = ClusterSubspace.from_cutoffs(lno_prim,\n",
" cutoffs={2: 5, 3: 4.1},\n",
" basis='sinusoid',\n",
" supercell_size='O2-')"
"subspace = ClusterSubspace.from_cutoffs(\n",
" lno_prim,\n",
" cutoffs={2: 5, 3: 4.1},\n",
" basis='sinusoid',\n",
" supercell_size='O2-'\n",
")"
]
},
{
Expand Down Expand Up @@ -136,13 +138,13 @@
" wrangler.get_property_vector('energy'))\n",
"\n",
"# save regression details\n",
"reg_data = RegressionData.from_sklearn(estimator,\n",
" wrangler.feature_matrix,\n",
" wrangler.get_property_vector('energy'))\n",
"reg_data = RegressionData.from_sklearn(\n",
" estimator, wrangler.feature_matrix, wrangler.get_property_vector('energy')\n",
")\n",
"# create the cluster expansion\n",
"expansion = ClusterExpansion(subspace,\n",
" coefficients=estimator.coef_,\n",
" regression_data=reg_data)"
"expansion = ClusterExpansion(\n",
" subspace, coefficients=estimator.coef_, regression_data=reg_data\n",
")"
]
},
{
Expand Down Expand Up @@ -176,10 +178,15 @@
"\n",
"train_predictions = np.dot(wrangler.feature_matrix,\n",
" expansion.coefs)\n",
"rmse = mean_squared_error(wrangler.get_property_vector('energy'),\n",
" train_predictions, squared=False)\n",
"maxer = max_error(wrangler.get_property_vector('energy'),\n",
" train_predictions)\n",
"rmse = mean_squared_error(\n",
" wrangler.get_property_vector('energy'),\n",
" train_predictions,\n",
" squared=False\n",
")\n",
"maxer = max_error(\n",
" wrangler.get_property_vector('energy'),\n",
" train_predictions\n",
")\n",
"\n",
"print(f'RMSE {1E3 * rmse} meV/prim')\n",
"print(f'MAX {1E3 * maxer} meV/prim')\n",
Expand Down Expand Up @@ -224,10 +231,15 @@
" wrangler.feature_matrix, wrangler.get_property_vector('energy')\n",
")\n",
"train_predictions = np.dot(wrangler.feature_matrix, coefs)\n",
"rmse = mean_squared_error(wrangler.get_property_vector('energy'),\n",
" train_predictions, squared=False)\n",
"maxer = max_error(wrangler.get_property_vector('energy'),\n",
" train_predictions)\n",
"rmse = mean_squared_error(\n",
" wrangler.get_property_vector('energy'),\n",
" train_predictions,\n",
" squared=False\n",
")\n",
"maxer = max_error(\n",
" wrangler.get_property_vector('energy'),\n",
" train_predictions\n",
")\n",
"\n",
"print(f'RMSE {1E3 * rmse} meV/prim')\n",
"print(f'MAX {1E3 * maxer} meV/prim')\n",
Expand Down Expand Up @@ -266,11 +278,15 @@
" wrangler.get_property_vector('energy'))\n",
"\n",
"train_predictions = np.dot(wrangler.feature_matrix, coefs)\n",
"rmse = mean_squared_error(wrangler.get_property_vector('energy'),\n",
" train_predictions, squared=False)\n",
"maxer = max_error(wrangler.get_property_vector('energy'),\n",
" train_predictions)\n",
"\n",
"rmse = mean_squared_error(\n",
" wrangler.get_property_vector('energy'),\n",
" train_predictions,\n",
" squared=False\n",
")\n",
"maxer = max_error(\n",
" wrangler.get_property_vector('energy'),\n",
" train_predictions\n",
")\n",
"\n",
"print(f'RMSE {1E3 * rmse} meV/prim')\n",
"print(f'MAX {1E3 * maxer} meV/prim')\n",
Expand Down Expand Up @@ -315,7 +331,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.5"
"version": "3.10.11"
}
},
"nbformat": 4,
Expand Down
67 changes: 40 additions & 27 deletions docs/src/notebooks/creating-a-ce.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
"Reduced Formula: Li0.5Ni1O2\n",
"abc : 2.969848 2.969848 5.143928\n",
"angles: 73.221350 73.221347 60.000002\n",
"pbc : True True True\n",
"Sites (4)\n",
" # SP a b c\n",
"--- ---------------------- ---- ---- ----\n",
Expand Down Expand Up @@ -104,7 +105,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Basis/Orthogonal/Orthonormal : sinusoid/True/{self.basis_orthonormal}\n",
"Basis/Orthogonal/Orthonormal : sinusoid/True/True\n",
" Unit Cell Composition : Li+0.5 Ni3+0.5 Ni4+0.5 O2-2\n",
" Number of Orbits : 11\n",
"No. of Correlation Functions : 11\n",
Expand All @@ -129,10 +130,12 @@
}
],
"source": [
"subspace = ClusterSubspace.from_cutoffs(lno_prim,\n",
" cutoffs={2: 5, 3: 4.1}, # will include orbits of 2 and 3 sites.\n",
" basis='sinusoid', # sets the site basis type, default is indicator\n",
" supercell_size='O2-')\n",
"subspace = ClusterSubspace.from_cutoffs(\n",
" lno_prim,\n",
" cutoffs={2: 5, 3: 4.1}, # will include orbits of 2 and 3 sites.\n",
" basis='sinusoid', # sets the site basis type, default is indicator\n",
" supercell_size='O2-'\n",
")\n",
"\n",
"# supercell_size specifies the method to determine the supercell size\n",
"# when trying to match a structure.\n",
Expand Down Expand Up @@ -194,16 +197,16 @@
"name": "stderr",
"output_type": "stream",
"text": [
"/home/lbluque/Develop/smol/smol/cofe/wrangling/wrangler.py:766: UserWarning: Unable to match Ni4+6 O2-12 with properties {} to supercell_structure. Throwing out.\n",
"/home/lbluque/Develop/smol/smol/cofe/wrangling/wrangler.py:770: UserWarning: Unable to match Ni4+6 O2-12 with energy -188.28833 to supercell_structure. Throwing out.\n",
" Error Message: Supercell could not be found from structure\n",
" warnings.warn(\n",
"/home/lbluque/Develop/smol/smol/cofe/wrangling/wrangler.py:766: UserWarning: Unable to match Li+2 Ni4+4 Ni3+2 O2-12 with properties {} to supercell_structure. Throwing out.\n",
"/home/lbluque/Develop/smol/smol/cofe/wrangling/wrangler.py:770: UserWarning: Unable to match Li+2 Ni4+4 Ni3+2 O2-12 with energy -200.13866 to supercell_structure. Throwing out.\n",
" Error Message: Mapping could not be found from structure.\n",
" warnings.warn(\n",
"/home/lbluque/Develop/smol/smol/cofe/wrangling/wrangler.py:766: UserWarning: Unable to match Li+2 Ni3+2 Ni4+4 O2-12 with properties {} to supercell_structure. Throwing out.\n",
"/home/lbluque/Develop/smol/smol/cofe/wrangling/wrangler.py:770: UserWarning: Unable to match Li+2 Ni3+2 Ni4+4 O2-12 with energy -200.42049 to supercell_structure. Throwing out.\n",
" Error Message: Mapping could not be found from structure.\n",
" warnings.warn(\n",
"/home/lbluque/Develop/smol/smol/cofe/wrangling/wrangler.py:766: UserWarning: Unable to match Li+3 Ni4+4 Ni2+1 Ni3+1 O2-12 with properties {} to supercell_structure. Throwing out.\n",
"/home/lbluque/Develop/smol/smol/cofe/wrangling/wrangler.py:770: UserWarning: Unable to match Li+3 Ni4+4 Ni2+1 Ni3+1 O2-12 with energy -206.70884 to supercell_structure. Throwing out.\n",
" Error Message: Supercell could not be found from structure\n",
" warnings.warn(\n"
]
Expand Down Expand Up @@ -251,8 +254,7 @@
"# Set fit_intercept to False because we already do this using\n",
"# the empty cluster.\n",
"estimator = LinearRegression(fit_intercept=False)\n",
"estimator.fit(wrangler.feature_matrix,\n",
" wrangler.get_property_vector('energy'))\n",
"estimator.fit(wrangler.feature_matrix, wrangler.get_property_vector('energy'))\n",
"coefs = estimator.coef_"
]
},
Expand All @@ -274,8 +276,8 @@
"name": "stdout",
"output_type": "stream",
"text": [
"RMSE 11.030076908761288 meV/prim\n",
"MAX 22.82618658175295 meV/prim\n"
"RMSE 11.03007690876058 meV/prim\n",
"MAX 22.826186581717423 meV/prim\n"
]
}
],
Expand All @@ -284,10 +286,10 @@
"\n",
"train_predictions = np.dot(wrangler.feature_matrix, coefs)\n",
"\n",
"rmse = mean_squared_error(wrangler.get_property_vector('energy'),\n",
" train_predictions, squared=False)\n",
"maxer = max_error(wrangler.get_property_vector('energy'),\n",
" train_predictions)\n",
"rmse = mean_squared_error(\n",
" wrangler.get_property_vector('energy'), train_predictions, squared=False\n",
")\n",
"maxer = max_error(wrangler.get_property_vector('energy'), train_predictions)\n",
"\n",
"print(f'RMSE {1E3 * rmse} meV/prim')\n",
"print(f'MAX {1E3 * maxer} meV/prim')"
Expand All @@ -312,7 +314,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"The predicted energy for a structure with composition Li+3 Ni3+3 Ni4+3 O2-12 is -34.47050332516923 eV/prim.\n",
"The predicted energy for a structure with composition Li+4 Ni3+4 Ni4+2 O2-12 is -35.48913608767167 eV/prim.\n",
"\n",
"The fitted coefficients are:\n",
"[-3.44424307e+01 1.52944807e+00 1.52944807e+00 -7.11937730e-02\n",
Expand All @@ -324,15 +326,15 @@
" 4.84174038e-02 1.41115811e-02 -1.54804679e-02 2.52894839e-03\n",
" -9.82873083e-03 1.34547722e-02 5.51053597e-03]\n",
"\n",
"Basis/Orthogonal/Orthonormal : sinusoid/True/{self.basis_orthonormal}\n",
"Basis/Orthogonal/Orthonormal : sinusoid/True/True\n",
" Unit Cell Composition : Li+0.5 Ni3+0.5 Ni4+0.5 O2-2\n",
" Number of Orbits : 11\n",
"No. of Correlation Functions : 11\n",
" Cluster Cutoffs : 2: 4.20, 3: 2.97\n",
" External Terms : []\n",
"Regression Data : estimator=LinearRegression\n",
" module=sklearn.linear_model._base\n",
" parameters={'copy_X': True, 'fit_intercept': False, 'n_jobs': None, 'normalize': False, 'positive': False}\n",
" parameters={'copy_X': True, 'fit_intercept': False, 'n_jobs': None, 'positive': False}\n",
"Target Property : mean=-34.6449 std=1.2993\n",
"ECI-based Property : mean=-34.4424 std=2.1655\n",
"Fit Summary\n",
Expand All @@ -351,23 +353,34 @@
" | 10 10 3 2.9698 0.006 -0.111 0.416 0.002 |\n",
" ----------------------------------------------------------------------------------------------------\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"/home/lbluque/opt/miniconda3/envs/matx_dev/lib/python3.10/site-packages/pymatgen/core/periodic_table.py:1051: UserWarning: Use of properties is now deprecated. Set the spin by setting the spin arg instead.\n",
" warnings.warn(\"Use of properties is now deprecated. Set the spin by setting the spin arg instead.\")\n"
]
}
],
"source": [
"reg_data = RegressionData.from_sklearn(\n",
" estimator, wrangler.feature_matrix,\n",
" wrangler.get_property_vector('energy'))\n",
" wrangler.get_property_vector('energy')\n",
")\n",
"\n",
"\n",
"expansion = ClusterExpansion(subspace,\n",
" coefficients=coefs,\n",
" regression_data=reg_data)\n",
"expansion = ClusterExpansion(\n",
" subspace, coefficients=coefs, regression_data=reg_data\n",
")\n",
"\n",
"structure = random.choice(wrangler.structures)\n",
"prediction = expansion.predict(structure, normalize=True)\n",
"prediction = expansion.predict(structure, normalized=True)\n",
"\n",
"print(f'The predicted energy for a structure with composition '\n",
" f'{structure.composition} is {prediction} eV/prim.\\n')\n",
"print(\n",
" f'The predicted energy for a structure with composition '\n",
" f'{structure.composition} is {prediction} eV/prim.\\n'\n",
")\n",
"print(f'The fitted coefficients are:\\n{expansion.coefs}\\n')\n",
"print(f'The effective cluster interactions are:\\n{expansion.eci}\\n')\n",
"print(expansion)"
Expand Down
Loading

0 comments on commit 2a47d17

Please sign in to comment.