{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example workbook for DIC <-> EBSD linking"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Load third-party packages\n",
    "import numpy as np\n",
    "%matplotlib qt\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "# Load quat, ebsd and hrdic packages from defdap package\n",
    "from defdap.quat import Quat\n",
    "import defdap.ebsd as ebsd\n",
    "import defdap.hrdic as hrdic"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Load in and display DIC map"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#Load in DIC map\n",
    "DicFilePath = \"example_data/B00005.txt\"\n",
    "DicMap = hrdic.Map(DicFilePath)\n",
    "\n",
    "#Set crop\n",
    "DicMap.setCrop(xMin=15, xMax=28, yMin=30, yMax=15)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "DicMap.setScale(micrometrePerPixel=0.02)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Display max shear map\n",
    "DicMap.plotMaxShear(plotColourBar=True, plotScaleBar=True, vmin=0, vmax=0.1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Print stats table"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "DicMap.printStatsTable(percentiles=['Min', 5, 'Mean', 95,'Max'], components = ['mss', 'f11', 'f22'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Effective shear strain histogram"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "histData = DicMap.crop(DicMap.max_shear).flatten()\n",
    "\n",
    "fig, ax = plt.subplots(1)\n",
    "\n",
    "counts, bins, bars = ax.hist(histData, bins=np.linspace(0,0.1,100), color='r', histtype='step')\n",
    "\n",
    "ax.set_xlabel(\"Effective shear strain\")\n",
    "ax.set_ylabel(\"Frequency\")\n",
    "\n",
    "fig.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Load in EBSD map then calculate local misorientation and display it"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Load in EBSD map\n",
    "EbsdFilePath = \"example_data/Map Data 2-DIC area.cpr\"\n",
    "EbsdMap = ebsd.Map(EbsdFilePath, \"cubic\")\n",
    "\n",
    "# Load in slip system definitions (optional)\n",
    "EbsdMap.loadSlipSystems(\"cubic_fcc\")\n",
    "\n",
    "# Rotate the map 180 degrees if necessary\n",
    "EbsdMap.transformData()\n",
    "\n",
    "# Create the internal array of quaternions for orientation calculations\n",
    "EbsdMap.buildQuatArray()\n",
    "\n",
    "# Find boundaries with given misorientation tolerance (in degrees)\n",
    "EbsdMap.findBoundaries(boundDef = 6)\n",
    "\n",
    "# Find grains, minGrainSize is the minimum number of pixels for a grain\n",
    "EbsdMap.findGrains(minGrainSize = 10)\n",
    "\n",
    "# Calculate grain local misorientation (Grain Reference Orientation Deviation (GROD) w.r.t grain mean orientations)\n",
    "EbsdMap.calcGrainMisOri(calcAxis = True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "EbsdMap.plotEulerMap()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "EbsdMap.plotMisOriMap(plotGBs=True, vmin=0, vmax=4)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Plot IPF of all grains mean orientation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Calculate grain mean orientations (but is already done here as part of calcGrainMisOri above)\n",
    "# EbsdMap.calcGrainAvOris()\n",
    "\n",
    "grainMeanOris = [grain.refOri for grain in EbsdMap]\n",
    "refDir = np.array([1, 0, 0])\n",
    "\n",
    "Quat.plotIPF(grainMeanOris, refDir, EbsdMap.crystalSym)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Find a grain of interest (click around the map)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "EbsdMap.locateGrainID()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Plot misorientation and IPF for isolated grain"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "currEbsdGrain = EbsdMap[EbsdMap.currGrainId]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "currEbsdGrain.plotMisOri()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "currEbsdGrain.plotMisOri(component=3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plot = currEbsdGrain.plotOriSpread(direction=refDir, c='b', s=1, alpha=0.2)\n",
    "currEbsdGrain.plotRefOri(direction=refDir, c='k', s=100, plot=plot)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Plot phase map"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "EbsdMap.plotPhaseMap()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Link EBSD map to DIC map"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Define homologous points"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Example of use of interactive tool coming soon..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Set homologous points   (x, y)\n",
    "DicMap.homogPoints = np.array((\n",
    "    (303, 408), (120, 392), (275, 25)\n",
    "))\n",
    "\n",
    "EbsdMap.homogPoints = np.array((\n",
    "    (536, 776), (236, 790), (466, 160)\n",
    "))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Display map with homologous points"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "EbsdMap.plotBoundaryMap()\n",
    "plt.scatter(x=EbsdMap.homogPoints[:, 0], y=EbsdMap.homogPoints[:, 1], c='y', s=60)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "DicMap.plotMaxShear()\n",
    "plt.scatter(x=DicMap.homogPoints[:, 0], y=DicMap.homogPoints[:, 1], c='y', s=60)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Link the EBSD and DIC map"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "DicMap.linkEbsdMap(EbsdMap)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Plot max shear with boundaries"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "DicMap.plotMaxShear(plotGBs=True, plotColourBar=True, plotScaleBar=True, vmin=0, vmax=0.1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Detect grains in the DIC map"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "DicMap.findGrains(minGrainSize=10)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Locate a grain of interest (click around)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "DicMap.locateGrainID(displaySelected=True)\n",
    "# displaySelected will show the selcted grain in a separate figure window"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Plot max shear for isolated grain"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "currDicGrain = DicMap[DicMap.currGrainId]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "currDicGrain.plotMaxShear()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Plot a histogram of it"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.figure()\n",
    "\n",
    "plt.hist(currDicGrain.maxShearList, bins=100, histtype='step');\n",
    "\n",
    "plt.xlabel(\"Effective shear strain\")\n",
    "plt.ylabel(\"Frequency\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Plot misorientation for isolated grain"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "ebsdGrainId = DicMap.ebsdGrainIds[DicMap.currGrainId]\n",
    "currEbsdGrain = EbsdMap[ebsdGrainId]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "currEbsdGrain.plotMisOri()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Plot max shear for isolated grain with slip traces"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "currDicGrain.calcSlipTraces()\n",
    "currDicGrain.plotMaxShear(plotSlipTraces=True)"
   ]
  },
  {
   "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.7.4"
  },
  "toc": {
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": "block",
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}