Ejemplo n.º 1
0
    def test_subscript_reference(self):
        from pysd.vensim2py import parse_general_expression
        res = parse_general_expression({'expr': 'Var A[Dim1, Dim2]'},
                                       {'Var A': 'var_a'}, {
                                           'Dim1': ['A', 'B'],
                                           'Dim2': ['C', 'D', 'E']
                                       })

        self.assertEqual(res[0]['py_expr'], 'var_a()')

        res = parse_general_expression({'expr': 'Var B[Dim1, C]'},
                                       {'Var B': 'var_b'}, {
                                           'Dim1': ['A', 'B'],
                                           'Dim2': ['C', 'D', 'E']
                                       })
        self.assertEqual(res[0]['py_expr'], "var_b().loc[{'Dim2': ['C']}]")

        res = parse_general_expression({'expr': 'Var C[Dim1, C, H]'},
                                       {'Var C': 'var_c'}, {
                                           'Dim1': ['A', 'B'],
                                           'Dim2': ['C', 'D', 'E'],
                                           'Dim3': ['F', 'G', 'H', 'I']
                                       })
        self.assertEqual(res[0]['py_expr'],
                         "var_c().loc[{'Dim2': ['C'], 'Dim3': ['H']}]")
Ejemplo n.º 2
0
    def test_caps_handling(self):
        from pysd.vensim2py import parse_general_expression
        res = parse_general_expression({'expr': 'Abs(-3)'})
        self.assertEqual(res[0]['py_expr'], 'abs(-3)')

        res = parse_general_expression({'expr': 'ABS(-3)'})
        self.assertEqual(res[0]['py_expr'], 'abs(-3)')

        res = parse_general_expression({'expr': 'aBS(-3)'})
        self.assertEqual(res[0]['py_expr'], 'abs(-3)')
Ejemplo n.º 3
0
    def test_caps_handling(self):
        from pysd.vensim2py import parse_general_expression
        res = parse_general_expression({'expr': 'Abs(-3)'})
        self.assertEqual(res[0]['py_expr'], 'abs(-3)')

        res = parse_general_expression({'expr': 'ABS(-3)'})
        self.assertEqual(res[0]['py_expr'], 'abs(-3)')

        res = parse_general_expression({'expr': 'aBS(-3)'})
        self.assertEqual(res[0]['py_expr'], 'abs(-3)')
Ejemplo n.º 4
0
    def test_function_calls(self):
        from pysd.vensim2py import parse_general_expression
        res = parse_general_expression({'expr': 'ABS(StockA)'}, {'StockA': 'stocka'})
        self.assertEqual(res[0]['py_expr'], 'abs(stocka())')

        res = parse_general_expression({'expr': 'If Then Else(A>B, 1, 0)'}, {'A': 'a', 'B':'b'})
        self.assertEqual(res[0]['py_expr'], 'functions.if_then_else(a()>b(),1,0)')

        # test that function calls are handled properly in arguments
        res = parse_general_expression({'expr': 'If Then Else(A>B,1,A)'}, {'A': 'a', 'B': 'b'})
        self.assertEqual(res[0]['py_expr'], 'functions.if_then_else(a()>b(),1,a())')
Ejemplo n.º 5
0
    def test_number_parsing(self):
        from pysd.vensim2py import parse_general_expression
        res = parse_general_expression({'expr': '20'})
        self.assertEqual(res[0]['py_expr'], '20')

        res = parse_general_expression({'expr': '3.14159'})
        self.assertEqual(res[0]['py_expr'], '3.14159')

        res = parse_general_expression({'expr': '+3.14159'})
        self.assertEqual(res[0]['py_expr'], '3.14159')

        res = parse_general_expression({'expr': '1.3e+10'})
        self.assertEqual(res[0]['py_expr'], '1.3e+10')

        res = parse_general_expression({'expr': '-1.3e-10'})
        self.assertEqual(res[0]['py_expr'], '-1.3e-10')
