Coverage for /builds/ase/ase/ase/io/py.py: 100.00%
14 statements
« prev ^ index » next coverage.py v7.5.3, created at 2025-08-02 00:12 +0000
« prev ^ index » next coverage.py v7.5.3, created at 2025-08-02 00:12 +0000
1# fmt: off
3import numpy as np
6def write_py(fileobj, images):
7 """Write to ASE-compatible python script."""
8 fileobj.write('import numpy as np\n\n')
9 fileobj.write('from ase import Atoms\n\n')
11 if hasattr(images, 'get_positions'):
12 images = [images]
13 fileobj.write('images = [\n')
15 for image in images:
16 fileobj.write(" Atoms(symbols='%s',\n"
17 " pbc=np.array(%s),\n"
18 " cell=np.array(\n%s),\n"
19 " positions=np.array(\n%s)),\n" % (
20 image.get_chemical_formula(mode='reduce'),
21 array_to_string(image.pbc, 0),
22 array_to_string(image.cell),
23 array_to_string(image.positions)))
25 fileobj.write(']\n')
28def array_to_string(array, indent=14):
29 """Converts given numpy array to a string, which when printed will pass
30 flake8 tests."""
31 text = np.array2string(
32 array,
33 separator=', ',
34 suppress_small=False,
35 formatter={'float': '{:.8f}'.format, 'bool': '{}'.format},
36 threshold=np.inf,
37 )
38 text = ' ' * indent + text.replace('\n', '\n' + ' ' * indent)
39 return text