IPF Colours Demo (ImageD11.rgb_colors)#

This notebook demonstrates ImageD11.rgb_colors on a synthetic 2D grain map with random crystal orientations:

  • spacegroup_group / point_group to select the Laue/point group

  • ubi_to_rgb to colour a grain map along sample X, Y, Z axes

  • plot_key to draw TSL and HSV IPF colour legends across all 11 Laue groups

  • plot_ipf to plot grain orientations on the inverse pole figure

Source: docs/rgb_colors_demo.ipynb. Executed during the documentation build.

import numpy as np
import matplotlib.pyplot as plt
from ImageD11 import rgb_colors, unitcell

plt.rcParams["figure.dpi"] = 180
rng = np.random.RandomState(42)

def random_rotations(n, rng):
    q = rng.normal(size=(n, 4))
    q /= np.linalg.norm(q, axis=1, keepdims=True)
    w, x, y, z = q.T
    R = np.empty((n, 3, 3))
    R[:, 0, 0] = 1 - 2 * (y * y + z * z)
    R[:, 0, 1] = 2 * (x * y - w * z)
    R[:, 0, 2] = 2 * (x * z + w * y)
    R[:, 1, 0] = 2 * (x * y + w * z)
    R[:, 1, 1] = 1 - 2 * (x * x + z * z)
    R[:, 1, 2] = 2 * (y * z - w * x)
    R[:, 2, 0] = 2 * (x * z - w * y)
    R[:, 2, 1] = 2 * (y * z + w * x)
    R[:, 2, 2] = 1 - 2 * (x * x + y * y)
    return R

1. Synthetic grain map#

Build a 256x256 Voronoi-style grain map with 30 grains of a cubic phase (space group 225, Fm-3m, lattice parameter \(a = 4.05\,\text{\AA}\)).

n_grains = 30
a = 4.05
U = random_rotations(n_grains, rng)
ubis = U.transpose(0, 2, 1) * a
group = rgb_colors.spacegroup_group(225)
print("Space group 225 -> point group:", group)

nx = 256
seeds = rng.uniform(0, nx, size=(n_grains, 2))
yy, xx = np.mgrid[0:nx, 0:nx]
d2 = (xx[..., None] - seeds[:, 0]) ** 2 + (yy[..., None] - seeds[:, 1]) ** 2
labels = np.argmin(d2, axis=2)

fig, axes = plt.subplots(1, 3, figsize=(10, 3.2), constrained_layout=True)
for ax, (name, axis) in zip(axes, [("X", (1, 0, 0)), ("Y", (0, 1, 0)), ("Z", (0, 0, 1))]):
    rgb = rgb_colors.ubi_to_rgb(ubis, group, key="tsl", axis=axis)
    ax.imshow(rgb[labels], origin="lower", interpolation="nearest")
    ax.set_title(f"IPF-{name} (TSL, {group})")
    ax.axis("off")
plt.show()
Space group 225 -> point group: m-3m
../_images/99ad58eed63324e5019ca183bbffc5e7c2a332b4841dbd172ecef617cca34c49.png

2. All 11 Laue group colour keys (plot_key)#

TSL (left) and HSV (right) IPF keys for each of the 11 Laue groups.

def cell_ubi(params):
    return np.linalg.inv(unitcell.unitcell(params, "P").B).T

laue_specs = [
    ("-1", [4.0, 5.0, 6.0, 80, 95, 105]),
    ("12/m1", [4.0, 5.0, 6.0, 90, 105, 90]),
    ("mmm", [4.0, 5.0, 6.0, 90, 90, 90]),
    ("4/m", [4.0, 4.0, 5.5, 90, 90, 90]),
    ("4/mmm", [4.0, 4.0, 5.5, 90, 90, 90]),
    ("-3", [4.0, 4.0, 5.5, 90, 90, 120]),
    ("-3m1", [4.0, 4.0, 5.5, 90, 90, 120]),
    ("6/m", [4.0, 4.0, 5.5, 90, 90, 120]),
    ("6/mmm", [4.0, 4.0, 5.5, 90, 90, 120]),
    ("m-3", [4.0, 4.0, 4.0, 90, 90, 90]),
    ("m-3m", [4.0, 4.0, 4.0, 90, 90, 90]),
]

fig, axes = plt.subplots(len(laue_specs), 2, figsize=(7.5, 26), constrained_layout=True)
for row, (grp, params) in zip(axes, laue_specs):
    ubi = cell_ubi(params)
    rgb_colors.plot_key(ubi, grp, key="tsl", n=601, axes=[row[0]])
    rgb_colors.plot_key(ubi, grp, key="hsv", n=601, axes=[row[1]])
plt.show()
../_images/03090dfaef91dc5da567da8f10b112ddf8cc68c6e8b41f4f55e2221e9de74576.png

3. Inverse pole figure (plot_ipf)#

Plot the 30 random cubic grain orientations on the TSL and HSV IPF legends along sample Z.

fig, axes = plt.subplots(1, 2, figsize=(7.5, 3.4), constrained_layout=True)
rgb_colors.plot_ipf(ubis, group, key="tsl", axis=(0, 0, 1), n=1001, alpha=0.55, axes=[axes[0]], s=40)
rgb_colors.plot_ipf(ubis, group, key="hsv", axis=(0, 0, 1), n=1001, alpha=0.55, axes=[axes[1]], s=40)
plt.show()
../_images/7f14a85f57b75595fb7904b75982696cd42eda06fa11177934fe66a83dc76608.png