forked from argoverse/argoverse-api
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_pkl_utils.py
58 lines (42 loc) · 1.96 KB
/
test_pkl_utils.py
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
# <Copyright 2019, Argo AI, LLC. Released under the MIT license.>
"""Pickle utility test functions."""
import pathlib
import pickle as pkl
from typing import Any, Dict
import numpy as np
from argoverse.utils.pkl_utils import load_pkl_dictionary, save_pkl_dictionary
_TEST_DIR = pathlib.Path(__file__).parent
def dictionaries_are_equal(dict_a: Dict[Any, Any], dict_b: Dict[Any, Any]) -> None:
"""Compare the keys and corresponding values of two dictionaries.
Args:
dict_a: Python dictionary
dict_b: Python dictionary
"""
# make sure key sets are identical
assert dict_a.keys() == dict_b.keys()
for k, v in dict_a.items():
if isinstance(v, np.ndarray):
assert np.allclose(v, dict_b[k])
else:
assert v == dict_b[k]
def test_save_pickle_from_disk() -> None:
"""Save a dictionary to a pickle file.
The file should contain the same Python dictionary as above:
{'a': 1, 'b':'2', 'c':[9,8,7,6,5,'d','c','b','a'], 'd': np.array([True,False,True]) }
"""
pkl_fpath = _TEST_DIR / "test_data/pkl_test_file.pkl"
intended_dict = {"a": 1, "b": "2", "c": [9, 8, 7, 6, 5, "d", "c", "b", "a"], "d": np.array([True, False, True])}
save_pkl_dictionary(pkl_fpath, intended_dict)
with open(pkl_fpath, "rb") as f:
loaded_pkl_dict = pkl.load(f)
dictionaries_are_equal(intended_dict, loaded_pkl_dict)
def test_load_pickle_from_disk() -> None:
"""Load a pickle file from disk.
The file should contain the following Python dictionary:
{'a': 1, 'b':'2', 'c':[9,8,7,6,5,'d','c','b','a'], 'd': np.array([True,False,True]) }
We demonstrate that we can load Numpy arrays and lists from Pickle files.
"""
gt_dict = {"a": 1, "b": "2", "c": [9, 8, 7, 6, 5, "d", "c", "b", "a"], "d": np.array([True, False, True])}
pkl_fpath = _TEST_DIR / "test_data/pkl_test_file.pkl"
loaded_pkl_dict = load_pkl_dictionary(pkl_fpath)
dictionaries_are_equal(gt_dict, loaded_pkl_dict)