1# Copyright 2021 Huawei Technologies Co., Ltd 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14# ============================================================================ 15# """test_hausdorff_distance""" 16 17import math 18import numpy as np 19import pytest 20from mindspore import Tensor 21from mindspore.nn.metrics import get_metric_fn, HausdorffDistance 22 23 24def test_hausdorff_distance(): 25 """test_hausdorff_distance""" 26 x = Tensor(np.array([[3, 0, 1], [1, 3, 0], [1, 0, 2]])) 27 y = Tensor(np.array([[0, 2, 1], [1, 2, 1], [0, 0, 1]])) 28 metric = get_metric_fn('hausdorff_distance') 29 metric.clear() 30 metric.update(x, y, 0) 31 distance = metric.eval() 32 33 assert math.isclose(distance, 1.4142135623730951, abs_tol=0.001) 34 35 36def test_hausdorff_distance_update1(): 37 x = Tensor(np.array([[0.2, 0.5, 0.7], [0.3, 0.1, 0.2], [0.9, 0.6, 0.5]])) 38 metric = HausdorffDistance() 39 metric.clear() 40 41 with pytest.raises(ValueError): 42 metric.update(x) 43 44 45def test_hausdorff_distance_update2(): 46 x = Tensor(np.array([[0.2, 0.5, 0.7], [0.3, 0.1, 0.2], [0.9, 0.6, 0.5]])) 47 y = Tensor(np.array([1, 0])) 48 metric = HausdorffDistance() 49 metric.clear() 50 51 with pytest.raises(ValueError): 52 metric.update(x, y) 53 54 55def test_hausdorff_distance_init(): 56 with pytest.raises(ValueError): 57 HausdorffDistance(distance_metric="eucli", percentile=None, directed=False, crop=False) 58 59 60def test_hausdorff_distance_runtime(): 61 metric = HausdorffDistance() 62 metric.clear() 63 64 with pytest.raises(RuntimeError): 65 metric.eval() 66