def test_interp_to_pts(self): """Interpolate to multiple points""" pts = ((0, 0, 0), (10, 0, 1), (10, 10, 2), (0, 10, 3)) interp = InterpIdw(pts) ret = interp.interpolate_to_points( np.array([(2.0, 1.0, 0.0), (5.0, 10.0, 2.5)])) np.testing.assert_array_almost_equal((0.02681550197303295, 2.5), ret)
def test_properties_PolyInput(self): out_poly = ((0, 0, 0), (1, 0, 0), (1, 1, 0), (0, 1, 0)) pi = PolyInput(out_poly) outside_poly = ((1, 2, 0), (5, 2, 0), (5, 9, 0), (1, 9, 0)) inside_polys = (((3, 3, 0), (2.5, 4, 0), (2, 3, 0)), ((4, 8, 0), (3, 7, 0), (2, 8, 0))) poly_corners = (0, 1, 2, 3) pts = ((1, 2, 3), (4, 5, 6), (0, 5, 7)) size_func = InterpLinear(pts) elev_func = InterpIdw(pts) seed_points = ((3, 3, 0), (4, 3, 0), (4, 8, 0), (3, 8, 0)) relaxation_method = "spring_relaxation" self.assertEqual(4, len(pi.outside_polygon)) pi.outside_polygon = outside_poly self.assertArraysEqual(outside_poly, pi.outside_polygon) self.assertEqual(0, len(pi.inside_polygons)) pi.inside_polygons = inside_polys self.assertInsidePolysEqual(inside_polys, pi.inside_polygons) self.assertEqual(0, len(pi.patch_polygon_corners)) pi.patch_polygon_corners = poly_corners self.assertArraysEqual(poly_corners, pi.patch_polygon_corners) self.assertEqual(1.0, pi.bias) pi.bias = 0.3 self.assertEqual(0.3, pi.bias) self.assertEqual(None, pi.size_function) pi.size_function = size_func self.assertEqual(size_func.to_string(), pi.size_function.to_string()) self.assertEqual(None, pi.elev_function) pi.elev_function = elev_func self.assertEqual(elev_func.to_string(), pi.elev_function.to_string()) self.assertEqual(-1, pi.const_size_bias) pi.const_size_bias = 4.0 self.assertEqual(4.0, pi.const_size_bias) self.assertEqual(-1, pi.const_size_function) pi.const_size_function = 1.2 self.assertEqual(1.2, pi.const_size_function) self.assertEqual(False, pi.remove_internal_four_triangle_pts) pi.remove_internal_four_triangle_pts = True self.assertEqual(True, pi.remove_internal_four_triangle_pts) self.assertEqual(0, len(pi.seed_points)) pi.seed_points = seed_points self.assertArraysEqual(seed_points, pi.seed_points) self.assertEqual("", pi.relaxation_method) pi.relaxation_method = relaxation_method self.assertEqual(relaxation_method, pi.relaxation_method)
def test_interp_to_pts_numpy(self): """Interpolate to multiple points""" import numpy as np pts = np.array([(0, 0, 0), (10, 0, 1), (10, 10, 2), (0, 10, 3)]) interp = InterpIdw(pts) ret = interp.interpolate_to_points( np.array(((2.0, 1.0, 0.0), (5.0, 10.0, 2.5)))) self.assertIsInstance(ret, np.ndarray) np.testing.assert_array_equal(np.array([0.02681550197303295, 2.5]), ret)
def test_set_pt_activity(self): """Setting point activity""" interp = self.interp_idw_obj pts = ((0, 0, 0), (10, 0, 1), (10, 10, 2), (0, 10, 3)) tris = (0, 1, 3, 1, 2, 3) interp = InterpIdw(pts, tris) act1 = (True, False, False, True) interp.point_activity = act1 act2 = (0, 1, 0, 1) interp.point_activity = act2
def test_triangle_activity(self): """Setting tri activity""" interp = self.interp_idw_obj pts = ((0, 0, 0), (10, 0, 1), (10, 10, 2), (0, 10, 3)) tris = (0, 1, 3, 1, 2, 3) interp = InterpIdw(pts, tris) act1 = (True, False) interp.triangle_activity = act1 act2 = (1, 1) interp.triangle_activity = act2
def test_set_trunc(self): """Test set_trunc""" import numpy as np t_min = 7.11 t_max = 11.7 pts = np.array([(0, 0, 0), (10, 0, 1), (10, 10, 2), (0, 10, 3)]) tris = np.array([0, 1, 3, 1, 2, 3]) interp = InterpIdw(pts, tris) interp.set_truncation(t_max, t_min) self.assertEquals(t_min, interp.truncate_min) self.assertEquals(t_max, interp.truncate_max)
def test_get_tris(self): """Getting interp object points""" import numpy as np interp = self.interp_idw_obj pts = np.array([(0, 0, 0), (10, 0, 1), (10, 10, 2), (0, 10, 3)]) tris = np.array([0, 1, 3, 1, 2, 3]) interp = InterpIdw(pts, tris) ret = interp.triangles np.testing.assert_array_equal(tris, ret)
def test_creating_PolyInput(self): outside_poly = ((1, 2, 0), (5, 2, 0), (5, 9, 0), (1, 9, 0)) inside_polys = (((3, 3, 0), (2.5, 4, 0), (2, 3, 0)), ((4, 8, 0), (3, 7, 0), (2, 8, 0))) poly_corners = (0, 1, 2, 3) bias = 3.14159 pts = ((1, 0, 0), (10, 0, 0), (10, 10, 0)) size_func = InterpLinear(pts) elev_func = InterpIdw(pts) pi = PolyInput(outside_polygon=outside_poly, inside_polygons=inside_polys, bias=bias, size_function=size_func, patch_polygon_corners=poly_corners, elev_function=elev_func) self.assertIsInstance(pi, PolyInput) self.assertArraysEqual(outside_poly, pi.outside_polygon) self.assertInsidePolysEqual(inside_polys, pi.inside_polygons) self.assertEqual(bias, pi.bias) self.assertEqual(size_func.to_string(), pi.size_function.to_string()) self.assertEqual(elev_func.to_string(), pi.elev_function.to_string()) self.assertEqual(-1, pi.const_size_bias) self.assertEqual(-1, pi.const_size_function) self.assertEqual(False, pi.remove_internal_four_triangle_pts)
def test_interpolate_weights(self): """Test interpolate_weights""" import numpy as np interp = self.interp_idw_obj pts = ((0, 0, 0), (10, 0, 1), (10, 10, 2), (0, 10, 3)) tris = (0, 1, 3, 1, 2, 3) interp = InterpIdw(pts, tris) idxs, wts = interp.interpolate_weights((5, 5, 2)) np.testing.assert_array_equal(np.array([0, 1, 2, 3], np.int32), idxs) np.testing.assert_array_equal(np.array([0.25, 0.25, 0.25, 0.25]), wts) idxs, wts = interp.interpolate_weights((10, 10, 0)) np.testing.assert_array_equal(np.array([2], np.int32), idxs) np.testing.assert_array_equal(np.array([1.]), wts) idxs, wts = interp.interpolate_weights((-10, -10, -10)) np.testing.assert_array_equal(np.array([0, 1, 2, 3], np.int32), idxs) np.testing.assert_array_almost_equal(np.array( [0.876919, 0.06154, 0.0, 0.06154]), wts, decimal=5)
def test_tutorial(self): """Ensure the tutorial will work.""" interp = self.interp_idw_obj pts = ((0, 0, 0), (10, 0, 1), (10, 10, 2), (0, 10, 3)) tris = () tris2 = (0, 1, 3, 1, 2, 3) interp = InterpIdw(pts, tris) val = interp.interpolate_to_point((5, 5, 0)) self.assertEqual(1.5, val) interp = InterpIdw(pts, tris2) val2 = interp.interpolate_to_point((5, 5, 0)) self.assertEqual(1.5, val2)
def test_set_size_func_02(self): r = PolyRedistributePts() pts = () sf = InterpIdw(pts) r.set_size_func(sf)
def setUp(self): """Set up for each test case.""" pts = ((1, 2, 3), (1, 2, 3), (1, 2, 3)) self.interp_idw_obj = InterpIdw(pts)
def test_set_nodal_function(self): """Setting nodal function""" import numpy as np observer = MockObserver() typeerror = "SetNodalFunction(): incompatible function arguments." pts = np.array([(0, 0, 0), (10, 0, 1), (10, 10, 2), (0, 10, 3)]) tris = np.array([0, 1, 3, 1, 2, 3]) interp = InterpIdw(pts, tris) # No Argument interp.set_nodal_function() # None Args for each argument with self.assertRaises(ValueError) as context: interp.set_nodal_function(None, 1, True, observer) err = context.exception func_type_error = '"nodal_function_type" must be one of {}, not None'.format( ", ".join(InterpIdw.nodal_function_types)) self.assertIn(func_type_error, str(err)) with self.assertRaises(TypeError) as context: interp.set_nodal_function("constant", None, True, observer) err = context.exception self.assertIn(typeerror, str(err)) # Bad Args with self.assertRaises(ValueError) as context: interp.set_nodal_function(1, 1, True, observer) err = context.exception func_type_error = '"nodal_function_type" must be one of {}, not 1'.format( ", ".join(InterpIdw.nodal_function_types)) self.assertIn(func_type_error, str(err)) with self.assertRaises(ValueError) as context: interp.set_nodal_function("abc", 1, True, observer) err = context.exception errStr = '"nodal_function_type" must be one of {}, not abc'.format( ", ".join(InterpIdw.nodal_function_types)) self.assertIn(errStr, str(err)) with self.assertRaises(TypeError) as context: interp.set_nodal_function("constant", "1234", True, observer) err = context.exception self.assertIn(typeerror, str(err)) with self.assertRaises(TypeError) as context: interp.set_nodal_function("constant", 1.4, True, observer) err = context.exception self.assertIn(typeerror, str(err)) with self.assertRaises(ValueError) as context: interp.set_nodal_function("constant", 1, True, 1) errStr = "observer must be of type xmscore.misc.Observer" err = context.exception self.assertIn(errStr, str(err)) with self.assertRaises(ValueError) as context: interp.set_nodal_function("constant", 1, True, "abcd") err = context.exception self.assertIn(errStr, str(err)) # Good Args base = str(interp) interp.set_nodal_function("constant", 7, True, observer) self.assertEqual(base, str(interp)) interp.set_nodal_function("grad_plane", 9, True, observer) # TODO: Removed to_string checks. Need another way to test this interp.set_nodal_function("quadratic", 11, True, observer)
def test_set_observer(self): """Test set_observer""" import numpy as np observer1 = MockObserver("Obs1") observer2 = MockObserver("Obs2") pts = ((0, 0, 0), (10, 0, 1), (10, 10, 2), (0, 10, 3)) tris = (0, 1, 3, 1, 2, 3) interp = InterpIdw(pts, tris) # Non-Observer Type with self.assertRaises(TypeError) as context: interp.set_observer("xyz") err = context.exception self.assertIn("SetObserver(): incompatible function arguments.", str(err)) interp.set_observer(observer1) interp.interpolate_to_points(np.random.rand(100000, 3) + 5) self.assertGreater(observer1.status['percent_complete'], 0.0) self.assertGreater(observer1.status['elapsed_seconds'], 0.0) interp.set_observer(observer2) interp.interpolate_to_points(np.random.rand(100000, 3) + 5) self.assertGreater(observer2.status['percent_complete'], 0.0) self.assertGreater(observer2.status['elapsed_seconds'], 0.0)