.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/reconstruction/PlotLFRE.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_examples_reconstruction_PlotLFRE.py: Pointwise Local Reconstruction Error ==================================== Example for the usage of the :class:`skmatter.metrics.pointwise_local_reconstruction_error` as pointwise local reconstruction error (LFRE) on the degenerate CH4 manifold. We apply the local reconstruction measure on the degenerate CH4 manifold dataset. This dataset was specifically constructed to be representable by a 4-body features (bispectrum) but not by a 3-body features (power spectrum). In other words the dataset contains environments which are different, but have the same 3-body features. For more details about the dataset please refer to `Pozdnyakov 2020 `_. The skmatter dataset already contains the 3 and 4-body features computed with `librascal `_ so we can load it and compare it with the LFRE. .. GENERATED FROM PYTHON SOURCE LINES 22-42 .. code-block:: Python import matplotlib as mpl import matplotlib.pyplot as plt import numpy as np from skmatter.datasets import load_degenerate_CH4_manifold from skmatter.metrics import pointwise_local_reconstruction_error mpl.rc("font", size=20) # load features degenerate_manifold = load_degenerate_CH4_manifold() power_spectrum_features = degenerate_manifold.data.SOAP_power_spectrum bispectrum_features = degenerate_manifold.data.SOAP_bispectrum print(degenerate_manifold.DESCR) .. rst-class:: sphx-glr-script-out .. code-block:: none .. _degenerate_manifold: Degenerate CH4 manifold ####################### The dataset contains two representations (SOAP power spectrum and bispectrum) of the two manifolds spanned by the carbon atoms of two times 81 methane structures. The SOAP power spectrum representation the two manifolds intersect creating a degenerate manifold/line for which the representation remains the same. In contrast for higher body order representations as the (SOAP) bispectrum the carbon atoms can be uniquely represented and do not create a degenerate manifold. Following the naming convention of [Pozdnyakov2020]_ for each representation the first 81 samples correspond to the X minus manifold and the second 81 samples contain the X plus manifold Function Call ------------- .. function:: skmatter.datasets.load_degenerate_CH4_manifold Data Set Characteristics ------------------------ :Number of Instances: Each representation 162 :Number of Features: Each representation 12 The representations were computed with [D1]_ using the hyperparameters: :rascal hyperparameters: +---------------------------+------------+ | key | value | +===========================+============+ | radial_basis: | "GTO" | +---------------------------+------------+ | interaction_cutoff: | 4 | +---------------------------+------------+ | max_radial: | 2 | +---------------------------+------------+ | max_angular: | 2 | +---------------------------+------------+ | gaussian_sigma_constant": | 0.5 | +---------------------------+------------+ | gaussian_sigma_type: | "Constant"| +---------------------------+------------+ | cutoff_smooth_width: | 0.5 | +---------------------------+------------+ | normalize: | False | +---------------------------+------------+ The SOAP bispectrum features were in addition reduced to 12 features with principal component analysis (PCA) [D2]_. References ---------- .. [D1] https://github.com/lab-cosmo/librascal commit 8d9ad7a .. [D2] https://scikit-learn.org/stable/modules/generated/sklearn.decomposition.PCA.html .. GENERATED FROM PYTHON SOURCE LINES 44-50 .. code-block:: Python n_local_points = 20 print("Computing pointwise LFRE...") .. rst-class:: sphx-glr-script-out .. code-block:: none Computing pointwise LFRE... .. GENERATED FROM PYTHON SOURCE LINES 51-89 .. code-block:: Python # local reconstruction error of power spectrum features using bispectrum features power_spectrum_to_bispectrum_pointwise_lfre = pointwise_local_reconstruction_error( power_spectrum_features, bispectrum_features, n_local_points, train_idx=np.arange(0, len(power_spectrum_features), 2), test_idx=np.arange(0, len(power_spectrum_features)), estimator=None, n_jobs=4, ) # local reconstruction error of bispectrum features using power spectrum features bispectrum_to_power_spectrum_pointwise_lfre = pointwise_local_reconstruction_error( bispectrum_features, power_spectrum_features, n_local_points, train_idx=np.arange(0, len(power_spectrum_features), 2), test_idx=np.arange(0, len(power_spectrum_features)), estimator=None, n_jobs=4, ) print("Computing pointwise LFRE finished.") print( "LFRE(3-body, 4-body) = ", np.linalg.norm(power_spectrum_to_bispectrum_pointwise_lfre) / np.sqrt(len(power_spectrum_to_bispectrum_pointwise_lfre)), ) print( "LFRE(4-body, 3-body) = ", np.linalg.norm(bispectrum_to_power_spectrum_pointwise_lfre) / np.sqrt(len(power_spectrum_to_bispectrum_pointwise_lfre)), ) .. rst-class:: sphx-glr-script-out .. code-block:: none Computing pointwise LFRE finished. LFRE(3-body, 4-body) = 0.17171573166237716 LFRE(4-body, 3-body) = 2.942859594116639e-10 .. GENERATED FROM PYTHON SOURCE LINES 91-129 .. code-block:: Python fig, (ax34, ax43) = plt.subplots( 1, 2, constrained_layout=True, figsize=(16, 7.5), sharey="row", sharex=True ) vmax = 0.5 X, Y = np.meshgrid(np.linspace(0.7, 0.9, 9), np.linspace(-0.1, 0.1, 9)) pcm = ax34.contourf( X, Y, power_spectrum_to_bispectrum_pointwise_lfre[81:].reshape(9, 9).T, vmin=0, vmax=vmax, ) ax43.contourf( X, Y, bispectrum_to_power_spectrum_pointwise_lfre[81:].reshape(9, 9).T, vmin=0, vmax=vmax, ) ax34.axhline(y=0, color="red", linewidth=5) ax43.axhline(y=0, color="red", linewidth=5) ax34.set_ylabel(r"v/$\pi$") ax34.set_xlabel(r"u/$\pi$") ax43.set_xlabel(r"u/$\pi$") ax34.set_title(r"$X^-$ LFRE(3-body, 4-body)") ax43.set_title(r"$X^-$ LFRE(4-body, 3-body)") cbar = fig.colorbar(pcm, ax=[ax34, ax43], label="LFRE", location="bottom") plt.show() .. image-sg:: /examples/reconstruction/images/sphx_glr_PlotLFRE_001.png :alt: $X^-$ LFRE(3-body, 4-body), $X^-$ LFRE(4-body, 3-body) :srcset: /examples/reconstruction/images/sphx_glr_PlotLFRE_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 130-140 The environments span a manifold which is described by the coordinates :math:`v/\pi` and :math:`u/\pi` (please refer to `Pozdnyakov 2020 `_ for a concrete understanding of the manifold). The LFRE is presented for each environment in the manifold in the two contour plots. It can be seen that the reconstruction error of 4-body features using 3-body features (the left plot) is most significant along the degenerate line (the horizontal red line). This agrees with the fact that the 3-body features remain the same on the degenerate line and can therefore not reconstruct the 4-body features. On the other hand the 4-body features can perfectly reconstruct the 3-body features as seen in the right plot. .. _sphx_glr_download_examples_reconstruction_PlotLFRE.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: PlotLFRE.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: PlotLFRE.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: PlotLFRE.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_