Coverage for python/lsst/images/utils.py: 91%

18 statements  

« prev     ^ index     » next       coverage.py v7.14.3, created at 2026-07-08 02:03 -0700

1# This file is part of lsst-images. 

2# 

3# Developed for the LSST Data Management System. 

4# This product includes software developed by the LSST Project 

5# (https://www.lsst.org). 

6# See the COPYRIGHT file at the top-level directory of this distribution 

7# for details of code ownership. 

8# 

9# Use of this source code is governed by a 3-clause BSD-style 

10# license that can be found in the LICENSE file. 

11 

12from __future__ import annotations 

13 

14__all__ = ("is_none", "round_half_away_from_zero", "round_half_down", "round_half_up") 

15 

16import math 

17import operator 

18import sys 

19from typing import TYPE_CHECKING 

20 

21if TYPE_CHECKING: 

22 from typing_extensions import TypeIs 

23 

24if sys.version_info >= (3, 14): 24 ↛ 25line 24 didn't jump to line 25 because the condition on line 24 was never true

25 is_none = operator.is_none 

26else: 

27 

28 def is_none(x: object) -> TypeIs[None]: 

29 """Test whether an object is None.""" 

30 return x is None 

31 

32 

33def round_half_up(x: float) -> int: 

34 """Round a `float` to an `int`, always rounding half up. 

35 

36 Note that Python's built-in `round` implements the "round half to even" 

37 strategy. This function implements the strategy used in `lsst.geom.Point` 

38 conversions. 

39 

40 Parameters 

41 ---------- 

42 x 

43 Value to round. 

44 """ 

45 return math.floor(x + 0.5) 

46 

47 

48def round_half_down(x: float) -> int: 

49 """Round a `float` to an `int`, always rounding half down. 

50 

51 Note that Python's built-in `round` implements the "round half to even" 

52 strategy. 

53 

54 Parameters 

55 ---------- 

56 x 

57 Value to round. 

58 """ 

59 return math.ceil(x - 0.5) 

60 

61 

62def round_half_away_from_zero(x: float) -> int: 

63 """Round a `float` to an `int`, always rounding away from zero. 

64 

65 Note that Python's built-in `round` implements the "round half to even" 

66 strategy. This function implements the C/C++ standard strategy. 

67 

68 Parameters 

69 ---------- 

70 x 

71 Value to round. 

72 """ 

73 if x > 0: 

74 return math.floor(x + 0.5) 

75 else: 

76 return math.ceil(x - 0.5)