Skip to content

Commit

Permalink
Revert "faster ctypes/numpy conversions"
Browse files Browse the repository at this point in the history
This reverts commit 5ce3423.
  • Loading branch information
AndrewAnnex committed Jun 9, 2018
1 parent 5ce3423 commit 612bb7a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
2 changes: 1 addition & 1 deletion spiceypy/spiceypy.py
Original file line number Diff line number Diff line change
Expand Up @@ -9085,7 +9085,7 @@ def oscltx(state, et, mu):
mu = ctypes.c_double(mu)
elts = stypes.emptyDoubleVector(20)
libspice.oscltx_c(state, et, mu, elts)
return stypes.cVectorToPython(elts)[0:11]
return stypes.cVectorToPython(elts[0:11])


################################################################################
Expand Down
30 changes: 18 additions & 12 deletions spiceypy/utils/support_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,12 @@ def emptyIntVector(n):
return (c_int * n)()


def emptyBoolVector(n):
if isinstance(n, c_int):
n = n.value
return (c_bool * n)()


def cVectorToPython(x):
"""
Convert the c vector data into the correct python data type
Expand All @@ -163,11 +169,11 @@ def cVectorToPython(x):
:return:
"""
if isinstance(x[0], bool):
return numpy.frombuffer(x, dtype=numpy.bool).copy()
return numpy.fromiter(x, numpy.bool, count=len(x))
elif isinstance(x[0], int):
return numpy.frombuffer(x, dtype=numpy.int32).copy()
return numpy.fromiter(x, numpy.int_, count=len(x))
elif isinstance(x[0], float):
return numpy.frombuffer(x, dtype=numpy.float64).copy()
return numpy.fromiter(x, numpy.float64, count=len(x))
elif isinstance(x[0].value, bytes):
return [toPythonString(y) for y in x]

Expand Down Expand Up @@ -254,7 +260,7 @@ def from_ndarray(self, param):
# return param.data_as(POINTER(c_double))
# the above older method does not work with
# functions which take vectors of known size
return numpc.as_ctypes(param)
return numpy.ctypeslib.as_ctypes(param)

# Cast from array.array objects
def from_array(self, param):
Expand Down Expand Up @@ -287,11 +293,11 @@ def from_tuple(self, param):

# Cast from a numpy array
def from_ndarray(self, param):
return numpc.as_ctypes(param)
return numpy.ctypeslib.as_ctypes(param)

# Cast from a numpy matrix
def from_matrix(self, param):
return numpc.as_ctypes(param)
return numpy.ctypeslib.as_ctypes(param)


class IntArrayType:
Expand All @@ -318,8 +324,10 @@ def from_tuple(self, param):

# Cast from a numpy array
def from_ndarray(self, param):
# cspice always uses a int size half as big as the float, ie int32 if a float64 system default
return numpc.as_ctypes(param.astype(numpy.int32))
# return param.data_as(POINTER(c_int))
# not sure if long is same as int, it should be..
# return numpy.ctypeslib.as_ctypes(param)
return self.from_param(param.tolist())

# Cast from array.array objects
def from_array(self, param):
Expand Down Expand Up @@ -352,13 +360,11 @@ def from_tuple(self, param):

# Cast from a numpy array
def from_ndarray(self, param):
# cspice always uses a int size half as big as the float, ie int32 if a float64 system default
return numpc.as_ctypes(param.astype(numpy.int32))
return numpy.ctypeslib.as_ctypes(param)

# Cast from a numpy matrix
def from_matrix(self, param):
# cspice always uses a int size half as big as the float, ie int32 if a float64 system default
return numpc.as_ctypes(param.astype(numpy.int32))
return numpy.ctypeslib.as_ctypes(param)


class BoolArrayType:
Expand Down

0 comments on commit 612bb7a

Please sign in to comment.