[5447] | 1 | #!/usr/bin/env python |
---|
| 2 | # |
---|
| 3 | """ |
---|
| 4 | I removed lone test vert's, since I'm working on removing lone verts at a lower |
---|
| 5 | level of the code, using the -j flag in triangle. |
---|
| 6 | """ |
---|
| 7 | import sys |
---|
| 8 | import os |
---|
| 9 | import unittest |
---|
| 10 | import tempfile |
---|
| 11 | |
---|
[5494] | 12 | from Numeric import array, allclose |
---|
| 13 | from anuga.utilities.numerical_tools import ensure_numeric |
---|
[5447] | 14 | |
---|
[5494] | 15 | |
---|
[5447] | 16 | from slope import * |
---|
| 17 | |
---|
| 18 | class slopeTestCase(unittest.TestCase): |
---|
| 19 | def setUp(self): |
---|
| 20 | pass |
---|
| 21 | |
---|
| 22 | def tearDown(self): |
---|
| 23 | pass |
---|
| 24 | |
---|
| 25 | def test_slope_from_file(self): |
---|
| 26 | handle, file_name = tempfile.mkstemp('.csv', __name__+'_') |
---|
| 27 | os.close(handle) |
---|
| 28 | handle = open(file_name,'w') |
---|
| 29 | |
---|
| 30 | sample = """time,0.5:0.5,0.501:0.5,0.502:0.5 |
---|
| 31 | 0,0.1,0.2,0.3 |
---|
| 32 | 0.01,0.1,0.2,1.2 |
---|
| 33 | 0.02,0.1,0.1,0.0 |
---|
| 34 | 0.03,0.5,0.501,0.502""" |
---|
| 35 | handle.write(sample) |
---|
| 36 | handle.close() |
---|
[5494] | 37 | dtimes, depths, sensors = load_sensors(file_name) |
---|
[5447] | 38 | actual = array([0,0.01,0.02,0.03]) |
---|
| 39 | self.assert_ (allclose(dtimes, actual, 0.001)) |
---|
[5503] | 40 | actual = array([0.5, 0.5005, 0.5015]) |
---|
[5447] | 41 | self.assert_ (allclose(depths, actual, 0.001)) |
---|
| 42 | |
---|
| 43 | actual = array([[ 0.1, 0.2, 0.3 ], |
---|
| 44 | [ 0.1 , 0.2 , 1.2 ], |
---|
| 45 | [ 0.1 , 0.1, 0. ], |
---|
| 46 | [ 0.5 , 0.501 , 0.502]]) |
---|
| 47 | self.assert_ (allclose(sensors, actual, 0.001)) |
---|
| 48 | |
---|
| 49 | times, slope_locations, slopes = load_slopes(file_name) |
---|
| 50 | actual = array([0.5005, 0.5015]) |
---|
| 51 | self.assert_ (allclose(slope_locations, actual, 0.001)) |
---|
| 52 | actual = array([0,0.01,0.02,0.03]) |
---|
| 53 | self.assert_ (allclose(times, actual, 0.001)) |
---|
| 54 | |
---|
| 55 | actual = array([[ 100., 100.], |
---|
| 56 | [ 100., 1000.], |
---|
| 57 | [ 0., -100.], |
---|
| 58 | [ 1., 1.]]) |
---|
| 59 | self.assert_ (allclose(slopes, actual, 0.001)) |
---|
| 60 | |
---|
| 61 | |
---|
| 62 | os.remove(file_name) |
---|
| 63 | |
---|
| 64 | def test_plot_from_file(self): |
---|
| 65 | handle, file_name = tempfile.mkstemp('.csv', __name__+'_') |
---|
| 66 | os.close(handle) |
---|
| 67 | handle = open(file_name,'w') |
---|
| 68 | |
---|
| 69 | 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 |
---|
| 70 | 0, 0, 1, 2, 1, 0, 0, 0, 0 |
---|
| 71 | 1, 0, 0, 1, 2, 1, 0, 0, 0 |
---|
| 72 | 2, 0, 0, 0, 1.5, 3, 1.5, 0, 0 |
---|
| 73 | 3, 0, 0, 0, 0, 1.5, 3, 0, 0 |
---|
| 74 | 4, 0, 0, 0, 0, 0, 1.5, 3, 0""" |
---|
| 75 | handle.write(sample) |
---|
| 76 | handle.close() |
---|
| 77 | |
---|
[5494] | 78 | # since it currently graphs |
---|
| 79 | #graph_slopes(file_name) |
---|
[5447] | 80 | |
---|
| 81 | os.remove(file_name) |
---|
| 82 | |
---|
[5494] | 83 | def test_get_min_in_band(self): |
---|
| 84 | time = array([0,1,2,3,4,5]) |
---|
| 85 | slopes = array([[10,12], |
---|
| 86 | [9,10], |
---|
| 87 | [8,9], |
---|
| 88 | [9,7], |
---|
| 89 | [10,6], |
---|
| 90 | [7,2]]) |
---|
| 91 | |
---|
| 92 | max_q, max_q_times = get_min_in_band(0,5,time,slopes) |
---|
| 93 | actual = array([8,6]) |
---|
| 94 | self.assert_ (allclose(max_q, actual, 0.001)) |
---|
| 95 | actual = array([2,4]) |
---|
| 96 | self.assert_ (allclose(max_q_times, actual, 0.001)) |
---|
| 97 | |
---|
| 98 | |
---|
| 99 | max_q, max_q_times = get_min_in_band(1,4,time,slopes) |
---|
| 100 | actual = array([8,7]) |
---|
| 101 | self.assert_ (allclose(max_q, actual, 0.001)) |
---|
| 102 | actual = array([2,3]) |
---|
| 103 | self.assert_ (allclose(max_q_times, actual, 0.001)) |
---|
| 104 | |
---|
| 105 | def test_break_times2bands(self): |
---|
| 106 | break_times = [1,3,4,8] |
---|
| 107 | band_offset = 0.0 |
---|
| 108 | bands = break_times2bands(break_times, band_offset) |
---|
| 109 | #print "bands", bands |
---|
| 110 | actual = array([0,2,3.5,6,10 ]) |
---|
| 111 | self.assert_ (allclose(ensure_numeric(bands), actual, 0.001)) |
---|
| 112 | |
---|
[5503] | 113 | def not_used_test_get_time_band(self): |
---|
[5494] | 114 | time = array([0,1,2,3,4,5]) |
---|
| 115 | slopes = array([[10,12], |
---|
| 116 | [9,10], |
---|
| 117 | [8,9], |
---|
| 118 | [9,7], |
---|
| 119 | [10,6], |
---|
| 120 | [7,2]]) |
---|
| 121 | |
---|
| 122 | band_time, band_quantity = get_time_band(0,5,time,slopes) |
---|
| 123 | actual = array([1,2,3,4]) |
---|
| 124 | self.assert_ (allclose(band_time, actual, 0.001)) |
---|
| 125 | actual = array([[9,10], |
---|
| 126 | [8,9], |
---|
| 127 | [9,7], |
---|
| 128 | [10,6]]) |
---|
| 129 | self.assert_ (allclose(band_quantity, actual, 0.001)) |
---|
| 130 | |
---|
| 131 | |
---|
| 132 | def test_get_band(self): |
---|
| 133 | time = array([0,1,2,3,4,5]) |
---|
| 134 | slopes = array([[10,12], |
---|
| 135 | [9,10], |
---|
| 136 | [8,9], |
---|
| 137 | [9,7], |
---|
| 138 | [10,6], |
---|
| 139 | [7,2]]) |
---|
| 140 | |
---|
| 141 | band_time, band_quantity = get_band(0,5,time,slopes,0) |
---|
| 142 | actual = array([1,2,3,4]) |
---|
| 143 | self.assert_ (allclose(band_time, actual, 0.001)) |
---|
| 144 | actual = array([[9,10], |
---|
| 145 | [8,9], |
---|
| 146 | [9,7], |
---|
| 147 | [10,6]]) |
---|
| 148 | self.assert_ (allclose(band_quantity, actual, 0.001)) |
---|
| 149 | |
---|
| 150 | location = array([0,1,2,3,4,5]) |
---|
| 151 | slopes = array([[0,1,2,3,4,5], |
---|
| 152 | [0,10,20,30,40,50]]) |
---|
| 153 | |
---|
| 154 | band_time, band_quantity = get_band(0,5,location,slopes,-1) |
---|
| 155 | actual = array([1,2,3,4]) |
---|
| 156 | self.assert_ (allclose(band_time, actual, 0.001)) |
---|
| 157 | actual = array([[1,2,3,4], |
---|
| 158 | [10,20,30,40]]) |
---|
| 159 | self.assert_ (allclose(band_quantity, actual, 0.001)) |
---|
[5503] | 160 | |
---|
| 161 | def test_find_froude(self): |
---|
| 162 | times_froude = array([0,1]) |
---|
| 163 | locations_froude = array([0,1,2,3,4,5]) |
---|
| 164 | froudes = array([[0,1,2,3,4,5], |
---|
| 165 | [0,10,20,30,40,50]]) |
---|
| 166 | times = array([0,0,1,1]) |
---|
| 167 | locations = array([0.5, 1.5, 2.5, 3.5]) |
---|
| 168 | froudes = find_froude(times_froude, locations_froude, |
---|
| 169 | froudes, times, locations) |
---|
| 170 | actual = array([0.5, 1.5, 25, 35]) |
---|
| 171 | self.assert_ (allclose(ensure_numeric(froudes), actual, 0.001)) |
---|
[5494] | 172 | |
---|
[5503] | 173 | |
---|
| 174 | |
---|
[5447] | 175 | if __name__ == "__main__": |
---|
| 176 | |
---|
| 177 | suite = unittest.makeSuite(slopeTestCase,'test') |
---|
[5503] | 178 | #suite = unittest.makeSuite(slopeTestCase,'test_find_froude') |
---|
[5494] | 179 | |
---|
[5447] | 180 | runner = unittest.TextTestRunner() #verbosity=2) |
---|
| 181 | runner.run(suite) |
---|