#!/usr/bin/env python # """ I removed lone test vert's, since I'm working on removing lone verts at a lower level of the code, using the -j flag in triangle. """ import sys import os import unittest import tempfile from Numeric import array, allclose from anuga.utilities.numerical_tools import ensure_numeric from slope import * class slopeTestCase(unittest.TestCase): def setUp(self): pass def tearDown(self): pass def test_slope_from_file(self): handle, file_name = tempfile.mkstemp('.csv', __name__+'_') os.close(handle) handle = open(file_name,'w') sample = """time,0.5:0.5,0.501:0.5,0.502:0.5 0,0.1,0.2,0.3 0.01,0.1,0.2,1.2 0.02,0.1,0.1,0.0 0.03,0.5,0.501,0.502""" handle.write(sample) handle.close() dtimes, depths, sensors = load_sensors(file_name) actual = array([0,0.01,0.02,0.03]) self.assert_ (allclose(dtimes, actual, 0.001)) actual = array([0.5, 0.5005, 0.5015]) self.assert_ (allclose(depths, actual, 0.001)) actual = array([[ 0.1, 0.2, 0.3 ], [ 0.1 , 0.2 , 1.2 ], [ 0.1 , 0.1, 0. ], [ 0.5 , 0.501 , 0.502]]) self.assert_ (allclose(sensors, actual, 0.001)) times, slope_locations, slopes = load_slopes(file_name) actual = array([0.5005, 0.5015]) self.assert_ (allclose(slope_locations, actual, 0.001)) actual = array([0,0.01,0.02,0.03]) self.assert_ (allclose(times, actual, 0.001)) actual = array([[ 100., 100.], [ 100., 1000.], [ 0., -100.], [ 1., 1.]]) self.assert_ (allclose(slopes, actual, 0.001)) os.remove(file_name) def test_plot_from_file(self): handle, file_name = tempfile.mkstemp('.csv', __name__+'_') os.close(handle) handle = open(file_name,'w') sample = """time,0.0:1,1:0.5,2:0.5,3:0.5,4:0.5,5:0.5,6:0.5,7:0.5 0, 0, 1, 2, 1, 0, 0, 0, 0 1, 0, 0, 1, 2, 1, 0, 0, 0 2, 0, 0, 0, 1.5, 3, 1.5, 0, 0 3, 0, 0, 0, 0, 1.5, 3, 0, 0 4, 0, 0, 0, 0, 0, 1.5, 3, 0""" handle.write(sample) handle.close() # since it currently graphs #graph_slopes(file_name) os.remove(file_name) def test_get_min_in_band(self): time = array([0,1,2,3,4,5]) slopes = array([[10,12], [9,10], [8,9], [9,7], [10,6], [7,2]]) max_q, max_q_times = get_min_in_band(0,5,time,slopes) actual = array([8,6]) self.assert_ (allclose(max_q, actual, 0.001)) actual = array([2,4]) self.assert_ (allclose(max_q_times, actual, 0.001)) max_q, max_q_times = get_min_in_band(1,4,time,slopes) actual = array([8,7]) self.assert_ (allclose(max_q, actual, 0.001)) actual = array([2,3]) self.assert_ (allclose(max_q_times, actual, 0.001)) def test_break_times2bands(self): break_times = [1,3,4,8] band_offset = 0.0 bands = break_times2bands(break_times, band_offset) #print "bands", bands actual = array([0,2,3.5,6,10 ]) self.assert_ (allclose(ensure_numeric(bands), actual, 0.001)) def not_used_test_get_time_band(self): time = array([0,1,2,3,4,5]) slopes = array([[10,12], [9,10], [8,9], [9,7], [10,6], [7,2]]) band_time, band_quantity = get_time_band(0,5,time,slopes) actual = array([1,2,3,4]) self.assert_ (allclose(band_time, actual, 0.001)) actual = array([[9,10], [8,9], [9,7], [10,6]]) self.assert_ (allclose(band_quantity, actual, 0.001)) def test_get_band(self): time = array([0,1,2,3,4,5]) slopes = array([[10,12], [9,10], [8,9], [9,7], [10,6], [7,2]]) band_time, band_quantity = get_band(0,5,time,slopes,0) actual = array([1,2,3,4]) self.assert_ (allclose(band_time, actual, 0.001)) actual = array([[9,10], [8,9], [9,7], [10,6]]) self.assert_ (allclose(band_quantity, actual, 0.001)) location = array([0,1,2,3,4,5]) slopes = array([[0,1,2,3,4,5], [0,10,20,30,40,50]]) band_time, band_quantity = get_band(0,5,location,slopes,-1) actual = array([1,2,3,4]) self.assert_ (allclose(band_time, actual, 0.001)) actual = array([[1,2,3,4], [10,20,30,40]]) self.assert_ (allclose(band_quantity, actual, 0.001)) def test_find_froude(self): times_froude = array([0,1]) locations_froude = array([0,1,2,3,4,5]) froudes = array([[0,1,2,3,4,5], [0,10,20,30,40,50]]) times = array([0,0,1,1]) locations = array([0.5, 1.5, 2.5, 3.5]) froudes = find_froude(times_froude, locations_froude, froudes, times, locations) actual = array([0.5, 1.5, 25, 35]) self.assert_ (allclose(ensure_numeric(froudes), actual, 0.001)) if __name__ == "__main__": suite = unittest.makeSuite(slopeTestCase,'test') #suite = unittest.makeSuite(slopeTestCase,'test_find_froude') runner = unittest.TextTestRunner() #verbosity=2) runner.run(suite)