|
From: | Marcus Müller |
Subject: | Re: [Discuss-gnuradio] trouble creating PMT uniform vectors in python that are the same type, but differ in value |
Date: | Sat, 19 Dec 2015 20:57:24 +0100 |
User-agent: | Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.3.0 |
Hi Richard, we need more mails like yours! Sharing recipes and problems is really heavily appreciated :) Let me comment on a few things; it's a while back that I worked with the PMT code, though. Because a PMT vector is pretty much like a python list: It can contain any combination of PMT types, for example: v = pmt.to_pmt(["This is a string", 42, complex(0,-1)]) is perfectly valid, but can't be a uniform vector. Because the input type->output type mapping should be consistent, I consider converting a python list to a PMT vector the right approach. I personally find "someone decided it was the right thing to do" a bit of a weak argument, though ;). So, here, to at least illustrate how it's done: pmt.to_pmt is actually a alias[0] for pmt_to_python.py:python_to_pmt(p) [1], which looks like this¹: def python_to_pmt(p): 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) raise ValueError("can't convert %s type to pmt (%s)"%(type(p),p)) The interesting part is "type_mappings", and that looks like this (just above the python_to_pmt function): type_mappings = ( #python type, check pmt type, to python, from python (None, pmt.is_null, lambda x: None, lambda x: PMT_NIL), ... (complex, pmt.is_complex, pmt.to_complex, pmt.from_complex), ... (list, pmt.is_vector, pmt_to_vector, pmt_from_vector), .... (numpy.ndarray, pmt.is_uniform_vector, uvector_to_numpy, numpy_to_uvector), )So, a Python object of type "list" is always mapped to a PMT vector. Also, you might guess what the trick to pmt.to_pmt'able uvectors is: create a numpy.ndarray, and convert it using pmt.to_pmt. numpy has handy conversion functions, as well as it allows you to allocate ndarrays of given type: #let numpy guess dtype from contents: arr = numpy.array([complex(-1,1), complex(1,-1)]) #or define a ndarray with given shape and type arr = numpy.ndarray(100, dtype=numpy.complex64) p = pmt.to_pmt(arr)If your uvectors migth be larger, I'd recommend pre-allocating the numpy.ndarray, i.e. the second approach. I'd agree that Tim's gr-eventstream needs quite a bit of understanding on what's happening behind the scenes, but really, it might not be as bad as you feel right now. Anyway, tagged stream blocks is probably the solution of choice here; however, I think the elegant solution would be to add tags to your "normal" stream with "length tags" (ie. add a tag to the first item of each "burst" of samples containing the number of samples to come in this burst), and connect your block to the "tagged stream align" block, which sees that its output is so aligned that it's Tagged Stream Block-compatible; I find it non-trivial to explain that concept, but tagged stream blocks are really just "normal" blocks, but for which it's defined that they a) always consume the whole item "chunk" they get, and b) the length of an item chunk is always defined by a value of a tag on the first item (and, c), there's no samples that don't belong to such a chunk); maybe this figure explains its better: Best regards, Marcus ¹ Honestly, I just realized that is a relatively inefficient way of implementing this, but I definitely have code in there, and I surely had a reason to do it that way... hm. [0] "from pmt_to_python import python_to_pmt as to_pmt" in https://github.com/gnuradio/gnuradio/blob/master/gnuradio-runtime/python/pmt/__init__.py#L59 [1] https://github.com/gnuradio/gnuradio/blob/master/gnuradio-runtime/python/pmt/pmt_to_python.py#L130 On 19.12.2015 04:34, Collins, Richard
wrote:
|
[Prev in Thread] | Current Thread | [Next in Thread] |