Beispiel #1
0
 def test_numpy_no_caller_modules(self):
     ee = ExprEvaluator('numpy.sin(x)', enable_caller_modules=False)
     with self.assertRaises(ValueError):
         self.assertEqual(
             ee.substitute(None, {
                 'x': [numpy.pi / 2]
             }).eval(), 1)
Beispiel #2
0
    def test_ExprEvaluator_instantiation(self):
        self.assertEqual(ExprEvaluator(3).eval(), 3)
        self.assertEqual(ExprEvaluator(3.1).eval(), 3.1)
        self.assertEqual(ExprEvaluator('abc').variables, {'abc'})

        with self.assertRaises(TypeError):
            ExprEvaluator({})
Beispiel #3
0
    def test_substitute_vars_2(self):
        ee = ExprEvaluator('a+(b*c)')
        ee2 = ee.substitute({'a': 'x[0]', 'b': 'x[1]', 'c': 'x[2]'})

        self.assertEqual(ee2.variables, {'x'})
        self.assertEqual(ee2.substitute(None, {
            'x': [2, 3, 4],
        }).eval(), 14)
Beispiel #4
0
    def test_enableCall(self):
        ee = ExprEvaluator('a+(b*c)')
        with self.assertRaises(RuntimeError):
            self.assertEqual(ee(1, 2, 3), 7)

        self.assertEqual(ee(a=1, b=2, c=3), 7)

        ee.enable_call(['a', 'b', 'c'])
        self.assertEqual(ee(1, 2, 3), 7)
Beispiel #5
0
    def test_substitute_4(self):
        ee = ExprEvaluator('a+(b*c)+a*(d*e)')
        ee2 = ee.substitute(None, {'b': 2, 'c': 3})
        ee3 = ee2.substitute(None, {'d': 4, 'e': 5})
        ee3r = ee3.reduce()

        self.assertEqual(ee3r.variables, {'a'})
        with self.assertRaises(ValueError):
            ee3r.eval()
Beispiel #6
0
    def test_substitute_3(self):
        ee = ExprEvaluator('a+(b*c)')
        ee2 = ExprEvaluator('(e*f)')

        eesub1 = ee.substitute({'a': ee2})
        self.assertEqual(eesub1.variables, {'e', 'f', 'b', 'c'})

        eesub2 = ee.substitute({'a': ast.parse('e*f', mode='eval').body})
        self.assertEqual(eesub1.variables, {'e', 'f', 'b', 'c'})
Beispiel #7
0
    def test_basiceval(self):
        self.assertEqual(ExprEvaluator('a', {'a': 3}).eval(), 3)
        self.assertEqual(ExprEvaluator('a+b', {
            'a': 3,
            'b': 4,
        }).eval(), 7)

        # Not enough variables to evaluate
        self.assertRaises(ValueError, ExprEvaluator('a').eval)
        self.assertRaises(ValueError, ExprEvaluator('a+b', {'a': 3}).eval)
        self.assertRaises(ValueError, ExprEvaluator('a+b', {'b': 4}).eval)
Beispiel #8
0
    def test_reduce(self):
        ee = ExprEvaluator('a+(b*c)')
        self.assertEqual(ee.variables, set(['a', 'b', 'c']))

        ee1r = ee.substitute(None, {'a': 2}).reduce()
        ee2r = ee.substitute(None, {'b': 3}).reduce()
        ee3r = ee.substitute(None, {'c': 4}).reduce()

        ee12r = ee.substitute(None, {'a': 2, 'b': 3}).reduce()
        ee13r = ee.substitute(None, {'a': 2, 'c': 4}).reduce()
        ee23r = ee.substitute(None, {'b': 3, 'c': 4}).reduce()
        ee123r = ee.substitute(None, {'a': 2, 'b': 3, 'c': 4}).reduce()

        self.assertEqual(ee1r.variables, set(['b', 'c']))
        self.assertEqual(ee1r.constants, set(['a']))
        self.assertEqual(ee2r.variables, set(['a', 'c']))
        self.assertEqual(ee2r.constants, set(['b']))
        self.assertEqual(ee3r.variables, set(['a', 'b']))
        self.assertEqual(ee3r.constants, set(['c']))

        self.assertEqual(ee12r.variables, set(['c']))
        self.assertEqual(ee12r.constants, set(['a', 'b']))

        self.assertEqual(ee13r.variables, set(['b']))
        self.assertEqual(ee13r.constants, set(['a', 'c']))

        self.assertEqual(ee23r.variables, set(['a']))
        # Should contain only one constant (b+c -> reduced)
        self.assertEqual(len(ee23r.constants), 1)
        self.assertRegex(
            list(ee23r.constants)[0], '^\_\_ExprEvaluator\_[0-9]+$')

        self.assertEqual(ee123r.variables, set([]))
        self.assertEqual(len(ee123r.constants), 1)
        self.assertRegex(
            list(ee123r.constants)[0], '^\_\_ExprEvaluator\_[0-9]+$')

        self.assertEqual(ee123r.eval(), 14)
