.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/vectors/clustering_vectors.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_vectors_clustering_vectors.py: ================== Clustering Vectors ================== This can be used to segment a 4-D STEM dataset into different clusters based on the diffraction pattern at each real space position. .. GENERATED FROM PYTHON SOURCE LINES 9-27 .. code-block:: Python import pyxem as pxm from scipy.ndimage import gaussian_filter import matplotlib.pyplot as plt from sklearn.cluster import DBSCAN # Getting the vectors for some dataset # s = pxm.data.mgo_nanocrystals(allow_download=True) s = pxm.data.simulated_overlap() s.filter(gaussian_filter, sigma=(0.5, 0.5, 0, 0), inplace=True) # only in real space s.template_match_disk(disk_r=5, subtract_min=False, inplace=True) vectors = s.get_diffraction_vectors(threshold_abs=0.5, min_distance=3) # Now we can convert the vectors into a 2D array of rows/columns flat_vectors = ( vectors.flatten_diffraction_vectors() ) # flatten the vectors into a 2D array scan = DBSCAN(eps=1.0, min_samples=2) .. rst-class:: sphx-glr-script-out .. code-block:: none /home/docs/checkouts/readthedocs.org/user_builds/pyxem/envs/1139/lib/python3.11/site-packages/diffsims/generators/sphere_mesh_generators.py:523: RuntimeWarning: invalid value encountered in divide phi2 = sign * np.nan_to_num(np.arccos(x_comp / norm_proj)) 0%| | 0/9 [00:00 .. GENERATED FROM PYTHON SOURCE LINES 58-64 .. code-block:: Python clusterer = DBSCAN(min_samples=2, eps=4) clustered2 = clustered.cluster_labeled_vectors(method=clusterer) m, p = clustered2.to_markers(s, alpha=0.8, get_polygons=True) .. rst-class:: sphx-glr-script-out .. code-block:: none 36 : Clusters Found! .. GENERATED FROM PYTHON SOURCE LINES 65-70 Visualizing the Clustering ========================== This clustering is works pretty good after the second step. We can interact with the results as well in order to see regions where the clustering doesn't work quite as well! .. GENERATED FROM PYTHON SOURCE LINES 72-81 .. code-block:: Python m, p = clustered2.to_markers( s, alpha=0.8, get_polygons=True, facecolor="none", sizes=(30,), lw=5 ) s.axes_manager.indices = (45, 45) s.plot() s.add_marker(m) s.add_marker(p, plot_on_signal=False) # sphinx_gallery_thumbnail_number = 5 .. rst-class:: sphx-glr-horizontal * .. image-sg:: /examples/vectors/images/sphx_glr_clustering_vectors_004.png :alt: clustering vectors :srcset: /examples/vectors/images/sphx_glr_clustering_vectors_004.png :class: sphx-glr-multi-img * .. image-sg:: /examples/vectors/images/sphx_glr_clustering_vectors_005.png :alt: Signal :srcset: /examples/vectors/images/sphx_glr_clustering_vectors_005.png :class: sphx-glr-multi-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 23.226 seconds) .. _sphx_glr_download_examples_vectors_clustering_vectors.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: clustering_vectors.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: clustering_vectors.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: clustering_vectors.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_