{"cells": [{"cell_type": "markdown", "id": "title", "metadata": {}, "source": ["# IPF Colours Demo (`ImageD11.rgb_colors`)\n", "\n", "This notebook demonstrates `ImageD11.rgb_colors` on a synthetic 2D grain map with random crystal orientations:\n", "- `spacegroup_group` / `point_group` to select the Laue/point group\n", "- `ubi_to_rgb` to colour a grain map along sample X, Y, Z axes\n", "- `plot_key` to draw TSL and HSV IPF colour legends across all 11 Laue groups\n", "- `plot_ipf` to plot grain orientations on the inverse pole figure"]}, {"cell_type": "markdown", "metadata": {}, "source": "Source: [docs/rgb_colors_demo.ipynb](https://github.com/FABLE-3DXRD/ImageD11/blob/master/docs/rgb_colors_demo.ipynb). Executed during the documentation build."}, {"cell_type": "code", "execution_count": null, "id": "setup", "metadata": {}, "outputs": [], "source": ["import numpy as np\n", "import matplotlib.pyplot as plt\n", "from ImageD11 import rgb_colors, unitcell\n", "\n", "plt.rcParams[\"figure.dpi\"] = 180\n", "rng = np.random.RandomState(42)\n", "\n", "def random_rotations(n, rng):\n", " q = rng.normal(size=(n, 4))\n", " q /= np.linalg.norm(q, axis=1, keepdims=True)\n", " w, x, y, z = q.T\n", " R = np.empty((n, 3, 3))\n", " R[:, 0, 0] = 1 - 2 * (y * y + z * z)\n", " R[:, 0, 1] = 2 * (x * y - w * z)\n", " R[:, 0, 2] = 2 * (x * z + w * y)\n", " R[:, 1, 0] = 2 * (x * y + w * z)\n", " R[:, 1, 1] = 1 - 2 * (x * x + z * z)\n", " R[:, 1, 2] = 2 * (y * z - w * x)\n", " R[:, 2, 0] = 2 * (x * z - w * y)\n", " R[:, 2, 1] = 2 * (y * z + w * x)\n", " R[:, 2, 2] = 1 - 2 * (x * x + y * y)\n", " return R"]}, {"cell_type": "markdown", "id": "grainmap_md", "metadata": {}, "source": ["## 1. Synthetic grain map\n", "\n", "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}$)."]}, {"cell_type": "code", "execution_count": null, "id": "grainmap", "metadata": {}, "outputs": [], "source": ["n_grains = 30\n", "a = 4.05\n", "U = random_rotations(n_grains, rng)\n", "ubis = U.transpose(0, 2, 1) * a\n", "group = rgb_colors.spacegroup_group(225)\n", "print(\"Space group 225 -> point group:\", group)\n", "\n", "nx = 256\n", "seeds = rng.uniform(0, nx, size=(n_grains, 2))\n", "yy, xx = np.mgrid[0:nx, 0:nx]\n", "d2 = (xx[..., None] - seeds[:, 0]) ** 2 + (yy[..., None] - seeds[:, 1]) ** 2\n", "labels = np.argmin(d2, axis=2)\n", "\n", "fig, axes = plt.subplots(1, 3, figsize=(10, 3.2), constrained_layout=True)\n", "for ax, (name, axis) in zip(axes, [(\"X\", (1, 0, 0)), (\"Y\", (0, 1, 0)), (\"Z\", (0, 0, 1))]):\n", " rgb = rgb_colors.ubi_to_rgb(ubis, group, key=\"tsl\", axis=axis)\n", " ax.imshow(rgb[labels], origin=\"lower\", interpolation=\"nearest\")\n", " ax.set_title(f\"IPF-{name} (TSL, {group})\")\n", " ax.axis(\"off\")\n", "plt.show()"]}, {"cell_type": "markdown", "id": "keys_md", "metadata": {}, "source": ["## 2. All 11 Laue group colour keys (`plot_key`)\n", "\n", "TSL (left) and HSV (right) IPF keys for each of the 11 Laue groups."]}, {"cell_type": "code", "execution_count": null, "id": "keys_all", "metadata": {}, "outputs": [], "source": ["def cell_ubi(params):\n", " return np.linalg.inv(unitcell.unitcell(params, \"P\").B).T\n", "\n", "laue_specs = [\n", " (\"-1\", [4.0, 5.0, 6.0, 80, 95, 105]),\n", " (\"12/m1\", [4.0, 5.0, 6.0, 90, 105, 90]),\n", " (\"mmm\", [4.0, 5.0, 6.0, 90, 90, 90]),\n", " (\"4/m\", [4.0, 4.0, 5.5, 90, 90, 90]),\n", " (\"4/mmm\", [4.0, 4.0, 5.5, 90, 90, 90]),\n", " (\"-3\", [4.0, 4.0, 5.5, 90, 90, 120]),\n", " (\"-3m1\", [4.0, 4.0, 5.5, 90, 90, 120]),\n", " (\"6/m\", [4.0, 4.0, 5.5, 90, 90, 120]),\n", " (\"6/mmm\", [4.0, 4.0, 5.5, 90, 90, 120]),\n", " (\"m-3\", [4.0, 4.0, 4.0, 90, 90, 90]),\n", " (\"m-3m\", [4.0, 4.0, 4.0, 90, 90, 90]),\n", "]\n", "\n", "fig, axes = plt.subplots(len(laue_specs), 2, figsize=(7.5, 26), constrained_layout=True)\n", "for row, (grp, params) in zip(axes, laue_specs):\n", " ubi = cell_ubi(params)\n", " rgb_colors.plot_key(ubi, grp, key=\"tsl\", n=601, axes=[row[0]])\n", " rgb_colors.plot_key(ubi, grp, key=\"hsv\", n=601, axes=[row[1]])\n", "plt.show()"]}, {"cell_type": "markdown", "id": "ipf_md", "metadata": {}, "source": ["## 3. Inverse pole figure (`plot_ipf`)\n", "\n", "Plot the 30 random cubic grain orientations on the TSL and HSV IPF legends along sample Z."]}, {"cell_type": "code", "execution_count": null, "id": "ipf", "metadata": {}, "outputs": [], "source": ["fig, axes = plt.subplots(1, 2, figsize=(7.5, 3.4), constrained_layout=True)\n", "rgb_colors.plot_ipf(ubis, group, key=\"tsl\", axis=(0, 0, 1), n=1001, alpha=0.55, axes=[axes[0]], s=40)\n", "rgb_colors.plot_ipf(ubis, group, key=\"hsv\", axis=(0, 0, 1), n=1001, alpha=0.55, axes=[axes[1]], s=40)\n", "plt.show()"]}], "metadata": {"kernelspec": {"display_name": "Python 3", "language": "python", "name": "python3"}, "language_info": {"name": "python", "version": "3.13.0"}}, "nbformat": 4, "nbformat_minor": 5}