def test_expression(self):
     # test an expression.  Also make sure that the generated variable can be accessued
     # using its short name and that dependencies are correct.
     expr = "2*sqrt(my_variable+10)"
     storage = StorageFactory().get_storage('dict_storage')
     storage.write_table(table_name='dataset',
                         table_data={
                             "my_variable": array([4, -8, 0.5, 1]),
                             "id": array([1, 2, 3, 4])
                         })
     dataset = Dataset(in_storage=storage,
                       in_table_name='dataset',
                       id_name="id",
                       dataset_name="mydataset")
     result = dataset.compute_variables([expr])
     should_be = array([7.48331477, 2.82842712, 6.4807407, 6.63324958])
     self.assert_(ma.allclose(result, should_be, rtol=1e-6),
                  "Error in test_expression")
     # check the name
     v = VariableName(expr)
     var = VariableFactory().get_variable(v, dataset)
     self.assertEqual(var.name(), expr, msg="name is incorrect")
     # check the dependencies
     self.assertEqual(var.dependencies(), ['mydataset.my_variable'],
                      msg="dependencies are incorrect")
     # test that the variable can now also be accessed using its short name in an expression
     result2 = dataset.compute_variables([v.get_short_name()])
     self.assert_(ma.allclose(result2, should_be, rtol=1e-6),
                  "Error in accessing a_test_variable")
Example #2
0
 def test_expression(self):
     # test an expression.  Also make sure that the generated variable can be accessued
     # using its short name and that dependencies are correct.
     expr = "2*sqrt(my_variable+10)"
     storage = StorageFactory().get_storage("dict_storage")
     storage.write_table(
         table_name="dataset", table_data={"my_variable": array([4, -8, 0.5, 1]), "id": array([1, 2, 3, 4])}
     )
     dataset = Dataset(in_storage=storage, in_table_name="dataset", id_name="id", dataset_name="mydataset")
     result = dataset.compute_variables([expr])
     should_be = array([7.48331477, 2.82842712, 6.4807407, 6.63324958])
     self.assert_(ma.allclose(result, should_be, rtol=1e-6), "Error in test_expression")
     # check the name
     v = VariableName(expr)
     var = VariableFactory().get_variable(v, dataset)
     self.assertEqual(var.name(), expr, msg="name is incorrect")
     # check the dependencies
     self.assertEqual(var.dependencies(), ["mydataset.my_variable"], msg="dependencies are incorrect")
     # test that the variable can now also be accessed using its short name in an expression
     result2 = dataset.compute_variables([v.get_short_name()])
     self.assert_(ma.allclose(result2, should_be, rtol=1e-6), "Error in accessing a_test_variable")