Ejemplo n.º 6
0
    def test_number_parsing(self):
        from pysd.vensim2py import parse_general_expression
        res = parse_general_expression({'expr': '20'})
        self.assertEqual(res[0]['py_expr'], '20')

        res = parse_general_expression({'expr': '3.14159'})
        self.assertEqual(res[0]['py_expr'], '3.14159')

        res = parse_general_expression({'expr': '+3.14159'})
        self.assertEqual(res[0]['py_expr'], '3.14159')

        res = parse_general_expression({'expr': '1.3e+10'})
        self.assertEqual(res[0]['py_expr'], '1.3e+10')

        res = parse_general_expression({'expr': '-1.3e-10'})
        self.assertEqual(res[0]['py_expr'], '-1.3e-10')
Ejemplo n.º 7
0
    def test_smooth_construction_function_no_subscripts(self):
        """ Tests translation of 'smooth'

        This translation should create a new stateful object to hold the delay elements,
        and then pass back a reference to that value
        """
        from pysd.vensim2py import parse_general_expression
        from pysd.functions import Smooth
        from pysd import functions
        res = parse_general_expression(
            {
                'expr': 'SMOOTH(Variable, DelayTime)',
                'subs': []
            },
            {
                'Variable': 'variable',
                'DelayTime': 'delaytime'
            },
        )

        # check stateful object creation
        self.assertEqual(res[1][0]['kind'], 'stateful')
        a = eval(res[1][0]['py_expr'])
        self.assertIsInstance(a, Smooth)

        # check the reference to that variable
        self.assertEqual(res[0]['py_expr'], res[1][0]['py_name'] + '()')
Ejemplo n.º 8
0
 def test_subscript_ranges(self):
     from pysd.vensim2py import parse_general_expression
     res = parse_general_expression({'expr': 'Var D[Range1]'},
                                  {'Var D': 'var_c'},
                                  {'Dim1': ['A', 'B', 'C', 'D', 'E', 'F'],
                                   'Range1': ['C', 'D', 'E']})
     self.assertEqual(res[0]['py_expr'], "var_c().loc[{'Dim1': ['C', 'D', 'E']}]")
Ejemplo n.º 9
0
 def test_subscript_ranges(self):
     from pysd.vensim2py import parse_general_expression
     res = parse_general_expression(
         {'expr': 'Var D[Range1]'}, {'Var D': 'var_c'}, {
             'Dim1': ['A', 'B', 'C', 'D', 'E', 'F'],
             'Range1': ['C', 'D', 'E']
         })
     self.assertEqual(res[0]['py_expr'],
                      "var_c().loc[{'Dim1': ['C', 'D', 'E']}]")
Ejemplo n.º 10
0
 def test_incomplete_expression(self):
     from pysd.vensim2py import parse_general_expression
     res = parse_general_expression(
         {'expr': 'A FUNCTION OF(Unspecified Eqn,Var A,Var B)'}, {
             'Unspecified Eqn': 'unspecified_eqn',
             'Var A': 'var_a',
             'Var B': 'var_b'
         })
     self.assertEqual(
         res[0]['py_expr'],
         "functions.incomplete(unspecified_eqn(), var_a(), var_b())")
Ejemplo n.º 11
0
    def test_function_calls(self):
        from pysd.vensim2py import parse_general_expression
        res = parse_general_expression({'expr': 'ABS(StockA)'},
                                       {'StockA': 'stocka'})
        self.assertEqual(res[0]['py_expr'], 'abs(stocka())')

        res = parse_general_expression({'expr': 'If Then Else(A>B, 1, 0)'}, {
            'A': 'a',
            'B': 'b'
        })
        self.assertEqual(res[0]['py_expr'],
                         'functions.if_then_else(a()>b(),1,0)')

        # test that function calls are handled properly in arguments
        res = parse_general_expression({'expr': 'If Then Else(A>B,1,A)'}, {
            'A': 'a',
            'B': 'b'
        })
        self.assertEqual(res[0]['py_expr'],
                         'functions.if_then_else(a()>b(),1,a())')
