Python* API Reference for Intel® Data Analytics Acceleration Library 2020 Update 1

serialization.py

1 # file: serialization.py
2 #===============================================================================
3 # Copyright 2014-2020 Intel Corporation
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 #===============================================================================
17 
18 
19 
20 
21 import os
22 import sys
23 
24 import numpy as np
25 
26 from daal.data_management import HomogenNumericTable, FileDataSource, DataSource, InputDataArchive, OutputDataArchive
27 
28 utils_folder = os.path.realpath(os.path.abspath(os.path.dirname(os.path.dirname(__file__))))
29 if utils_folder not in sys.path:
30  sys.path.insert(0, utils_folder)
31 from utils import printNumericTable
32 
33 # Input data set parameters
34 datasetFileName = os.path.join('..', 'data', 'batch', 'serialization.csv')
35 
36 
37 def serializeNumericTable(dataTable):
38 
39  # Create a data archive to serialize the numeric table
40  dataArch = InputDataArchive()
41 
42  # Serialize the numeric table into the data archive
43  dataTable.serialize(dataArch)
44 
45  # Get the length of the serialized data in bytes
46  length = dataArch.getSizeOfArchive()
47 
48  # Store the serialized data in an array
49  buffer = np.zeros(length, dtype=np.ubyte)
50  dataArch.copyArchiveToArray(buffer)
51 
52  return buffer
53 
54 
55 def deserializeNumericTable(buffer):
56 
57  # Create a data archive to deserialize the numeric table
58  dataArch = OutputDataArchive(buffer)
59 
60  # Create a numeric table object
61  dataTable = HomogenNumericTable()
62 
63  # Deserialize the numeric table from the data archive
64  dataTable.deserialize(dataArch)
65 
66  return dataTable
67 
68 
69 if __name__ == "__main__":
70 
71  # Initialize FileDataSource_CSVFeatureManager to retrieve the input data from a .csv file
72  dataSource = FileDataSource(
73  datasetFileName, DataSource.doAllocateNumericTable, DataSource.doDictionaryFromContext
74  )
75 
76  # Retrieve the data from the input file
77  dataSource.loadDataBlock()
78 
79  # Retrieve a numeric table
80  dataTable = dataSource.getNumericTable()
81 
82  # Print the original data
83  printNumericTable(dataTable, "Data before serialization:")
84 
85  # Serialize the numeric table into the memory buffer
86  buffer = serializeNumericTable(dataTable)
87 
88  # Deserialize the numeric table from the memory buffer
89  restoredDataTable = deserializeNumericTable(buffer)
90 
91  # Print the restored data
92  printNumericTable(restoredDataTable, "Data after deserialization:")

For more complete information about compiler optimizations, see our Optimization Notice.