|
| 1 | +# ------------------------------------------------------------- |
| 2 | +# |
| 3 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 4 | +# or more contributor license agreements. See the NOTICE file |
| 5 | +# distributed with this work for additional information |
| 6 | +# regarding copyright ownership. The ASF licenses this file |
| 7 | +# to you under the Apache License, Version 2.0 (the |
| 8 | +# "License"); you may not use this file except in compliance |
| 9 | +# with the License. You may obtain a copy of the License at |
| 10 | +# |
| 11 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +# |
| 13 | +# Unless required by applicable law or agreed to in writing, |
| 14 | +# software distributed under the License is distributed on an |
| 15 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 16 | +# KIND, either express or implied. See the License for the |
| 17 | +# specific language governing permissions and limitations |
| 18 | +# under the License. |
| 19 | +# |
| 20 | +# ------------------------------------------------------------- |
| 21 | + |
| 22 | +import unittest |
| 23 | +import numpy as np |
| 24 | +from systemds.context import SystemDSContext |
| 25 | + |
| 26 | +np.random.seed(7) |
| 27 | +m = np.array([[1, 2, 3], [6, 5, 4], [8, 7, 9]]) |
| 28 | +M = np.random.random_integers(9, size=300).reshape(100, 3) |
| 29 | +p = np.array([0.25, 0.5, 0.75]) |
| 30 | +m2 = np.array([1, 2, 3, 4, 5]) |
| 31 | +w2 = np.array([1, 1, 1, 1, 5]) |
| 32 | + |
| 33 | + |
| 34 | +def weighted_quantiles(values, weights, quantiles=0.5): |
| 35 | + i = np.argsort(values) |
| 36 | + c = np.cumsum(weights[i]) |
| 37 | + return values[i[np.searchsorted(c, np.array(quantiles) * c[-1])]] |
| 38 | + |
| 39 | + |
| 40 | +class TestARGMINMAX(unittest.TestCase): |
| 41 | + def setUp(self): |
| 42 | + self.sds = SystemDSContext() |
| 43 | + |
| 44 | + def tearDown(self): |
| 45 | + self.sds.close() |
| 46 | + |
| 47 | + def test_argmin_basic1(self): |
| 48 | + sds_input = self.sds.from_numpy(m) |
| 49 | + sds_result = sds_input.argmin(0).compute() |
| 50 | + np_result = np.argmin(m, axis=0).reshape(-1, 1) |
| 51 | + assert np.allclose(sds_result - 1, np_result, 1e-9) |
| 52 | + |
| 53 | + def test_argmin_basic2(self): |
| 54 | + sds_input = self.sds.from_numpy(m) |
| 55 | + sds_result = sds_input.argmin(1).compute() |
| 56 | + np_result = np.argmin(m, axis=1).reshape(-1, 1) |
| 57 | + assert np.allclose(sds_result - 1, np_result, 1e-9) |
| 58 | + |
| 59 | + def test_argmin_basic3(self): |
| 60 | + sds_input = self.sds.from_numpy(m) |
| 61 | + sds_result = sds_input.argmin().compute(verbose=True) |
| 62 | + np_result = np.argmin(m) |
| 63 | + assert np.allclose(sds_result - 1, np_result, 1e-9) |
| 64 | + |
| 65 | + def test_argmin_basic4(self): |
| 66 | + sds_input = self.sds.from_numpy(m) |
| 67 | + with self.assertRaises(ValueError): |
| 68 | + sds_input.argmin(3) |
| 69 | + |
| 70 | + def test_argmax_basic1(self): |
| 71 | + sds_input = self.sds.from_numpy(m) |
| 72 | + sds_result = sds_input.argmax(0).compute() |
| 73 | + np_result = np.argmax(m, axis=0).reshape(-1, 1) |
| 74 | + assert np.allclose(sds_result - 1, np_result, 1e-9) |
| 75 | + |
| 76 | + def test_argmax_basic2(self): |
| 77 | + sds_input = self.sds.from_numpy(m) |
| 78 | + sds_result = sds_input.argmax(1).compute() |
| 79 | + np_result = np.argmax(m, axis=1).reshape(-1, 1) |
| 80 | + assert np.allclose(sds_result - 1, np_result, 1e-9) |
| 81 | + |
| 82 | + def test_argmax_basic3(self): |
| 83 | + sds_input = self.sds.from_numpy(m) |
| 84 | + sds_result = sds_input.argmax().compute() |
| 85 | + np_result = np.argmax(m) |
| 86 | + assert np.allclose(sds_result - 1, np_result, 1e-9) |
| 87 | + |
| 88 | + def test_argmax_basic4(self): |
| 89 | + sds_input = self.sds.from_numpy(m) |
| 90 | + with self.assertRaises(ValueError): |
| 91 | + sds_input.argmax(3) |
| 92 | + |
| 93 | + |
| 94 | +if __name__ == "__main__": |
| 95 | + unittest.main() |
0 commit comments