Beispiel #9
0
    def test_substitute_vars(self):
        ee = ExprEvaluator('a+(b*c)')
        self.assertEqual(ee.variables, set(['a', 'b', 'c']))

        ee0 = ee.substitute()
        self.assertEqual(ee0.variables, set(['a', 'b', 'c']))

        ee1 = ee.substitute(None, {
            'a': 2,
        })
        self.assertEqual(ee1.variables, set(['b', 'c']))
        self.assertEqual(ee1.constants, set(['a']))

        ee2 = ee.substitute(None, {
            'b': 3,
        })
        self.assertEqual(ee2.variables, set(['a', 'c']))
        self.assertEqual(ee2.constants, set(['b']))

        ee3 = ee.substitute(None, {
            'c': 4,
        })
        self.assertEqual(ee3.variables, set(['a', 'b']))
        self.assertEqual(ee3.constants, set(['c']))

        ee12 = ee1.substitute(None, {
            'b': 3,
        })
        self.assertEqual(ee12.variables, set(['c']))
        self.assertEqual(ee12.constants, set(['a', 'b']))

        ee23 = ee2.substitute(None, {
            'c': 4,
        })
        self.assertEqual(ee23.variables, set(['a']))
        self.assertEqual(ee23.constants, set(['b', 'c']))

        ee31 = ee3.substitute(None, {
            'a': 2,
        })
        self.assertEqual(ee31.variables, set(['b']))
        self.assertEqual(ee31.constants, set(['a', 'c']))

        ee123a = ee12.substitute(None, {
            'c': 4,
        })
        self.assertEqual(ee123a.variables, set([]))
        self.assertEqual(ee123a.constants, set(['a', 'b', 'c']))

        ee123b = ee23.substitute(None, {
            'a': 2,
        })
        self.assertEqual(ee123b.variables, set([]))
        self.assertEqual(ee123b.constants, set(['a', 'b', 'c']))

        ee123c = ee31.substitute(None, {
            'b': 3,
        })
        self.assertEqual(ee123c.variables, set([]))
        self.assertEqual(ee123c.constants, set(['a', 'b', 'c']))

        self.assertEqual(ee123a.eval(), 14)
        self.assertEqual(ee123b.eval(), 14)
        self.assertEqual(ee123c.eval(), 14)

        self.assertEqual(ee123a.substitute(None, {'a': 10}).eval(), 22)
        self.assertEqual(ee123b.substitute(None, {'b': 4}).eval(), 18)
        self.assertEqual(ee123c.substitute(None, {'c': 10}).eval(), 32)

        eeast = ee.substitute({ast.parse('b*c', mode='eval').body: 'd'})
        self.assertEqual(eeast.variables, set(['a', 'd']))
Beispiel #10
0
    def _test_str_parsability(self, str_expr):
        e1 = ExprEvaluator(str_expr)
        e2 = ExprEvaluator(str(e1))

        self.assertEqual(repr(e1), repr(e2))
        self.assertEqual(str(e1), str(e2))
Beispiel #11
0
 def test_ExprEvaluator_to_string_and_repr(self):
     from ast import BinOp, Name, Load, Add, Mult
     ee = ExprEvaluator('a+(b*c)')
     ee2 = eval(repr(ee))
     self.assertEqual(str(ee), str(ee2))
Beispiel #12
0
 def test_numpy(self):
     ee = ExprEvaluator('numpy.sin(x)')
     self.assertEqual(ee.substitute(None, {'x': [numpy.pi / 2]}).eval(), 1)
Beispiel #13
0
 def test_evaluable(self):
     ee = ExprEvaluator('2+4')
     self.assertEqual(ee.eval(), 6)
Beispiel #14
0
import sys

if __name__ == '__main__':
    from mmappickle import mmapdict
    from mmappickle.stubs import EmptyNDArray
    from multilstsq import ExprEvaluator
    import numpy
    import itertools

    d = mmapdict(sys.argv[1])
    d['model'] = 'b0 + b1*x0 + b2*(x0**2)'
    nval = 100000
    shape = (10000, )

    expr = ExprEvaluator(d['model'])

    explanatories = list(sorted(x for x in expr.variables
                                if x.startswith('x')))
    parameters = list(sorted(x for x in expr.variables if x.startswith('b')))

    d['explanatories'] = EmptyNDArray(shape + (nval, len(explanatories)))
    d['response'] = EmptyNDArray(shape + (nval, 1))
    d['parameters'] = numpy.random.normal(size=shape + (len(parameters), ))

    d.vacuum()

    m_explanatories = d['explanatories']
    m_response = d['response']
    m_parameters = d['parameters']

    for pos in itertools.product(*(range(x) for x in shape)):