Skip to content

Commit

Permalink
faster ctypes/numpy conversions
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewAnnex committed Jun 8, 2018
1 parent 11fad22 commit 5ce3423
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 19 deletions.
2 changes: 1 addition & 1 deletion spiceypy/spiceypy.py
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: 12 additions & 18 deletions spiceypy/utils/support_types.py
Expand Up @@ -155,12 +155,6 @@ 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 @@ -169,11 +163,11 @@ def cVectorToPython(x):
:return:
"""
if isinstance(x[0], bool):
return numpy.fromiter(x, numpy.bool, count=len(x))
return numpy.frombuffer(x, dtype=numpy.bool).copy()
elif isinstance(x[0], int):
return numpy.fromiter(x, numpy.int_, count=len(x))
return numpy.frombuffer(x, dtype=numpy.int32).copy()
elif isinstance(x[0], float):
return numpy.fromiter(x, numpy.float64, count=len(x))
return numpy.frombuffer(x, dtype=numpy.float64).copy()
elif isinstance(x[0].value, bytes):
return [toPythonString(y) for y in x]

Expand Down Expand Up @@ -260,7 +254,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 numpy.ctypeslib.as_ctypes(param)
return numpc.as_ctypes(param)

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

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

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


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

# Cast from a numpy array
def from_ndarray(self, param):
# 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())
# 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))

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

# Cast from a numpy array
def from_ndarray(self, param):
return numpy.ctypeslib.as_ctypes(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))

# Cast from a numpy matrix
def from_matrix(self, param):
return numpy.ctypeslib.as_ctypes(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))


class BoolArrayType:
Expand Down

0 comments on commit 5ce3423

Please sign in to comment.