File: test_json_arrays.py

package info (click to toggle)
python-ase 3.21.1-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 13,936 kB
  • sloc: python: 122,428; xml: 946; makefile: 111; javascript: 47
file content (24 lines) | stat: -rw-r--r-- 726 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
def test_json_arrays():
    import numpy as np
    from ase.io.jsonio import encode, decode


    def check(obj):
        txt = encode(obj)
        newobj = decode(txt, always_array=False)
        print(obj, '-->', newobj)
        assert type(obj) is type(newobj), '{} vs {}'.format(type(obj),
                                                            type(newobj))
        assert np.shape(obj) == np.shape(newobj)
        assert np.array_equal(obj, newobj)

    check([1, 2, 3])
    check([1.0, 2.0, 3.0])
    check([])

    check(np.arange(3))
    check(np.arange(3).astype(float))
    check(np.empty((3, 0, 7)))
    check(np.empty((0, 3, 7), dtype=int))
    check(np.ones(2, complex))
    check(np.ones(2, np.complex64))