A method of grain2d.

the pairs of ids of grains sharing a boundary, both in the selection, or either with full; as a symmetric sparse matrix indexed by id with matrix; with other grains the pairs of one of these and one of those; MATLAB’s grain2d.neighbors

Syntax

pairs = grains.neighbors()

The pairs of neighbours among the grains.

pairs = g1.neighbors(g2)

A grain of g1 beside a grain of g2.

pairs = grains.neighbors(full=True)

Every neighbour, inside the selection or not.

A = grains.neighbors(matrix=True)

The adjacency matrix.

Description

A pair is listed once, the smaller id first; the rim of the map and a grain’s own inner boundary make no pair. With other grains the first column is the grain of these, the second the one of those. The adjacency matrix is a symmetric boolean scipy.sparse matrix, the row and column id - 1 for the grain id.

Input

grains grain2d

Keywords

keyword default description
full False take the neighbours outside the selection as well; another grain2d in its place gives the pairs between the two sets
matrix False the result as the adjacency matrix

Output

pairs the grain ids, (n, 2)
A scipy.sparse.csr_matrix (matrix=True)

Example

Find grain-id pairs and the sparse adjacency matrix of a reconstructed map. The small Forsterite sample is downloaded and cached on first use.

import numpy as np
import matplotlib.pyplot as plt
from mtex import mtexdata, calcGrains, degree, plot, mtexColorbar

ebsd = mtexdata('small', verbose=False)['Forsterite']
grains = calcGrains(ebsd, angle=10*degree, minPixel=5)
pairs = grains.neighbors()
adjacency = grains.neighbors(matrix=True)
print('First five neighbour pairs:', pairs[:5].tolist())
print('Matrix is symmetric:', (adjacency != adjacency.T).nnz == 0)
print('Each pair contributes two entries:', adjacency.nnz == 2*len(pairs))
plot(grains, grains.numNeighbors)
mtexColorbar(title='Number of neighbouring grains')
plt.show()

Pairs contain grain ids, not zero-based positions in the current selection. The matrix uses row id-1 and column id-1; its shape can therefore exceed the number of selected grains. Use full=True to include neighbours outside a selected set of grains.

First five neighbour pairs: [[1, 2], [1, 5], [2, 5], [2, 6], [2, 7]]
Matrix is symmetric: True
Each pair contributes two entries: True

Example output for grain2d.neighbors

See also

grain2d.numNeighbors, grainBoundary.selectByGrainId

MATLAB’s grain2d.neighbors

source

Citing this page. This page is part of the documentation of MTEX, a free and open source MATLAB toolbox for analyzing and modeling crystallographic textures. It was written by The MTEX Developers and is published at https://mtex-toolbox.github.io/grain2d.neighbors_py.html. If you use MTEX, or reuse text or figures from this page, in your research, please cite

F. Bachmann, R. Hielscher, H. Schaeben: Texture Analysis with MTEX - Free and Open Source Software Toolbox, Solid State Phenomena 160 (2010), 63-68. 10.4028/www.scientific.net/SSP.160.63

BibTeX
@article{bachmann2010mtex,
  author  = {F. Bachmann and R. Hielscher and H. Schaeben},
  title   = {Texture Analysis with MTEX - Free and Open Source Software Toolbox},
  journal = {Solid State Phenomena},
  volume  = {160},
  pages   = {63-68},
  year    = {2010},
  doi     = {10.4028/www.scientific.net/SSP.160.63},
  url     = {https://doi.org/10.4028/www.scientific.net/SSP.160.63}
}

Other papers describing specific MTEX methods are listed under Publications — please cite the one that best fits your application. The MTEX source code is licensed under the GNU General Public License v2.0; the text and figures of this documentation are licensed under CC BY 4.0, which permits reuse — including by automated systems — provided The MTEX Developers and this page are credited.