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