File: testcjsonreader.py

package info (click to toggle)
cclib 1.8.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 55,412 kB
  • sloc: python: 23,605; makefile: 75; sh: 31
file content (74 lines) | stat: -rw-r--r-- 3,144 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
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# Copyright (c) 2024, the cclib development team
#
# This file is part of cclib (http://cclib.github.io) and is distributed under
# the terms of the BSD 3-Clause License.

"""Unit tests for the CJSON reader."""

import os
import tempfile

import cclib

import numpy as np

__filedir__ = os.path.dirname(__file__)
__filepath__ = os.path.realpath(__filedir__)
__datadir__ = os.path.join(__filepath__, "..", "..")


class CJSONReaderTest:
    """Unit tests for the CJSON reader."""

    def test_cjson_read(self):
        """File->ccData->CJSON->attribute_dict, the attributes within ccData and attribute_dict
        should be the same."""
        fpath = os.path.join(__datadir__, "data/ADF/basicADF2007.01/dvb_gopt.adfout")
        data = cclib.io.ccread(fpath)
        assert data is not None, "The logfileparser failed to parse the output file"

        cjson_obj = cclib.io.cjsonwriter.CJSON(data, terse=True)
        assert (
            cjson_obj.ccdata == data
        ), "The ccData instance within the CJSON class should be the same as the one generated by the logfileparsers"

        # Generate the CJSON object to be written into a file.
        cjson_data = cjson_obj.generate_repr()

        with tempfile.NamedTemporaryFile(mode="w") as fp:
            fp.write(cjson_data)
            fp.flush()
            read_cjson_data = cclib.io.ccread(fp.name, cjson=True)
        assert read_cjson_data is not None, "The CJSON reader failed to read attributes"

        # The attribute values read by the CJSON reader will be a subset of the
        # total attributes stored by the logfileparser in the ccData object.
        ccdata_dict = data.getattributes()

        # Check if each 'key:value' pair read by the CJSON reader is equal to
        # the corresponding 'key:value' pair present in the ccData object.
        for key in read_cjson_data:
            ccdata_value = ccdata_dict[key]
            cjson_value = read_cjson_data[key]

            # The values in the ccData object might be of numpy types whereas
            # the values obtained by the CJSON reader are of the inbuilt python
            # types.  Conversion of numpy types into python types happens here:
            if isinstance(ccdata_value, (np.ndarray, list)):
                ccdata_value = np.asarray(ccdata_value).tolist()
            if isinstance(ccdata_value, dict):
                temp_dict = {}
                for ccdata_key in ccdata_value:
                    dict_value = ccdata_value[ccdata_key]
                    if isinstance(dict_value, np.ndarray):
                        dict_value = np.asarray(dict_value).tolist()
                        temp_dict[ccdata_key] = dict_value
                ccdata_value = temp_dict

            # The 'moments' attribute present in the CJSON is a post processed
            # value obtained from the moments key within ccData. Hence the
            # values for moments will be always different.  TODO: Create a
            # naming convention for post processed attributes within the CJSON
            # structure.
            if key != "moments":
                assert ccdata_value == cjson_value