ChooseLattice#

Open in Colab

ChooseLattice displays a single 2D image and lets you click an ordered origin, a1, and a2 on it. The picked pixel coordinates and the derived lattice vectors u = a1 - origin and v = a2 - origin are exposed for downstream lattice calculations.

Tip

Run this exact notebook with the Colab badge above, or View or download this notebook on GitHub.

Hide synthetic data generation code

import numpy as np

size = 240
origin_rc = (60.0, 54.0)
u_vec = (2.0, 30.0)
v_vec = (26.0, -10.0)

rng = np.random.default_rng(0)
rows, cols = np.mgrid[0:size, 0:size]
image = 0.05 * rng.standard_normal((size, size)).astype(np.float32)
for i in range(-1, 6):
    for j in range(-1, 6):
        peak_row = origin_rc[0] + i * u_vec[0] + j * v_vec[0]
        peak_col = origin_rc[1] + i * u_vec[1] + j * v_vec[1]
        image += np.exp(
            -(((rows - peak_row) ** 2 + (cols - peak_col) ** 2) / (2 * 3.2**2))
        ).astype(np.float32)

Pick the lattice points#

Click the origin, then a1, then a2 on the image below. Wheel-zoom and drag-pan to line the click up with a specific feature, then drag an existing point to nudge it. Use the Clear Points button to start over.

from quantem.widget import ChooseLattice

widget = ChooseLattice(image, cmap="inferno", title="Synthetic atomic lattice")
widget

Read back the picked points#

origin, a1, and a2 are the raw (row, col) clicks; u and v are the derived lattice vectors a1 - origin and a2 - origin. All five are None until enough points are placed. Click the three points above first, then run the cell below yourself (it is skipped during the automated docs build since it depends on a live click).

print(f"origin: {tuple(round(x, 2) for x in widget.origin)}")
print(f"u: {tuple(round(x, 2) for x in widget.u)}")
print(f"v: {tuple(round(x, 2) for x in widget.v)}")

widget.points_array returns the same picks as an (n, 2) NumPy array, and widget.clear_points() (or the Clear Points button) resets them.