Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/scipy/ndimage/_ni_support.py: 12%

51 statements  

« prev     ^ index     » next       coverage.py v7.3.2, created at 2023-12-12 06:31 +0000

1# Copyright (C) 2003-2005 Peter J. Verveer 

2# 

3# Redistribution and use in source and binary forms, with or without 

4# modification, are permitted provided that the following conditions 

5# are met: 

6# 

7# 1. Redistributions of source code must retain the above copyright 

8# notice, this list of conditions and the following disclaimer. 

9# 

10# 2. Redistributions in binary form must reproduce the above 

11# copyright notice, this list of conditions and the following 

12# disclaimer in the documentation and/or other materials provided 

13# with the distribution. 

14# 

15# 3. The name of the author may not be used to endorse or promote 

16# products derived from this software without specific prior 

17# written permission. 

18# 

19# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS 

20# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 

21# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 

22# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY 

23# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 

24# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 

25# GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 

26# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 

27# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 

28# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 

29# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 

30 

31from collections.abc import Iterable 

32import warnings 

33import numpy 

34 

35 

36def _extend_mode_to_code(mode): 

37 """Convert an extension mode to the corresponding integer code. 

38 """ 

39 if mode == 'nearest': 

40 return 0 

41 elif mode == 'wrap': 

42 return 1 

43 elif mode in ['reflect', 'grid-mirror']: 

44 return 2 

45 elif mode == 'mirror': 

46 return 3 

47 elif mode == 'constant': 

48 return 4 

49 elif mode == 'grid-wrap': 

50 return 5 

51 elif mode == 'grid-constant': 

52 return 6 

53 else: 

54 raise RuntimeError('boundary mode not supported') 

55 

56 

57def _normalize_sequence(input, rank): 

58 """If input is a scalar, create a sequence of length equal to the 

59 rank by duplicating the input. If input is a sequence, 

60 check if its length is equal to the length of array. 

61 """ 

62 is_str = isinstance(input, str) 

63 if not is_str and isinstance(input, Iterable): 

64 normalized = list(input) 

65 if len(normalized) != rank: 

66 err = "sequence argument must have length equal to input rank" 

67 raise RuntimeError(err) 

68 else: 

69 normalized = [input] * rank 

70 return normalized 

71 

72 

73def _get_output(output, input, shape=None, complex_output=False): 

74 if shape is None: 

75 shape = input.shape 

76 if output is None: 

77 if not complex_output: 

78 output = numpy.zeros(shape, dtype=input.dtype.name) 

79 else: 

80 complex_type = numpy.promote_types(input.dtype, numpy.complex64) 

81 output = numpy.zeros(shape, dtype=complex_type) 

82 elif isinstance(output, (type, numpy.dtype)): 

83 # Classes (like `np.float32`) and dtypes are interpreted as dtype 

84 if complex_output and numpy.dtype(output).kind != 'c': 

85 warnings.warn("promoting specified output dtype to complex") 

86 output = numpy.promote_types(output, numpy.complex64) 

87 output = numpy.zeros(shape, dtype=output) 

88 elif isinstance(output, str): 

89 output = numpy.sctypeDict[output] 

90 if complex_output and numpy.dtype(output).kind != 'c': 

91 raise RuntimeError("output must have complex dtype") 

92 output = numpy.zeros(shape, dtype=output) 

93 elif output.shape != shape: 

94 raise RuntimeError("output shape not correct") 

95 elif complex_output and output.dtype.kind != 'c': 

96 raise RuntimeError("output must have complex dtype") 

97 return output