From 29a6b80f2f3b79fc27829d962475e2bbaf864fdf Mon Sep 17 00:00:00 2001 From: Marcus Müller <marcus@hostalia.de> Date: Sat, 5 Apr 2014 15:00:16 +0200 Subject: set numpy_mappings -> dict, to fix py2.6 incompatibility --- gnuradio-runtime/python/pmt/pmt_to_python.py | 45 ++++++++++++++-------------- 1 file changed, 23 insertions(+), 22 deletions(-) (limited to 'gnuradio-runtime/python/pmt/pmt_to_python.py') diff --git a/gnuradio-runtime/python/pmt/pmt_to_python.py b/gnuradio-runtime/python/pmt/pmt_to_python.py index 3c4dcf4425..3da826a294 100644 --- a/gnuradio-runtime/python/pmt/pmt_to_python.py +++ b/gnuradio-runtime/python/pmt/pmt_to_python.py @@ -63,35 +63,36 @@ def pmt_from_dict(p): d = pmt.dict_add(d, python_to_pmt(k), python_to_pmt(v)) return d + #(numpy.float32,pmt.init_f32vector, float, pmt.f32vector_elements, pmt.is_f32vector), 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.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), } def numpy_to_uvector(p): - if not p.dtype in map(lambda x: x[0], numpy_mappings): + try: + mapping = numpy_mappings + pc = map(mapping[1], p) + return mapping[0](p.size, pc) + except KeyError: 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); 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]); + for key in numpy_mappings: + m = numpy_mappings[key] + if(m[3](p)): + return numpy.array(m[2](p), dtype=key); raise ValueError("unsupported numpy array dtype for converstion from pmt %s"%(p)) THE_TABLE = ( #python type, check pmt type, to python, from python -- cgit v1.2.3 From c201f977d25c5df0834ce2486b707e3df22a4f95 Mon Sep 17 00:00:00 2001 From: Marcus Müller <marcus@hostalia.de> Date: Mon, 7 Apr 2014 22:00:39 +0200 Subject: pmt_to_python: numpy_to_uvector and reverse works, QA added --- gnuradio-runtime/python/pmt/pmt_to_python.py | 54 +++++++++++++------------ gnuradio-runtime/python/pmt/qa_pmt_to_python.py | 16 +++++++- 2 files changed, 44 insertions(+), 26 deletions(-) (limited to 'gnuradio-runtime/python/pmt/pmt_to_python.py') diff --git a/gnuradio-runtime/python/pmt/pmt_to_python.py b/gnuradio-runtime/python/pmt/pmt_to_python.py index 3da826a294..83b209de1a 100644 --- a/gnuradio-runtime/python/pmt/pmt_to_python.py +++ b/gnuradio-runtime/python/pmt/pmt_to_python.py @@ -65,35 +65,39 @@ def pmt_from_dict(p): #(numpy.float32,pmt.init_f32vector, float, pmt.f32vector_elements, pmt.is_f32vector), 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): +uvector_mappings = dict([ (numpy_mappings[key][3], (numpy_mappings[key][2], key)) for key in numpy_mappings ]) + +def numpy_to_uvector(numpy_array): try: - mapping = numpy_mappings - pc = map(mapping[1], p) - return mapping[0](p.size, pc) + 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"%(p.dtype)) + raise ValueError("unsupported numpy array dtype for converstion to pmt %s"%(numpy_array.dtype)) -def uvector_to_numpy(p): - for key in numpy_mappings: - m = numpy_mappings[key] - if(m[3](p)): - return numpy.array(m[2](p), dtype=key); - raise ValueError("unsupported numpy array dtype for converstion from pmt %s"%(p)) +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)) THE_TABLE = ( #python type, check pmt type, to python, from python (None, pmt.is_null, lambda x: None, lambda x: pmt.PMT_NIL), 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() -- cgit v1.2.3 From ac33347c3e685b25be611d4405521e36310852a8 Mon Sep 17 00:00:00 2001 From: Marcus Müller <marcus@hostalia.de> Date: Mon, 7 Apr 2014 22:06:05 +0200 Subject: renamed and cleaned up a bit --- gnuradio-runtime/python/pmt/pmt_to_python.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) (limited to 'gnuradio-runtime/python/pmt/pmt_to_python.py') diff --git a/gnuradio-runtime/python/pmt/pmt_to_python.py b/gnuradio-runtime/python/pmt/pmt_to_python.py index 83b209de1a..3344eba163 100644 --- a/gnuradio-runtime/python/pmt/pmt_to_python.py +++ b/gnuradio-runtime/python/pmt/pmt_to_python.py @@ -63,7 +63,6 @@ def pmt_from_dict(p): d = pmt.dict_add(d, python_to_pmt(k), python_to_pmt(v)) return d - #(numpy.float32,pmt.init_f32vector, float, pmt.f32vector_elements, pmt.is_f32vector), numpy_mappings = { 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), @@ -99,7 +98,7 @@ def uvector_to_numpy(uvector): else: raise ValueError("unsupported uvector data type for conversion to numpy array %s"%(uvector)) -THE_TABLE = ( #python type, check pmt type, to python, from python +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), @@ -115,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) -- cgit v1.2.3