Skip to content

Instantly share code, notes, and snippets.

@thomasaarholt
Created February 22, 2019 21:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thomasaarholt/e2ba7e644638d8ef876ccf5a766c3077 to your computer and use it in GitHub Desktop.
Save thomasaarholt/e2ba7e644638d8ef876ccf5a766c3077 to your computer and use it in GitHub Desktop.
Timer unit: 1e-06 s
Total time: 0.265183 s
File: /mnt/c/Users/thomasaar/Documents/atomap/atomap/atom_finding_refining.py
Function: _make_model_from_atom_list at line 740
Line # Hits Time Per Hit % Time Line Contents
==============================================================
740 def _make_model_from_atom_list(
741 atom_list,
742 image_data,
743 percent_to_nn=0.40,
744 mask_radius=None):
745 """
746 Make a HyperSpy model from a list of Atom_Position objects and
747 an image.
748
749 Parameters
750 ----------
751 atom_list : list of Atom_Position objects
752 List of atoms to be included in the model.
753 image_data : NumPy 2D array
754 percent_to_nn : float, optional
755 mask_radius : float, optional
756 Radius of the mask around each atom. If this is not set,
757 the radius will be the distance to the nearest atom in the
758 same sublattice times the `percent_to_nn` value.
759
760 Returns
761 -------
762 model : HyperSpy model
763 Model where the atoms are added as gaussian components.
764 mask : NumPy 2D array
765 The mask from _make_mask_from_positions()
766
767 See also
768 --------
769 _fit_atom_positions_with_gaussian_model
770 fit_atom_positions_gaussian
771
772 Examples
773 --------
774 >>> import numpy as np
775 >>> from atomap.atom_position import Atom_Position
776 >>> from atomap.atom_finding_refining import _make_model_from_atom_list
777 >>> atom_list = [Atom_Position(2, 2), Atom_Position(4, 4)]
778 >>> image = np.random.random((100, 100))
779 >>> m, mask = _make_model_from_atom_list(
780 ... atom_list=atom_list, image_data=image, mask_radius=3)
781 >>> m.fit()
782
783 """
784 1 37211.0 37211.0 14.0 image_data = image_data.astype('float64')
785 1 50082.0 50082.0 18.9 mask = np.zeros_like(image_data)
786
787 1 4.0 4.0 0.0 position_list, radius_list = [], []
788 2 5.0 2.5 0.0 for atom in atom_list:
789 1 6.0 6.0 0.0 position_list.append((atom.pixel_y, atom.pixel_x))
790 1 1.0 1.0 0.0 if mask_radius is None:
791 1 34.0 34.0 0.0 mask_radius = atom.get_closest_neighbor() * percent_to_nn
792 1 2.0 2.0 0.0 radius_list.append(mask_radius)
793 1 3.0 3.0 0.0 mask = _make_mask_from_positions(
794 1 65850.0 65850.0 24.8 position_list, radius_list, image_data.shape)
795 1 50197.0 50197.0 18.9 x0, x1, y0, y1 = _crop_mask_slice_indices(mask)
796 1 15.0 15.0 0.0 mask_crop = mask[x0:x1, y0:y1].astype('bool')
797 1 44049.0 44049.0 16.6 data_mask_crop = (image_data*mask)[x0:x1, y0:y1]
798
799 1 4.0 4.0 0.0 upper_value = _find_median_upper_percentile(
800 1 400.0 400.0 0.2 data_mask_crop[mask_crop], upper_percentile=0.03)
801 1 2.0 2.0 0.0 lower_value = _find_background_value(
802 1 110.0 110.0 0.0 data_mask_crop[mask_crop], lowest_percentile=0.03)
803 1 19.0 19.0 0.0 data_mask_crop -= lower_value
804 1 22.0 22.0 0.0 data_mask_crop[data_mask_crop < 0] = 0.
805
806 1 3736.0 3736.0 1.4 s = Signal2D(data_mask_crop)
807 1 2.0 2.0 0.0 gaussian_list = []
808 2 4.0 2.0 0.0 for atom in atom_list:
809 1 1688.0 1688.0 0.6 gaussian = _atom_to_gaussian_component(atom)
810 1 3.0 3.0 0.0 if atom._gaussian_fitted:
811 1 16.0 16.0 0.0 gaussian.A.value = atom.amplitude_gaussian
812 else:
813 gaussian.A.value = upper_value*10
814 1 2.0 2.0 0.0 gaussian_list.append(gaussian)
815
816 1 206.0 206.0 0.1 s.axes_manager[0].offset = y0
817 1 122.0 122.0 0.0 s.axes_manager[1].offset = x0
818 1 11246.0 11246.0 4.2 m = s.create_model()
819 1 140.0 140.0 0.1 m.extend(gaussian_list)
820 1 2.0 2.0 0.0 return(m, mask)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment