Python* API Reference for Intel® Data Analytics Acceleration Library 2019 Update 4

serialization.py

Deprecation Notice: With the introduction of daal4py, a package that supersedes PyDAAL, Intel is deprecating PyDAAL and will discontinue support starting with Intel® DAAL 2021 and Intel® Distribution for Python 2021. Until then Intel will continue to provide compatible pyDAAL pip and conda packages for newer releases of Intel DAAL and make it available in open source. However, Intel will not add the new features of Intel DAAL to pyDAAL. Intel recommends developers switch to and use daal4py.

Note: To find daal4py examples, refer to daal4py documentation or browse github repository.

1 # file: serialization.py
2 #===============================================================================
3 # Copyright 2014-2019 Intel Corporation.
4 #
5 # This software and the related documents are Intel copyrighted materials, and
6 # your use of them is governed by the express license under which they were
7 # provided to you (License). Unless the License provides otherwise, you may not
8 # use, modify, copy, publish, distribute, disclose or transmit this software or
9 # the related documents without Intel's prior written permission.
10 #
11 # This software and the related documents are provided as is, with no express
12 # or implied warranties, other than those that are expressly stated in the
13 # License.
14 #===============================================================================
15 
16 ## <a name="DAAL-EXAMPLE-PY-SERIALIZATION"></a>
17 ## \example serialization.py
18 
19 import os
20 import sys
21 
22 import numpy as np
23 
24 from daal.data_management import HomogenNumericTable, FileDataSource, DataSource, InputDataArchive, OutputDataArchive
25 
26 utils_folder = os.path.realpath(os.path.abspath(os.path.dirname(os.path.dirname(__file__))))
27 if utils_folder not in sys.path:
28  sys.path.insert(0, utils_folder)
29 from utils import printNumericTable
30 
31 # Input data set parameters
32 datasetFileName = os.path.join('..', 'data', 'batch', 'serialization.csv')
33 
34 
35 def serializeNumericTable(dataTable):
36 
37  # Create a data archive to serialize the numeric table
38  dataArch = InputDataArchive()
39 
40  # Serialize the numeric table into the data archive
41  dataTable.serialize(dataArch)
42 
43  # Get the length of the serialized data in bytes
44  length = dataArch.getSizeOfArchive()
45 
46  # Store the serialized data in an array
47  buffer = np.zeros(length, dtype=np.ubyte)
48  dataArch.copyArchiveToArray(buffer)
49 
50  return buffer
51 
52 
53 def deserializeNumericTable(buffer):
54 
55  # Create a data archive to deserialize the numeric table
56  dataArch = OutputDataArchive(buffer)
57 
58  # Create a numeric table object
59  dataTable = HomogenNumericTable()
60 
61  # Deserialize the numeric table from the data archive
62  dataTable.deserialize(dataArch)
63 
64  return dataTable
65 
66 
67 if __name__ == "__main__":
68 
69  # Initialize FileDataSource_CSVFeatureManager to retrieve the input data from a .csv file
70  dataSource = FileDataSource(
71  datasetFileName, DataSource.doAllocateNumericTable, DataSource.doDictionaryFromContext
72  )
73 
74  # Retrieve the data from the input file
75  dataSource.loadDataBlock()
76 
77  # Retrieve a numeric table
78  dataTable = dataSource.getNumericTable()
79 
80  # Print the original data
81  printNumericTable(dataTable, "Data before serialization:")
82 
83  # Serialize the numeric table into the memory buffer
84  buffer = serializeNumericTable(dataTable)
85 
86  # Deserialize the numeric table from the memory buffer
87  restoredDataTable = deserializeNumericTable(buffer)
88 
89  # Print the restored data
90  printNumericTable(restoredDataTable, "Data after deserialization:")

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