|
| 1 | +# ============================================================================== |
| 2 | +# Copyright 2024 Intel Corporation |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | +# ============================================================================== |
| 16 | + |
| 17 | +import pickle |
| 18 | +import unittest |
| 19 | + |
| 20 | +import numpy as np |
| 21 | + |
| 22 | +import daal4py |
| 23 | + |
| 24 | + |
| 25 | +class Test(unittest.TestCase): |
| 26 | + def test_serialization_of_qr(self): |
| 27 | + obj_original = daal4py.qr(fptype="float") |
| 28 | + obj_deserialized = pickle.loads(pickle.dumps(obj_original)) |
| 29 | + |
| 30 | + rng = np.random.default_rng(seed=123) |
| 31 | + X = rng.standard_normal(size=(10, 5)) |
| 32 | + |
| 33 | + Q_orig = obj_original.compute(X).matrixQ |
| 34 | + Q_deserialized = obj_deserialized.compute(X).matrixQ |
| 35 | + np.testing.assert_almost_equal(Q_orig, Q_deserialized) |
| 36 | + assert Q_orig.dtype == Q_deserialized.dtype |
| 37 | + |
| 38 | + def test_serialization_of_kmeans(self): |
| 39 | + obj_original = daal4py.kmeans_init(nClusters=4) |
| 40 | + obj_deserialized = pickle.loads(pickle.dumps(obj_original)) |
| 41 | + |
| 42 | + rng = np.random.default_rng(seed=123) |
| 43 | + X = rng.standard_normal(size=(100, 20)) |
| 44 | + |
| 45 | + np.testing.assert_almost_equal( |
| 46 | + obj_original.compute(X).centroids, |
| 47 | + obj_deserialized.compute(X).centroids, |
| 48 | + ) |
0 commit comments