{"cells": [{"cell_type": "markdown", "metadata": {}, "source": "# weight_gv_refine"}, {"cell_type": "markdown", "metadata": {}, "source": "Source: [docs/weight_gv_refine.ipynb](https://github.com/FABLE-3DXRD/ImageD11/blob/master/docs/weight_gv_refine.ipynb). Shown as saved in git; the documentation build does not run it."}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["import numpy as np\n", "from scipy.spatial.transform import Rotation"]}, {"cell_type": "markdown", "metadata": {}, "source": ["Introduce a weighting factor into the UB/UBI refinement. Effectively like adding in the contribution of the same peak over and over again.\n", "\n", "This weighting factor could be:\n", "- the pixel by pixel intensity ... this is just taking the center of mass position in g-vector space\n", "- a robust function which downweights outliers ... seems non-linear or iterative for two grains (which one do you pick for the solution?)"]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["def robust_weights(UBI, gv, sigma = 0.005, eta = 0.5 ):\n", " \"\"\" \n", " Given an approximate UBI and experimental g-vectors ...\n", " Computes integer hkl : hint= VVVVVVVVVVVVVVVVVV\n", " Finds computed g-vectors : dot(UB), round(dot(UBI,gv))\n", " gerr**2 = sum_{xyz} (gobs - gcalc)**2 \n", " Weights are:\n", " Gauss term : exp( -gerr^2 / (2*sigma*sigma) ) ... local\n", " Cauchy term : sigma*sigma / (sigma*sigma + )\n", " Computes p = exp()\n", " \n", " \"\"\"\n", " hint = np.round( np.dot( UBI, gv ) )\n", " gcalc = np.dot( np.linalg.inv(UBI), hint )\n", " dg2 = ((gv - gcalc)**2).sum( axis = 0 )\n", " isig = 1/sigma\n", " # probability this peak is indexed : Gaussian errors\n", " pg = np.exp( - dg2*(isig*isig/2) ) # exp(0) == 1\n", " # probability this peak is indexed : Cauchy errors\n", " pe = sigma * sigma / ( sigma * sigma + dg2 )\n", " p = pg * eta + pe * (1-eta)\n", " pm = p.max()\n", " assert pm <= 1 and pm > 0\n", " return hint, p, dg2.mean() # estimate for what sigma might be\n", "\n", "def weighted_refine(hkl, gv, weights ):\n", " \"\"\"\n", " Refine an orientation matrix with weights\n", "\n", " Based on Paciorek et al Acta A55 543 (1999)\n", " UB = R H-1\n", " ... add a weighting per peak (like adding the peak w times)\n", " \n", " where:\n", " R = sum_n r_n h_n^t\n", " H = sum_n h_n h_n^t\n", " w = weights for each peak\n", " r = g-vectors\n", " h = hkl indices\n", " \"\"\"\n", " R = np.zeros((3,3),float)\n", " H = np.zeros((3,3),float)\n", " assert gv.shape[0] == 3\n", " for w,h,g in zip(weights, hkl.T, gv.T ):\n", " R += (np.outer(g,h)*w)\n", " H += (np.outer(h,h)*w)\n", " HI = np.linalg.pinv(H, hermitian=True) # might fail\n", " UBoptimal = np.dot(R,HI)\n", " UBIo = np.linalg.inv(UBoptimal)\n", " return UBIo, UBoptimal, R, H"]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["np.random.seed(13)\n", "a = np.random.normal(loc=0,scale=1,size=3)\n", "r = Rotation.from_euler('xyz', a, degrees=True )\n", "a0 = 5.43094\n", "ubi0 = np.eye(3)*a0\n", "ubi1 = np.dot( ubi0, r.as_dcm() )\n", "print(ubi0,'\\n', ubi1)"]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["hi = np.mgrid[ -4:5, -4:5, -4:5 ]\n", "hi = hi.reshape(3, -1)\n", "ub0 = np.linalg.inv( ubi0 )\n", "g0 = np.dot( ub0, hi )\n", "ub1 = np.linalg.inv( ubi1 )\n", "g1 = np.dot( ub1, hi )"]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["g0.T[3],g1.T[3]"]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["gall = np.concatenate( (g0, g1), axis=1 )\n", "gall.shape"]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["w = np.ones( len(gall[0]) )\n", "ubiavg = 0.5*(ubi0 + ubi1)"]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["m = (ubi0+ubi1*2)/3\n", "e = 1e-4\n", "for i in range(10):\n", " hint, w, e = robust_weights( m, gall, e, eta=0.5)\n", " ret = weighted_refine( hint, gall, 1-w )\n", " m = ret[0]\n", " print('e',e, 'err',((ubi0 - m)**2).sum(), ((ubi1 - m)**2).sum(), end=' ')\n", " ret = weighted_refine( hint, gall, w )\n", " m = ret[0]\n", " print('e',e, 'err',((ubi0 - m)**2).sum(), ((ubi1 - m)**2).sum())"]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": []}], "metadata": {"kernelspec": {"display_name": "Python 3", "language": "python", "name": "python3"}, "language_info": {"codemirror_mode": {"name": "ipython", "version": 3}, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.10"}}, "nbformat": 4, "nbformat_minor": 4}