@@ -20,9 +20,11 @@ import Python
20
20
// a Python import error until it is first used.
21
21
private let np = Python . import ( " numpy " )
22
22
23
- // private func debugLogNumpyError(_ message: String) {
24
- // debugLog("NumPy conversion error: " + message)
25
- // }
23
+ private func debugLogNumpyError( _ message: String ) {
24
+ #if ENABLE_NUMPY_LOGGING
25
+ debugLog ( " NumPy conversion error: " + message)
26
+ #endif
27
+ }
26
28
27
29
extension ShapedArray : ConvertibleFromNumpyArray
28
30
where Scalar: NumpyScalarCompatible {
@@ -35,25 +37,25 @@ extension ShapedArray: ConvertibleFromNumpyArray
35
37
public init ? ( numpy numpyArray: PythonObject ) {
36
38
// Check if input is a `numpy.ndarray` instance.
37
39
guard Python . isinstance ( numpyArray, np. ndarray) == true else {
38
- // debugLogNumpyError("""
39
- // PythonObject input has type '\(Python.type(numpyArray))' and is not \
40
- // an instance of 'numpy.ndarray'.
41
- // """)
40
+ debugLogNumpyError ( """
41
+ PythonObject input has type ' \( Python . type ( numpyArray) ) ' and is not \
42
+ an instance of 'numpy.ndarray'.
43
+ """ )
42
44
return nil
43
45
}
44
46
// Check if the dtype of the `ndarray` is compatible with the `Scalar`
45
47
// type.
46
48
guard Scalar . numpyScalarTypes. contains ( numpyArray. dtype) else {
47
- // debugLogNumpyError("""
48
- // 'numpy.ndarray' dtype '\(numpyArray.dtype)' is incompatible with \
49
- // Swift type '\(Scalar.self)'.
50
- // """)
49
+ debugLogNumpyError ( """
50
+ 'numpy.ndarray' dtype ' \( numpyArray. dtype) ' is incompatible with \
51
+ Swift type ' \( Scalar . self) '.
52
+ """ )
51
53
return nil
52
54
}
53
55
54
56
let pyShape = numpyArray. __array_interface__ [ " shape " ]
55
57
guard let shape = [ Int] ( pyShape) else {
56
- // debugLogNumpyError("cannot access shape of 'numpy.ndarray' instance.")
58
+ debugLogNumpyError ( " cannot access shape of 'numpy.ndarray' instance. " )
57
59
return nil
58
60
}
59
61
@@ -63,7 +65,7 @@ extension ShapedArray: ConvertibleFromNumpyArray
63
65
64
66
guard let ptrVal =
65
67
UInt ( contiguousNumpyArray. __array_interface__ [ " data " ] . tuple2. 0 ) else {
66
- // debugLogNumpyError("cannot access data of 'numpy.ndarray' instance.")
68
+ debugLogNumpyError ( " cannot access data of 'numpy.ndarray' instance. " )
67
69
return nil
68
70
}
69
71
// Note: `ptr` is not nil even if the `ndarray` is empty (i.e. has a shape
@@ -98,25 +100,25 @@ extension Tensor: ConvertibleFromNumpyArray where Scalar: NumpyScalarCompatible
98
100
public init ? ( numpy numpyArray: PythonObject ) {
99
101
// Check if input is a `numpy.ndarray` instance.
100
102
guard Python . isinstance ( numpyArray, np. ndarray) == true else {
101
- // debugLogNumpyError("""
102
- // PythonObject input has type '\(Python.type(numpyArray))' and is not \
103
- // an instance of 'numpy.ndarray'.
104
- // """)
103
+ debugLogNumpyError ( """
104
+ PythonObject input has type ' \( Python . type ( numpyArray) ) ' and is not \
105
+ an instance of 'numpy.ndarray'.
106
+ """ )
105
107
return nil
106
108
}
107
109
// Check if the dtype of the `ndarray` is compatible with the `Scalar`
108
110
// type.
109
111
guard Scalar . numpyScalarTypes. contains ( numpyArray. dtype) else {
110
- // debugLogNumpyError("""
111
- // 'numpy.ndarray' dtype '\(numpyArray.dtype)' is incompatible with \
112
- // Swift type '\(Scalar.self)'.
113
- // """)
112
+ debugLogNumpyError ( """
113
+ 'numpy.ndarray' dtype ' \( numpyArray. dtype) ' is incompatible with \
114
+ Swift type ' \( Scalar . self) '.
115
+ """ )
114
116
return nil
115
117
}
116
118
117
119
let pyShape = numpyArray. __array_interface__ [ " shape " ]
118
120
guard let dimensions = [ Int] ( pyShape) else {
119
- // debugLogNumpyError("cannot access shape of 'numpy.ndarray' instance.")
121
+ debugLogNumpyError ( " cannot access shape of 'numpy.ndarray' instance. " )
120
122
return nil
121
123
}
122
124
let shape = TensorShape ( dimensions)
@@ -126,7 +128,7 @@ extension Tensor: ConvertibleFromNumpyArray where Scalar: NumpyScalarCompatible
126
128
let contiguousNumpyArray = np. ascontiguousarray ( numpyArray)
127
129
128
130
guard let ptrVal = UInt ( contiguousNumpyArray. __array_interface__ [ " data " ] . tuple2. 0 ) else {
129
- // debugLogNumpyError("cannot access data of 'numpy.ndarray' instance.")
131
+ debugLogNumpyError ( " cannot access data of 'numpy.ndarray' instance. " )
130
132
return nil
131
133
}
132
134
// Note: `ptr` is not nil even if the `ndarray` is empty (i.e. has a shape
0 commit comments