.. 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.data[s.data < 120] = 1 s.filter(gaussian_filter, sigma=(0.5, 0.5, 0, 0), inplace=True) # only in real space s.template_match_disk(disk_r=3, 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 0%| | 0.00/104M [00:00 .. GENERATED FROM PYTHON SOURCE LINES 58-64 .. code-block:: Python clusterer = DBSCAN(min_samples=2, eps=20) 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 7 : Clusters Found! .. GENERATED FROM PYTHON SOURCE LINES 65-72 Visualizing the Clustering ========================== This clustering is decent. It shows that there might be some small tilt boundaries in the data which segment some of the nano-crystals into different clusters. It also shows the effect of using a phosphor screen which has some pretty severe after glow. This results in a smearing of the features and elongated clusters along the scan direction. .. GENERATED FROM PYTHON SOURCE LINES 72-75 .. code-block:: Python s.plot() s.add_marker(m) s.add_marker(p, plot_on_signal=False) .. rst-class:: sphx-glr-horizontal * .. image-sg:: /examples/vectors/images/sphx_glr_clustering_vectors_004.png :alt: MgO Nano-Crystals Navigator :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: MgO Nano-Crystals Signal :srcset: /examples/vectors/images/sphx_glr_clustering_vectors_005.png :class: sphx-glr-multi-img .. GENERATED FROM PYTHON SOURCE LINES 76-78 .. code-block:: Python # sphinx_gallery_thumbnail_number = 3 .. rst-class:: sphx-glr-timing **Total running time of the script:** (1 minutes 3.304 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 ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_