Ejemplo n.º 12
0
 def test_subscript_1d_constant(self):
     from pysd.vensim2py import parse_general_expression
     element = parse_general_expression({'expr': '1, 2, 3',
                                         'subs': ['Dim1']},
                                        {},
                                        {'Dim1': ['A', 'B', 'C'],
                                         'Dim2': ['D', 'E']})
     string = element[0]['py_expr']
     a = eval(string)
     self.assertDictEqual({key: list(val.values) for key, val in a.coords.items()},
                          {'Dim1': ['A', 'B', 'C']})
     self.assertEqual(a.loc[{'Dim1': 'A'}], 1)
Ejemplo n.º 13
0
    def test_subscript_reference(self):
        from pysd.vensim2py import parse_general_expression
        res = parse_general_expression({'expr': 'Var A[Dim1, Dim2]'},
                                     {'Var A': 'var_a'},
                                     {'Dim1': ['A', 'B'],
                                      'Dim2': ['C', 'D', 'E']})

        self.assertEqual(res[0]['py_expr'], 'var_a()')

        res = parse_general_expression({'expr': 'Var B[Dim1, C]'},
                                     {'Var B': 'var_b'},
                                     {'Dim1': ['A', 'B'],
                                      'Dim2': ['C', 'D', 'E']})
        self.assertEqual(res[0]['py_expr'], "var_b().loc[{'Dim2': ['C']}]")

        res = parse_general_expression({'expr': 'Var C[Dim1, C, H]'},
                                     {'Var C': 'var_c'},
                                     {'Dim1': ['A', 'B'],
                                      'Dim2': ['C', 'D', 'E'],
                                      'Dim3': ['F', 'G', 'H', 'I']})
        self.assertEqual(res[0]['py_expr'], "var_c().loc[{'Dim2': ['C'], 'Dim3': ['H']}]")
Ejemplo n.º 14
0
 def test_subscript_float_initialization(self):
     from pysd.vensim2py import parse_general_expression
     element = parse_general_expression({
         'expr': '3.32',
         'subs': ['Dim1']
     }, {}, {
         'Dim1': ['A', 'B', 'C'],
         'Dim2': ['D', 'E']
     })
     string = element[0]['py_expr']
     a = eval(string)
     self.assertDictEqual(
         {key: list(val.values)
          for key, val in a.coords.items()}, {'Dim1': ['A', 'B', 'C']})
     self.assertEqual(a.loc[{'Dim1': 'B'}], 3.32)
Ejemplo n.º 15
0
    def test_stock_construction_function_no_subscripts(self):
        """ stock construction should create a stateful variable and reference it """
        from pysd.vensim2py import parse_general_expression
        from pysd.functions import Integ
        from pysd import functions
        res = parse_general_expression({'expr': 'INTEG (FlowA, -10)',
                                      'py_name': 'test_stock',
                                      'subs': []},
                                     {'FlowA': 'flowa'})

        self.assertEqual(res[1][0]['kind'], 'stateful')
        a = eval(res[1][0]['py_expr'])
        self.assertIsInstance(a, Integ)

        # check the reference to that variable
        self.assertEqual(res[0]['py_expr'], res[1][0]['py_name'] + '()')
Ejemplo n.º 16
0
    def test_delay_construction_function_no_subscripts(self):
        from pysd.vensim2py import parse_general_expression
        from pysd.functions import Delay
        from pysd import functions
        res = parse_general_expression({'expr': 'DELAY1(Variable, DelayTime)',
                                        'subs': []},
                                       {'Variable': 'variable',
                                        'DelayTime': 'delaytime'},
                                       )

        self.assertEqual(res[1][0]['kind'], 'stateful')
        a = eval(res[1][0]['py_expr'])
        self.assertIsInstance(a, Delay)

        # check the reference to that variable
        self.assertEqual(res[0]['py_expr'], res[1][0]['py_name'] + '()')
