commit-gnuradio
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Commit-gnuradio] [gnuradio] 04/15: pmt_to_python: numpy_to_uvector and


From: git
Subject: [Commit-gnuradio] [gnuradio] 04/15: pmt_to_python: numpy_to_uvector and reverse works, QA added
Date: Thu, 10 Apr 2014 19:38:17 +0000 (UTC)

This is an automated email from the git hooks/post-receive script.

trondeau pushed a commit to branch master
in repository gnuradio.

commit c201f977d25c5df0834ce2486b707e3df22a4f95
Author: Marcus Müller <address@hidden>
Date:   Mon Apr 7 22:00:39 2014 +0200

    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(-)

diff --git a/gnuradio-runtime/python/pmt/pmt_to_python.py 
b/gnuradio-runtime/python/pmt/pmt_to_python.py
index 3da826a..83b209d 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 ae86fc6..39cfc05 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()



reply via email to

[Prev in Thread] Current Thread [Next in Thread]