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

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

from __future__ import absolute_import 

 

import os 

import sys 

 

import numpy as np 

 

 

# Copy-pasted from Python 3.4's shutil. 

def which(cmd, mode=os.F_OK | os.X_OK, path=None): 

"""Given a command, mode, and a PATH string, return the path which 

conforms to the given mode on the PATH, or None if there is no such 

file. 

 

`mode` defaults to os.F_OK | os.X_OK. `path` defaults to the result 

of os.environ.get("PATH"), or can be overridden with a custom search 

path. 

 

""" 

# Check that a given file can be accessed with the correct mode. 

# Additionally check that `file` is not a directory, as on Windows 

# directories pass the os.access check. 

def _access_check(fn, mode): 

return (os.path.exists(fn) and os.access(fn, mode) 

and not os.path.isdir(fn)) 

 

# If we're given a path with a directory part, look it up directly rather 

# than referring to PATH directories. This includes checking relative to the 

# current directory, e.g. ./script 

if os.path.dirname(cmd): 

if _access_check(cmd, mode): 

return cmd 

return None 

 

if path is None: 

path = os.environ.get("PATH", os.defpath) 

if not path: 

return None 

path = path.split(os.pathsep) 

 

if sys.platform == "win32": 

# The current directory takes precedence on Windows. 

if not os.curdir in path: 

path.insert(0, os.curdir) 

 

# PATHEXT is necessary to check on Windows. 

pathext = os.environ.get("PATHEXT", "").split(os.pathsep) 

# See if the given file matches any of the expected path extensions. 

# This will allow us to short circuit when given "python.exe". 

# If it does match, only test that one, otherwise we have to try 

# others. 

if any(cmd.lower().endswith(ext.lower()) for ext in pathext): 

files = [cmd] 

else: 

files = [cmd + ext for ext in pathext] 

else: 

# On other platforms you don't have things like PATHEXT to tell you 

# what file suffixes are executable, so just pass on cmd as-is. 

files = [cmd] 

 

seen = set() 

for dir in path: 

normdir = os.path.normcase(dir) 

if not normdir in seen: 

seen.add(normdir) 

for thefile in files: 

name = os.path.join(dir, thefile) 

if _access_check(name, mode): 

return name 

return None 

 

 

# Copy-pasted from numpy.lib.stride_tricks 1.11.2. 

def _maybe_view_as_subclass(original_array, new_array): 

if type(original_array) is not type(new_array): 

# if input was an ndarray subclass and subclasses were OK, 

# then view the result as that subclass. 

new_array = new_array.view(type=type(original_array)) 

# Since we have done something akin to a view from original_array, we 

# should let the subclass finalize (if it has it implemented, i.e., is 

# not None). 

if new_array.__array_finalize__: 

new_array.__array_finalize__(original_array) 

return new_array 

 

 

# Copy-pasted from numpy.lib.stride_tricks 1.11.2. 

def _broadcast_to(array, shape, subok, readonly): 

shape = tuple(shape) if np.iterable(shape) else (shape,) 

array = np.array(array, copy=False, subok=subok) 

if not shape and array.shape: 

raise ValueError('cannot broadcast a non-scalar to a scalar array') 

if any(size < 0 for size in shape): 

raise ValueError('all elements of broadcast shape must be non-' 

'negative') 

needs_writeable = not readonly and array.flags.writeable 

extras = ['reduce_ok'] if needs_writeable else [] 

op_flag = 'readwrite' if needs_writeable else 'readonly' 

broadcast = np.nditer( 

(array,), flags=['multi_index', 'refs_ok', 'zerosize_ok'] + extras, 

op_flags=[op_flag], itershape=shape, order='C').itviews[0] 

result = _maybe_view_as_subclass(array, broadcast) 

if needs_writeable and not result.flags.writeable: 

result.flags.writeable = True 

return result 

 

 

# Copy-pasted from numpy.lib.stride_tricks 1.11.2. 

def broadcast_to(array, shape, subok=False): 

"""Broadcast an array to a new shape. 

 

Parameters 

---------- 

array : array_like 

The array to broadcast. 

shape : tuple 

The shape of the desired array. 

subok : bool, optional 

If True, then sub-classes will be passed-through, otherwise 

the returned array will be forced to be a base-class array (default). 

 

Returns 

------- 

broadcast : array 

A readonly view on the original array with the given shape. It is 

typically not contiguous. Furthermore, more than one element of a 

broadcasted array may refer to a single memory location. 

 

Raises 

------ 

ValueError 

If the array is not compatible with the new shape according to NumPy's 

broadcasting rules. 

 

Notes 

----- 

.. versionadded:: 1.10.0 

 

Examples 

-------- 

>>> x = np.array([1, 2, 3]) 

>>> np.broadcast_to(x, (3, 3)) 

array([[1, 2, 3], 

[1, 2, 3], 

[1, 2, 3]]) 

""" 

return _broadcast_to(array, shape, subok=subok, readonly=True)