{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"pycharm": {
"name": "#%% md\n"
}
},
"source": [
"# DefDAP Example notebook\n",
"\n",
"This notebook will outline basic usage of DefDAP, including loading a DIC and EBSD map, linking them with homologous points and producing maps"
]
},
{
"cell_type": "markdown",
"metadata": {
"pycharm": {
"name": "#%% md\n"
}
},
"source": [
"## Load in packages"
]
},
{
"cell_type": "markdown",
"metadata": {
"pycharm": {
"name": "#%% md\n"
}
},
"source": [
"DefDAP is split into modules for processing EBSD (`defdap.ebsd`) and HRDIC (`defdap.hrdic`) data. There are also modules for manpulating orientations (`defdap.quat`) and creating custom figures (`defdap.plotting`) which is introduced later. We also import some of the usual suspects of the python scientific stack: `numpy` and `matplotlib`."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"pycharm": {
"name": "#%%\n"
},
"tags": []
},
"outputs": [],
"source": [
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"\n",
"import defdap.hrdic as hrdic\n",
"import defdap.ebsd as ebsd\n",
"from defdap.quat import Quat\n",
"\n",
"# try tk, qt, osx (if using mac) or notebook for interactive plots. If none work, use inline\n",
"%matplotlib widget"
]
},
{
"cell_type": "markdown",
"metadata": {
"pycharm": {
"name": "#%% md\n"
}
},
"source": [
"## Load in a HRDIC map"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [],
"source": [
"dic_filepath = \"../tests/data/testDataDIC.txt\"\n",
"dic_map = hrdic.Map(dic_filepath)"
]
},
{
"cell_type": "markdown",
"metadata": {
"pycharm": {
"name": "#%% md\n"
}
},
"source": [
"### Set the scale of the map\n",
"This is defined as the pixel size in the DIC pattern images, measured in microns per pixel."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [],
"source": [
"field_width = 20 # microns\n",
"num_pixels = 2048\n",
"dic_map.set_scale(field_width / num_pixels)"
]
},
{
"cell_type": "markdown",
"metadata": {
"pycharm": {
"name": "#%% md\n"
}
},
"source": [
"### Plot the map with a scale bar"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [],
"source": [
"dic_map.plot_map('max_shear', vmin=0, vmax=0.10, plot_scale_bar=True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can print out the names of all data currently available in the map. Try plotting different data, `plot_map` has a parameter `component` that is either an int `0` or tuple of ints `(0,1)` for tensor components or a named component such as `'norm'`."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(dic_map.data)"
]
},
{
"cell_type": "markdown",
"metadata": {
"pycharm": {
"name": "#%% md\n"
}
},
"source": [
"### Crop the map\n",
"HRDIC maps often contain spurious data at the edges which should be removed before performing any analysis. The crop is defined by the number of points to remove from each edge of the map. Note that the test data doesn not require cropping as it is a subset of a larger dataset."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [],
"source": [
"dic_map.set_crop(left=0, right=0, top=0, bottom=0)"
]
},
{
"cell_type": "markdown",
"metadata": {
"pycharm": {
"name": "#%% md\n"
}
},
"source": [
"### Statistics\n",
"Some simple statistics such as the minimum, mean and maximum of the effective shear strain, E11 and E22 components can be printed."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [],
"source": [
"dic_map.print_stats_table(percentiles=[0, 50, 100], components=['max_shear', 'e'])"
]
},
{
"cell_type": "markdown",
"metadata": {
"pycharm": {
"name": "#%% md\n"
}
},
"source": [
"### Set the location of the DIC pattern images \n",
"The pattern images are used later to define the position of homologous material points. The path is relative to the directory set when loading in the map. The second parameter is the pixel binning factor of the image relative to the DIC sub-region size i.e. the number of pixels in the image across a single datapoint in the DIC map. We recommend binning the pattern images by the same factor as the DIC sub-region size, doing so enhances the contrast between microstructure features."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [],
"source": [
"# set the path of the pattern image, this is relative to the location of the DIC data file\n",
"dic_map.set_pattern(\"testDataPat.bmp\", 1)"
]
},
{
"cell_type": "markdown",
"metadata": {
"pycharm": {
"name": "#%% md\n"
}
},
"source": [
"## Load in an EBSD map\n",
"Currently, OxfordBinary (a .crc and .cpr file pair), OxfordText (.ctf file), EdaxAng (.ang file) or PythonDict (Python dictionary) filetypes are supported. The crystal structure and slip systems are automatically loaded for each phase in the map. The orientation in the EBSD are converted to a quaternion representation so calculations can be applied later."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [],
"source": [
"ebsd_map = ebsd.Map(\"../tests/data/testDataEBSD.cpr\")"
]
},
{
"cell_type": "markdown",
"metadata": {
"pycharm": {
"name": "#%% md\n"
}
},
"source": [
"A list of detected phases and crystal structures can be printed"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [],
"source": [
"for i, phase in enumerate(ebsd_map.phases):\n",
" print(i+1)\n",
" print(phase)"
]
},
{
"cell_type": "markdown",
"metadata": {
"pycharm": {
"name": "#%% md\n"
}
},
"source": [
"A list of the slip planes, colours and slip directions can be printed"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [],
"source": [
"ebsd_map.phases[0].print_slip_systems()"
]
},
{
"cell_type": "markdown",
"metadata": {
"pycharm": {
"name": "#%% md\n"
}
},
"source": [
"### Plot the EBSD map\n",
"Using an Euler colour mapping or inverse pole figure colouring with the sample reference direction passed as a vector."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [],
"source": [
"ebsd_map.plot_map('euler_angle', 'all_euler', plot_scale_bar=True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [],
"source": [
"ebsd_map.plot_map('orientation', 'IPF_x', plot_scale_bar=True)"
]
},
{
"cell_type": "markdown",
"metadata": {
"pycharm": {
"name": "#%% md\n"
}
},
"source": [
"A KAM map can also be plotted as follows"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [],
"source": [
"ebsd_map.plot_map('KAM', vmin=0, vmax=2*np.pi/180)"
]
},
{
"cell_type": "markdown",
"metadata": {
"pycharm": {
"name": "#%% md\n"
}
},
"source": [
"### Detect grains in the EBSD\n",
"This is done in two stages: first bounaries are detected in the map as any point with a misorientation to a neighbouring point greater than a critical value (`boundDef` in degrees). A flood fill type algorithm is then applied to segment the map into grains, with any grains containining fewer than a critical number of pixels removed (`minGrainSize` in pixels). The data e.g. orientations associated with each grain are then stored (referenced strictly, the data isn't stored twice) in a grain object and a list of the grains is stored in the EBSD map (named `grainList`). This allows analysis routines to be applied to each grain in a map in turn."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [],
"source": [
"ebsd_map.data.generate('grain_boundaries', misori_tol=8)\n",
"ebsd_map.data.generate('grains', min_grain_size=10)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now when we print the available data there is a section for dervied data, this comes from data defined at the grain level. This derived data can be from different sources and later you will see data shared between linked HRDIC and EBSD maps."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(ebsd_map.data)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can use this derived data as with other map data to plots maps, statistics or directly access the data as a numpy array."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The Schmid factors for each grain can be calculated and plotted. The `slip_systems` argument can be specified, to only calculate the Schmid factor for certain planes, otherwise the maximum for all slip systems is calculated.\n",
"\n",
"Try changing the loading direction."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [],
"source": [
"ebsd_map.calc_average_grain_schmid_factors(\n",
" load_vector=np.array([1,0,0]), \n",
" slip_systems=None\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [],
"source": [
"ebsd_map.plot_average_grain_schmid_factors_map()"
]
},
{
"cell_type": "markdown",
"metadata": {
"pycharm": {
"name": "#%% md\n"
}
},
"source": [
"### Single grain analysis\n",
"The `locate_grain` method allows interactive selection of a grain of intereset to apply any analysis to. Clicking on grains in the map will highlight the grain and print out the grain ID (position in the grain list) of the grain."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"ebsd_map.locate_grain()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(f'Grain ID of last selected grain: {ebsd_map.sel_grain.grain_id}')"
]
},
{
"cell_type": "markdown",
"metadata": {
"pycharm": {
"name": "#%% md\n"
}
},
"source": [
"A built-in example is to calculate the average orientation of the grain and plot this orientation in a IPF"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [],
"source": [
"grain_id = 48\n",
"grain = ebsd_map[grain_id]\n",
"grain.calc_average_ori() # stored as a quaternion named grain.refOri\n",
"print(grain.ref_ori)\n",
"grain.plot_ref_ori(direction=[0, 0, 1])"
]
},
{
"cell_type": "markdown",
"metadata": {
"pycharm": {
"name": "#%% md\n"
}
},
"source": [
"The spread of orientations in a given grain can also be plotted on an IPF"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [],
"source": [
"plot = grain.plot_ori_spread(direction=np.array([0, 0, 1]), c='b', s=1, alpha=0.2)\n",
"grain.plot_ref_ori(direction=[0, 0, 1], c='k', plot=plot)"
]
},
{
"cell_type": "markdown",
"metadata": {
"pycharm": {
"name": "#%% md\n"
}
},
"source": [
"The unit cell for the average grain orientation can also be ploted"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [],
"source": [
"grain.plot_unit_cell()"
]
},
{
"cell_type": "markdown",
"metadata": {
"pycharm": {
"name": "#%% md\n"
}
},
"source": [
"Printing a list of the slip plane indices, angle of slip plane intersection with the screen (defined as counter-clockwise from upwards), colour defined for the slip plane and also the slip directions and corresponding Schmid factors, is also built in"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [],
"source": [
"grain.print_slip_traces()"
]
},
{
"cell_type": "markdown",
"metadata": {
"pycharm": {
"name": "#%% md\n"
}
},
"source": [
"A second built-in example is to calcuate the grain misorientation, specifically the grain reference orientation deviation (GROD). This shows another feature of the `locate_grain` method, which stores the last selected grain in a variable called `sel_grain` in the EBSD map."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [],
"source": [
"if ebsd_map.sel_grain is None: \n",
" ebsd_map.sel_grain = ebsd_map[57]\n",
" \n",
"ebsd_map.sel_grain.build_mis_ori_list()\n",
"ebsd_map.sel_grain.plot_mis_ori(plot_scale_bar=True, vmin=0, vmax=3)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can also explore and visulaise all data available for a grain"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"grain_id = 40\n",
"grain = ebsd_map[grain_id]\n",
"print(grain.data)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"grain.plot_map('band_contrast')"
]
},
{
"cell_type": "markdown",
"metadata": {
"pycharm": {
"name": "#%% md\n"
}
},
"source": [
"### Multi grain analysis\n",
"Once an analysis routine has been prototyped for a single grain it can be applied to all the grains in a map using a loop over the grains and any results added to a list for use later. Of couse you could also apply to a smaller subset of grains as well."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [],
"source": [
"grain_av_oris = []\n",
"for grain in ebsd_map:\n",
" grain.calc_average_ori()\n",
" grain_av_oris.append(grain.ref_ori)\n",
"\n",
"# Plot all the grain orientations in the map\n",
"Quat.plot_ipf(grain_av_oris, [0, 0, 1], ebsd_map.crystal_sym, marker='o', s=10)\n",
"plt.tight_layout()"
]
},
{
"cell_type": "markdown",
"metadata": {
"pycharm": {
"name": "#%% md\n"
}
},
"source": [
"Some common grain analysis routines are built into the EBSD map object, including:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [],
"source": [
"ebsd_map.calc_grain_av_oris()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [],
"source": [
"ebsd_map.calc_grain_mis_ori()\n",
"ebsd_map.plot_mis_ori_map(vmin=0, vmax=5, plot_gbs=True, plot_scale_bar=True)"
]
},
{
"cell_type": "markdown",
"metadata": {
"pycharm": {
"name": "#%% md\n"
}
},
"source": [
"There are also methods for plotting GND density, phases and boundaries. All of the plotting functions in DefDAP use the same parameters to modify the plot, examples seen so far are `plot_gbs`, `plotScaleBar`, `vmin`, `vmax`."
]
},
{
"cell_type": "markdown",
"metadata": {
"pycharm": {
"name": "#%% md\n"
}
},
"source": [
"## Linking the HRDIC and EBSD\n",
"### Define homologous points\n",
"To register the two datasets, homologous points (points at the same material location) within each map are used to estimate a transformation between the two frames the data are defined in. The homologous points are selected manually using an interactive tool within DefDAP. To select homologous call the method `set_homog_point` on each of the data maps, which will open a plot window with a button labelled 'save point' in the bottom right. You select a point by right clicking on the map, adjust the position with the arrow and accept the point by with the save point button. Then select the same location in the other map. Note that as we set the location of the pattern image for the HRDIC map that the points can be selected on the pattern image rather than the strain data.\n",
"\n",
"Select 3-4 homologous points in spread over each map in the same order."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [],
"source": [
"dic_map.set_homog_point(map_name=\"pattern\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [],
"source": [
"ebsd_map.set_homog_point()"
]
},
{
"cell_type": "markdown",
"metadata": {
"pycharm": {
"name": "#%% md\n"
}
},
"source": [
"The points are stored as a list of tuples `(x, y)` in each of the maps. This means the points can be set from previous values."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [],
"source": [
"dic_map.frame.homog_points"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [],
"source": [
"ebsd_map.frame.homog_points"
]
},
{
"cell_type": "markdown",
"metadata": {
"pycharm": {
"name": "#%% md\n"
}
},
"source": [
"Here are some example homologous points for this data, after setting these by running the cells below you can view the locations in the maps by running the `set_homog_point` methods (above) again. These will not be in the correct location if the crop values of the HRDIC map have been changed from 0."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [],
"source": [
"dic_map.frame.homog_points = [\n",
" (36, 72), \n",
" (279, 27), \n",
" (162, 174), \n",
" (60, 157)\n",
"]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [],
"source": [
"ebsd_map.frame.homog_points = [\n",
" (68, 95), \n",
" (308, 45), \n",
" (191, 187), \n",
" (89, 174)\n",
"]"
]
},
{
"cell_type": "markdown",
"metadata": {
"pycharm": {
"name": "#%% md\n"
}
},
"source": [
"### Link the maps\n",
"Finally the two data maps are linked. The type of transform between the two frames can be affine, projective, polynomial.\n",
"\n",
"Try the different projections and see if the make any difference to the percieved transoformation for the points you selected."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [],
"source": [
"dic_map.link_ebsd_map(ebsd_map, transform_type=\"affine\")\n",
"# dic_map.link_ebsd_map(ebsd_map, transform_type=\"projective\")\n",
"# dic_map.link_ebsd_map(ebsd_map, transform_type=\"polynomial\", order=2)"
]
},
{
"cell_type": "markdown",
"metadata": {
"pycharm": {
"name": "#%% md\n"
}
},
"source": [
"### Show the transformation"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [],
"source": [
"from skimage import transform as tf\n",
"\n",
"data = np.zeros((2000, 2000), dtype=float)\n",
"data[500:1500, 500:1500] = 1.\n",
"transform = dic_map.experiment.get_frame_transform(dic_map.frame, ebsd_map.frame)\n",
"dataWarped = tf.warp(data, transform)\n",
"\n",
"fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(8,4))\n",
"ax1.set_title('Reference')\n",
"ax1.imshow(data)\n",
"ax2.set_title('Transformed')\n",
"ax2.imshow(dataWarped)"
]
},
{
"cell_type": "markdown",
"metadata": {
"pycharm": {
"name": "#%% md\n"
}
},
"source": [
"### Segment into grains\n",
"The HRDIC map can now be segmented into grains using the grain boundaries detected in the EBSD map. Analysis rountines can then be applied to individual grain, as with the EBSD grains. The grain finding process will also attempt to link the grains between the EBSD and HRDIC and each grain in the HRDIC has a reference (`ebsdGrain`) to the corrosponding grain in the EBSD map."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [],
"source": [
"dic_map.data.generate('grains', min_grain_size=10)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"dic_map.plot_map(\n",
" 'max_shear', vmin=0, vmax=0.10, \n",
" plot_scale_bar=True, plot_gbs='pixel'\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {
"pycharm": {
"name": "#%% md\n"
}
},
"source": [
"Now, a grain can also be selected interactively in the DIC map, in the same way a grain can be selected from an EBSD map. If `display_grain` is set to true, then a plot shows the map segmented for the grain with coloured lines to display the slip trace direction of the set slip systems (see colours `grain.print_slip_traces()`) and black lines marking direction of slip bands detected in the grain."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [],
"source": [
"dic_map.locate_grain(display_grain=True)"
]
},
{
"cell_type": "markdown",
"metadata": {
"pycharm": {
"name": "#%% md\n"
}
},
"source": [
"## Plotting examples"
]
},
{
"cell_type": "markdown",
"metadata": {
"pycharm": {
"name": "#%% md\n"
}
},
"source": [
"Some of the plotting features are shown in examples below."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Built-in plots\n",
"These are the plotting functions you have been using so far, run by calling methods of the data objects. Each method returns a plot object that can be used to modify the plot after it has been created."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [],
"source": [
"plot = dic_map.plot_map(\n",
" 'max_shear', vmin=0, vmax=0.10, \n",
" plot_scale_bar=True, plot_gbs='line'\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [],
"source": [
"plot = ebsd_map.plot_map(\n",
" 'euler_angle', component='all_euler',\n",
" plot_scale_bar=True, plot_gbs=True,\n",
" highlight_grains=[10, 20, 45], highlight_alpha=0.9, highlight_colours=['r']\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [],
"source": [
"dic_grain_id = 42\n",
"dic_grain = dic_map[dic_grain_id]\n",
"\n",
"plot = dic_grain.plot_map(\n",
" 'max_shear', plot_scale_bar=True, \n",
" plot_slip_traces=True, plot_slip_bands=True\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {
"pycharm": {
"name": "#%% md\n"
}
},
"source": [
"### IPF plotting"
]
},
{
"cell_type": "markdown",
"metadata": {
"pycharm": {
"name": "#%% md\n"
}
},
"source": [
"This plot will show the positions of selected grains in an IPF pole figure, with the marker size representing grain area and mean effective shear strain."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [],
"source": [
"# For all grains in the DIC map\n",
"\n",
"# Make an array of quaternions\n",
"grain_oris = [grain.ebsd_grain.ref_ori for grain in dic_map]\n",
"\n",
"# Make an array of grain area\n",
"grain_areas = np.array([len(grain) for grain in dic_map]) * dic_map.scale**2\n",
"\n",
"# Scaling the grain area, so that the maximum size of a marker is 200 points^2\n",
"grain_area_scaling = 200. / grain_areas.max()\n",
"\n",
"# Make an array of mean effective shear strain\n",
"grain_strains = [np.array(grain.data.max_shear).mean() for grain in dic_map]\n",
"\n",
"plot = Quat.plot_ipf(\n",
" grain_oris, direction=[1,0,0], sym_group='cubic', \n",
" marker='o', s=grain_areas*grain_area_scaling, \n",
" c=grain_strains, vmin=0, vmax=0.018, cmap='viridis'\n",
")\n",
"plot.add_colour_bar(label='Mean effective shear strain')\n",
"plot.add_legend(scaling=grain_area_scaling)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [],
"source": [
"# For selected grains in the DIC map\n",
"\n",
"# Select grains from the DIC map\n",
"dic_grain_ids = [\n",
" 2, 5, 7, 9, 15, 17, 18, 23, 29, 32, \n",
" 33, 37, 40, 42, 49, 50, 51, 54, 58, 60\n",
"]\n",
"\n",
"# Make an array of quaternions\n",
"grain_oris = np.array([\n",
" dic_map[grain_id].ebsd_grain.ref_ori for grain_id in dic_grain_ids\n",
"])\n",
"\n",
"# Make an array of grain area\n",
"grain_areas = np.array([\n",
" len(dic_map[grain_id]) for grain_id in dic_grain_ids\n",
"]) * dic_map.scale**2\n",
"\n",
"# Scaling the grain area, so that the maximum size of a marker is 200 points^2\n",
"grain_area_scaling = 200. / grain_areas.max()\n",
"\n",
"# Make an array of mean effective shear strain\n",
"grain_strains = np.array([\n",
" np.mean(dic_map[grain].data.max_shear) for grain in dic_grain_ids\n",
"])\n",
"\n",
"plot = Quat.plot_ipf(\n",
" grain_oris, direction=[1,0,0], sym_group='cubic', \n",
" marker='o', s=grain_areas*grain_area_scaling, \n",
" c=grain_strains, vmin=0, vmax=0.018, cmap='viridis'\n",
")\n",
"plot.add_colour_bar(label='Mean effective shear strain')\n",
"plot.add_legend(scaling=grain_area_scaling)"
]
},
{
"cell_type": "markdown",
"metadata": {
"pycharm": {
"name": "#%% md\n"
}
},
"source": [
"### Create your own"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [],
"source": [
"from defdap.plotting import MapPlot, GrainPlot, HistPlot"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [],
"source": [
"map_data = dic_map.data['e'][0,0]\n",
"map_data = dic_map.crop(map_data)\n",
"\n",
"plot = MapPlot.create(\n",
" dic_map, map_data,\n",
" vmin=-0.1, vmax=0.1, plot_colour_bar=True, cmap=\"seismic\",\n",
" plot_gbs=True, dilate_boundaries=True, boundary_colour='black'\n",
")\n",
"\n",
"plot.add_scale_bar()"
]
},
{
"cell_type": "markdown",
"metadata": {
"pycharm": {
"name": "#%% md\n"
}
},
"source": [
"### Functions for grain averaging and grain segmentation"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [],
"source": [
"plot = dic_map.plot_grain_data_map(\n",
" map_data,\n",
" vmin=-0.06, vmax=0.06, plot_colour_bar=True,\n",
" cmap=\"seismic\", clabel=\"Axial strain ($e_11$)\",\n",
" plot_scale_bar=True\n",
")\n",
"\n",
"plot.add_grain_boundaries(dilate=True, colour=\"white\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [],
"source": [
"plot = dic_map.plot_grain_data_ipf(\n",
" np.array((1,0,0)), map_data, marker='o', \n",
" vmin=-0.06, vmax=0.06, plot_colour_bar=True, \n",
" clabel=\"Axial strain ($e_11$)\", cmap=\"seismic\",\n",
")\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [],
"source": [
"dic_grain_id = 42\n",
"dic_grain = dic_map[dic_grain_id]\n",
"\n",
"plot = dic_grain.plot_grain_data(\n",
" map_data, \n",
" vmin=-0.1, vmax=0.1, plot_colour_bar=True, \n",
" clabel=\"Axial strain ($e_11$)\", cmap=\"seismic\",\n",
" plot_scale_bar=True\n",
")\n",
"\n",
"plot.add_slip_traces()"
]
},
{
"cell_type": "markdown",
"metadata": {
"pycharm": {
"name": "#%% md\n"
}
},
"source": [
"### Composite plots"
]
},
{
"cell_type": "markdown",
"metadata": {
"pycharm": {
"name": "#%% md\n"
}
},
"source": [
"By utilising some additional functionality within matplotlib, composite plots can be produced."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [],
"source": [
"from matplotlib import gridspec"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [],
"source": [
"# Create a figure with 3 sets of axes\n",
"fig = plt.figure(figsize=(8, 4))\n",
"gs = gridspec.GridSpec(2, 2, width_ratios=[3, 1],\n",
" wspace=0.15, hspace=0.15, \n",
" left=0.02, right=0.98,\n",
" bottom=0.12, top=0.95) \n",
"ax0 = plt.subplot(gs[:, 0])\n",
"ax1 = plt.subplot(gs[0, 1])\n",
"ax2 = plt.subplot(gs[1, 1])\n",
"\n",
"\n",
"# add a strain map\n",
"plot0 = dic_map.plot_map(\n",
" map_name='max_shear',\n",
" ax=ax0, fig=fig, \n",
" vmin=0, vmax=0.08, plot_scale_bar=True, \n",
" plot_gbs=True, dilate_boundaries=True\n",
")\n",
"\n",
"# add an IPF of grain orientations\n",
"dic_oris = []\n",
"for grain in dic_map:\n",
" if len(grain) > 20:\n",
" dic_oris.append(grain.ref_ori)\n",
"plot1 = Quat.plot_ipf(\n",
" dic_oris, np.array((1,0,0)), 'cubic', \n",
" ax=ax1, fig=fig, s=10\n",
")\n",
"\n",
"# add histrogram of strain values\n",
"plot2 = HistPlot.create(\n",
" dic_map.crop(dic_map.data.max_shear),\n",
" ax=ax2, fig=fig, marker='o', markersize=2,\n",
" axes_type=\"logy\", bins=50, range=(0,0.06)\n",
")\n",
"plot2.ax.set_xlabel(\"Effective shear strain\")"
]
},
{
"cell_type": "markdown",
"metadata": {
"pycharm": {
"name": "#%% md\n"
}
},
"source": [
"Figures can be saved to raster (png, jpg, ..) and vector formats (eps, svg), the format is guessed from the file extension given. The last displayed figure can be saved using:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [],
"source": [
"#plt.savefig(\"test_save_fig.png\", dpi=200)\n",
"#plt.savefig(\"test_save_fig.eps\", dpi=200)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [],
"source": [
"fig, ((ax0, ax1), (ax2, ax3)) = plt.subplots(2, 2, figsize=(8, 6))\n",
"\n",
"dic_grain_id = 42\n",
"dic_grain = dic_map[dic_grain_id]\n",
"\n",
"# add a strain map\n",
"plot0 = dic_grain.plot_map(\n",
" 'max_shear',\n",
" ax=ax0, fig=fig, \n",
" vmin=0, vmax=0.08, plot_scale_bar=True,\n",
" plot_slip_traces=True\n",
")\n",
"\n",
"\n",
"# add a misorientation\n",
"ebsd_grain = dic_grain.ebsd_grain\n",
"plot1 = ebsd_grain.plot_mis_ori(component=0, ax=ax1, fig=fig, vmin=0, vmax=1, clabel=\"GROD\", plot_scale_bar=True)\n",
"\n",
"\n",
"# add an IPF\n",
"plot2 = ebsd_grain.plot_ori_spread(\n",
" direction=np.array((1,0,0)), c='b', s=1, alpha=0.2,\n",
" ax=ax2, fig=fig\n",
")\n",
"ebsd_grain.plot_ref_ori(\n",
" direction=np.array((1,0,0)), c='k', s=100, plot=plot2\n",
")\n",
"\n",
"# add histrogram of strain values\n",
"plot3 = HistPlot.create(\n",
" dic_map.crop(dic_map.data.max_shear),\n",
" ax=ax3, fig=fig,\n",
" axes_type=\"logy\", bins=50, range=(0,0.06))\n",
" \n",
"plot3.ax.set_xlabel(\"Effective shear strain\")\n",
"\n",
"\n",
"plt.tight_layout()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Exercise"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Calculate the z component of rigid body rotation, $$ \\omega_3 = \\frac{F_{12}-F_{21}}{2}, $$ for the HRDIC map. (HINT: the deformation gradient (F) can be accessed as numpy array with `dic_map.data.f`)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Plot $\\omega_3$ as a map with grain boundaries and a scale bar. Choose an appropiate colourmap (see https://matplotlib.org/stable/gallery/color/colormap_reference.html) and scale. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Make a figure containing a map of $\\omega_3$ and a map of misorientaion for a single grain in the DIC map."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Calculate grain average $\\omega_3$ and then plot these on a IPF."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"pycharm": {
"name": "#%%\n"
},
"tags": []
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "defdap",
"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.11.13"
}
},
"nbformat": 4,
"nbformat_minor": 4
}