pyxem / kikuchipy

Toolbox for analysis of electron backscatter diffraction (EBSD) patterns
https://kikuchipy.org
GNU General Public License v3.0
79 stars 30 forks source link

Add saving and loading an EBSD detector to/from a text file #595

Closed hakonanes closed 1 year ago

hakonanes commented 1 year ago

Description of the change

This PR adds saving and loading an EBSD detector to/from a text file using NumPy's loadtxt(). The method EBSDDetector.save() saves to file, while the classmethod EBSDDetector.load() returns an EBSDDetector from a text file.

Progress of the PR

Minimal example of the bug fix or new feature

>>> import kikuchipy as kp
>>> det = kp.detectors.EBSDDetector(shape=(60, 60), pc=[0.3, 0.2, 0.6])
>>> det
EBSDDetector (60, 60), px_size 1 um, binning 1, tilt 0, azimuthal 0, pc (0.3, 0.2, 0.6)
>>> fname = "det.txt"
>>> det.save(fname, convention="tsl", fmt="%.5f")
>>> header = []
>>> with open(fname, mode="r") as f:
>>>     for line in f.readlines():
>>>         if line.startswith("#"):
>>>             header.append(line)
>>>         else:
>>>             break
>>> header
['# EBSDDetector\n',
 '#   shape: (60, 60)\n',
 '#   px_size: 1\n',
 '#   binning: 1\n',
 '#   tilt: 0 deg\n',
 '#   azimuthal: 0 deg\n',
 '#   sample_tilt: 70 deg\n',
 '#   convention: tsl\n',
 '#   navigation_shape: (1,)\n',
 '# \n',
 '# kikuchipy version: 0.8.dev0\n',
 '# Time: 2023-01-23 13:04:34\n',
 '# \n',
 '# Column names: PCx, PCy, PCz\n']
>>> det2 = kp.detectors.EBSDDetector.load(fname)
>>> det2
EBSDDetector (60, 60), px_size 1 um, binning 1, tilt 0, azimuthal 0, pc (0.3, 0.2, 0.6)

For reviewers