-
Notifications
You must be signed in to change notification settings - Fork 0
/
sccommon.py
100 lines (75 loc) · 2.51 KB
/
sccommon.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
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
import json
from json import JSONEncoder
import numpy as np
import bson
class NumpyArrayEncoder(JSONEncoder):
def default(self, obj):
if isinstance(obj, np.ndarray):
return obj.tolist()
return JSONEncoder.default(self, obj)
def ndarray_to_json(data):
encodedNumpyData = json.dumps({"data":data.tolist()})
return encodedNumpyData
def json_to_ndarray(data):
decodedArrays = json.loads(data)
finalNumpyArray = np.asarray(decodedArrays["array"])
return finalNumpyArray
def ndarray_to_bson(data):
encodedNumpyData = bson.dumps({"data":data.tolist()})
return encodedNumpyData
def bson_to_ndarray(data):
decodedArrays = bson.loads(data)
finalNumpyArray = np.asarray(decodedArrays["array"])
return finalNumpyArray
def ndarray_list_to_json(data):
list_of_lists = []
for item in data:
list_of_lists.append({"item":item.tolist()})
encoded_list = json.dumps(list_of_lists)
return encoded_list
def json_to_ndarray_list(data):
decodeddata = json.loads(data)
final_data = []
for item in decodeddata:
final_data.append(np.asarray(item["item"]))
return final_data
def ndarray_list_to_bson(data):
list_of_lists = []
for item in data:
#list_of_lists.append({"item":item.tolist()})
print(type(item))
list_of_lists.append(item.tolist())
#print(list_of_lists)
encoded_list = bson.dumps({"data": list_of_lists})
return encoded_list
def bson_to_ndarray_list(data):
#decodeddata = bson.loads(data)
decodeddata = bson.loads(data)
final_data = []
for item in decodeddata["data"]:
final_data.append(np.asarray(item))
return final_data
if __name__ == "__main__":
numpyArrayOne = np.array([[11, 22, 33], [44, 55, 66], [77, 88, 99]])
numpyArrayTwo = np.array([[1, 2, 3],[11, 22, 33], [44, 55, 66], [77, 88, 99]])
l = [numpyArrayOne, numpyArrayTwo]
print(l)
print()
ll = ndarray_list_to_bson(l)
print(ll)
print()
lll = bson_to_ndarray_list(ll)
print(lll)
"""
# Serialization
numpyData = {"array": numpyArrayOne}
encodedNumpyData = json.dumps(numpyData, cls=NumpyArrayEncoder) # use dump() to write array into file
print("Printing JSON serialized NumPy array")
print(encodedNumpyData)
# Deserialization
print("Decode JSON serialized NumPy array")
decodedArrays = json.loads(encodedNumpyData)
finalNumpyArray = np.asarray(decodedArrays["array"])
print("NumPy Array")
print(finalNumpyArray)
"""