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

17 statements  

« prev     ^ index     » next       coverage.py v7.14.1, created at 2026-05-29 01:48 -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 

19 

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

21 is_none = operator.is_none # type: ignore[attr-defined] 

22else: 

23 

24 def is_none(x: object) -> bool: 

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

26 return x is None 

27 

28 

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

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

31 

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

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

34 conversions. 

35 """ 

36 return math.floor(x + 0.5) 

37 

38 

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

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

41 

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

43 strategy. 

44 """ 

45 return math.ceil(x - 0.5) 

46 

47 

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

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

50 

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

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

53 """ 

54 if x > 0: 

55 return math.floor(x + 0.5) 

56 else: 

57 return math.ceil(x - 0.5)