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_groupto select the Laue/point groupubi_to_rgbto colour a grain map along sample X, Y, Z axesplot_keyto draw TSL and HSV IPF colour legends across all 11 Laue groupsplot_ipfto 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
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()
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()