aboutsummaryrefslogtreecommitdiff
path: root/verif/tests/test_json2numpy.py
blob: 63bc2d99c3ea5f2f755a2ad2edb84065c8f3bcfb (plain)
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
"""Tests for json2numpy.py."""
# Copyright (c) 2021-2022, ARM Limited.
# SPDX-License-Identifier: Apache-2.0
import json
import os

import numpy as np
import pytest
from json2numpy.json2numpy import main


@pytest.mark.parametrize(
    "npy_filename,json_filename,data_type",
    [
        ("single_num.npy", "single_num.json", np.int8),
        ("multiple_num.npy", "multiple_num.json", np.int8),
        ("single_num.npy", "single_num.json", np.int16),
        ("multiple_num.npy", "multiple_num.json", np.int16),
        ("single_num.npy", "single_num.json", np.int32),
        ("multiple_num.npy", "multiple_num.json", np.int32),
        ("single_num.npy", "single_num.json", np.int64),
        ("multiple_num.npy", "multiple_num.json", np.int64),
        ("single_num.npy", "single_num.json", np.uint8),
        ("multiple_num.npy", "multiple_num.json", np.uint8),
        ("single_num.npy", "single_num.json", np.uint16),
        ("multiple_num.npy", "multiple_num.json", np.uint16),
        ("single_num.npy", "single_num.json", np.uint32),
        ("multiple_num.npy", "multiple_num.json", np.uint32),
        ("single_num.npy", "single_num.json", np.uint64),
        ("multiple_num.npy", "multiple_num.json", np.uint64),
        ("single_num.npy", "single_num.json", np.float16),
        ("multiple_num.npy", "multiple_num.json", np.float16),
        ("single_num.npy", "single_num.json", np.float32),
        ("multiple_num.npy", "multiple_num.json", np.float32),
        ("single_num.npy", "single_num.json", np.float64),
        ("multiple_num.npy", "multiple_num.json", np.float64),
        ("single_num.npy", "single_num.json", bool),
        ("multiple_num.npy", "multiple_num.json", bool),
    ],
)
def test_json2numpy_npy_file(npy_filename, json_filename, data_type):
    """Test conversion to JSON."""
    # Generate numpy data.
    if "single" in npy_filename:
        npy_data = np.ndarray(shape=(1, 1), dtype=data_type)
    elif "multiple" in npy_filename:
        npy_data = np.ndarray(shape=(2, 3), dtype=data_type)

    # Get filepaths
    npy_file = os.path.join(os.path.dirname(__file__), npy_filename)
    json_file = os.path.join(os.path.dirname(__file__), json_filename)

    # Save npy data to file and reload it.
    with open(npy_file, "wb") as f:
        np.save(f, npy_data)
    npy_data = np.load(npy_file)

    args = [npy_file]
    """Converts npy file to json"""
    assert main(args) == 0

    json_data = json.load(open(json_file))
    assert np.dtype(json_data["type"]) == npy_data.dtype
    assert np.array(json_data["data"]).shape == npy_data.shape
    assert (np.array(json_data["data"]) == npy_data).all()

    # Remove files created
    if os.path.exists(npy_file):
        os.remove(npy_file)
    if os.path.exists(json_file):
        os.remove(json_file)


@pytest.mark.parametrize(
    "npy_filename,json_filename,data_type",
    [
        ("single_num.npy", "single_num.json", np.int8),
        ("multiple_num.npy", "multiple_num.json", np.int8),
        ("single_num.npy", "single_num.json", np.int16),
        ("multiple_num.npy", "multiple_num.json", np.int16),
        ("single_num.npy", "single_num.json", np.int32),
        ("multiple_num.npy", "multiple_num.json", np.int32),
        ("single_num.npy", "single_num.json", np.int64),
        ("multiple_num.npy", "multiple_num.json", np.int64),
        ("single_num.npy", "single_num.json", np.uint8),
        ("multiple_num.npy", "multiple_num.json", np.uint8),
        ("single_num.npy", "single_num.json", np.uint16),
        ("multiple_num.npy", "multiple_num.json", np.uint16),
        ("single_num.npy", "single_num.json", np.uint32),
        ("multiple_num.npy", "multiple_num.json", np.uint32),
        ("single_num.npy", "single_num.json", np.uint64),
        ("multiple_num.npy", "multiple_num.json", np.uint64),
        ("single_num.npy", "single_num.json", np.float16),
        ("multiple_num.npy", "multiple_num.json", np.float16),
        ("single_num.npy", "single_num.json", np.float32),
        ("multiple_num.npy", "multiple_num.json", np.float32),
        ("single_num.npy", "single_num.json", np.float64),
        ("multiple_num.npy", "multiple_num.json", np.float64),
        ("single_num.npy", "single_num.json", bool),
        ("multiple_num.npy", "multiple_num.json", bool),
    ],
)
def test_json2numpy_json_file(npy_filename, json_filename, data_type):
    """Test conversion to binary."""
    # Generate json data.
    if "single" in npy_filename:
        npy_data = np.ndarray(shape=(1, 1), dtype=data_type)
    elif "multiple" in npy_filename:
        npy_data = np.ndarray(shape=(2, 3), dtype=data_type)

    # Generate json dictionary
    list_data = npy_data.tolist()
    json_data_type = str(npy_data.dtype)

    json_data = {}
    json_data["type"] = json_data_type
    json_data["data"] = list_data

    # Get filepaths
    npy_file = os.path.join(os.path.dirname(__file__), npy_filename)
    json_file = os.path.join(os.path.dirname(__file__), json_filename)

    # Save json data to file and reload it.
    with open(json_file, "w") as f:
        json.dump(json_data, f)
    json_data = json.load(open(json_file))

    args = [json_file]
    """Converts json file to npy"""
    assert main(args) == 0

    npy_data = np.load(npy_file)
    assert np.dtype(json_data["type"]) == npy_data.dtype
    assert np.array(json_data["data"]).shape == npy_data.shape
    assert (np.array(json_data["data"]) == npy_data).all()

    # Remove files created
    if os.path.exists(npy_file):
        os.remove(npy_file)
    if os.path.exists(json_file):
        os.remove(json_file)