- Authors: Mahesh R.G Prasad, Abhishek Biswas, Golsa Tolooei Eshlaghi, Ronak Shoghi, Napat Vajragupta, Yousef Rezek, Hrushikesh Uday Bhimavarapu, Jun Xue, Alexander Hartmaier
- Organization: ICAMS / Ruhr-Universität Bochum, Germany
- Contact: alexander.hartmaier@rub.de
Kanapy is a python package for generating complex three-dimensional (3D) synthetic polycrystalline microstructures. The microstructures are built based on statistical information about phase and grain morphologies, given as size distributions and aspect ratio distrubitions of grains and phase regions. Furthermore, crystallographic texture is considered in form of orientation distribution functions (ODF) and misorientation distribution functions (MDF). Kanapy offers tools to analyze EBSD maps with respect to the morphology and texture of microstructures. Based on this experimental data, it generates 3D synthetic microstructures mimicking real ones in a statistical sense.
The basic implementation of Kanapy is done in form of a Python Appplication Programming Interface (API). There is also a command line interface (CLI) for administrative functions and and Graphical User Interface (GUI), which is still under development.
- Kanapy offers a Python Application Programming Interface (API).
- Possibility to analyze experimental microstructures based on orix functions.
- Reusable EBSD grain graphs with phase labels, combined phase overlays, and optional data exports.
- Support of multiphase microstructures.
- Generation of 3D microstructure morphology based on statistical features as size distributions and aspect ratio distributions of grains and phase regions.
- Crystallographic texture reconstruction using orientations from experimental data in form of Orientation Distribution Function (ODF).
- Optimal orientation assignment based on measured Misorientation Distribution Function (MDF) that maintains correct statistical description of high-angle or low-angle grain boundary characteristics.
- Independent execution of individual modules through easy data storage and handling.
- In-built hexahedral mesh generator for representation of complex polycrystalline microstructures in form of voxels.
- Efficient generation of space filling structures for elongated grains by particle dynamics method and voxel-assignment based on anisotropic power diagrams (APD).
- Collision handling of particles through a two-layer collision detection method employing the Octree spatial data structure and the bounding sphere hierarchy.
- Option to generate spherical particle position and radius files that can be read by the Voronoi tessellation software Neper.
- Option to generate input files for finite-element packages.
- Import and export of voxel structures according to following the modular materials data schema published on GitHub for data transfer between different tools.
The preferred method to use Kanapy is within Anaconda or Miniconda, into which it can be easily installed from conda-forge by
$ conda install conda-forge::kanapy
or
$ conda install kanapy -c conda-forge
Generally, it can be installed within any Python environment supporting the package installer for python (pip) from its latest PyPi release via the shell command
$ pip install kanapy
Alternatively, the most recent version of the complete repository, including the source code, documentation and examples, can be cloned and installed locally. It is recommended to create a conda environment before installation. This can be done by the following the command line instructions
$ git clone https://github.com/ICAMS/Kanapy.git ./kanapy
$ cd kanapy
$ conda env create -f environment.yml
$ conda activate knpy
(knpy) $ python -m pip install .
Kanapy is now installed along with all its dependencies. The correct installation with this method can be tested with
(knpy) $ kanapy runTests
After installation by any of those methods, the package can be used as API within python, e.g. by importing the entire package with
import kanapy as knpyKanapy supports some command line tools, a list of supported tools can be displayed with
(knpy) $ kanapy --help
The alpha-version of the GUI can be started with the shell command
(knpy) $ kanapy gui
Kanapy comes with several examples in form of Python scripts and Juypter notebooks. If you want to create a local copy of the kanapy/examples directory within the current working directory (cwd), please run the command
(knpy) $ kanapy copyExamples
Kanapy notebooks can also be used on Binder.
The examples provide standalone EBSD analysis and two complete EBSD to RVE workflows:
graph_based_analysis.pyruns complete two dimensional EBSD analysis: grain graphs, equivalent diameters, aspect ratios, tilt angles, fitted distributions, and EBSD plots. It saves graph files,statistics.json, histograms, maps, and pole figures inexamples/EBSD_analysis/2D_graph_result/. Every retained phase has separate statistics. File output switches are listed at the top of the script, and no RVE is generated.graph_to_rve.pyruns the complete workflow from EBSD reading and graph reconstruction through statistics, RVE initialization, packing, voxelization, orientation assignment, and export. It saves figures and results inexamples/EBSD_to_RVE/outputs/. Experiment parameters are listed at the top of the script;--helplists command line overrides, including--showfor interactive previews and--extra-outputsfor additional exports.convert_ang2rve.pyretains the original Kanapy example's complete EBSD to RVE computation sequence and interactive plots. Close each figure to continue. Its single voxel JSON is saved directly inexamples/EBSD_to_RVE/, for exampleIron fcc_voxels.json.
The grain graph is the first displayed figure in all three examples. The two graph examples also save it before the other figures: 00_graph_overlay.png in the standalone analysis and ebsd_graph_overlay_01.png in the complete workflow. These names sort before the other generated PNG files.
Run an example from the repository root:
python examples/EBSD_analysis/graph_based_analysis.py
python examples/EBSD_to_RVE/graph_to_rve.py
python examples/EBSD_to_RVE/convert_ang2rve.pyAll three examples use Kanapy installed in the active environment. For development, run python -m pip install -e . from the repository root to use this checkout. Alternatively, prefix a command with PYTHONPATH=src when running from the repository root.
The graph workflows use four neighboring pixels, a minimum grain size of 10 pixels, and a minimum phase fraction of 3% by default:
- Phase fractions are measured before resampling. Pixels belonging to omitted indexed phases are assigned once to the nearest retained measurement. Equally near measurements vote by phase; tied phase counts use the smallest phase ID. Phase, orientation, and confidence come from the same donor. Unindexed pixels remain unindexed.
- Hexagonal scans are resampled once over the full scan extent using all retained phases. Square scans keep their grid. Original measurements and phase fractions remain unchanged, with donor indices recorded for traceability.
- Each phase completes its usual segmentation and cleanup first. Remaining regions without a same phase neighbor undergo the boundary fragment, invalid hull, and small region checks (Steps 3, 4, and 6). Step 3 chooses the longest shared boundary, then the largest neighbor; Steps 4 and 6 choose the largest neighbor. Cross phase absorption retains the receiving grain's phase and orientation. A qualifying region without any valid neighbor is excluded and recorded. A valid isolated grain that passes these checks remains.
- A combined graph records contacts across actual shared pixel sides. The largest measured phase uses black nodes and solid edges; other phases use distinct colors. Cross phase edges are gray and dashed. The legend shows original measured fractions.
result.graph remains the selected phase; result.full_graph / result.overview_graph provide the combined graph. Pickle exports preserve both, including cleanup records. Empty phases remain in the analysis metadata with statistics_available=False and no fitted parameters. graph_to_rve.py selects the largest original phase, while convert_ang2rve.py selects the first retained phase; either reports a clear error if that phase has no usable statistics after cleanup. The RVE generation algorithms are unchanged.
The Kanapy documentation is available online on GitHub Pages: https://icams.github.io/Kanapy/ and can directly be displayed with
(knpy) $ kanapy readDocs
The documentation for Kanapy is generated using Sphinx.
Below are the listed dependencies for running Kanapy:
- NumPy for array manipulation.
- SciPy for functionalities like Convexhull.
- Matplotlib for plotting and visualizing.
- orix for reading and analyzing EBSD maps and for generation of crystal orientations
- NetworkX generating graph networks of microstructures
- scikit-image processing of microstructure images
The preferred way to cite Kanapy is:
@article{Biswas2020,
doi = {10.5281/zenodo.3662366},
url = {https://doi.org/10.5281/zenodo.3662366},
author = {Abhishek Biswas and Mahesh R.G. Prasad and Napat Vajragupta and Alexander Hartmaier},
title = {Kanapy: Synthetic polycrystalline microstructure generator with geometry and texture},
journal = {Zenodo},
year = {2020}
}- Prasad et al., (2019). Kanapy: A Python package for generating complex synthetic polycrystalline microstructures. Journal of Open Source Software, 4(43), 1732. https://doi.org/10.21105/joss.01732
- Biswas, Abhishek, R.G. Prasad, Mahesh, Vajragupta, Napat, & Hartmaier, Alexander. (2020, February 11). Kanapy: Synthetic polycrystalline microstructure generator with geometry and texture (Version v2.0.0). Zenodo. http://doi.org/10.5281/zenodo.3662366
- Biswas, A., Prasad, M.R.G., Vajragupta, N., ul Hassan, H., Brenne, F., Niendorf, T. and Hartmaier, A. (2019), Influence of Microstructural Features on the Strain Hardening Behavior of Additively Manufactured Metallic Components. Adv. Eng. Mater., 21: 1900275. http://doi.org/10.1002/adem.201900275
- Biswas, A., Vajragupta, N., Hielscher, R. & Hartmaier, A. (2020). J. Appl. Cryst. 53, 178-187. https://doi.org/10.1107/S1600576719017138
- Biswas, A., Prasad, M.R.G., Vajragupta, N., Kostka, A., Niendorf, T. and Hartmaier, A. (2020), Effect of Grain Statistics on Micromechanical Modeling: The Example of Additively Manufactured Materials Examined by Electron Backscatter Diffraction. Adv. Eng. Mater., 22: 1901416. http://doi.org/10.1002/adem.201901416
- R.G. Prasad, M., Biswas, A., Geenen, K., Amin, W., Gao, S., Lian, J., Röttger, A., Vajragupta, N. and Hartmaier, A. (2020), Influence of Pore Characteristics on Anisotropic Mechanical Behavior of Laser Powder Bed Fusion--Manufactured Metal by Micromechanical Modeling. Adv. Eng. Mater., https://doi.org/10.1002/adem.202000641
- v3: Introduction of Python API
- v4: Import and export of microstructures in form of voxels
- v5: Pure Python version, support of CLI functions suspended
- v6: Major revision of internal data structure and statistical microstructure parameters
- v6.1: Full support of dual-phase and porous microstructures
- v6.2: Possibility of other geometries than ellipsoids as basic microstructure shapes
- v6.3: Implementation of velocity-Verlet algorithm to integrate particle trajectories during packing
- v6.4: Support of the modular materials data schema for import and export of microstructures
- v6.5: Switched to orix library for EBSD import and analysis and generation of textures to have a pure Python code. The MTEX backend is still available with Kanapy-mtex.
- v6.5.4: Modified support for multiphase structures without downwards comaptibility: Matrix or dispersed phases are now introduced explicitly. EBSD map analysis based on microstructure graphs.
- v6.5.5: APD voxelization and
generate_grains()use anisotropic power diagrams. Grain reconstruction uses a uniform tetrahedral background with a resolution independent of the voxel mesh. See the background mesh guide. Periodic reconstruction retains whole grains, phase metadata, and shared surfaces.
Kanapy, including all examples and notebooks, is made available under the GNU Affero General Public License (AGPL) v3
license.
The additional material, texts and artwork contained in examples, notebooks and documentation are published under the Creative Commons Attribution-NonCommercial-ShareAlike (CC BY-NC-SA 4.0) license.
© 2025, 2026 by Authors, ICAMS/Ruhr University Bochum, Germany
The name Kanapy is derived from the sanskrit word káṇa meaning particle. Kanapy is primarily developed at the Interdisciplinary Center for Advanced Materials Simulation (ICAMS), Ruhr University Bochum - Germany. Our goal is to build a complete synthetic microstructure generation tool for research and industry use.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.



