def testFullProvenance(self): u_uri = compute_graph.RawValueNode("file://" + self.getDataFilePath("u_2000.nc")) v_uri = compute_graph.RawValueNode("file://" + self.getDataFilePath("v_2000.nc")) u_var = prov.DatasetFunction(u_uri, "variable", "u") u_sub = prov.GeospatialFunction("subset", u_var, latitude=(-90, 0)) v_var = prov.DatasetFunction(v_uri, "variable", "v") v_sub = prov.GeospatialFunction("subset", v_var, latitude=(-90, 0)) u_square = compute_graph.ArithmeticOperation("**", u_sub, 2) v_square = prov.NDArrayBinaryFunction("power", v_sub, 2) wind_magnitude_square = prov.NDArrayBinaryFunction( "add", u_square, v_square) wind_magnitude = compute_graph.ArithmeticOperation( "**", wind_magnitude_square, .5) wind_mag = wind_magnitude.derive() ufile = self.getDataFile("u_2000.nc") vfile = self.getDataFile("v_2000.nc") u = ufile("u", latitude=(-90, 0)) v = vfile("v", latitude=(-90, 0)) w = (u**2 + v**2)**.5 self.assertArraysEqual(w, wind_mag)
def test_derive(self): # Simple fibonacci derivation last_v = compute_graph.RawValueNode(1) v = compute_graph.RawValueNode(1) for _ in range(200): last_v, v = v, compute_graph.ArithmeticOperation("+", v, last_v) self.assertEqual(v.derive(), 734544867157818093234908902110449296423351)
def test_value_counts(self): # Test 0 values with self.assertRaises(ValueError): operation_node = compute_graph.ArithmeticOperation("-") # Test 1 value for binary operators with self.assertRaises(ValueError): operation_node = compute_graph.ArithmeticOperation("|", 1) # Test 2 values for unary operator with self.assertRaises(ValueError): operation_node = compute_graph.ArithmeticOperation("not", 1, 2) # Test more than 2 values for binary operator with self.assertRaises(ValueError): operation_node = compute_graph.ArithmeticOperation(">=", 1, 2, 3) # Test more than 2 values for unary operator with self.assertRaises(ValueError): operation_node = compute_graph.ArithmeticOperation("not", 1, 2, 3) # Test 2 values for binary operation_node = compute_graph.ArithmeticOperation(">=", 2, 3) # Test 1 value for unary operation_node = compute_graph.ArithmeticOperation("not", False)
def test_int_computations(self): left_value, right_value = 1, 2 unary_results = [-1, -2, False] binary_results = [3, -1, 3, 0, 0, 4, 1, 2, 1, 3, 0, False, True, False, True, True, False] self.assertEqual(len(unary_results), len(compute_graph.arithmetic.unary_operators)) for op, result in zip(compute_graph.arithmetic.unary_operators, unary_results): node = compute_graph.ArithmeticOperation(op, left_value) self.assertEqual(node.derive(), result) self.assertEqual(len(binary_results), len(compute_graph.arithmetic.binary_operators)) for op, result in zip(compute_graph.arithmetic.binary_operators, binary_results): node = compute_graph.ArithmeticOperation(op, left_value, right_value) self.assertEqual(node.derive(), result) for op, result in zip(compute_graph.arithmetic.inplace_binary_operators, binary_results): node = compute_graph.ArithmeticOperation(op, left_value, right_value) self.assertEqual(node.derive(), result)
def test_dumpjson(self): div = compute_graph.ArithmeticOperation("/", 2.6, 1.3) mul = compute_graph.ArithmeticOperation("*", div, 2) sqrt = compute_graph.ArithmeticOperation("**", mul, .5) eq = compute_graph.ArithmeticOperation("==", sqrt, 2) self.assertTrue(eq.derive()) json_blob = compute_graph.dumpjson(eq) v = json.loads(json_blob) operators = ["/", "*", "**", "=="] for index, operator, cur_step in zip(list(range(len(operators))), operators, v["derivation"]): self.assertEqual("arithmetic", cur_step["node_type"]) self.assertEqual(operator, cur_step["attribute_values"]["operator"]) if index > 0: self.assertEqual(index - 1, cur_step["attribute_values"]["left_value"]) self.assertEqual("left_value", cur_step["dependent_attributes"][0])
def test_node_validation(self): # Invalid operator with self.assertRaises(ValueError): n = compute_graph.ArithmeticOperation("blahblahblah") # No values with self.assertRaises(ValueError): n = compute_graph.ArithmeticOperation("+") # Non unary operator with 1 value with self.assertRaises(ValueError): n = compute_graph.ArithmeticOperation("*", 1) # Non-binary operator with 2 values with self.assertRaises(ValueError): n = compute_graph.ArithmeticOperation("not", 2, 3) # Too many values with self.assertRaises(ValueError): n = compute_graph.ArithmeticOperation("*", 2, 3, 4)