Crop data#

In this notebook, we will see how to crop the aita data.

import xarrayaita.loadData_aita as lda #here are some function to build xarrayaita structure
import xarrayaita.aita as xa

import xarray as xr
import matplotlib.pyplot as plt
import matplotlib.cm as cm
import numpy as np
%matplotlib widget

Load your data#

# path to data and microstructure
path_data='orientation_test.dat'
path_micro='micro_test.bmp'
data=lda.aita5col(path_data,path_micro)
data
<xarray.Dataset>
Dimensions:      (y: 2500, x: 1000, uvecs: 2)
Coordinates:
  * x            (x) float64 0.0 0.02 0.04 0.06 0.08 ... 19.92 19.94 19.96 19.98
  * y            (y) float64 49.98 49.96 49.94 49.92 49.9 ... 0.06 0.04 0.02 0.0
Dimensions without coordinates: uvecs
Data variables:
    orientation  (y, x, uvecs) float64 2.395 0.6451 5.377 ... 0.6098 0.6473
    quality      (y, x) int64 0 90 92 93 92 92 94 94 ... 96 96 96 96 96 97 97 96
    micro        (y, x) float64 0.0 0.0 0.0 0.0 0.0 0.0 ... 0.0 0.0 0.0 0.0 0.0
    grainId      (y, x) int64 1 1 1 1 1 1 1 1 1 1 1 1 ... 1 1 1 1 1 1 1 1 1 1 1
Attributes:
    date:       Thursday, 19 Nov 2015, 11:24 am
    unit:       millimeters
    step_size:  0.02
    path_dat:   orientation_test.dat

Use interactive crop for croping#

Note

The option rebuild_gId can be use if you want to recompute the grainId labelling. If you use this function it will change the grainId label of every grain. It can be usefull if the grain boundaries on the border are not closed. By default the rebuild_gId=True.

help(xa.interactive_crop)
Help on function interactive_crop in module xarrayaita.aita_interactive_nb:

interactive_crop(self, rebuild_gId=True)
    out=data.aita.interactive_crop()
    
    :param rebuild_gId: recompute the grainID
    :type rebuild_gId: bool
    
    This function can be use to crop within a jupyter notebook
    It will crop the data and export the value of the crop in out.pos
out=data.aita.interactive_crop()
1. click and drag the mouse on the figure to selecte the area
2. you can draw the rectangle using the button "Draw area"
3. if you are unhappy with the selection restart to 1.
4. if you are happy with the selection click on "Export crop" (only the last rectangle is taken into account)

The rectangle coordinates are given in out.crop. In out.crop[0] is the x coordinate of the rectangle corner and in out.crop[1] the y coordinate

print('Corner, x coordinate :',out.crop[0])
print('Corner, y coordinate :',out.crop[1])
Corner, x coordinate : [ 3.98641665 15.68534138 15.68534138  3.98641665]
Corner, y coordinate : [22.82477651 22.82477651 44.33992803 44.33992803]

The new data can be found in out.ds

plt.figure()
out.ds.orientation.uvecs.calc_colormap().plot.imshow()
plt.axis('equal')
(-0.5, 584.5, -0.5, 1074.5)
out.ds
<xarray.Dataset>
Dimensions:      (y: 1075, x: 585, uvecs: 2)
Coordinates:
  * x            (x) float64 4.0 4.02 4.04 4.06 4.08 ... 15.62 15.64 15.66 15.68
  * y            (y) float64 44.32 44.3 44.28 44.26 ... 22.9 22.88 22.86 22.84
Dimensions without coordinates: uvecs
Data variables:
    orientation  (y, x, uvecs) float64 0.1868 0.6983 0.1848 ... 1.334 1.531
    quality      (y, x) float64 95.0 94.0 94.0 85.0 56.0 ... 79.0 81.0 81.0 81.0
    micro        (y, x) float64 0.0 0.0 0.0 0.0 0.0 0.0 ... 0.0 0.0 0.0 0.0 0.0
    grainId      (y, x) int64 1 1 1 1 1 1 0 0 ... 380 380 380 380 380 380 380
Attributes:
    date:       Thursday, 19 Nov 2015, 11:24 am
    unit:       millimeters
    step_size:  0.02
    path_dat:   orientation_test.dat

Overwrite the data#

If you want to overwrite the variable data.

data=out.ds
data
<xarray.Dataset>
Dimensions:      (y: 1075, x: 585, uvecs: 2)
Coordinates:
  * x            (x) float64 4.0 4.02 4.04 4.06 4.08 ... 15.62 15.64 15.66 15.68
  * y            (y) float64 44.32 44.3 44.28 44.26 ... 22.9 22.88 22.86 22.84
Dimensions without coordinates: uvecs
Data variables:
    orientation  (y, x, uvecs) float64 0.1868 0.6983 0.1848 ... 1.334 1.531
    quality      (y, x) float64 95.0 94.0 94.0 85.0 56.0 ... 79.0 81.0 81.0 81.0
    micro        (y, x) float64 0.0 0.0 0.0 0.0 0.0 0.0 ... 0.0 0.0 0.0 0.0 0.0
    grainId      (y, x) int64 1 1 1 1 1 1 0 0 ... 380 380 380 380 380 380 380
Attributes:
    date:       Thursday, 19 Nov 2015, 11:24 am
    unit:       millimeters
    step_size:  0.02
    path_dat:   orientation_test.dat