def test_expression_2vars(self): # test an expression with 2 variables expr = "2*sqrt(var1+var2)" storage = StorageFactory().get_storage('dict_storage') storage.write_table(table_name='dataset', table_data={ "var1": array([4, -8, 0.5, 1]), "var2": array([3, 3, 7, 7]), "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([5.29150262, 0.0, 5.47722558, 5.65685425]) self.assert_(ma.allclose(result, should_be, rtol=1e-6), "Error in test_expression_2vars") # check the dependencies (will depend on two different other variables) v = VariableName(expr) var = VariableFactory().get_variable(v, dataset) # use sets for the equality test, since we don't know in what order the dependencies will be returned self.assertEqual(set(var.dependencies()), set(['mydataset.var1', 'mydataset.var2']), msg="dependencies are incorrect")
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")
def test_sqrt_constant(self): # test an expression that is constant -- should have no dependencies expr = "sqrt(25)" storage = StorageFactory().get_storage("dict_storage") storage.write_table(table_name="dataset", table_data={"id": array([1, 2])}) # we don't actually use anything in the dataset dataset = Dataset(in_storage=storage, in_table_name="dataset", id_name="id", dataset_name="mydataset") result = dataset.compute_variables([expr]) self.assert_(4.99 < result and result < 5.01, "Error in test_sqrt_constant") # check the dependencies v = VariableName(expr) var = VariableFactory().get_variable(v, dataset) self.assertEqual(var.dependencies(), [], msg="dependencies are incorrect")
def test_expression_1var_2times(self): # test an expression with two occurences of the same variable # (the var should just occur once in dependencies) expr = "var1+sqrt(var1)" storage = StorageFactory().get_storage("dict_storage") storage.write_table(table_name="dataset", table_data={"var1": array([4, 25, 0, 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([6, 30, 0, 2]) self.assert_(ma.allclose(result, should_be, rtol=1e-6), "Error in test_expression_2vars") # check the dependencies v = VariableName(expr) var = VariableFactory().get_variable(v, dataset) self.assertEqual(var.dependencies(), ["mydataset.var1"], msg="dependencies are incorrect")
def test_fully_qualified_name_power(self): # test fully qualified name to a power expr = "opus_core.tests.a_test_variable**2" storage = StorageFactory().get_storage("dict_storage") storage.write_table(table_name="tests", table_data={"a_dependent_variable": array([1, 0]), "id": array([1, 3])}) dataset = Dataset(in_storage=storage, in_table_name="tests", id_name="id", dataset_name="tests") result = dataset.compute_variables([expr]) should_be = array([100, 0]) self.assertEqual( ma.allclose(result, should_be, rtol=1e-5), True, msg="error in test_fully_qualified_name_power" ) # check the dependencies v = VariableName(expr) var = VariableFactory().get_variable(v, dataset) self.assertEqual(var.dependencies(), ["opus_core.tests.a_test_variable"], msg="dependencies are incorrect")
def test_sqrt_constant(self): # test an expression that is constant -- should have no dependencies expr = "sqrt(25)" storage = StorageFactory().get_storage('dict_storage') storage.write_table( table_name='dataset', table_data={"id": array([1,2])} ) # we don't actually use anything in the dataset dataset = Dataset(in_storage=storage, in_table_name='dataset', id_name="id", dataset_name="mydataset") result = dataset.compute_variables([expr]) self.assert_(4.99<result and result<5.01, "Error in test_sqrt_constant") # check the dependencies v = VariableName(expr) var = VariableFactory().get_variable(v, dataset) self.assertEqual(var.dependencies(), [], msg="dependencies are incorrect")
def test_attr_power(self): # Attributes and fully-qualified names to a power require separate parse tree patterns, # which are tested in the following two tests. # test attribute to a power expr = "var1**3" storage = StorageFactory().get_storage("dict_storage") storage.write_table( table_name="dataset", table_data={"var1": 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([64, -512, 0.125, 1]) self.assert_(ma.allclose(result, should_be, rtol=1e-6), "Error in test_attr_power") # check the dependencies (trickier for ** because we need a separate attribute tree pattern) v = VariableName(expr) var = VariableFactory().get_variable(v, dataset) self.assertEqual(var.dependencies(), ["mydataset.var1"], msg="dependencies are incorrect")
def test_expression_1var_2times(self): # test an expression with two occurences of the same variable # (the var should just occur once in dependencies) expr = "var1+sqrt(var1)" storage = StorageFactory().get_storage('dict_storage') storage.write_table( table_name='dataset', table_data={"var1": array([4,25,0,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([ 6, 30, 0, 2]) self.assert_(ma.allclose(result, should_be, rtol=1e-6), "Error in test_expression_2vars") # check the dependencies v = VariableName(expr) var = VariableFactory().get_variable(v, dataset) self.assertEqual(var.dependencies(), ['mydataset.var1'], msg="dependencies are incorrect")
def test_expression_2vars(self): # test an expression with 2 variables expr = "2*sqrt(var1+var2)" storage = StorageFactory().get_storage('dict_storage') storage.write_table( table_name='dataset', table_data={"var1": array([4,-8,0.5,1]), "var2": array([3,3,7,7]), "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([ 5.29150262, 0.0, 5.47722558, 5.65685425]) self.assert_(ma.allclose(result, should_be, rtol=1e-6), "Error in test_expression_2vars") # check the dependencies (will depend on two different other variables) v = VariableName(expr) var = VariableFactory().get_variable(v, dataset) # use sets for the equality test, since we don't know in what order the dependencies will be returned self.assertEqual(set(var.dependencies()), set(['mydataset.var1', 'mydataset.var2']), msg="dependencies are incorrect")
def test_attr_power(self): # Attributes and fully-qualified names to a power require separate parse tree patterns, # which are tested in the following two tests. # test attribute to a power expr = "var1**3" storage = StorageFactory().get_storage('dict_storage') storage.write_table( table_name='dataset', table_data={"var1": 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([64, -512, 0.125, 1]) self.assert_(ma.allclose(result, should_be, rtol=1e-6), "Error in test_attr_power") # check the dependencies (trickier for ** because we need a separate attribute tree pattern) v = VariableName(expr) var = VariableFactory().get_variable(v, dataset) self.assertEqual(var.dependencies(), ['mydataset.var1'], msg="dependencies are incorrect")
def test_fully_qualified_name_power(self): # test fully qualified name to a power expr = "opus_core.tests.a_test_variable**2" storage = StorageFactory().get_storage('dict_storage') storage.write_table( table_name='tests', table_data={ "a_dependent_variable":array([1,0]), "id":array([1,3]) } ) dataset = Dataset(in_storage=storage, in_table_name='tests', id_name="id", dataset_name="tests") result = dataset.compute_variables([expr]) should_be = array([100,0]) self.assertEqual(ma.allclose(result, should_be, rtol=1e-5), True, msg="error in test_fully_qualified_name_power") # check the dependencies v = VariableName(expr) var = VariableFactory().get_variable(v, dataset) self.assertEqual(var.dependencies(), ['opus_core.tests.a_test_variable'], msg="dependencies are incorrect")
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")