Coverage for  / home / jenkins / .local / lib / python3.10 / site-packages / hyper_parallel / auto_parallel / sapp_nd / memory_estimation / score.py: 100%

16 statements  

« prev     ^ index     » next       coverage.py v7.13.1, created at 2026-07-06 05:41 +0800

1# Copyright 2025 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 score functions""" 

16import numpy as np 

17 

18 

19def mape(pred, real): 

20 """Mean absolute percentage error. 

21 

22 Args: 

23 pred (list): Predicted values. 

24 real (list): Real values. 

25 

26 Returns: 

27 float: Mean absolute percentage error, or None if no valid pair exists. 

28 """ 

29 valid_pairs = [(p, r) for p, r in zip(pred, real) if p > 0 and r > 0] 

30 if not valid_pairs: 

31 return None 

32 return 100 / len(valid_pairs) * sum( 

33 abs((r - p) / r) for p, r in valid_pairs 

34 ) 

35 

36 

37def r2(pred, real): 

38 """Coefficient of determination. 

39 

40 Args: 

41 pred (list): Predicted values. 

42 real (list): Real values. 

43 

44 Returns: 

45 float: Coefficient of determination, or None if unavailable. 

46 """ 

47 pairs = list(zip(pred, real)) 

48 if len(pairs) < 2: 

49 return None 

50 real_values = [r for _, r in pairs] 

51 m = np.mean(real_values) 

52 denominator = sum((r - m) ** 2 for _, r in pairs) 

53 if denominator == 0: 

54 return None 

55 return 1 - sum((r - p) ** 2 for p, r in pairs) / denominator