summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Rondeau <tom@trondeau.com>2014-04-10 13:45:43 -0400
committerTom Rondeau <tom@trondeau.com>2014-04-10 13:45:43 -0400
commit071c36265f97e0a13b00564b1f46e75be1e1522f (patch)
tree84684e9b2be005dd1fc43fc85907ff9ffd5b2086
parent9880ba4cd98925c9e5803847ff22af8038514fba (diff)
parent10c48ced05c22266ae089ac560b2beb45f67851f (diff)
Merge remote-tracking branch 'mmueller/pmt_to_python-py2.6-compat'
-rw-r--r--gnuradio-runtime/python/pmt/pmt_to_python.py64
-rwxr-xr-xgnuradio-runtime/python/pmt/qa_pmt_to_python.py16
2 files changed, 49 insertions, 31 deletions
diff --git a/gnuradio-runtime/python/pmt/pmt_to_python.py b/gnuradio-runtime/python/pmt/pmt_to_python.py
index 3c4dcf4425..3344eba163 100644
--- a/gnuradio-runtime/python/pmt/pmt_to_python.py
+++ b/gnuradio-runtime/python/pmt/pmt_to_python.py
@@ -64,37 +64,41 @@ def pmt_from_dict(p):
return d
numpy_mappings = {
- (numpy.float32,pmt.init_f32vector, float, pmt.f32vector_elements, pmt.is_f32vector),
- (numpy.float64,pmt.init_f64vector, float, pmt.f64vector_elements, pmt.is_f64vector),
- (numpy.complex64,pmt.init_c32vector, complex, pmt.c32vector_elements, pmt.is_c32vector),
- (numpy.complex128,pmt.init_c64vector, complex, pmt.c64vector_elements, pmt.is_c64vector),
- (numpy.int8,pmt.init_s8vector, int, pmt.s8vector_elements, pmt.is_s8vector),
- (numpy.int16,pmt.init_s16vector, int, pmt.s16vector_elements, pmt.is_s16vector),
- (numpy.int32,pmt.init_s32vector, int, pmt.s32vector_elements, pmt.is_s32vector),
-# (numpy.int64,pmt.init_s64vector, int, pmt.s64vector_elements, pmt.is_s64vector),
- (numpy.uint8,pmt.init_u8vector, int, pmt.u8vector_elements, pmt.is_u8vector),
- (numpy.uint16,pmt.init_u16vector, int, pmt.u16vector_elements, pmt.is_u16vector),
- (numpy.uint32,pmt.init_u32vector, int, pmt.u32vector_elements, pmt.is_u32vector),
-# (numpy.uint64,pmt.init_u64vector, int, pmt.u64vector_elements, pmt.is_u64vector),
- (numpy.byte,pmt.init_u8vector, int, pmt.u8vector_elements, pmt.is_u8vector),
- }
+ numpy.dtype(numpy.float32): (pmt.init_f32vector, float, pmt.f32vector_elements, pmt.is_f32vector),
+ numpy.dtype(numpy.float64): (pmt.init_f64vector, float, pmt.f64vector_elements, pmt.is_f64vector),
+ numpy.dtype(numpy.complex64): (pmt.init_c32vector, complex, pmt.c32vector_elements, pmt.is_c32vector),
+ numpy.dtype(numpy.complex128): (pmt.init_c64vector, complex, pmt.c64vector_elements, pmt.is_c64vector),
+ numpy.dtype(numpy.int8): (pmt.init_s8vector, int, pmt.s8vector_elements, pmt.is_s8vector),
+ numpy.dtype(numpy.int16): (pmt.init_s16vector, int, pmt.s16vector_elements, pmt.is_s16vector),
+ numpy.dtype(numpy.int32): (pmt.init_s32vector, int, pmt.s32vector_elements, pmt.is_s32vector),
+# numpy.dtype(numpy.int64): (pmt.init_s64vector, int, pmt.s64vector_elements, pmt.is_s64vector),
+ numpy.dtype(numpy.uint8): (pmt.init_u8vector, int, pmt.u8vector_elements, pmt.is_u8vector),
+ numpy.dtype(numpy.uint16): (pmt.init_u16vector, int, pmt.u16vector_elements, pmt.is_u16vector),
+ numpy.dtype(numpy.uint32): (pmt.init_u32vector, int, pmt.u32vector_elements, pmt.is_u32vector),
+# numpy.dtype(numpy.uint64): (pmt.init_u64vector, int, pmt.u64vector_elements, pmt.is_u64vector),
+ numpy.dtype(numpy.byte): (pmt.init_u8vector, int, pmt.u8vector_elements, pmt.is_u8vector),
+}
-def numpy_to_uvector(p):
- if not p.dtype in map(lambda x: x[0], numpy_mappings):
- raise ValueError("unsupported numpy array dtype for converstion to pmt %s"%(p.dtype))
- for m in numpy_mappings:
- if(m[0] == p.dtype):
- pc = map(lambda i: m[2](i), p)
- return m[1](p.size, pc);
+uvector_mappings = dict([ (numpy_mappings[key][3], (numpy_mappings[key][2], key)) for key in numpy_mappings ])
-def uvector_to_numpy(p):
- for m in numpy_mappings:
- if(m[4](p)):
- a= m[3](p);
- return numpy.array(m[3](p), dtype=m[0]);
- raise ValueError("unsupported numpy array dtype for converstion from pmt %s"%(p))
+def numpy_to_uvector(numpy_array):
+ try:
+ mapping = numpy_mappings[numpy_array.dtype]
+ pc = map(mapping[1], numpy.ravel(numpy_array))
+ return mapping[0](numpy_array.size, pc)
+ except KeyError:
+ raise ValueError("unsupported numpy array dtype for converstion to pmt %s"%(numpy_array.dtype))
-THE_TABLE = ( #python type, check pmt type, to python, from python
+def uvector_to_numpy(uvector):
+ match = None
+ for test_func in uvector_mappings.keys():
+ if test_func(uvector):
+ match = uvector_mappings[test_func]
+ return numpy.array(match[0](uvector), dtype = match[1])
+ else:
+ raise ValueError("unsupported uvector data type for conversion to numpy array %s"%(uvector))
+
+type_mappings = ( #python type, check pmt type, to python, from python
(None, pmt.is_null, lambda x: None, lambda x: pmt.PMT_NIL),
(bool, pmt.is_bool, pmt.to_bool, pmt.from_bool),
(str, pmt.is_symbol, pmt.symbol_to_string, pmt.string_to_symbol),
@@ -110,12 +114,12 @@ THE_TABLE = ( #python type, check pmt type, to python, from python
)
def pmt_to_python(p):
- for python_type, pmt_check, to_python, from_python in THE_TABLE:
+ for python_type, pmt_check, to_python, from_python in type_mappings:
if pmt_check(p): return to_python(p)
raise ValueError("can't convert %s type to pmt (%s)"%(type(p),p))
def python_to_pmt(p):
- for python_type, pmt_check, to_python, from_python in THE_TABLE:
+ for python_type, pmt_check, to_python, from_python in type_mappings:
if python_type is None:
if p == None: return from_python(p)
elif isinstance(p, python_type): return from_python(p)
diff --git a/gnuradio-runtime/python/pmt/qa_pmt_to_python.py b/gnuradio-runtime/python/pmt/qa_pmt_to_python.py
index ae86fc6d53..39cfc05dd6 100755
--- a/gnuradio-runtime/python/pmt/qa_pmt_to_python.py
+++ b/gnuradio-runtime/python/pmt/qa_pmt_to_python.py
@@ -22,13 +22,27 @@
import unittest
import pmt
+import pmt_to_python as pmt2py
class test_pmt_to_python(unittest.TestCase):
- def test01 (self):
+ def test_pmt_from_double(self):
b = pmt.from_double(123765)
self.assertEqual(pmt.to_python(b), 123765)
t = pmt.to_pmt(range(5))
+
+ def test_numpy_to_uvector_and_reverse(self):
+ import numpy as np
+ N = 100
+ narr = np.ndarray(N, dtype=np.complex128)
+ narr.real[:] = np.random.uniform(size=N)
+ narr.imag[:] = np.random.uniform(size=N)
+ uvector = pmt2py.numpy_to_uvector(narr)
+ nparr = pmt2py.uvector_to_numpy(uvector)
+ self.assertTrue(nparr.dtype==narr.dtype)
+ self.assertTrue(np.alltrue(nparr == narr))
+
+
if __name__ == '__main__':
unittest.main()