Ejemplo n.º 17
0
    def test_stock_construction_function_no_subscripts(self):
        """ stock construction should create a stateful variable and reference it """
        from pysd.vensim2py import parse_general_expression
        from pysd.functions import Integ
        from pysd import functions
        res = parse_general_expression(
            {
                'expr': 'INTEG (FlowA, -10)',
                'py_name': 'test_stock',
                'subs': []
            }, {'FlowA': 'flowa'})

        self.assertEqual(res[1][0]['kind'], 'stateful')
        a = eval(res[1][0]['py_expr'])
        self.assertIsInstance(a, Integ)

        # check the reference to that variable
        self.assertEqual(res[0]['py_expr'], res[1][0]['py_name'] + '()')
Ejemplo n.º 18
0
 def test_subscript_3d_depth(self):
     from pysd.vensim2py import parse_general_expression
     element = parse_general_expression(
         {
             'expr': '1, 2; 3, 4; 5, 6;',
             'subs': ['Dim1', 'Dim2']
         }, {}, {
             'Dim1': ['A', 'B', 'C'],
             'Dim2': ['D', 'E']
         })
     string = element[0]['py_expr']
     a = eval(string)
     self.assertDictEqual(
         {key: list(val.values)
          for key, val in a.coords.items()}, {
              'Dim1': ['A', 'B', 'C'],
              'Dim2': ['D', 'E']
          })
     self.assertEqual(a.loc[{'Dim1': 'A', 'Dim2': 'D'}], 1)
     self.assertEqual(a.loc[{'Dim1': 'B', 'Dim2': 'E'}], 4)
Ejemplo n.º 19
0
    def test_delay_construction_function_no_subscripts(self):
        from pysd.vensim2py import parse_general_expression
        from pysd.functions import Delay
        from pysd import functions
        res = parse_general_expression(
            {
                'expr': 'DELAY1(Variable, DelayTime)',
                'subs': []
            },
            {
                'Variable': 'variable',
                'DelayTime': 'delaytime'
            },
        )

        self.assertEqual(res[1][0]['kind'], 'stateful')
        a = eval(res[1][0]['py_expr'])
        self.assertIsInstance(a, Delay)

        # check the reference to that variable
        self.assertEqual(res[0]['py_expr'], res[1][0]['py_name'] + '()')
Ejemplo n.º 20
0
    def test_smooth_construction_function_no_subscripts(self):
        """ Tests translation of 'smooth'

        This translation should create a new stateful object to hold the delay elements,
        and then pass back a reference to that value
        """
        from pysd.vensim2py import parse_general_expression
        from pysd.functions import Smooth
        from pysd import functions
        res = parse_general_expression({'expr': 'SMOOTH(Variable, DelayTime)',
                                        'subs': []},
                                       {'Variable': 'variable',
                                        'DelayTime': 'delaytime'},
                                       )

        # check stateful object creation
        self.assertEqual(res[1][0]['kind'], 'stateful')
        a = eval(res[1][0]['py_expr'])
        self.assertIsInstance(a, Smooth)

        # check the reference to that variable
        self.assertEqual(res[0]['py_expr'], res[1][0]['py_name']+'()')
Ejemplo n.º 21
0
 def test_id_parsing(self):
     from pysd.vensim2py import parse_general_expression
     res = parse_general_expression({'expr': 'StockA'}, {'StockA': 'stocka'})
     self.assertEqual(res[0]['py_expr'], 'stocka()')
Ejemplo n.º 22
0
 def test_id_parsing(self):
     from pysd.vensim2py import parse_general_expression
     res = parse_general_expression({'expr': 'StockA'},
                                    {'StockA': 'stocka'})
     self.assertEqual(res[0]['py_expr'], 'stocka()')
Ejemplo n.º 23
0
 def test_arithmetic(self):
     from pysd.vensim2py import parse_general_expression
     res = parse_general_expression({'expr': '-10^3+4'})
     self.assertEqual(res[0]['py_expr'], '-10**3+4')
Ejemplo n.º 24
0
 def test_arithmetic(self):
     from pysd.vensim2py import parse_general_expression
     res = parse_general_expression({'expr': '-10^3+4'})
     self.assertEqual(res[0]['py_expr'], '-10**3+4')