-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnumexpr_evaluate.py
80 lines (71 loc) · 2.63 KB
/
numexpr_evaluate.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright 2015 <+YOU OR YOUR COMPANY+>.
#
# This is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3, or (at your option)
# any later version.
#
# This software is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this software; see the file COPYING. If not, write to
# the Free Software Foundation, Inc., 51 Franklin Street,
# Boston, MA 02110-1301, USA.
import numpy
try:
import numexpr
except ImportError:
numexpr = None
from gnuradio import gr
class numexpr_evaluate(gr.sync_block):
"""
Let's you run any NumExpr as a GR block. NumExpr are optimal; generic
callables can also be used
"""
def __init__(
self, expression='in0',
in_sig=(numpy.complex64,), out_sig=(numpy.complex64,),
nthreads=None
):
"""
Args:
expression: either a NumExpr string (in0, in1, ... are the inputs) or
a callable (in0, in1 as args) to be used in work()
in_sig: a list of numpy dtype as input signature
out_sig: a list of numpy dtype as output signature
nthreads: how many threads NumExpr should use
"""
gr.sync_block.__init__(self, "numexpr_evaluate", in_sig, out_sig)
self._expression = None
if numexpr and nthreads:
numexpr.set_num_threads(nthreads)
self.expression = expression
@property
def expression(self):
return self._expression
@expression.setter
def expression(self, value):
self._expression = value
if numexpr:
self.work = self.__class__.work
elif callable(value):
self.work = work_callable
else:
raise ValueError("Can't import 'numexpr'. You can only pass a"
"callable as 'expression' in this mode.")
def work_numexpr(self, input_items, output_items):
local_dict = dict(
('in'+str(i), items) for i, items in enumerate(input_items)
)
output_items[0][:] = numexpr.evaluate(self.expression, local_dict)
return len(output_items[0])
def work_callable(self, input_items, output_items):
output_items[0][:] = self.expression(*input_items)
return len(output_items[0])
work = work_numexpr