Coverage for ase/io/py.py: 100.00%

14 statements  

« prev     ^ index     » next       coverage.py v7.16.2, created at 2026-11-27 15:21 +0000

1import numpy as np 

2 

3 

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') 

8 

9 if hasattr(images, 'get_positions'): 

10 images = [images] 

11 fileobj.write('images = [\n') 

12 

13 for image in images: 

14 fileobj.write( 

15 " Atoms(symbols='%s',\n" 

16 ' pbc=np.array(%s),\n' 

17 ' cell=np.array(\n%s),\n' 

18 ' positions=np.array(\n%s)),\n' 

19 % ( 

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), 

24 ) 

25 ) 

26 

27 fileobj.write(']\n') 

28 

29 

30def array_to_string(array, indent=14): 

31 """Converts given numpy array to a string, which when printed will pass 

32 flake8 tests.""" 

33 text = np.array2string( 

34 array, 

35 separator=', ', 

36 suppress_small=False, 

37 formatter={'float': '{:.8f}'.format, 'bool': '{}'.format}, 

38 threshold=np.inf, 

39 ) 

40 text = ' ' * indent + text.replace('\n', '\n' + ' ' * indent) 

41 return text