Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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 numpy 

33 

34 

35def _extend_mode_to_code(mode): 

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

37 """ 

38 if mode == 'nearest': 

39 return 0 

40 elif mode == 'wrap': 

41 return 1 

42 elif mode == 'reflect': 

43 return 2 

44 elif mode == 'mirror': 

45 return 3 

46 elif mode == 'constant': 

47 return 4 

48 else: 

49 raise RuntimeError('boundary mode not supported') 

50 

51 

52def _normalize_sequence(input, rank): 

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

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

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

56 """ 

57 is_str = isinstance(input, str) 

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

59 normalized = list(input) 

60 if len(normalized) != rank: 

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

62 raise RuntimeError(err) 

63 else: 

64 normalized = [input] * rank 

65 return normalized 

66 

67 

68def _get_output(output, input, shape=None): 

69 if shape is None: 

70 shape = input.shape 

71 if output is None: 

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

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

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

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

76 elif isinstance(output, str): 

77 output = numpy.typeDict[output] 

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

79 elif output.shape != shape: 

80 raise RuntimeError("output shape not correct") 

81 return output